MayaChemTools

   1 #!/usr/bin/perl -w
   2 #
   3 # $RCSfile: ElementalAnalysisSDFiles.pl,v $
   4 # $Date: 2008/01/30 21:44:45 $
   5 # $Revision: 1.9 $
   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 SDFileUtil;
  38 use TextUtil;
  39 use MolecularFormula;
  40 
  41 my($ScriptName, %Options, $StartTime, $EndTime, $TotalTime);
  42 
  43 # Autoflush STDOUT
  44 $| = 1;
  45 
  46 # Starting message...
  47 $ScriptName = basename($0);
  48 print "\n$ScriptName: Starting...\n\n";
  49 $StartTime = new Benchmark;
  50 
  51 # Get the options and setup script...
  52 SetupScriptUsage();
  53 if ($Options{help} || @ARGV < 1) {
  54   die GetUsageFromPod("$FindBin::Bin/$ScriptName");
  55 }
  56 
  57 my(@SDFilesList);
  58 @SDFilesList = ExpandFileNames(\@ARGV, "sdf sd");
  59 
  60 my($SpecifiedFormulaFieldName, $CheckFormula, $Precision, $DetailLevel, @SpecifiedCalculations, @SpecifiedValueFieldNames);
  61 ProcessOptions();
  62 
  63 print "Checking input SD file(s)...\n";
  64 my(@SDFilesOkay, @SDFilesOutFile, @SDFilesFormulaFieldName, @SDFilesValueFieldNamesMap);
  65 RetrieveSDFilesInfo();
  66 
  67 # Generate output files...
  68 my($Index, $SDFile);
  69 if (@SDFilesList > 1) {
  70   print "Processing SD file(s)...\n";
  71 }
  72 for $Index (0 .. $#SDFilesList) {
  73   if ($SDFilesOkay[$Index]) {
  74     $SDFile = $SDFilesList[$Index];
  75     if (@SDFilesList > 1) {
  76       print "\nProcessing file $SDFile...\n";
  77     }
  78     else {
  79       print "Processing file $SDFile...\n"
  80     }
  81     PerformElementalAnalysis($Index);
  82   }
  83 }
  84 
  85 print "$ScriptName:Done...\n\n";
  86 
  87 $EndTime = new Benchmark;
  88 $TotalTime = timediff ($EndTime, $StartTime);
  89 print "Total time: ", timestr($TotalTime), "\n";
  90 
  91 ###############################################################################
  92 
  93 # Perform analysis...
  94 sub PerformElementalAnalysis {
  95   my($Index) = @_;
  96   my($SDFile, $NewSDFile, $KeyDataFieldName);
  97 
  98   $SDFile = $SDFilesList[$Index];
  99   $NewSDFile = $SDFilesOutFile[$Index];
 100 
 101   print "Generating new SD file $NewSDFile...\n";
 102   open NEWSDFILE, ">$NewSDFile" or die "Error: Couldn't open $NewSDFile: $! \n";
 103   open SDFILE, "$SDFile" or die "Error: Can't open $SDFile: $! \n";
 104 
 105   # Go over all compound records and store 'em using key value as hash...
 106   my($CmpdCount, $FormulaFieldName, $FormulaFieldValue, $CmpdString, $Value, $CalculationType, $CalculatedValue, $ErrorMsg, $Status, $ElementsRef, $ElementCompositionRef, @CalculatedValues, @CmpdLines, %DataFieldValuesMap);
 107   $CmpdCount = 0;
 108   $FormulaFieldName = $SDFilesFormulaFieldName[$Index];
 109 
 110   COMPOUND: while ($CmpdString = ReadCmpdString(\*SDFILE)) {
 111     $CmpdCount++;
 112     @CmpdLines = split "\n", $CmpdString;
 113     %DataFieldValuesMap = GetCmpdDataHeaderLabelsAndValues(\@CmpdLines);
 114 
 115     @CalculatedValues = ();
 116     for $Value (@SpecifiedCalculations) {
 117       push @CalculatedValues, '';
 118     }
 119 
 120     if (!exists $DataFieldValuesMap{$FormulaFieldName}) {
 121       $ErrorMsg = "Ignoring compound record $CmpdCount: Formula field $FormulaFieldName not found";
 122       PrintErrorMsg($CmpdString, $ErrorMsg);
 123       WriteNewCompoundRecord($Index, \*NEWSDFILE, \@CmpdLines, \@CalculatedValues);
 124       next COMPOUND;
 125     }
 126 
 127     # Make sure it's a valid molecular formula...
 128     $FormulaFieldValue = $DataFieldValuesMap{$FormulaFieldName};
 129     if ($CheckFormula) {
 130       ($Status, $ErrorMsg) = MolecularFormula::IsMolecularFormula($FormulaFieldValue);
 131       if (!$Status) {
 132 	$ErrorMsg = "Ignoring compound record $CmpdCount: Formula field value $FormulaFieldValue is not valid: $ErrorMsg";
 133 	PrintErrorMsg($CmpdString, $ErrorMsg);
 134 	WriteNewCompoundRecord($Index, \*NEWSDFILE, \@CmpdLines, \@CalculatedValues);
 135 	next COMPOUND;
 136       }
 137     }
 138     # Calculate appropriate values and write 'em out...
 139     @CalculatedValues = ();
 140     for $CalculationType (@SpecifiedCalculations) {
 141       if ($CalculationType =~ /^ElementalAnalysis$/i) {
 142 	($ElementsRef, $ElementCompositionRef) = MolecularFormula::CalculateElementalComposition($FormulaFieldValue);
 143 	$CalculatedValue = (defined($ElementsRef) && defined($ElementCompositionRef)) ? MolecularFormula::FormatCompositionInfomation($ElementsRef, $ElementCompositionRef, $Precision) : '';
 144       }
 145       elsif ($CalculationType =~ /^MolecularWeight$/i) {
 146 	$CalculatedValue = MolecularFormula::CalculateMolecularWeight($FormulaFieldValue);
 147 	$CalculatedValue = (defined($CalculatedValue) && length($CalculatedValue)) ? (sprintf("%.${Precision}f", $CalculatedValue)) : "";
 148       }
 149       elsif ($CalculationType =~ /^ExactMass$/i) {
 150 	$CalculatedValue = MolecularFormula::CalculateExactMass($FormulaFieldValue);
 151 	$CalculatedValue = (defined($CalculatedValue) && length($CalculatedValue)) ? (sprintf("%.${Precision}f", $CalculatedValue)) : "";
 152       }
 153       else {
 154 	$CalculatedValue = '';
 155       }
 156       push @CalculatedValues, $CalculatedValue;
 157     }
 158     WriteNewCompoundRecord($Index, \*NEWSDFILE, \@CmpdLines, \@CalculatedValues);
 159   }
 160   close NEWSDFILE;
 161   close SDFILE;
 162 }
 163 
 164 # Write out compound record with calculated values...
 165 sub WriteNewCompoundRecord {
 166   my($Index, $SDFileRef, $CmpdLinesRef, $CalculatedValuesRef) = @_;
 167 
 168   # Write out compound lines except the last line which contains $$$$...
 169   my($LineIndex);
 170   for $LineIndex (0 .. ($#{$CmpdLinesRef} - 1)) {
 171     print $SDFileRef "$CmpdLinesRef->[$LineIndex]\n";
 172   }
 173 
 174   # Write out calculated values...
 175   my($CalcIndex, $FieldName, $FieldValue);
 176   for $CalcIndex (0 .. $#SpecifiedCalculations) {
 177     $FieldName = $SDFilesValueFieldNamesMap[$Index]{$SpecifiedCalculations[$CalcIndex]};
 178     $FieldValue = $CalculatedValuesRef->[$CalcIndex];
 179     print  $SDFileRef ">  <$FieldName>\n$FieldValue\n\n";
 180   }
 181   print $SDFileRef  "\$\$\$\$\n";
 182 }
 183 
 184 # Print out error message...
 185 sub PrintErrorMsg {
 186   my($CmpdString, $ErrorMsg) = @_;
 187 
 188   if ($DetailLevel >= 2 ) {
 189     print "$ErrorMsg:\n$CmpdString\n";
 190   }
 191   elsif ($DetailLevel >= 1) {
 192     print "$ErrorMsg\n";
 193   }
 194 }
 195 
 196 # Process option values...
 197 sub ProcessOptions {
 198   $DetailLevel = $Options{detail};
 199   $CheckFormula = $Options{fast} ? 0 : 1;
 200   $Precision = $Options{precision};
 201 
 202   $SpecifiedFormulaFieldName = "";
 203   if (defined $Options{formulafield}) {
 204     $SpecifiedFormulaFieldName = $Options{formulafield};
 205   }
 206 
 207   # Setup what to calculate...
 208   @SpecifiedCalculations = ();
 209   if ($Options{mode} =~ /^All$/i) {
 210     @SpecifiedCalculations = qw(ElementalAnalysis MolecularWeight ExactMass);
 211   }
 212   else {
 213     my($Mode, $ModeValue, @SpecifiedModeValues);
 214     $Mode = $Options{mode};
 215     $Mode =~ s/ //g;
 216     @SpecifiedModeValues = split /\,/, $Mode;
 217     for $ModeValue (@SpecifiedModeValues) {
 218       if ($ModeValue !~ /^(ElementalAnalysis|MolecularWeight|ExactMass)$/i) {
 219 	if ($ModeValue =~ /^All$/i) {
 220 	  die "Error: All value for option \"-m --mode\" is not allowed with other valid values.\n";
 221 	}
 222 	else {
 223 	  die "Error: The value specified, $ModeValue, for option \"-m --mode\" is not valid. Allowed values: ElementalAnalysis, MolecularWeight, or ExactMass\n";
 224 	}
 225       }
 226       push @SpecifiedCalculations, $ModeValue;
 227     }
 228   }
 229 
 230   @SpecifiedValueFieldNames = ();
 231   if ($Options{valuefieldnames}) {
 232     my($Value, $Label, @ValueLabels);
 233     @ValueLabels = split /\,/, $Options{valuefieldnames};
 234     if (@ValueLabels % 2) {
 235       die "Error: The value specified, $Options{valuefieldnames}, for option \"-v --valuefieldnames\" is not valid: It must contain even number of comma delimited values\n";
 236     }
 237     for ($Index = 0; $Index < @ValueLabels; $Index +=2) {
 238       $Value = $ValueLabels[$Index];
 239       $Value =~ s/ //g;
 240       $Label = $ValueLabels[$Index + 1];
 241       if ($Value !~ /^(ElementalAnalysis|MolecularWeight|ExactMass)$/i) {
 242 	die "Error: The value specified, $Value, using option \"-v --valuefieldnames\" is not valid. Allowed values: ElementalAnalysis, MolecularWeight, or ExactMass\n";
 243       }
 244       push @SpecifiedValueFieldNames, ($Value, $Label);
 245     }
 246   }
 247 }
 248 
 249 # Retrieve information about input SD files...
 250 sub RetrieveSDFilesInfo {
 251   my($Index, $SDFile, $FileDir, $FileName, $FileExt, $OutFileRoot,  $OutFile, $FormulaFieldName, $Value, $FieldName, $NewFieldName, $Count);
 252 
 253   my(%NewValueFieldNames) = (ElementalAnalysis => 'ElementalAnalysis', MolecularWeight => 'MolecularWeight', ExactMass => 'ExactMass');
 254   if (@SpecifiedValueFieldNames) {
 255     for ($Index = 0; $Index < @SpecifiedValueFieldNames; $Index +=2) {
 256       $Value = $SpecifiedValueFieldNames[$Index];
 257       $FieldName = $SpecifiedValueFieldNames[$Index + 1];
 258       if (exists $NewValueFieldNames{$Value}) {
 259 	$NewValueFieldNames{$Value} = $FieldName;
 260       }
 261     }
 262   }
 263 
 264   @SDFilesOkay = ();
 265   @SDFilesOutFile = ();
 266   @SDFilesFormulaFieldName = ();
 267   @SDFilesValueFieldNamesMap = ();
 268 
 269  FILELIST: for $Index (0 .. $#SDFilesList) {
 270     $SDFile = $SDFilesList[$Index];
 271     $SDFilesOkay[$Index] = 0;
 272     $SDFilesOutFile[$Index] = '';
 273     $SDFilesFormulaFieldName[$Index] = '';
 274     %{$SDFilesValueFieldNamesMap[$Index]} = ();
 275 
 276     if (!(-e $SDFile)) {
 277       warn "Warning: Ignoring file $SDFile: It doesn't exist\n";
 278       next FILELIST;
 279     }
 280     if (!CheckFileType($SDFile, "sd sdf")) {
 281       warn "Warning: Ignoring file $SDFile: It's not a SD file\n";
 282       next FILELIST;
 283     }
 284     $FileDir = ""; $FileName = ""; $FileExt = "";
 285     ($FileDir, $FileName, $FileExt) = ParseFileName($SDFile);
 286     if ($Options{root} && (@SDFilesList == 1)) {
 287       my ($RootFileDir, $RootFileName, $RootFileExt) = ParseFileName($Options{root});
 288       if ($RootFileName && $RootFileExt) {
 289 	$FileName = $RootFileName;
 290       }
 291       else {
 292 	$FileName = $Options{root};
 293       }
 294       $OutFileRoot = $FileName;
 295     }
 296     else {
 297       $OutFileRoot = $FileName . "ElementalAnalysis";
 298     }
 299 
 300     $OutFile = $OutFileRoot . ".$FileExt";
 301     if (lc($OutFile) eq lc($SDFile)) {
 302       warn "Warning: Ignoring file $SDFile:Output file name, $OutFile, is same as input SD file name, $SDFile\n";
 303       next FILELIST;
 304     }
 305     if (!$Options{overwrite}) {
 306       if (-e $OutFile) {
 307 	warn "Warning: Ignoring file $SDFile: The file $OutFile already exists\n";
 308 	next FILELIST;
 309       }
 310     }
 311     # Get data field names and values...
 312     my($CmpdString, $FieldName, @CmpdLines, @DataFieldNames, %DataFieldNamesMap);
 313     @DataFieldNames = ();
 314     if (!open(SDFILE, "$SDFile")) {
 315       warn "Warning: Ignoring file $SDFile: Couldn't open it: $! \n";
 316       next FILELIST;
 317     }
 318     $CmpdString = ReadCmpdString(\*SDFILE);
 319     close SDFILE;
 320 
 321     @CmpdLines = split "\n", $CmpdString;
 322     @DataFieldNames = GetCmpdDataHeaderLabels(\@CmpdLines);
 323     %DataFieldNamesMap = GetCmpdDataHeaderLabelsAndValues(\@CmpdLines);
 324 
 325     # Setup formula field name...
 326     $FormulaFieldName = '';
 327     if ($SpecifiedFormulaFieldName) {
 328       $FormulaFieldName = $SpecifiedFormulaFieldName;
 329     }
 330     else {
 331       FIELDNAME: for $FieldName (@DataFieldNames) {
 332 	if ($FieldName =~ /Formula/i) {
 333 	  $FormulaFieldName = $FieldName;
 334 	  last FIELDNAME;
 335 	}
 336       }
 337       if (!$FormulaFieldName) {
 338 	warn "Warning: Ignoring file $SDFile: Data field label containing the word Formula doesn't exist\n";
 339 	next FILELIST;
 340       }
 341     }
 342     $SDFilesOkay[$Index] = 1;
 343     $SDFilesOutFile[$Index] = $OutFile;
 344     $SDFilesFormulaFieldName[$Index] = $FormulaFieldName;
 345 
 346     # Setup value data field names for calculated values...
 347     for $Value (keys %NewValueFieldNames) {
 348       $FieldName = $NewValueFieldNames{$Value};
 349 
 350       # Make sure it doesn't already exists...
 351       $Count = 1;
 352       $NewFieldName = $FieldName;
 353       while (exists $DataFieldNamesMap{$NewFieldName}) {
 354 	$Count++;
 355 	$NewFieldName = $FieldName . $Count;
 356       }
 357       $SDFilesValueFieldNamesMap[$Index]{$Value} = $NewFieldName;
 358     }
 359   }
 360 }
 361 
 362 # Setup script usage  and retrieve command line arguments specified using various options...
 363 sub SetupScriptUsage {
 364 
 365   # Retrieve all the options...
 366   %Options = ();
 367   $Options{detail} = 1;
 368   $Options{mode} = "All";
 369   $Options{precision} = 2;
 370 
 371   if (!GetOptions(\%Options, "detail|d=i", "fast", "formulafield|f=s", "mode|m=s", "help|h", "overwrite|o", "precision|p=i", "root|r=s", "valuefieldnames|v=s", "workingdir|w=s")) {
 372     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";
 373   }
 374   if ($Options{workingdir}) {
 375     if (! -d $Options{workingdir}) {
 376       die "Error: The value specified, $Options{workingdir}, for option \"-w --workingdir\" is not a directory name.\n";
 377     }
 378     chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n";
 379   }
 380   if (!IsPositiveInteger($Options{detail})) {
 381     die "Error: The value specified, $Options{detail}, for option \"-d --detail\" is not valid. Allowed values: > 0\n";
 382   }
 383   if (!IsPositiveInteger($Options{precision})) {
 384     die "Error: The value specified, $Options{precision}, for option \"-p --precision\" is not valid. Allowed values: > 0 \n";
 385   }
 386 }
 387