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