1 #!/usr/bin/perl -w 2 # 3 # $RCSfile: InfoPeriodicTableElements.pl,v $ 4 # $Date: 2008/01/30 21:44:47 $ 5 # $Revision: 1.14 $ 6 # 7 # Author: Manish Sud <msud@san.rr.com> 8 # 9 # Copyright (C) 2004-2008 Manish Sud. All rights reserved. 10 # 11 # This file is part of MayaChemTools. 12 # 13 # MayaChemTools is free software; you can redistribute it and/or modify it under 14 # the terms of the GNU Lesser General Public License as published by the Free 15 # Software Foundation; either version 3 of the License, or (at your option) any 16 # later version. 17 # 18 # MayaChemTools is distributed in the hope that it will be useful, but without 19 # any warranty; without even the implied warranty of merchantability of fitness 20 # for a particular purpose. See the GNU Lesser General Public License for more 21 # details. 22 # 23 # You should have received a copy of the GNU Lesser General Public License 24 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or 25 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330, 26 # Boston, MA, 02111-1307, USA. 27 # 28 29 use 5.006; 30 use strict; 31 use FindBin; use lib "$FindBin::Bin/../lib"; 32 use Getopt::Long; 33 use File::Basename; 34 use Text::ParseWords; 35 use Benchmark; 36 use FileUtil; 37 use TextUtil; 38 use PeriodicTable; 39 40 my($ScriptName, %Options, $StartTime, $EndTime, $TotalTime); 41 42 # Autoflush STDOUT 43 $| = 1; 44 45 # Starting message... 46 $ScriptName = basename($0); 47 print "\n$ScriptName: Starting...\n\n"; 48 $StartTime = new Benchmark; 49 50 # Get the options and setup script... 51 SetupScriptUsage(); 52 if ($Options{help}) { 53 die GetUsageFromPod("$FindBin::Bin/$ScriptName"); 54 } 55 56 my($OutDelim, $OutQuote, $ElementRowsOutput, $FileOutput, $Precision, $OutFileName, @SpecifiedElementIDs, @SpecifiedProperies,); 57 ProcessOptions(); 58 59 ListElementProperties(); 60 print "$ScriptName:Done...\n\n"; 61 62 $EndTime = new Benchmark; 63 $TotalTime = timediff ($EndTime, $StartTime); 64 print "Total time: ", timestr($TotalTime), "\n"; 65 66 ############################################################################### 67 68 # Get propery names from categories... 69 sub GetPropertyNamesFromCategories { 70 my($CategoryName) = @_; 71 my(@PropertyNames); 72 73 @PropertyNames = (); 74 if ($CategoryName =~ /^Basic$/i) { 75 @PropertyNames = ('AtomicNumber', 'ElementSymbol', 'ElementName', 'AtomicWeight', 'GroundStateConfiguration', 'GroupNumber', 'PeriodNumber', 'FirstIonizationEnergy'); 76 } elsif ($CategoryName =~ /^BasicAndNaturalIsotope$/i) { 77 # Natural isotope data includes: 'MassNumber', 'RelativeAtomicMass', 'NaturalAbundance' 78 @PropertyNames = ('AtomicNumber', 'ElementSymbol', 'ElementName', 'AtomicWeight', 'GroundStateConfiguration', 'GroupNumber', 'PeriodNumber', 'FirstIonizationEnergy', 'NaturalIsotopeData'); 79 } elsif ($CategoryName =~ /^NaturalIsotope$/i) { 80 @PropertyNames = ('AtomicNumber', 'ElementSymbol', 'ElementName', 'NaturalIsotopeData'); 81 } 82 83 return @PropertyNames; 84 } 85 86 # List data for an element... 87 sub ListElementData { 88 my($DataLabelRef, $DataValueRef) = @_; 89 my($Index, $Line, $Value); 90 91 if ($ElementRowsOutput) { 92 $Line = ''; 93 # Format data... 94 if ($OutQuote || $Options{outdelim} !~ /^comma$/i) { 95 $Line = JoinWords($DataValueRef, $OutDelim, $OutQuote); 96 } 97 else { 98 # Always quote values containing commas... 99 $Line = ($DataValueRef->[0] =~ /\,/) ? qq("$DataValueRef->[0]") : $DataValueRef->[0]; 100 for $Index (1 .. $#{$DataValueRef} ) { 101 $Value = $DataValueRef->[$Index]; 102 if ($Value =~ /\,/) { 103 $Value = qq("$Value"); 104 } 105 $Line .= $OutDelim . $Value; 106 } 107 } 108 if ($FileOutput) { 109 print OUTFILE "$Line\n"; 110 } 111 else { 112 print "$Line\n"; 113 } 114 } 115 else { 116 # Format and list data... 117 $Line = ''; 118 for $Index (0 .. $#{$DataLabelRef} ) { 119 $Line = $DataLabelRef->[$Index] . ' ' . $DataValueRef->[$Index]; 120 if ($FileOutput) { 121 print OUTFILE "$Line\n"; 122 } 123 else { 124 print "$Line\n"; 125 } 126 } 127 } 128 } 129 130 # List data for an element... 131 sub ListHeaderRowData { 132 my($DataLabelRef) = @_; 133 my($Line); 134 135 # Format data... 136 $Line = JoinWords($DataLabelRef, $OutDelim, $OutQuote); 137 $Line =~ s/\://g; 138 # List data... 139 if ($FileOutput) { 140 print OUTFILE "$Line\n"; 141 } 142 else { 143 print "$Line\n"; 144 } 145 } 146 147 # List atomic properties for elements... 148 sub ListElementProperties { 149 my($ElementID, $ElementDataRef, $PropertyName, $PropertyValue, $PropertyUnits, $PropertyUnitsRef, @PropertyLabels, @PropertyValues); 150 151 print "Listing information for periodic table element(s)...\n"; 152 153 if ($FileOutput) { 154 print "Generating file $OutFileName...\n"; 155 open OUTFILE, ">$OutFileName" or die "Couldn't open $OutFileName: $!\n"; 156 } 157 158 # Setup property labels... 159 @PropertyLabels = (); 160 $PropertyUnitsRef = PeriodicTable::GetElementPropertiesNamesAndUnits(); 161 for $PropertyName (@SpecifiedProperies) { 162 $PropertyUnits = (exists $PropertyUnitsRef->{$PropertyName}) ? $PropertyUnitsRef->{$PropertyName} : ''; 163 if ($PropertyName =~ /^NaturalIsotopeData$/i) { 164 push @PropertyLabels, qw(MassNumber: RelativeAtomicMass: NaturalAbundance:); 165 } 166 else { 167 push @PropertyLabels, ($PropertyUnits ? "$PropertyName ($PropertyUnits):" : "$PropertyName:"); 168 } 169 } 170 171 if ($ElementRowsOutput) { 172 ListHeaderRowData(\@PropertyLabels); 173 } 174 175 # Go over specified properties... 176 for $ElementID (@SpecifiedElementIDs) { 177 $ElementDataRef = PeriodicTable::GetElementPropertiesData($ElementID); 178 179 if (!$ElementRowsOutput) { 180 if ($FileOutput) { 181 print OUTFILE "\nListing atomic properties for element $ElementID...\n\n"; 182 } 183 else { 184 print "\nListing atomic properties for element $ElementID...\n\n"; 185 } 186 } 187 188 # Collect data.. 189 @PropertyValues = (); 190 for $PropertyName (@SpecifiedProperies) { 191 if ($PropertyName =~ /^NaturalIsotopeData$/i) { 192 push @PropertyValues, SetupIsotopeData($ElementID); 193 } 194 else { 195 $PropertyValue = $ElementDataRef->{$PropertyName}; 196 if (IsFloat($PropertyValue)) { 197 $PropertyValue = sprintf("%.${Precision}f", $PropertyValue) + 0; 198 } 199 push @PropertyValues, $PropertyValue; 200 } 201 } 202 # List data... 203 ListElementData(\@PropertyLabels, \@PropertyValues); 204 } 205 if ($FileOutput) { 206 close OUTFILE; 207 } 208 print "\n"; 209 } 210 211 # Setup isotope data strings... 212 sub SetupIsotopeData { 213 my($ElementID) = @_; 214 my($MassNumber, $RelativeAtomicMass, $NaturalAbundance, $NaturalIsotopeDataRef, @MassNumbers, @RelativeAtomicMasses, @NaturalAbundances); 215 216 # Get natural isotope data: MassNumber, RelativeAtomicMass and NaturalAbundance 217 @MassNumbers = (); @RelativeAtomicMasses = (); @NaturalAbundances = (); 218 $NaturalIsotopeDataRef = PeriodicTable::GetElementNaturalIsotopesData($ElementID); 219 for $MassNumber (sort {$a <=> $b} keys %{$NaturalIsotopeDataRef}) { 220 $RelativeAtomicMass = $NaturalIsotopeDataRef->{$MassNumber}{RelativeAtomicMass}; 221 $NaturalAbundance = $NaturalIsotopeDataRef->{$MassNumber}{NaturalAbundance}; 222 push @MassNumbers, $MassNumber; 223 $RelativeAtomicMass = ($RelativeAtomicMass > 0) ? (sprintf("%.${Precision}f", $RelativeAtomicMass) + 0) : ''; 224 push @RelativeAtomicMasses, $RelativeAtomicMass; 225 $NaturalAbundance = ($NaturalAbundance > 0) ? (sprintf("%.${Precision}f", $NaturalAbundance) + 0) : ''; 226 push @NaturalAbundances, $NaturalAbundance; 227 } 228 $MassNumber = JoinWords(\@MassNumbers, ",", 0); 229 $RelativeAtomicMass = JoinWords(\@RelativeAtomicMasses, ",", 0); 230 $NaturalAbundance = JoinWords(\@NaturalAbundances, ",", 0); 231 return ($MassNumber, $RelativeAtomicMass, $NaturalAbundance); 232 } 233 234 # Process option values... 235 sub ProcessOptions { 236 $OutDelim = ($Options{outdelim} =~ /^tab$/i ) ? "\t" : (($Options{outdelim} =~ /^semicolon$/i) ? "\;" : "\,"); 237 $OutQuote = ($Options{quote} =~ /^yes$/i) ? 1 : 0; 238 239 $ElementRowsOutput = ($Options{outputstyle} =~ /^ElementRows$/i) ? 1 : 0; 240 $FileOutput = ($Options{output} =~ /^File$/i) ? 1 : 0; 241 242 $Precision = $Options{precision}; 243 244 my($ElementID, @ElementIDs, @GroupElements, @PeriodElements, %GroupNamesMap); 245 246 @SpecifiedElementIDs = (); 247 if (@ARGV >=1 && ($Options{mode} =~ /^All$/i) ) { 248 warn "Warning: Ignoring comman line element IDs: Not valid for All value of \"-m --mode\" option...\n"; 249 } 250 251 # Set up element IDs except for All mode... 252 @ElementIDs = (); 253 %GroupNamesMap = (); 254 255 if (@ARGV >=1 ) { 256 if ($Options{mode} !~ /^All$/i) { 257 push @ElementIDs, @ARGV; 258 } 259 } 260 else { 261 # Setup mode specified default values... 262 my($Nothing); 263 MODE: { 264 if ($Options{mode} =~ /^ElementID$/i) { push @ElementIDs, 'H'; last MODE; }; 265 if ($Options{mode} =~ /^AmericanGroupLabel$/i) { push @ElementIDs, 'IA'; last MODE; }; 266 if ($Options{mode} =~ /^EuropeanGroupLabel$/i) { push @ElementIDs, 'IA'; last MODE; }; 267 if ($Options{mode} =~ /^GroupNumber$/i) { push @ElementIDs, '1'; last MODE; }; 268 if ($Options{mode} =~ /^GroupName$/i) { push @ElementIDs, 'AlkaliMetals'; last MODE; }; 269 if ($Options{mode} =~ /^PeriodNumber$/i) { push @ElementIDs, '1'; last MODE; }; 270 $Nothing = 1; 271 } 272 } 273 if ($Options{mode} =~ /^GroupName$/i) { 274 # Map group names to what's stored in Perioidic table data file... 275 %GroupNamesMap = ('alkalimetals', 'Alkali metal', 'alkalineearthmetals', 'Alkaline earth metal', 'chalcogens', 'Chalcogen', 'coinagemetals', 'Coinage metal', 'halogens', 'Halogen', 'noblegases', 'Noble gas', 'pnictogens', 'Pnictogen', 'lanthanides', 'Lanthanoid', 'lanthanoids', 'Lanthanoid', 'actinides', 'Actinoid', 'actinoids', 'Actinoid' ); 276 } 277 278 # Generate list of elements... 279 if ($Options{mode} =~ /^All$/i) { 280 push @SpecifiedElementIDs, PeriodicTable::GetElements(); 281 } 282 else { 283 ELEMENTID: for $ElementID (@ElementIDs) { 284 if ($Options{mode} =~ /^ElementID$/i) { 285 if (PeriodicTable::IsElement($ElementID)) { 286 push @SpecifiedElementIDs, $ElementID; 287 } 288 else { 289 warn "Ignoring element ID, $ElementID, specified using command line parameter: Unknown element ID...\n"; 290 next ELEMENTID; 291 } 292 } 293 elsif ($Options{mode} =~ /^AmericanGroupLabel$/i) { 294 if (@GroupElements = PeriodicTable::GetElementsByAmericanStyleGroupLabel($ElementID)) { 295 push @SpecifiedElementIDs, @GroupElements; 296 } 297 else { 298 warn "Ignoring American style group label, $ElementID, specified using command line parameter: Unknown group label...\n"; 299 next ELEMENTID; 300 } 301 } 302 elsif ($Options{mode} =~ /^EuropeanGroupLabel$/i) { 303 if (@GroupElements = PeriodicTable::GetElementsByEuropeanStyleGroupLabel($ElementID)) { 304 push @SpecifiedElementIDs, @GroupElements; 305 } 306 else { 307 warn "Ignoring American style group label, $ElementID, specified using command line parameter: Unknown group label...\n"; 308 next ELEMENTID; 309 } 310 } 311 elsif ($Options{mode} =~ /^GroupNumber$/i) { 312 if (@GroupElements = PeriodicTable::GetElementsByGroupNumber($ElementID)) { 313 push @SpecifiedElementIDs, @GroupElements; 314 } 315 else { 316 warn "Ignoring group number, $ElementID, specified using command line parameter: Unknown group number...\n"; 317 next ELEMENTID; 318 } 319 } 320 elsif ($Options{mode} =~ /^GroupName$/i) { 321 if (exists $GroupNamesMap{lc($ElementID)}) { 322 @GroupElements = PeriodicTable::GetElementsByGroupName($GroupNamesMap{lc($ElementID)}); 323 push @SpecifiedElementIDs, @GroupElements; 324 } 325 else { 326 warn "Ignoring group name, $ElementID, specified using command line parameter: Unknown group name...\n"; 327 next ELEMENTID; 328 } 329 } 330 elsif ($Options{mode} =~ /^PeriodNumber$/i) { 331 if (@GroupElements = PeriodicTable::GetElementsByPeriodNumber($ElementID)) { 332 push @SpecifiedElementIDs, @GroupElements; 333 } 334 else { 335 warn "Ignoring period number, $ElementID, specified using command line parameter: Unknown period number...\n"; 336 next ELEMENTID; 337 } 338 } 339 } 340 } 341 SetupSpecifiedProperties(); 342 343 # Setup output file name... 344 $OutFileName = ''; 345 if ($FileOutput) { 346 my($OutFileRoot, $OutFileExt); 347 348 $OutFileRoot = ''; 349 $OutFileExt = "csv"; 350 if ($Options{outdelim} =~ /^tab$/i) { 351 $OutFileExt = "tsv"; 352 } 353 if ($Options{root}) { 354 my ($RootFileDir, $RootFileName, $RootFileExt) = ParseFileName($Options{root}); 355 if ($RootFileName && $RootFileExt) { 356 $OutFileRoot = $RootFileName; 357 } 358 else { 359 $OutFileRoot = $Options{root}; 360 } 361 } 362 else { 363 $OutFileRoot = 'PeriodicTableElementsInfo' . $Options{mode}; 364 } 365 $OutFileName = $OutFileRoot . '.' . $OutFileExt; 366 if (!$Options{overwrite}) { 367 if (-e $OutFileName) { 368 die "Error: Output file, $OutFileName, already exists.\nUse \-o --overwrite\ option or specify a different name using \"-r --root\" option.\n"; 369 } 370 } 371 } 372 } 373 374 # Setup properties to list... 375 sub SetupSpecifiedProperties { 376 # Make sure atomic appropriate properties/category names are specified... 377 @SpecifiedProperies = (); 378 if ($Options{properties} && ($Options{propertiesmode} =~ /^All$/i) ) { 379 warn "Warning: Ignoring values specifed by \"-p --properties\" option: Not valid for All value of \"--propertiesmode\" option...\n"; 380 } 381 if ($Options{propertiesmode} =~ /^All$/i) { 382 if ($Options{propertieslisting} =~ /^Alphabetical$/i) { 383 push @SpecifiedProperies, PeriodicTable::GetElementPropertiesNames('Alphabetical'); 384 } 385 else { 386 push @SpecifiedProperies, PeriodicTable::GetElementPropertiesNames(); 387 } 388 push @SpecifiedProperies, 'NaturalIsotopeData'; 389 } 390 else { 391 if ($Options{properties}) { 392 if ($Options{propertiesmode} =~ /^Categories$/i) { 393 # Check category name... 394 if ($Options{properties} !~ /^(Basic|BasicAndNaturalIsotope|NaturalIsotope)$/i) { 395 die "Error: The value specified, $Options{properties}, for option \"-p --properties\" in conjunction with \"Categories\" value for option \"--propertiesmode\" is not valid. Allowed values: Basic, BasicAndNaturalIsotope, NaturalIsotope\n"; 396 } 397 # Set propertynames... 398 push @SpecifiedProperies, GetPropertyNamesFromCategories($Options{properties}); 399 } 400 else { 401 # Check property names.. 402 my($Name, $PropertyName, @Names); 403 @Names = split /\,/, $Options{properties}; 404 NAME: for $Name (@Names) { 405 $PropertyName = RemoveLeadingAndTrailingWhiteSpaces($Name); 406 if ($PropertyName =~ /^NaturalIsotopeData$/i) { 407 push @SpecifiedProperies, $PropertyName; 408 next NAME; 409 } 410 if (PeriodicTable::IsElementProperty($PropertyName)) { 411 push @SpecifiedProperies, $PropertyName; 412 } 413 else { 414 warn "Warning: Ignoring value, $Name, specifed by \"-p --properties\" option: Unknown property name...\n"; 415 } 416 } 417 if ($Options{propertieslisting} =~ /^Alphabetical$/i) { 418 # AtomicNumber, ElementSymbol and ElementName are always listed first and 419 # NaturalIsotopeData in the end... 420 my($AtomicNumberPresent, $ElementSymbolPresent, $ElementNamePresent, $NaturalIsotopeDataPresent, @AlphabeticalProperties, %PropertiesMap); 421 %PropertiesMap = (); 422 @AlphabeticalProperties = (); 423 $AtomicNumberPresent = 0; $ElementSymbolPresent = 0; $ElementNamePresent = 0; $NaturalIsotopeDataPresent = 0; 424 NAME: for $Name (@SpecifiedProperies) { 425 if ($Name =~ /^AtomicNumber$/i) { 426 $AtomicNumberPresent = 1; 427 next NAME; 428 } 429 if ($Name =~ /^ElementSymbol$/i) { 430 $ElementSymbolPresent = 1; 431 next NAME; 432 } 433 if ($Name =~ /^ElementName$/i) { 434 $ElementNamePresent = 1; 435 next NAME; 436 } 437 if ($Name =~ /^NaturalIsotopeData$/i) { 438 $NaturalIsotopeDataPresent = 1; 439 next NAME; 440 } 441 $PropertiesMap{$Name} = $Name; 442 } 443 # Setup the alphabetical list... 444 if ($AtomicNumberPresent) { 445 push @AlphabeticalProperties, 'AtomicNumber'; 446 } 447 if ($ElementSymbolPresent) { 448 push @AlphabeticalProperties, 'ElementSymbol'; 449 } 450 if ($ElementNamePresent) { 451 push @AlphabeticalProperties, 'ElementName'; 452 } 453 for $Name (sort keys %PropertiesMap) { 454 push @AlphabeticalProperties, $Name; 455 } 456 if ($NaturalIsotopeDataPresent) { 457 push @AlphabeticalProperties, 'NaturalIsotopeData'; 458 } 459 @SpecifiedProperies = (); 460 push @SpecifiedProperies, @AlphabeticalProperties; 461 } 462 } 463 } 464 else { 465 # Set default value... 466 push @SpecifiedProperies, GetPropertyNamesFromCategories('Basic'); 467 } 468 } 469 } 470 471 # Setup script usage and retrieve command line arguments specified using various options... 472 sub SetupScriptUsage { 473 474 # Retrieve all the options... 475 %Options = (); 476 $Options{mode} = "ElementID"; 477 $Options{outdelim} = "comma"; 478 $Options{output} = "STDOUT"; 479 $Options{outputstyle} = "ElementBlock"; 480 $Options{precision} = 4; 481 $Options{propertiesmode} = "Categories"; 482 $Options{propertieslisting} = "ByGroup"; 483 $Options{quote} = "yes"; 484 485 if (!GetOptions(\%Options, "help|h", "mode|m=s", "outdelim=s", "output=s", "outputstyle=s", "overwrite|o", "precision=i", "properties|p=s", "propertieslisting=s", "propertiesmode=s", "quote|q=s", "root|r=s", "workingdir|w=s")) { 486 die "\nTo get a list of valid options and their values, use \"$ScriptName -h\" or\n\"perl -S $ScriptName -h\" command and try again...\n"; 487 } 488 if ($Options{workingdir}) { 489 if (! -d $Options{workingdir}) { 490 die "Error: The value specified, $Options{workingdir}, for option \"-w --workingdir\" is not a directory name.\n"; 491 } 492 chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n"; 493 } 494 if ($Options{mode} !~ /^(ElementID|AmericanGroupLabel|EuropeanGroupLabel|GroupNumber|GroupName|PeriodNumber|All)$/i) { 495 die "Error: The value specified, $Options{mode}, for option \"-m --mode\" is not valid. Allowed values: ElementID, AmericanGroupLabel, EuropeanGroupLabel, GroupNumber, GroupName, PeriodNumber, or All\n"; 496 } 497 if ($Options{outdelim} !~ /^(comma|semicolon|tab)$/i) { 498 die "Error: The value specified, $Options{outdelim}, for option \"--outdelim\" is not valid. Allowed values: comma, tab, or semicolon\n"; 499 } 500 if ($Options{output} !~ /^(STDOUT|File)$/i) { 501 die "Error: The value specified, $Options{output}, for option \"--output\" is not valid. Allowed values: STDOUT or File\n"; 502 } 503 if ($Options{outputstyle} !~ /^(ElementBlock|ElementRows)$/i) { 504 die "Error: The value specified, $Options{outputstyle}, for option \"--outputstyle\" is not valid. Allowed values: ElementBlock or ElementRows\n"; 505 } 506 if (!IsPositiveInteger($Options{precision})) { 507 die "Error: The value specified, $Options{precision}, for option \"-p --precision\" is not valid. Allowed values: > 0 \n"; 508 } 509 if ($Options{propertiesmode} !~ /^(Categories|Names|All)$/i) { 510 die "Error: The value specified, $Options{propertiesmode}, for option \"--propertiesmode\" is not valid. Allowed values: Categories, Names, or All\n"; 511 } 512 if ($Options{propertieslisting} !~ /^(ByGroup|Alphabetical)$/i) { 513 die "Error: The value specified, $Options{propertieslisting}, for option \"--propertieslisting\" is not valid. Allowed values: ByGroup, or Alphabetical\n"; 514 } 515 if ($Options{quote} !~ /^(yes|no)$/i) { 516 die "Error: The value specified, $Options{quote}, for option \"-q --quote\" is not valid. Allowed values: yes or no\n"; 517 } 518 } 519