MayaChemTools

   1 #!/usr/bin/perl -w
   2 #
   3 # $RCSfile: ExtendedConnectivityFingerprints.pl,v $
   4 # $Date: 2011/12/27 20:27:03 $
   5 # $Revision: 1.30 $
   6 #
   7 # Author: Manish Sud <msud@san.rr.com>
   8 #
   9 # Copyright (C) 2004-2012 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 SDFileUtil;
  38 use MoleculeFileIO;
  39 use FileIO::FingerprintsSDFileIO;
  40 use FileIO::FingerprintsTextFileIO;
  41 use FileIO::FingerprintsFPFileIO;
  42 use AtomTypes::AtomicInvariantsAtomTypes;
  43 use AtomTypes::FunctionalClassAtomTypes;
  44 use Fingerprints::ExtendedConnectivityFingerprints;
  45 
  46 my($ScriptName, %Options, $StartTime, $EndTime, $TotalTime);
  47 
  48 # Autoflush STDOUT
  49 $| = 1;
  50 
  51 # Starting message...
  52 $ScriptName = basename($0);
  53 print "\n$ScriptName: Starting...\n\n";
  54 $StartTime = new Benchmark;
  55 
  56 # Get the options and setup script...
  57 SetupScriptUsage();
  58 if ($Options{help} || @ARGV < 1) {
  59   die GetUsageFromPod("$FindBin::Bin/$ScriptName");
  60 }
  61 
  62 my(@SDFilesList);
  63 @SDFilesList = ExpandFileNames(\@ARGV, "sdf sd");
  64 
  65 # Process options...
  66 print "Processing options...\n";
  67 my(%OptionsInfo);
  68 ProcessOptions();
  69 
  70 # Setup information about input files...
  71 print "Checking input SD file(s)...\n";
  72 my(%SDFilesInfo);
  73 RetrieveSDFilesInfo();
  74 
  75 # Process input files..
  76 my($FileIndex);
  77 if (@SDFilesList > 1) {
  78   print "\nProcessing SD files...\n";
  79 }
  80 for $FileIndex (0 .. $#SDFilesList) {
  81   if ($SDFilesInfo{FileOkay}[$FileIndex]) {
  82     print "\nProcessing file $SDFilesList[$FileIndex]...\n";
  83     GenerateExtendedConnectivityFingerprints($FileIndex);
  84   }
  85 }
  86 print "\n$ScriptName:Done...\n\n";
  87 
  88 $EndTime = new Benchmark;
  89 $TotalTime = timediff ($EndTime, $StartTime);
  90 print "Total time: ", timestr($TotalTime), "\n";
  91 
  92 ###############################################################################
  93 
  94 # Generate fingerprints for a SD file...
  95 #
  96 sub GenerateExtendedConnectivityFingerprints {
  97   my($FileIndex) = @_;
  98   my($CmpdCount, $IgnoredCmpdCount, $SDFile, $MoleculeFileIO, $Molecule, $ExtendedConnectivityFingerprints, $NewFPSDFileIO, $NewFPTextFileIO, $NewFPFileIO);
  99 
 100   $SDFile = $SDFilesList[$FileIndex];
 101 
 102   # Setup output files...
 103   #
 104   ($NewFPSDFileIO, $NewFPTextFileIO, $NewFPFileIO) = SetupAndOpenOutputFiles($FileIndex);
 105 
 106   $MoleculeFileIO = new MoleculeFileIO('Name' => $SDFile);
 107   $MoleculeFileIO->Open();
 108 
 109   $CmpdCount = 0;
 110   $IgnoredCmpdCount = 0;
 111 
 112   COMPOUND: while ($Molecule = $MoleculeFileIO->ReadMolecule()) {
 113     $CmpdCount++;
 114 
 115     # Filter compound data before calculating fingerprints...
 116     if ($OptionsInfo{Filter}) {
 117       if (CheckAndFilterCompound($CmpdCount, $Molecule)) {
 118         $IgnoredCmpdCount++;
 119         next COMPOUND;
 120       }
 121     }
 122 
 123     $ExtendedConnectivityFingerprints = GenerateMoleculeFingerprints($Molecule);
 124     if (!$ExtendedConnectivityFingerprints) {
 125       $IgnoredCmpdCount++;
 126       ProcessIgnoredCompound('FingerprintsGenerationFailed', $CmpdCount, $Molecule);
 127       next COMPOUND;
 128     }
 129 
 130     WriteDataToOutputFiles($FileIndex, $CmpdCount, $Molecule, $ExtendedConnectivityFingerprints, $NewFPSDFileIO, $NewFPTextFileIO, $NewFPFileIO);
 131   }
 132   $MoleculeFileIO->Close();
 133 
 134   if ($NewFPSDFileIO) {
 135     $NewFPSDFileIO->Close();
 136   }
 137   if ($NewFPTextFileIO) {
 138     $NewFPTextFileIO->Close();
 139   }
 140   if ($NewFPFileIO) {
 141     $NewFPFileIO->Close();
 142   }
 143 
 144   WriteFingerprintsGenerationSummaryStatistics($CmpdCount, $IgnoredCmpdCount);
 145 }
 146 
 147 # Process compound being ignored due to problems in fingerprints geneation...
 148 #
 149 sub ProcessIgnoredCompound {
 150   my($Mode, $CmpdCount, $Molecule) = @_;
 151   my($CmpdID, $DataFieldLabelAndValuesRef);
 152 
 153   $DataFieldLabelAndValuesRef = $Molecule->GetDataFieldLabelAndValues();
 154   $CmpdID = SetupCmpdIDForOutputFiles($CmpdCount, $Molecule, $DataFieldLabelAndValuesRef);
 155 
 156   MODE: {
 157     if ($Mode =~ /^ContainsNonElementalData$/i) {
 158       warn "\nWarning: Ignoring compound record number $CmpdCount with ID $CmpdID: Compound contains atom data corresponding to non-elemental atom symbol(s)...\n\n";
 159       next MODE;
 160     }
 161 
 162     if ($Mode =~ /^ContainsNoElementalData$/i) {
 163       warn "\nWarning: Ignoring compound record number $CmpdCount with ID $CmpdID: Compound contains no atom data...\n\n";
 164       next MODE;
 165     }
 166 
 167     if ($Mode =~ /^FingerprintsGenerationFailed$/i) {
 168       warn "\nWarning: Ignoring compound record number $CmpdCount with ID $CmpdID: Fingerprints generation didn't succeed...\n\n";
 169       next MODE;
 170     }
 171     warn "\nWarning: Ignoring compound record number $CmpdCount with ID $CmpdID: Fingerprints generation didn't succeed...\n\n";
 172   }
 173 }
 174 
 175 # Check and filter compounds....
 176 #
 177 sub CheckAndFilterCompound {
 178   my($CmpdCount, $Molecule) = @_;
 179   my($ElementCount, $NonElementCount);
 180 
 181   ($ElementCount, $NonElementCount) = $Molecule->GetNumOfElementsAndNonElements();
 182 
 183   if ($NonElementCount) {
 184     ProcessIgnoredCompound('ContainsNonElementalData', $CmpdCount, $Molecule);
 185     return 1;
 186   }
 187 
 188   if (!$ElementCount) {
 189     ProcessIgnoredCompound('ContainsNoElementalData', $CmpdCount, $Molecule);
 190     return 1;
 191   }
 192 
 193   return 0;
 194 }
 195 
 196 # Write out compounds fingerprints generation summary statistics...
 197 #
 198 sub WriteFingerprintsGenerationSummaryStatistics {
 199   my($CmpdCount, $IgnoredCmpdCount) = @_;
 200   my($ProcessedCmpdCount);
 201 
 202   $ProcessedCmpdCount = $CmpdCount - $IgnoredCmpdCount;
 203 
 204   print "\nNumber of compounds: $CmpdCount\n";
 205   print "Number of compounds processed successfully during fingerprints generation: $ProcessedCmpdCount\n";
 206   print "Number of compounds ignored during fingerprints generation: $IgnoredCmpdCount\n";
 207 }
 208 
 209 # Open output files...
 210 #
 211 sub SetupAndOpenOutputFiles {
 212   my($FileIndex) = @_;
 213   my($NewFPSDFile, $NewFPFile, $NewFPTextFile, $NewFPSDFileIO, $NewFPTextFileIO, $NewFPFileIO, %FingerprintsFileIOParams);
 214 
 215   ($NewFPSDFileIO, $NewFPTextFileIO, $NewFPFileIO) = (undef) x 3;
 216 
 217   # Setup common parameters for fingerprints file IO objects...
 218   #
 219   %FingerprintsFileIOParams = ();
 220   if ($OptionsInfo{Mode} =~ /^(ExtendedConnectivity|ExtendedConnectivityCount)$/i) {
 221     %FingerprintsFileIOParams = ('Mode' => 'Write', 'Overwrite' => $OptionsInfo{OverwriteFiles}, 'FingerprintsStringMode' => 'FingerprintsVectorString', 'VectorStringFormat' => $OptionsInfo{VectorStringFormat});
 222   }
 223   elsif ($OptionsInfo{Mode} =~ /^ExtendedConnectivityBits$/i) {
 224     %FingerprintsFileIOParams = ('Mode' => 'Write', 'Overwrite' => $OptionsInfo{OverwriteFiles}, 'FingerprintsStringMode' => 'FingerprintsBitVectorString', 'BitStringFormat' => $OptionsInfo{BitStringFormat}, 'BitsOrder' => $OptionsInfo{BitsOrder});
 225   }
 226 
 227   if ($OptionsInfo{SDOutput}) {
 228     $NewFPSDFile = $SDFilesInfo{SDOutFileNames}[$FileIndex];
 229     print "Generating SD file $NewFPSDFile...\n";
 230     $NewFPSDFileIO = new FingerprintsSDFileIO('Name' => $NewFPSDFile, %FingerprintsFileIOParams, 'FingerprintsFieldLabel' => $OptionsInfo{FingerprintsLabel});
 231     $NewFPSDFileIO->Open();
 232   }
 233 
 234   if ($OptionsInfo{FPOutput}) {
 235     $NewFPFile = $SDFilesInfo{FPOutFileNames}[$FileIndex];
 236     print "Generating FP file $NewFPFile...\n";
 237     $NewFPFileIO = new FingerprintsFPFileIO('Name' => $NewFPFile, %FingerprintsFileIOParams);
 238     $NewFPFileIO->Open();
 239   }
 240 
 241   if ($OptionsInfo{TextOutput}) {
 242     my($ColLabelsRef);
 243 
 244     $NewFPTextFile = $SDFilesInfo{TextOutFileNames}[$FileIndex];
 245     $ColLabelsRef = SetupFPTextFileCoulmnLabels($FileIndex);
 246 
 247     print "Generating text file $NewFPTextFile...\n";
 248     $NewFPTextFileIO = new FingerprintsTextFileIO('Name' => $NewFPTextFile, %FingerprintsFileIOParams, 'DataColLabels' => $ColLabelsRef, 'OutDelim' => $OptionsInfo{OutDelim}, 'OutQuote' => $OptionsInfo{OutQuote});
 249     $NewFPTextFileIO->Open();
 250   }
 251 
 252   return ($NewFPSDFileIO, $NewFPTextFileIO, $NewFPFileIO);
 253 }
 254 
 255 # Write fingerpritns and other data to appropriate output files...
 256 #
 257 sub WriteDataToOutputFiles {
 258   my($FileIndex, $CmpdCount, $Molecule, $ExtendedConnectivityFingerprints, $NewFPSDFileIO, $NewFPTextFileIO, $NewFPFileIO) = @_;
 259   my($DataFieldLabelAndValuesRef);
 260 
 261   $DataFieldLabelAndValuesRef = undef;
 262   if ($NewFPTextFileIO || $NewFPFileIO) {
 263     $DataFieldLabelAndValuesRef = $Molecule->GetDataFieldLabelAndValues();
 264   }
 265 
 266   if ($NewFPSDFileIO) {
 267     my($CmpdString);
 268 
 269     $CmpdString = $Molecule->GetInputMoleculeString();
 270     $NewFPSDFileIO->WriteFingerprints($ExtendedConnectivityFingerprints, $CmpdString);
 271   }
 272 
 273   if ($NewFPTextFileIO) {
 274     my($ColValuesRef);
 275 
 276     $ColValuesRef = SetupFPTextFileCoulmnValues($FileIndex, $CmpdCount, $Molecule, $DataFieldLabelAndValuesRef);
 277     $NewFPTextFileIO->WriteFingerprints($ExtendedConnectivityFingerprints, $ColValuesRef);
 278   }
 279 
 280   if ($NewFPFileIO) {
 281     my($CompoundID);
 282 
 283     $CompoundID = SetupCmpdIDForOutputFiles($CmpdCount, $Molecule, $DataFieldLabelAndValuesRef);
 284     $NewFPFileIO->WriteFingerprints($ExtendedConnectivityFingerprints, $CompoundID);
 285   }
 286 }
 287 
 288 # Generate approriate column labels for FPText output file...
 289 #
 290 sub SetupFPTextFileCoulmnLabels {
 291   my($FileIndex) = @_;
 292   my($Line, @ColLabels);
 293 
 294   @ColLabels = ();
 295   if ($OptionsInfo{DataFieldsMode} =~ /^All$/i) {
 296     push @ColLabels, @{$SDFilesInfo{AllDataFieldsRef}[$FileIndex]};
 297   }
 298   elsif ($OptionsInfo{DataFieldsMode} =~ /^Common$/i) {
 299     push @ColLabels, @{$SDFilesInfo{CommonDataFieldsRef}[$FileIndex]};
 300   }
 301   elsif ($OptionsInfo{DataFieldsMode} =~ /^Specify$/i) {
 302     push @ColLabels, @{$OptionsInfo{SpecifiedDataFields}};
 303   }
 304   elsif ($OptionsInfo{DataFieldsMode} =~ /^CompoundID$/i) {
 305     push @ColLabels, $OptionsInfo{CompoundIDLabel};
 306   }
 307   # Add fingerprints label...
 308   push @ColLabels, $OptionsInfo{FingerprintsLabel};
 309 
 310   return \@ColLabels;
 311 }
 312 
 313 # Generate column values FPText output file..
 314 #
 315 sub SetupFPTextFileCoulmnValues {
 316   my($FileIndex, $CmpdCount, $Molecule, $DataFieldLabelAndValuesRef) = @_;
 317   my(@ColValues);
 318 
 319   @ColValues = ();
 320   if ($OptionsInfo{DataFieldsMode} =~ /^CompoundID$/i) {
 321     push @ColValues, SetupCmpdIDForOutputFiles($CmpdCount, $Molecule, $DataFieldLabelAndValuesRef);
 322   }
 323   elsif ($OptionsInfo{DataFieldsMode} =~ /^All$/i) {
 324     @ColValues = map { exists $DataFieldLabelAndValuesRef->{$_} ? $DataFieldLabelAndValuesRef->{$_} : ''} @{$SDFilesInfo{AllDataFieldsRef}[$FileIndex]};
 325   }
 326   elsif ($OptionsInfo{DataFieldsMode} =~ /^Common$/i) {
 327     @ColValues = map { exists $DataFieldLabelAndValuesRef->{$_} ? $DataFieldLabelAndValuesRef->{$_} : ''} @{$SDFilesInfo{CommonDataFieldsRef}[$FileIndex]};
 328   }
 329   elsif ($OptionsInfo{DataFieldsMode} =~ /^Specify$/i) {
 330     @ColValues = map { exists $DataFieldLabelAndValuesRef->{$_} ? $DataFieldLabelAndValuesRef->{$_} : ''} @{$OptionsInfo{SpecifiedDataFields}};
 331   }
 332 
 333   return \@ColValues;
 334 }
 335 
 336 # Generate compound ID for FP and FPText output files..
 337 #
 338 sub SetupCmpdIDForOutputFiles {
 339   my($CmpdCount, $Molecule, $DataFieldLabelAndValuesRef) = @_;
 340   my($CmpdID);
 341 
 342   $CmpdID = '';
 343   if ($OptionsInfo{CompoundIDMode} =~ /^MolNameOrLabelPrefix$/i) {
 344     my($MolName);
 345     $MolName = $Molecule->GetName();
 346     $CmpdID = $MolName ? $MolName : "$OptionsInfo{CompoundID}${CmpdCount}";
 347   }
 348   elsif ($OptionsInfo{CompoundIDMode} =~ /^LabelPrefix$/i) {
 349     $CmpdID = "$OptionsInfo{CompoundID}${CmpdCount}";
 350   }
 351   elsif ($OptionsInfo{CompoundIDMode} =~ /^DataField$/i) {
 352     my($SpecifiedDataField);
 353     $SpecifiedDataField = $OptionsInfo{CompoundID};
 354     $CmpdID = exists $DataFieldLabelAndValuesRef->{$SpecifiedDataField} ? $DataFieldLabelAndValuesRef->{$SpecifiedDataField} : '';
 355   }
 356   elsif ($OptionsInfo{CompoundIDMode} =~ /^MolName$/i) {
 357     $CmpdID = $Molecule->GetName();
 358   }
 359   return $CmpdID;
 360 }
 361 
 362 # Generate fingerprints for molecule...
 363 #
 364 sub GenerateMoleculeFingerprints {
 365   my($Molecule) = @_;
 366   my($ExtendedConnectivityFingerprints);
 367 
 368   if ($OptionsInfo{KeepLargestComponent}) {
 369     $Molecule->KeepLargestComponent();
 370   }
 371   if (!$Molecule->DetectRings()) {
 372     return undef;
 373   }
 374   $Molecule->DetectAromaticity();
 375 
 376   $ExtendedConnectivityFingerprints = undef;
 377   if ($OptionsInfo{Mode} =~ /^(ExtendedConnectivity|ExtendedConnectivityCount)$/i ) {
 378     $ExtendedConnectivityFingerprints = new ExtendedConnectivityFingerprints('Type' => $OptionsInfo{Mode}, 'Molecule' => $Molecule, 'NeighborhoodRadius' => $OptionsInfo{NeighborhoodRadius}, 'AtomIdentifierType' => $OptionsInfo{AtomIdentifierType});
 379   }
 380   elsif ($OptionsInfo{Mode} =~ /^ExtendedConnectivityBits$/i) {
 381     $ExtendedConnectivityFingerprints = new ExtendedConnectivityFingerprints('Type' => $OptionsInfo{Mode}, 'Molecule' => $Molecule, 'NeighborhoodRadius' => $OptionsInfo{NeighborhoodRadius}, 'AtomIdentifierType' => $OptionsInfo{AtomIdentifierType}, 'Size' => $OptionsInfo{Size}, 'UsePerlCoreRandom' => $OptionsInfo{UsePerlCoreRandom});
 382   }
 383   else {
 384     die "Error: The value specified, $Options{mode}, for option \"-m, --mode\" is not valid. Allowed values: ExtendedConnectivity,  ExtendedConnectivityCount or ExtendedConnectivityBits\n";
 385   }
 386   SetAtomIdentifierTypeValuesToUse($ExtendedConnectivityFingerprints);
 387 
 388   # Generate fingerprints...
 389   $ExtendedConnectivityFingerprints->GenerateFingerprints();
 390 
 391   # Make sure fingerprints generation is successful...
 392   if (!$ExtendedConnectivityFingerprints->IsFingerprintsGenerationSuccessful()) {
 393     return undef;
 394   }
 395 
 396   return $ExtendedConnectivityFingerprints;
 397 }
 398 
 399 # Set atom identifier type to use for generating fingerprints...
 400 #
 401 sub SetAtomIdentifierTypeValuesToUse {
 402   my($ExtendedConnectivityFingerprints) = @_;
 403 
 404   if ($OptionsInfo{AtomIdentifierType} =~ /^AtomicInvariantsAtomTypes$/i) {
 405     $ExtendedConnectivityFingerprints->SetAtomicInvariantsToUse(\@{$OptionsInfo{AtomicInvariantsToUse}});
 406   }
 407   elsif ($OptionsInfo{AtomIdentifierType} =~ /^FunctionalClassAtomTypes$/i) {
 408     $ExtendedConnectivityFingerprints->SetFunctionalClassesToUse(\@{$OptionsInfo{FunctionalClassesToUse}});
 409   }
 410   elsif ($OptionsInfo{AtomIdentifierType} =~ /^(DREIDINGAtomTypes|EStateAtomTypes|MMFF94AtomTypes|SLogPAtomTypes|SYBYLAtomTypes|TPSAAtomTypes|UFFAtomTypes)$/i) {
 411     # Nothing to do for now...
 412   }
 413   else {
 414     die "Error: The value specified, $Options{atomidentifiertype}, for option \"-a, --AtomIdentifierType\" is not valid. Supported atom identifier types in current release of MayaChemTools: AtomicInvariantsAtomTypes, DREIDINGAtomTypes, EStateAtomTypes, FunctionalClassAtomTypes, MMFF94AtomTypes, SLogPAtomTypes, SYBYLAtomTypes, TPSAAtomTypes, UFFAtomTypes\n";
 415   }
 416 }
 417 
 418 # Retrieve information about SD files...
 419 #
 420 sub RetrieveSDFilesInfo {
 421   my($SDFile, $Index, $FileDir, $FileExt, $FileName, $OutFileRoot, $TextOutFileExt, $SDOutFileExt, $FPOutFileExt, $NewSDFileName, $NewFPFileName, $NewTextFileName, $CheckDataField, $CollectDataFields, $AllDataFieldsRef, $CommonDataFieldsRef);
 422 
 423   %SDFilesInfo = ();
 424   @{$SDFilesInfo{FileOkay}} = ();
 425   @{$SDFilesInfo{OutFileRoot}} = ();
 426   @{$SDFilesInfo{SDOutFileNames}} = ();
 427   @{$SDFilesInfo{FPOutFileNames}} = ();
 428   @{$SDFilesInfo{TextOutFileNames}} = ();
 429   @{$SDFilesInfo{AllDataFieldsRef}} = ();
 430   @{$SDFilesInfo{CommonDataFieldsRef}} = ();
 431 
 432   $CheckDataField = ($OptionsInfo{TextOutput} && ($OptionsInfo{DataFieldsMode} =~ /^CompoundID$/i) && ($OptionsInfo{CompoundIDMode} =~ /^DataField$/i)) ? 1 : 0;
 433   $CollectDataFields = ($OptionsInfo{TextOutput} && ($OptionsInfo{DataFieldsMode} =~ /^(All|Common)$/i)) ? 1 : 0;
 434 
 435   FILELIST: for $Index (0 .. $#SDFilesList) {
 436     $SDFile = $SDFilesList[$Index];
 437 
 438     $SDFilesInfo{FileOkay}[$Index] = 0;
 439     $SDFilesInfo{OutFileRoot}[$Index] = '';
 440     $SDFilesInfo{SDOutFileNames}[$Index] = '';
 441     $SDFilesInfo{FPOutFileNames}[$Index] = '';
 442     $SDFilesInfo{TextOutFileNames}[$Index] = '';
 443 
 444     $SDFile = $SDFilesList[$Index];
 445     if (!(-e $SDFile)) {
 446       warn "Warning: Ignoring file $SDFile: It doesn't exist\n";
 447       next FILELIST;
 448     }
 449     if (!CheckFileType($SDFile, "sd sdf")) {
 450       warn "Warning: Ignoring file $SDFile: It's not a SD file\n";
 451       next FILELIST;
 452     }
 453 
 454     if ($CheckDataField) {
 455       # Make sure data field exists in SD file..
 456       my($CmpdString, $SpecifiedDataField, @CmpdLines, %DataFieldValues);
 457 
 458       @CmpdLines = ();
 459       open SDFILE, "$SDFile" or die "Error: Couldn't open $SDFile: $! \n";
 460       $CmpdString = ReadCmpdString(\*SDFILE);
 461       close SDFILE;
 462       @CmpdLines = split "\n", $CmpdString;
 463       %DataFieldValues = GetCmpdDataHeaderLabelsAndValues(\@CmpdLines);
 464       $SpecifiedDataField = $OptionsInfo{CompoundID};
 465       if (!exists $DataFieldValues{$SpecifiedDataField}) {
 466         warn "Warning: Ignoring file $SDFile: Data field value, $SpecifiedDataField, using  \"--CompoundID\" option in \"DataField\" \"--CompoundIDMode\" doesn't exist\n";
 467         next FILELIST;
 468       }
 469     }
 470 
 471     $AllDataFieldsRef = '';
 472     $CommonDataFieldsRef = '';
 473     if ($CollectDataFields) {
 474       my($CmpdCount);
 475       open SDFILE, "$SDFile" or die "Error: Couldn't open $SDFile: $! \n";
 476       ($CmpdCount, $AllDataFieldsRef, $CommonDataFieldsRef) = GetAllAndCommonCmpdDataHeaderLabels(\*SDFILE);
 477       close SDFILE;
 478     }
 479 
 480     # Setup output file names...
 481     $FileDir = ""; $FileName = ""; $FileExt = "";
 482     ($FileDir, $FileName, $FileExt) = ParseFileName($SDFile);
 483 
 484     $TextOutFileExt = "csv";
 485     if ($Options{outdelim} =~ /^tab$/i) {
 486       $TextOutFileExt = "tsv";
 487     }
 488     $SDOutFileExt = $FileExt;
 489     $FPOutFileExt = "fpf";
 490 
 491     if ($OptionsInfo{OutFileRoot} && (@SDFilesList == 1)) {
 492       my ($RootFileDir, $RootFileName, $RootFileExt) = ParseFileName($OptionsInfo{OutFileRoot});
 493       if ($RootFileName && $RootFileExt) {
 494         $FileName = $RootFileName;
 495       }
 496       else {
 497         $FileName = $OptionsInfo{OutFileRoot};
 498       }
 499       $OutFileRoot = $FileName;
 500     }
 501     else {
 502       $OutFileRoot = "${FileName}ExtendedConnectivityFP";
 503     }
 504 
 505     $NewSDFileName = "${OutFileRoot}.${SDOutFileExt}";
 506     $NewFPFileName = "${OutFileRoot}.${FPOutFileExt}";
 507     $NewTextFileName = "${OutFileRoot}.${TextOutFileExt}";
 508 
 509     if ($OptionsInfo{SDOutput}) {
 510       if ($SDFile =~ /$NewSDFileName/i) {
 511         warn "Warning: Ignoring input file $SDFile: Same output, $NewSDFileName, and input file names.\n";
 512         print "Specify a different name using \"-r --root\" option or use default name.\n";
 513         next FILELIST;
 514       }
 515     }
 516 
 517     if (!$OptionsInfo{OverwriteFiles}) {
 518       # Check SD and text outout files...
 519       if ($OptionsInfo{SDOutput}) {
 520         if (-e $NewSDFileName) {
 521           warn "Warning: Ignoring file $SDFile: The file $NewSDFileName already exists\n";
 522           next FILELIST;
 523         }
 524       }
 525       if ($OptionsInfo{FPOutput}) {
 526         if (-e $NewFPFileName) {
 527           warn "Warning: Ignoring file $SDFile: The file $NewFPFileName already exists\n";
 528           next FILELIST;
 529         }
 530       }
 531       if ($OptionsInfo{TextOutput}) {
 532         if (-e $NewTextFileName) {
 533           warn "Warning: Ignoring file $SDFile: The file $NewTextFileName already exists\n";
 534           next FILELIST;
 535         }
 536       }
 537     }
 538 
 539     $SDFilesInfo{FileOkay}[$Index] = 1;
 540 
 541     $SDFilesInfo{OutFileRoot}[$Index] = $OutFileRoot;
 542     $SDFilesInfo{SDOutFileNames}[$Index] = $NewSDFileName;
 543     $SDFilesInfo{FPOutFileNames}[$Index] = $NewFPFileName;
 544     $SDFilesInfo{TextOutFileNames}[$Index] = $NewTextFileName;
 545 
 546     $SDFilesInfo{AllDataFieldsRef}[$Index] = $AllDataFieldsRef;
 547     $SDFilesInfo{CommonDataFieldsRef}[$Index] = $CommonDataFieldsRef;
 548   }
 549 }
 550 
 551 # Process option values...
 552 sub ProcessOptions {
 553   %OptionsInfo = ();
 554 
 555   ProcessAtomIdentifierTypeOptions();
 556 
 557   $OptionsInfo{BitsOrder} = $Options{bitsorder};
 558   $OptionsInfo{BitStringFormat} = $Options{bitstringformat};
 559 
 560   $OptionsInfo{CompoundIDMode} = $Options{compoundidmode};
 561   $OptionsInfo{CompoundIDLabel} = $Options{compoundidlabel};
 562   $OptionsInfo{DataFieldsMode} = $Options{datafieldsmode};
 563 
 564   my(@SpecifiedDataFields);
 565   @SpecifiedDataFields = ();
 566 
 567   @{$OptionsInfo{SpecifiedDataFields}} = ();
 568   $OptionsInfo{CompoundID} = '';
 569 
 570   if ($Options{datafieldsmode} =~ /^CompoundID$/i) {
 571     if ($Options{compoundidmode} =~ /^DataField$/i) {
 572       if (!$Options{compoundid}) {
 573         die "Error: You must specify a value for \"--CompoundID\" option in \"DataField\" \"--CompoundIDMode\". \n";
 574       }
 575       $OptionsInfo{CompoundID} = $Options{compoundid};
 576     }
 577     elsif ($Options{compoundidmode} =~ /^(LabelPrefix|MolNameOrLabelPrefix)$/i) {
 578       $OptionsInfo{CompoundID} = $Options{compoundid} ? $Options{compoundid} : 'Cmpd';
 579     }
 580   }
 581   elsif ($Options{datafieldsmode} =~ /^Specify$/i) {
 582     if (!$Options{datafields}) {
 583       die "Error: You must specify a value for \"--DataFields\" option in \"Specify\" \"-d, --DataFieldsMode\". \n";
 584     }
 585     @SpecifiedDataFields = split /\,/, $Options{datafields};
 586     push @{$OptionsInfo{SpecifiedDataFields}}, @SpecifiedDataFields;
 587   }
 588 
 589   $OptionsInfo{FingerprintsLabel} = $Options{fingerprintslabel} ? $Options{fingerprintslabel} : 'ExtendedConnectivityFingerprints';
 590 
 591   $OptionsInfo{Filter} = ($Options{filter} =~ /^Yes$/i) ? 1 : 0;
 592 
 593   $OptionsInfo{KeepLargestComponent} = ($Options{keeplargestcomponent} =~ /^Yes$/i) ? 1 : 0;
 594 
 595   $OptionsInfo{Mode} = $Options{mode};
 596 
 597   $OptionsInfo{NeighborhoodRadius} = $Options{neighborhoodradius};
 598 
 599   $OptionsInfo{UsePerlCoreRandom} = ($Options{useperlcorerandom} =~ /^Yes$/i) ? 1 : 0;
 600 
 601   $OptionsInfo{Output} = $Options{output};
 602   $OptionsInfo{SDOutput} = ($Options{output} =~ /^(SD|All)$/i) ? 1 : 0;
 603   $OptionsInfo{FPOutput} = ($Options{output} =~ /^(FP|All)$/i) ? 1 : 0;
 604   $OptionsInfo{TextOutput} = ($Options{output} =~ /^(Text|All)$/i) ? 1 : 0;
 605 
 606   $OptionsInfo{OutDelim} = $Options{outdelim};
 607   $OptionsInfo{OutQuote} = ($Options{quote} =~ /^Yes$/i) ? 1 : 0;
 608 
 609   $OptionsInfo{OverwriteFiles} = $Options{overwrite} ? 1 : 0;
 610   $OptionsInfo{OutFileRoot} = $Options{root} ? $Options{root} : 0;
 611 
 612   my($Size, $MinSize, $MaxSize);
 613   $MinSize = 32;
 614   $MaxSize = 2**32;
 615   $Size = $Options{size};
 616   if (!(IsPositiveInteger($Size) && $Size >= $MinSize && $Size <= $MaxSize && IsNumberPowerOfNumber($Size, 2))) {
 617     die "Error: Invalid size value, $Size, for \"-s, --size\" option. Allowed values: power of 2, >= minimum size of $MinSize, and <= maximum size of $MaxSize.\n";
 618   }
 619   $OptionsInfo{Size} = $Size;
 620 
 621   # Setup default vector string format...
 622   #
 623   my($VectorStringFormat);
 624   $VectorStringFormat = '';
 625   if ($Options{vectorstringformat}) {
 626     $VectorStringFormat = $Options{vectorstringformat};
 627   }
 628   else {
 629     $VectorStringFormat = ($Options{mode} =~ /^ExtendedConnectivity$/) ? "ValuesString" : "IDsAndValuesString";
 630   }
 631   $OptionsInfo{VectorStringFormat} = $VectorStringFormat;
 632 }
 633 
 634 # Process atom identifier type and related options...
 635 #
 636 sub ProcessAtomIdentifierTypeOptions {
 637 
 638   $OptionsInfo{AtomIdentifierType} = $Options{atomidentifiertype};
 639 
 640   if ($Options{atomidentifiertype} =~ /^AtomicInvariantsAtomTypes$/i) {
 641     ProcessAtomicInvariantsToUseOption();
 642   }
 643   elsif ($Options{atomidentifiertype} =~ /^FunctionalClassAtomTypes$/i) {
 644     ProcessFunctionalClassesToUse();
 645   }
 646   elsif ($Options{atomidentifiertype} =~ /^(DREIDINGAtomTypes|EStateAtomTypes|MMFF94AtomTypes|SLogPAtomTypes|SYBYLAtomTypes|TPSAAtomTypes|UFFAtomTypes)$/i) {
 647     # Nothing to do for now...
 648   }
 649   else {
 650     die "Error: The value specified, $Options{atomidentifiertype}, for option \"-a, --AtomIdentifierType\" is not valid. Supported atom identifier types in current release of MayaChemTools: AtomicInvariantsAtomTypes, DREIDINGAtomTypes, EStateAtomTypes, FunctionalClassAtomTypes, MMFF94AtomTypes, SLogPAtomTypes, SYBYLAtomTypes, TPSAAtomTypes, UFFAtomTypes\n";
 651   }
 652 }
 653 
 654 # Process specified atomic invariants to use...
 655 #
 656 sub ProcessAtomicInvariantsToUseOption {
 657   my($AtomicInvariant, $AtomSymbolSpecified, @AtomicInvariantsWords);
 658 
 659   @{$OptionsInfo{AtomicInvariantsToUse}} = ();
 660   if (IsEmpty($Options{atomicinvariantstouse})) {
 661     die "Error: Atomic invariants value specified using \"--AtomicInvariantsToUse\" option is empty\n";
 662   }
 663   $AtomSymbolSpecified = 0;
 664   @AtomicInvariantsWords = split /\,/, $Options{atomicinvariantstouse};
 665   for $AtomicInvariant (@AtomicInvariantsWords) {
 666     if (!AtomicInvariantsAtomTypes::IsAtomicInvariantAvailable($AtomicInvariant)) {
 667       die "Error: Atomic invariant specified, $AtomicInvariant, using \"--AtomicInvariantsToUse\" option is not valid...\n ";
 668     }
 669     if ($AtomicInvariant =~ /^(AS|AtomSymbol)$/i) {
 670       $AtomSymbolSpecified = 1;
 671     }
 672     push @{$OptionsInfo{AtomicInvariantsToUse}}, $AtomicInvariant;
 673   }
 674   if (!$AtomSymbolSpecified) {
 675     die "Error: Atomic invariant, AS or AtomSymbol, must be specified as using \"--AtomicInvariantsToUse\" option...\n ";
 676   }
 677 }
 678 
 679 # Process specified functional classes invariants to use...
 680 #
 681 sub ProcessFunctionalClassesToUse {
 682   my($FunctionalClass, @FunctionalClassesToUseWords);
 683 
 684   @{$OptionsInfo{FunctionalClassesToUse}} = ();
 685   if (IsEmpty($Options{functionalclassestouse})) {
 686     die "Error: Functional classes value specified using \"--FunctionalClassesToUse\" option is empty\n";
 687   }
 688   @FunctionalClassesToUseWords = split /\,/, $Options{functionalclassestouse};
 689   for $FunctionalClass (@FunctionalClassesToUseWords) {
 690     if (!FunctionalClassAtomTypes::IsFunctionalClassAvailable($FunctionalClass)) {
 691       die "Error: Functional class specified, $FunctionalClass, using \"--FunctionalClassesToUse\" option is not valid...\n ";
 692     }
 693     push @{$OptionsInfo{FunctionalClassesToUse}}, $FunctionalClass;
 694   }
 695 }
 696 
 697 # Setup script usage  and retrieve command line arguments specified using various options...
 698 sub SetupScriptUsage {
 699 
 700   # Retrieve all the options...
 701   %Options = ();
 702 
 703   $Options{atomidentifiertype} = 'AtomicInvariantsAtomTypes';
 704   $Options{atomicinvariantstouse} = 'AS,X,BO,H,FC,MN';
 705   $Options{functionalclassestouse} = 'HBD,HBA,PI,NI,Ar,Hal';
 706 
 707   $Options{bitsorder} = 'Ascending';
 708   $Options{bitstringformat} = 'HexadecimalString';
 709 
 710   $Options{compoundidmode} = 'LabelPrefix';
 711   $Options{compoundidlabel} = 'CompoundID';
 712   $Options{datafieldsmode} = 'CompoundID';
 713 
 714   $Options{filter} = 'Yes';
 715 
 716   $Options{keeplargestcomponent} = 'Yes';
 717 
 718   $Options{mode} = 'ExtendedConnectivity';
 719 
 720   $Options{neighborhoodradius} = 2;
 721 
 722   $Options{useperlcorerandom} = 'yes';
 723 
 724   $Options{output} = 'text';
 725   $Options{outdelim} = 'comma';
 726   $Options{quote} = 'yes';
 727 
 728   $Options{size} = 1024;
 729 
 730   $Options{vectorstringformat} = '';
 731 
 732   if (!GetOptions(\%Options, "atomidentifiertype|a=s", "atomicinvariantstouse=s", "functionalclassestouse=s", "bitsorder=s", "bitstringformat|b=s", "compoundid=s", "compoundidlabel=s", "compoundidmode=s", "datafields=s", "datafieldsmode|d=s", "filter|f=s", "fingerprintslabel=s",  "help|h", "keeplargestcomponent|k=s",  "mode|m=s", "neighborhoodradius|n=s", "outdelim=s", "output=s", "overwrite|o", "quote|q=s", "root|r=s", "size|s=i", "useperlcorerandom=s", "vectorstringformat|v=s", "workingdir|w=s")) {
 733     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";
 734   }
 735   if ($Options{workingdir}) {
 736     if (! -d $Options{workingdir}) {
 737       die "Error: The value specified, $Options{workingdir}, for option \"-w --workingdir\" is not a directory name.\n";
 738     }
 739     chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n";
 740   }
 741   if ($Options{atomidentifiertype} !~ /^(AtomicInvariantsAtomTypes|FunctionalClassAtomTypes|DREIDINGAtomTypes|EStateAtomTypes|MMFF94AtomTypes|SLogPAtomTypes|SYBYLAtomTypes|TPSAAtomTypes|UFFAtomTypes)$/i) {
 742     die "Error: The value specified, $Options{atomidentifiertype}, for option \"-a, --AtomIdentifierType\" is not valid. Supported atom identifier types in current release of MayaChemTools: AtomicInvariantsAtomTypes, FunctionalClassAtomTypes, DREIDINGAtomTypes, EStateAtomTypes, MMFF94AtomTypes, SLogPAtomTypes, SYBYLAtomTypes, TPSAAtomTypes, UFFAtomTypes\n";
 743   }
 744   if ($Options{bitsorder} !~ /^(Ascending|Descending)$/i) {
 745     die "Error: The value specified, $Options{bitsorder}, for option \"--BitsOrder\" is not valid. Allowed values: Ascending or Descending\n";
 746   }
 747   if ($Options{bitstringformat} !~ /^(BinaryString|HexadecimalString)$/i) {
 748     die "Error: The value specified, $Options{bitstringformat}, for option \"-b, --bitstringformat\" is not valid. Allowed values: BinaryString or HexadecimalString\n";
 749   }
 750   if ($Options{compoundidmode} !~ /^(DataField|MolName|LabelPrefix|MolNameOrLabelPrefix)$/i) {
 751     die "Error: The value specified, $Options{compoundidmode}, for option \"--CompoundIDMode\" is not valid. Allowed values: DataField, MolName, LabelPrefix or MolNameOrLabelPrefix\n";
 752   }
 753   if ($Options{datafieldsmode} !~ /^(All|Common|Specify|CompoundID)$/i) {
 754     die "Error: The value specified, $Options{datafieldsmode}, for option \"-d, --DataFieldsMode\" is not valid. Allowed values: All, Common, Specify or CompoundID\n";
 755   }
 756   if ($Options{filter} !~ /^(Yes|No)$/i) {
 757     die "Error: The value specified, $Options{filter}, for option \"-f, --Filter\" is not valid. Allowed values: Yes or No\n";
 758   }
 759   if ($Options{keeplargestcomponent} !~ /^(Yes|No)$/i) {
 760     die "Error: The value specified, $Options{keeplargestcomponent}, for option \"-k, --KeepLargestComponent\" is not valid. Allowed values: Yes or No\n";
 761   }
 762   if ($Options{mode} !~ /^(ExtendedConnectivity|ExtendedConnectivityCount|ExtendedConnectivityBits)$/i) {
 763     die "Error: The value specified, $Options{mode}, for option \"-m, --mode\" is not valid. Allowed values: ExtendedConnectivity, ExtendedConnecticityCount, or ExtendedConnectivityBits\n";
 764   }
 765   if (!(IsInteger($Options{neighborhoodradius}) && ($Options{neighborhoodradius} >= 0))) {
 766     die "Error: The value specified, $Options{neighborhoodradius}, for option \"-n, --NeighborhoodRadius\" is not valid. Allowed values: >= 0 \n";
 767   }
 768   if ($Options{output} !~ /^(SD|FP|text|all)$/i) {
 769     die "Error: The value specified, $Options{output}, for option \"--output\" is not valid. Allowed values: SD, FP, text, or all\n";
 770   }
 771   if ($Options{outdelim} !~ /^(comma|semicolon|tab)$/i) {
 772     die "Error: The value specified, $Options{outdelim}, for option \"--outdelim\" is not valid. Allowed values: comma, tab, or semicolon\n";
 773   }
 774   if ($Options{quote} !~ /^(Yes|No)$/i) {
 775     die "Error: The value specified, $Options{quote}, for option \"-q --quote\" is not valid. Allowed values: Yes or No\n";
 776   }
 777   if (!IsPositiveInteger($Options{size})) {
 778     die "Error: The value specified, $Options{size}, for option \"-s, --size\" is not valid. Allowed values: > 0 \n";
 779   }
 780   if ($Options{outdelim} =~ /semicolon/i && $Options{quote} =~ /^No$/i) {
 781     die "Error: The value specified, $Options{quote}, for option \"-q --quote\" is not allowed with, semicolon value of \"--outdelim\" option: Fingerprints string use semicolon as delimiter for various data fields and must be quoted.\n";
 782   }
 783   if ($Options{useperlcorerandom} !~ /^(Yes|No)$/i) {
 784     die "Error: The value specified, $Options{useperlcorerandom}, for option \"--UsePerlCoreRandom\" is not valid. Allowed values: Yes or No\n";
 785   }
 786   if ($Options{vectorstringformat} && $Options{vectorstringformat} !~ /^(ValuesString|IDsAndValuesString|IDsAndValuesPairsString|ValuesAndIDsString|ValuesAndIDsPairsString)$/i) {
 787     die "Error: The value specified, $Options{vectorstringformat}, for option \"-v, --VectorStringFormat\" is not valid. Allowed values: ValuesString, IDsAndValuesString, IDsAndValuesPairsString, ValuesAndIDsString or ValuesAndIDsPairsString\n";
 788   }
 789 }
 790