| 1 | package Chemistry::MolecularMass; | ||||||||||
| 2 | |||||||||||
| 3 | =pod | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Chemistry::MolecularMass - Perl extension for calculating | ||||||||||
| 8 | molecular mass of a chemical compound given its chemical formula. | ||||||||||
| 9 | |||||||||||
| 10 | =head1 VERSION | ||||||||||
| 11 | |||||||||||
| 12 | 0.1 | ||||||||||
| 13 | |||||||||||
| 14 | =head1 AUTHOR AND COPYRIGHT | ||||||||||
| 15 | |||||||||||
| 16 | Copyright (C) 2000, Maksim A. Khrapov | ||||||||||
| 17 | maksim@recursivemind.com | ||||||||||
| 18 | http://www.recursivemind.com | ||||||||||
| 19 | |||||||||||
| 20 | This program is distributed under Perl Artistic Lisence. | ||||||||||
| 21 | No warranty. Use at your own risk. | ||||||||||
| 22 | |||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||
| 24 | |||||||||||
| 25 | use Chemistry::MolecularMass; | ||||||||||
| 26 | my $mm = new Chemistry::MolecularMass; | ||||||||||
| 27 | my $mass = $mm->calc_mass("C2H5OH"); | ||||||||||
| 28 | print $mass if defined($mass); | ||||||||||
| 29 | |||||||||||
| 30 | my %default_macros = $mm->all_macros; | ||||||||||
| 31 | $mm->add_macros("Ts" => "CH3C6H4SO2", "Bs" => "BrC6H4SO2"); | ||||||||||
| 32 | $mass = $mm->calc_mass("TsOEt"); | ||||||||||
| 33 | $mass = $mm->calc_mass("{[(CH3)3Si]2N}2CHCH<CH2Br>2"); | ||||||||||
| 34 | |||||||||||
| 35 | ### if you are an organic chemist, you might want to do this: | ||||||||||
| 36 | $mm->add_macros("Pr" => "C3H7", "Ac" => "CH3COO"); | ||||||||||
| 37 | |||||||||||
| 38 | my %elements = $mm->all_elements; | ||||||||||
| 39 | $mm->replace_elements("Na" => 24.00, "Cl" => 37.00); | ||||||||||
| 40 | $mass = $mm->calc_mass("NaCl"); | ||||||||||
| 41 | |||||||||||
| 42 | =head1 DESCRIPTION | ||||||||||
| 43 | |||||||||||
| 44 | Chemistry::MolecularMass is an Object Oriented Perl module for calculating | ||||||||||
| 45 | molcular mass of chemical compounds implemented with Perl and C. | ||||||||||
| 46 | Molecular masses of elements stored in the module follow recommendations | ||||||||||
| 47 | of IUPAC (1995). The module includes elements from H(1) through | ||||||||||
| 48 | Uuu(113) and isotopes of hydrogen: deuterium and tritium. The module | ||||||||||
| 49 | also allows a programmer to change the default masses of elements | ||||||||||
| 50 | for work with isotopes. It also includes some of the more common | ||||||||||
| 51 | chemical abbreviations as macros and allows to add new macros and | ||||||||||
| 52 | change the values of old macros. A hash of all macros and a hash | ||||||||||
| 53 | of all elements can be returned. | ||||||||||
| 54 | |||||||||||
| 55 | Arbitrary element names can be added, they are expected, however, to | ||||||||||
| 56 | start with an upper case letter followed by zero or more lower case | ||||||||||
| 57 | letters. Macros can be any string of characters. Macros are substituted | ||||||||||
| 58 | only once, so a macro should not evaluate to another macro. Legal | ||||||||||
| 59 | characters in a formula are: A-Za-z0-9<>{}[]() | ||||||||||
| 60 | Spaces are not allowed. Parentheses can be nested arbitrarily deep. | ||||||||||
| 61 | |||||||||||
| 62 | Each MolecularMass object has its own hashes of macros and element | ||||||||||
| 63 | masses, so modifications made to one MolecularMass object do NOT | ||||||||||
| 64 | affect another. The whole thing was programmed with reentrancy | ||||||||||
| 65 | in mind, so it should be thread safe as well. | ||||||||||
| 66 | |||||||||||
| 67 | =cut | ||||||||||
| 68 | |||||||||||
| 69 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 70 | 1 | ▶ | use vars qw($VERSION @ISA); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 71 | |||||||||||
| 72 | 1 | require DynaLoader; | |||||||||
| 73 | |||||||||||
| 74 | 1 | @ISA = qw(DynaLoader); | |||||||||
| 75 | |||||||||||
| 76 | 1 | $VERSION = '0.1'; | |||||||||
| 77 | |||||||||||
| 78 | 1 | bootstrap Chemistry::MolecularMass $VERSION; | |||||||||
| 79 | |||||||||||
| 80 | ######################### Globals ############################### | ||||||||||
| 81 | |||||||||||
| 82 | 1 | my %macros = ( | |||||||||
| 83 | 'Me' => 'CH3', | ||||||||||
| 84 | 'Et' => 'CH3CH2', | ||||||||||
| 85 | # Pr is an element, if user wants it, let them install it themselves | ||||||||||
| 86 | 'Bu' => 'C4H9', | ||||||||||
| 87 | 'Bn' => 'C6H5CH2', | ||||||||||
| 88 | 'Cp' => 'C5H5', | ||||||||||
| 89 | 'Ph' => 'C6H5', | ||||||||||
| 90 | # Ac is an element | ||||||||||
| 91 | 'Bz' => 'C6H5CO', | ||||||||||
| 92 | ); | ||||||||||
| 93 | |||||||||||
| 94 | 1 | my %elements = ( | |||||||||
| 95 | "H" => 1.00794, | ||||||||||
| 96 | "D" => 2.014101, | ||||||||||
| 97 | "T" => 3.016049, | ||||||||||
| 98 | "He" => 4.002602, | ||||||||||
| 99 | "Li" => 6.941, | ||||||||||
| 100 | "Be" => 9.012182, | ||||||||||
| 101 | "B" => 10.811, | ||||||||||
| 102 | "C" => 12.0107, | ||||||||||
| 103 | "N" => 14.00674, | ||||||||||
| 104 | "O" => 15.9994, | ||||||||||
| 105 | "F" => 18.9984032, | ||||||||||
| 106 | "Ne" => 20.1797, | ||||||||||
| 107 | "Na" => 22.989770, | ||||||||||
| 108 | "Mg" => 24.3050, | ||||||||||
| 109 | "Al" => 26.981538, | ||||||||||
| 110 | "Si" => 28.0855, | ||||||||||
| 111 | "P" => 30.973761, | ||||||||||
| 112 | "S" => 32.066, | ||||||||||
| 113 | "Cl" => 35.4527, | ||||||||||
| 114 | "Ar" => 39.948, | ||||||||||
| 115 | "K" => 39.0983, | ||||||||||
| 116 | "Ca" => 40.078, | ||||||||||
| 117 | "Sc" => 44.955910, | ||||||||||
| 118 | "Ti" => 47.867, | ||||||||||
| 119 | "V" => 50.9415, | ||||||||||
| 120 | "Cr" => 51.9961, | ||||||||||
| 121 | "Mn" => 54.938049, | ||||||||||
| 122 | "Fe" => 55.845, | ||||||||||
| 123 | "Co" => 58.933200, | ||||||||||
| 124 | "Ni" => 58.6934, | ||||||||||
| 125 | "Cu" => 63.546, | ||||||||||
| 126 | "Zn" => 65.39, | ||||||||||
| 127 | "Ga" => 69.723, | ||||||||||
| 128 | "Ge" => 72.61, | ||||||||||
| 129 | "As" => 74.92160, | ||||||||||
| 130 | "Se" => 78.96, | ||||||||||
| 131 | "Br" => 79.904, | ||||||||||
| 132 | "Kr" => 83.80, | ||||||||||
| 133 | "Rb" => 85.4678, | ||||||||||
| 134 | "Sr" => 87.62, | ||||||||||
| 135 | "Y" => 88.90585, | ||||||||||
| 136 | "Zr" => 91.224, | ||||||||||
| 137 | "Nb" => 92.90638, | ||||||||||
| 138 | "Mo" => 95.94, | ||||||||||
| 139 | "Tc" => 98, | ||||||||||
| 140 | "Ru" => 101.07, | ||||||||||
| 141 | "Rh" => 102.90550, | ||||||||||
| 142 | "Pd" => 106.42, | ||||||||||
| 143 | "Ag" => 107.8682, | ||||||||||
| 144 | "Cd" => 112.411, | ||||||||||
| 145 | "In" => 114.818, | ||||||||||
| 146 | "Sn" => 118.710, | ||||||||||
| 147 | "Sb" => 121.760, | ||||||||||
| 148 | "Te" => 127.60, | ||||||||||
| 149 | "I" => 126.90447, | ||||||||||
| 150 | "Xe" => 131.29, | ||||||||||
| 151 | "Cs" => 132.90545, | ||||||||||
| 152 | "Ba" => 137.327, | ||||||||||
| 153 | "La" => 138.9055, | ||||||||||
| 154 | "Ce" => 140.116, | ||||||||||
| 155 | "Pr" => 140.90765, | ||||||||||
| 156 | "Nd" => 144.24, | ||||||||||
| 157 | "Pm" => 145, | ||||||||||
| 158 | "Sm" => 150.36, | ||||||||||
| 159 | "Eu" => 151.964, | ||||||||||
| 160 | "Gd" => 157.25, | ||||||||||
| 161 | "Tb" => 158.92534, | ||||||||||
| 162 | "Dy" => 162.50, | ||||||||||
| 163 | "Ho" => 164.93032, | ||||||||||
| 164 | "Er" => 167.26, | ||||||||||
| 165 | "Tm" => 168.93421, | ||||||||||
| 166 | "Yb" => 173.04, | ||||||||||
| 167 | "Lu" => 174.967, | ||||||||||
| 168 | "Hf" => 178.49, | ||||||||||
| 169 | "Ta" => 180.9479, | ||||||||||
| 170 | "W" => 183.84, | ||||||||||
| 171 | "Re" => 186.207, | ||||||||||
| 172 | "Os" => 190.23, | ||||||||||
| 173 | "Ir" => 192.217, | ||||||||||
| 174 | "Pt" => 195.078, | ||||||||||
| 175 | "Au" => 196.96655, | ||||||||||
| 176 | "Hg" => 200.59, | ||||||||||
| 177 | "Tl" => 204.3833, | ||||||||||
| 178 | "Pb" => 207.2, | ||||||||||
| 179 | "Bi" => 208.98038, | ||||||||||
| 180 | "Po" => 209, | ||||||||||
| 181 | "At" => 210, | ||||||||||
| 182 | "Rn" => 222, | ||||||||||
| 183 | "Fr" => 223, | ||||||||||
| 184 | "Ra" => 226, | ||||||||||
| 185 | "Ac" => 227, | ||||||||||
| 186 | "Th" => 232.038, | ||||||||||
| 187 | "Pa" => 231.03588, | ||||||||||
| 188 | "U" => 238.0289, | ||||||||||
| 189 | "Np" => 237, | ||||||||||
| 190 | "Pu" => 244, | ||||||||||
| 191 | "Am" => 243, | ||||||||||
| 192 | "Cm" => 247, | ||||||||||
| 193 | "Bk" => 247, | ||||||||||
| 194 | "Cf" => 251, | ||||||||||
| 195 | "Es" => 252, | ||||||||||
| 196 | "Fm" => 257, | ||||||||||
| 197 | "Md" => 258, | ||||||||||
| 198 | "No" => 259, | ||||||||||
| 199 | "Lr" => 262, | ||||||||||
| 200 | "Rf" => 261, | ||||||||||
| 201 | "Db" => 262, | ||||||||||
| 202 | "Sg" => 266, | ||||||||||
| 203 | "Bh" => 264, | ||||||||||
| 204 | "Hs" => 269, | ||||||||||
| 205 | "Mt" => 268, | ||||||||||
| 206 | "Uun" => 271, | ||||||||||
| 207 | "Uuu" => 272, | ||||||||||
| 208 | ); | ||||||||||
| 209 | |||||||||||
| 210 | ##################################################################### | ||||||||||
| 211 | # | ||||||||||
| 212 | # Methods | ||||||||||
| 213 | # | ||||||||||
| 214 | ##################################################################### | ||||||||||
| 215 | |||||||||||
| 216 | sub new | ||||||||||
| 217 | 1 | ▶ | { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 218 | 1 | my $class = shift; | |||||||||
| 219 | |||||||||||
| 220 | 1 | my $hr = {}; | |||||||||
| 221 | 1 | my %ma = %macros; | |||||||||
| 222 | 1 | my %el = %elements; | |||||||||
| 223 | |||||||||||
| 224 | 1 | $hr->{macros} = \%ma; | |||||||||
| 225 | 1 | $hr->{elements} = \%el; | |||||||||
| 226 | |||||||||||
| 227 | 1 | bless $hr, $class; | |||||||||
| 228 | 1 | return $hr; | |||||||||
| 229 | } | ||||||||||
| 230 | |||||||||||
| 231 | ##################################################################### | ||||||||||
| 232 | |||||||||||
| 233 | sub calc_mass | ||||||||||
| 234 | 9 | ▶ | { | ||||||||
|
Subroutine
calc_mass: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 235 | 9 | my $self = shift; | |||||||||
| 236 | 9 | my $formula = shift; | |||||||||
| 237 | |||||||||||
| 238 | 9 | ▶ | if($formula =~ /[^a-zA-Z\d()<>{}\[\]]/) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 239 | { | ||||||||||
| 240 | 1 | warn "Forbidden chars\n"; | |||||||||
| 241 | 1 | return undef; ### Forbidden characters | |||||||||
| 242 | } | ||||||||||
| 243 | |||||||||||
| 244 | 8 | ▶ | unless(verify_parens($formula)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 245 | { | ||||||||||
| 246 | 2 | warn "Parentheses don't match\n"; | |||||||||
| 247 | 2 | return undef; ### Parentheses do not match | |||||||||
| 248 | } | ||||||||||
| 249 | |||||||||||
| 250 | 6 | $formula =~ tr/<>{}[]/()()()/; | |||||||||
| 251 | 6 | my $exp_formula = $self->expand_macros($formula); | |||||||||
| 252 | 6 | my %symbol_table = parse_formula($exp_formula); | |||||||||
| 253 | 6 | my $weight = $self->sum_el_masses(\%symbol_table); | |||||||||
| 254 | |||||||||||
| 255 | 6 | return $weight; | |||||||||
| 256 | } | ||||||||||
| 257 | |||||||||||
| 258 | ##################################################################### | ||||||||||
| 259 | |||||||||||
| 260 | sub expand_macros | ||||||||||
| 261 | 6 | ▶ | { | ||||||||
|
Subroutine
expand_macros: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 262 | 6 | my $self = shift; | |||||||||
| 263 | 6 | my $formula = shift; | |||||||||
| 264 | |||||||||||
| 265 | 6 | my $macro; | |||||||||
| 266 | 6 | foreach $macro (keys %{$self->{macros}}) | |||||||||
| 267 | { | ||||||||||
| 268 | 42 | my $value = $self->{macros}->{$macro}; | |||||||||
| 269 | 42 | eval '$formula =~ s/$macro/($value)/g'; | |||||||||
| 270 | } | ||||||||||
| 271 | 6 | return $formula; | |||||||||
| 272 | } | ||||||||||
| 273 | |||||||||||
| 274 | ##################################################################### | ||||||||||
| 275 | |||||||||||
| 276 | sub sum_el_masses | ||||||||||
| 277 | 6 | ▶ | { | ||||||||
|
Subroutine
sum_el_masses: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 278 | 6 | my $self = shift; | |||||||||
| 279 | 6 | my $symtab = shift; | |||||||||
| 280 | |||||||||||
| 281 | 6 | my $weight = 0; | |||||||||
| 282 | |||||||||||
| 283 | 6 | my $el; | |||||||||
| 284 | 6 | foreach $el (keys %$symtab) | |||||||||
| 285 | { | ||||||||||
| 286 | 10 | ▶ | unless(exists $self->{elements}->{$el}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 287 | { | ||||||||||
| 288 | 1 | warn "No such element: $el\n"; | |||||||||
| 289 | 1 | return undef; ### No such element | |||||||||
| 290 | } | ||||||||||
| 291 | |||||||||||
| 292 | 9 | $weight += $symtab->{$el} * $self->{elements}->{$el}; ### Number of atoms | |||||||||
| 293 | ### of this element multiplied by its atomic weight | ||||||||||
| 294 | } | ||||||||||
| 295 | 5 | return $weight; | |||||||||
| 296 | } | ||||||||||
| 297 | |||||||||||
| 298 | ##################################################################### | ||||||||||
| 299 | |||||||||||
| 300 | sub add_macros | ||||||||||
| 301 | 0 | ▶ | { | ||||||||
|
Subroutine
add_macros: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 302 | 0 | my $self = shift; | |||||||||
| 303 | 0 | my %ma = @_; | |||||||||
| 304 | |||||||||||
| 305 | 0 | my $key; | |||||||||
| 306 | 0 | foreach $key (keys %ma) | |||||||||
| 307 | { | ||||||||||
| 308 | 0 | ▶ | unless(exists $self->{macros}->{$key}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 309 | { | ||||||||||
| 310 | 0 | $self->{macros}->{$key} = $ma{$key}; | |||||||||
| 311 | } | ||||||||||
| 312 | } | ||||||||||
| 313 | 0 | return 1; | |||||||||
| 314 | } | ||||||||||
| 315 | |||||||||||
| 316 | ##################################################################### | ||||||||||
| 317 | |||||||||||
| 318 | sub replace_macros | ||||||||||
| 319 | 0 | ▶ | { | ||||||||
|
Subroutine
replace_macros: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 320 | 0 | my $self = shift; | |||||||||
| 321 | 0 | my %ma = @_; | |||||||||
| 322 | |||||||||||
| 323 | 0 | my $key; | |||||||||
| 324 | 0 | foreach $key (keys %ma) | |||||||||
| 325 | { | ||||||||||
| 326 | 0 | $self->{macros}->{$key} = $ma{$key}; | |||||||||
| 327 | } | ||||||||||
| 328 | 0 | return 1; | |||||||||
| 329 | } | ||||||||||
| 330 | |||||||||||
| 331 | ##################################################################### | ||||||||||
| 332 | |||||||||||
| 333 | sub add_elements | ||||||||||
| 334 | 0 | ▶ | { | ||||||||
|
Subroutine
add_elements: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 335 | 0 | my $self = shift; | |||||||||
| 336 | 0 | my %el = @_; | |||||||||
| 337 | |||||||||||
| 338 | 0 | my $key; | |||||||||
| 339 | 0 | foreach $key (%el) | |||||||||
| 340 | { | ||||||||||
| 341 | 0 | ▶ | unless(exists $self->{elements}->{$key}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 342 | { | ||||||||||
| 343 | 0 | $self->{elements}->{$key} = $el{$key}; | |||||||||
| 344 | } | ||||||||||
| 345 | } | ||||||||||
| 346 | 0 | return 1; | |||||||||
| 347 | } | ||||||||||
| 348 | |||||||||||
| 349 | #################################################################### | ||||||||||
| 350 | |||||||||||
| 351 | sub replace_elements | ||||||||||
| 352 | 0 | ▶ | { | ||||||||
|
Subroutine
replace_elements: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 353 | 0 | my $self = shift; | |||||||||
| 354 | 0 | my %el = @_; | |||||||||
| 355 | |||||||||||
| 356 | 0 | my $key; | |||||||||
| 357 | 0 | foreach $key (%el) | |||||||||
| 358 | { | ||||||||||
| 359 | 0 | $self->{elements}->{$key} = $el{$key}; | |||||||||
| 360 | } | ||||||||||
| 361 | 0 | return 1; | |||||||||
| 362 | } | ||||||||||
| 363 | |||||||||||
| 364 | ##################################################################### | ||||||||||
| 365 | |||||||||||
| 366 | sub all_macros | ||||||||||
| 367 | 0 | ▶ | { | ||||||||
|
Subroutine
all_macros: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 368 | 0 | my $self = shift; | |||||||||
| 369 | 0 | return %{$self->{macros}}; | |||||||||
| 370 | } | ||||||||||
| 371 | |||||||||||
| 372 | ##################################################################### | ||||||||||
| 373 | |||||||||||
| 374 | sub all_elements | ||||||||||
| 375 | 0 | ▶ | { | ||||||||
|
Subroutine
all_elements: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 376 | 0 | my $self = shift; | |||||||||
| 377 | 0 | return %{$self->{elements}}; | |||||||||
| 378 | } | ||||||||||
| 379 | |||||||||||
| 380 | ##################################################################### | ||||||||||
| 381 | |||||||||||
| 382 | ### End of game | ||||||||||
| 383 | 1 | 1; | |||||||||