MayaChemTools

   1 #!/usr/bin/perl -w
   2 #
   3 # File: ExtendedConnectivityFingerprints.pl
   4 # Author: Manish Sud <msud@san.rr.com>
   5 #
   6 # Copyright (C) 2024 Manish Sud. All rights reserved.
   7 #
   8 # This file is part of MayaChemTools.
   9 #
  10 # MayaChemTools is free software; you can redistribute it and/or modify it under
  11 # the terms of the GNU Lesser General Public License as published by the Free
  12 # Software Foundation; either version 3 of the License, or (at your option) any
  13 # later version.
  14 #
  15 # MayaChemTools is distributed in the hope that it will be useful, but without
  16 # any warranty; without even the implied warranty of merchantability of fitness
  17 # for a particular purpose.  See the GNU Lesser General Public License for more
  18 # details.
  19 #
  20 # You should have received a copy of the GNU Lesser General Public License
  21 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or
  22 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330,
  23 # Boston, MA, 02111-1307, USA.
  24 #
  25 
  26 use strict;
  27 use FindBin; use lib "$FindBin::Bin/../lib";
  28 use Getopt::Long;
  29 use File::Basename;
  30 use Text::ParseWords;
  31 use Benchmark;
  32 use FileUtil;
  33 use TextUtil;
  34 use SDFileUtil;
  35 use MoleculeFileIO;
  36 use FileIO::FingerprintsSDFileIO;
  37 use FileIO::FingerprintsTextFileIO;
  38 use FileIO::FingerprintsFPFileIO;
  39 use AtomTypes::AtomicInvariantsAtomTypes;
  40 use AtomTypes::FunctionalClassAtomTypes;
  41 use Fingerprints::ExtendedConnectivityFingerprints;
  42 
  43 my($ScriptName, %Options, $StartTime, $EndTime, $TotalTime);
  44 
  45 # Autoflush STDOUT
  46 $| = 1;
  47 
  48 # Starting message...
  49 $ScriptName = basename($0);
  50 print "\n$ScriptName: Starting...\n\n";
  51 $StartTime = new Benchmark;
  52 
  53 # Get the options and setup script...
  54 SetupScriptUsage();
  55 if ($Options{help} || @ARGV < 1) {
  56   die GetUsageFromPod("$FindBin::Bin/$ScriptName");
  57 }
  58 
  59 my(@SDFilesList);
  60 @SDFilesList = ExpandFileNames(\@ARGV, "sdf sd");
  61 
  62 # Process options...
  63 print "Processing options...\n";
  64 my(%OptionsInfo);
  65 ProcessOptions();
  66 
  67 # Setup information about input files...
  68 print "Checking input SD file(s)...\n";
  69 my(%SDFilesInfo);
  70 RetrieveSDFilesInfo();
  71 
  72 # Process input files..
  73 my($FileIndex);
  74 if (@SDFilesList > 1) {
  75   print "\nProcessing SD files...\n";
  76 }
  77 for $FileIndex (0 .. $#SDFilesList) {
  78   if ($SDFilesInfo{FileOkay}[$FileIndex]) {
  79     print "\nProcessing file $SDFilesList[$FileIndex]...\n";
  80     GenerateExtendedConnectivityFingerprints($FileIndex);
  81   }
  82 }
  83 print "\n$ScriptName:Done...\n\n";
  84 
  85 $EndTime = new Benchmark;
  86 $TotalTime = timediff ($EndTime, $StartTime);
  87 print "Total time: ", timestr($TotalTime), "\n";
  88 
  89 ###############################################################################
  90 
  91 # Generate fingerprints for a SD file...
  92 #
  93 sub GenerateExtendedConnectivityFingerprints {
  94   my($FileIndex) = @_;
  95   my($CmpdCount, $IgnoredCmpdCount, $SDFile, $MoleculeFileIO, $Molecule, $ExtendedConnectivityFingerprints, $NewFPSDFileIO, $NewFPTextFileIO, $NewFPFileIO);
  96 
  97   $SDFile = $SDFilesList[$FileIndex];
  98 
  99   # Setup output files...
 100   #
 101   ($NewFPSDFileIO, $NewFPTextFileIO, $NewFPFileIO) = SetupAndOpenOutputFiles($FileIndex);
 102 
 103   $MoleculeFileIO = new MoleculeFileIO('Name' => $SDFile);
 104   $MoleculeFileIO->Open();
 105 
 106   $CmpdCount = 0;
 107   $IgnoredCmpdCount = 0;
 108 
 109   COMPOUND: while ($Molecule = $MoleculeFileIO->ReadMolecule()) {
 110     $CmpdCount++;
 111 
 112     # Filter compound data before calculating fingerprints...
 113     if ($OptionsInfo{Filter}) {
 114       if (CheckAndFilterCompound($CmpdCount, $Molecule)) {
 115         $IgnoredCmpdCount++;
 116         next COMPOUND;
 117       }
 118     }
 119 
 120     $ExtendedConnectivityFingerprints = GenerateMoleculeFingerprints($Molecule);
 121     if (!$ExtendedConnectivityFingerprints) {
 122       $IgnoredCmpdCount++;
 123       ProcessIgnoredCompound('FingerprintsGenerationFailed', $CmpdCount, $Molecule);
 124       next COMPOUND;
 125     }
 126 
 127     WriteDataToOutputFiles($FileIndex, $CmpdCount, $Molecule, $ExtendedConnectivityFingerprints, $NewFPSDFileIO, $NewFPTextFileIO, $NewFPFileIO);
 128   }
 129   $MoleculeFileIO->Close();
 130 
 131   if ($NewFPSDFileIO) {
 132     $NewFPSDFileIO->Close();
 133   }
 134   if ($NewFPTextFileIO) {
 135     $NewFPTextFileIO->Close();
 136   }
 137   if ($NewFPFileIO) {
 138     $NewFPFileIO->Close();
 139   }
 140 
 141   WriteFingerprintsGenerationSummaryStatistics($CmpdCount, $IgnoredCmpdCount);
 142 }
 143 
 144 # Process compound being ignored due to problems in fingerprints geneation...
 145 #
 146 sub ProcessIgnoredCompound {
 147   my($Mode, $CmpdCount, $Molecule) = @_;
 148   my($CmpdID, $DataFieldLabelAndValuesRef);
 149 
 150   $DataFieldLabelAndValuesRef = $Molecule->GetDataFieldLabelAndValues();
 151   $CmpdID = SetupCmpdIDForOutputFiles($CmpdCount, $Molecule, $DataFieldLabelAndValuesRef);
 152 
 153   MODE: {
 154     if ($Mode =~ /^ContainsNonElementalData$/i) {
 155       warn "\nWarning: Ignoring compound record number $CmpdCount with ID $CmpdID: Compound contains atom data corresponding to non-elemental atom symbol(s)...\n\n";
 156       next MODE;
 157     }
 158 
 159     if ($Mode =~ /^ContainsNoElementalData$/i) {
 160       warn "\nWarning: Ignoring compound record number $CmpdCount with ID $CmpdID: Compound contains no atom data...\n\n";
 161       next MODE;
 162     }
 163 
 164     if ($Mode =~ /^FingerprintsGenerationFailed$/i) {
 165       warn "\nWarning: Ignoring compound record number $CmpdCount with ID $CmpdID: Fingerprints generation didn't succeed...\n\n";
 166       next MODE;
 167     }
 168     warn "\nWarning: Ignoring compound record number $CmpdCount with ID $CmpdID: Fingerprints generation didn't succeed...\n\n";
 169   }
 170 }
 171 
 172 # Check and filter compounds....
 173 #
 174 sub CheckAndFilterCompound {
 175   my($CmpdCount, $Molecule) = @_;
 176   my($ElementCount, $NonElementCount);
 177 
 178   ($ElementCount, $NonElementCount) = $Molecule->GetNumOfElementsAndNonElements();
 179 
 180   if ($NonElementCount) {
 181     ProcessIgnoredCompound('ContainsNonElementalData', $CmpdCount, $Molecule);
 182     return 1;
 183   }
 184 
 185   if (!$ElementCount) {
 186     ProcessIgnoredCompound('ContainsNoElementalData', $CmpdCount, $Molecule);
 187     return 1;
 188   }
 189 
 190   return 0;
 191 }
 192 
 193 # Write out compounds fingerprints generation summary statistics...
 194 #
 195 sub WriteFingerprintsGenerationSummaryStatistics {
 196   my($CmpdCount, $IgnoredCmpdCount) = @_;
 197   my($ProcessedCmpdCount);
 198 
 199   $ProcessedCmpdCount = $CmpdCount - $IgnoredCmpdCount;
 200 
 201   print "\nNumber of compounds: $CmpdCount\n";
 202   print "Number of compounds processed successfully during fingerprints generation: $ProcessedCmpdCount\n";
 203   print "Number of compounds ignored during fingerprints generation: $IgnoredCmpdCount\n";
 204 }
 205 
 206 # Open output files...
 207 #
 208 sub SetupAndOpenOutputFiles {
 209   my($FileIndex) = @_;
 210   my($NewFPSDFile, $NewFPFile, $NewFPTextFile, $NewFPSDFileIO, $NewFPTextFileIO, $NewFPFileIO, %FingerprintsFileIOParams);
 211 
 212   ($NewFPSDFileIO, $NewFPTextFileIO, $NewFPFileIO) = (undef) x 3;
 213 
 214   # Setup common parameters for fingerprints file IO objects...
 215   #
 216   %FingerprintsFileIOParams = ();
 217   if ($OptionsInfo{Mode} =~ /^(ExtendedConnectivity|ExtendedConnectivityCount)$/i) {
 218     %FingerprintsFileIOParams = ('Mode' => 'Write', 'Overwrite' => $OptionsInfo{OverwriteFiles}, 'FingerprintsStringMode' => 'FingerprintsVectorString', 'VectorStringFormat' => $OptionsInfo{VectorStringFormat});
 219   }
 220   elsif ($OptionsInfo{Mode} =~ /^ExtendedConnectivityBits$/i) {
 221     %FingerprintsFileIOParams = ('Mode' => 'Write', 'Overwrite' => $OptionsInfo{OverwriteFiles}, 'FingerprintsStringMode' => 'FingerprintsBitVectorString', 'BitStringFormat' => $OptionsInfo{BitStringFormat}, 'BitsOrder' => $OptionsInfo{BitsOrder});
 222   }
 223 
 224   if ($OptionsInfo{SDOutput}) {
 225     $NewFPSDFile = $SDFilesInfo{SDOutFileNames}[$FileIndex];
 226     print "Generating SD file $NewFPSDFile...\n";
 227     $NewFPSDFileIO = new FileIO::FingerprintsSDFileIO('Name' => $NewFPSDFile, %FingerprintsFileIOParams, 'FingerprintsFieldLabel' => $OptionsInfo{FingerprintsLabel});
 228     $NewFPSDFileIO->Open();
 229   }
 230 
 231   if ($OptionsInfo{FPOutput}) {
 232     $NewFPFile = $SDFilesInfo{FPOutFileNames}[$FileIndex];
 233     print "Generating FP file $NewFPFile...\n";
 234     $NewFPFileIO = new FileIO::FingerprintsFPFileIO('Name' => $NewFPFile, %FingerprintsFileIOParams);
 235     $NewFPFileIO->Open();
 236   }
 237 
 238   if ($OptionsInfo{TextOutput}) {
 239     my($ColLabelsRef);
 240 
 241     $NewFPTextFile = $SDFilesInfo{TextOutFileNames}[$FileIndex];
 242     $ColLabelsRef = SetupFPTextFileCoulmnLabels($FileIndex);
 243 
 244     print "Generating text file $NewFPTextFile...\n";
 245     $NewFPTextFileIO = new FileIO::FingerprintsTextFileIO('Name' => $NewFPTextFile, %FingerprintsFileIOParams, 'DataColLabels' => $ColLabelsRef, 'OutDelim' => $OptionsInfo{OutDelim}, 'OutQuote' => $OptionsInfo{OutQuote});
 246     $NewFPTextFileIO->Open();
 247   }
 248 
 249   return ($NewFPSDFileIO, $NewFPTextFileIO, $NewFPFileIO);
 250 }
 251 
 252 # Write fingerpritns and other data to appropriate output files...
 253 #
 254 sub WriteDataToOutputFiles {
 255   my($FileIndex, $CmpdCount, $Molecule, $ExtendedConnectivityFingerprints, $NewFPSDFileIO, $NewFPTextFileIO, $NewFPFileIO) = @_;
 256   my($DataFieldLabelAndValuesRef);
 257 
 258   $DataFieldLabelAndValuesRef = undef;
 259   if ($NewFPTextFileIO || $NewFPFileIO) {
 260     $DataFieldLabelAndValuesRef = $Molecule->GetDataFieldLabelAndValues();
 261   }
 262 
 263   if ($NewFPSDFileIO) {
 264     my($CmpdString);
 265 
 266     $CmpdString = $Molecule->GetInputMoleculeString();
 267     $NewFPSDFileIO->WriteFingerprints($ExtendedConnectivityFingerprints, $CmpdString);
 268   }
 269 
 270   if ($NewFPTextFileIO) {
 271     my($ColValuesRef);
 272 
 273     $ColValuesRef = SetupFPTextFileCoulmnValues($FileIndex, $CmpdCount, $Molecule, $DataFieldLabelAndValuesRef);
 274     $NewFPTextFileIO->WriteFingerprints($ExtendedConnectivityFingerprints, $ColValuesRef);
 275   }
 276 
 277   if ($NewFPFileIO) {
 278     my($CompoundID);
 279 
 280     $CompoundID = SetupCmpdIDForOutputFiles($CmpdCount, $Molecule, $DataFieldLabelAndValuesRef);
 281     $NewFPFileIO->WriteFingerprints($ExtendedConnectivityFingerprints, $CompoundID);
 282   }
 283 }
 284 
 285 # Generate approriate column labels for FPText output file...
 286 #
 287 sub SetupFPTextFileCoulmnLabels {
 288   my($FileIndex) = @_;
 289   my($Line, @ColLabels);
 290 
 291   @ColLabels = ();
 292   if ($OptionsInfo{DataFieldsMode} =~ /^All$/i) {
 293     push @ColLabels, @{$SDFilesInfo{AllDataFieldsRef}[$FileIndex]};
 294   }
 295   elsif ($OptionsInfo{DataFieldsMode} =~ /^Common$/i) {
 296     push @ColLabels, @{$SDFilesInfo{CommonDataFieldsRef}[$FileIndex]};
 297   }
 298   elsif ($OptionsInfo{DataFieldsMode} =~ /^Specify$/i) {
 299     push @ColLabels, @{$OptionsInfo{SpecifiedDataFields}};
 300   }
 301   elsif ($OptionsInfo{DataFieldsMode} =~ /^CompoundID$/i) {
 302     push @ColLabels, $OptionsInfo{CompoundIDLabel};
 303   }
 304   # Add fingerprints label...
 305   push @ColLabels, $OptionsInfo{FingerprintsLabel};
 306 
 307   return \@ColLabels;
 308 }
 309 
 310 # Generate column values FPText output file..
 311 #
 312 sub SetupFPTextFileCoulmnValues {
 313   my($FileIndex, $CmpdCount, $Molecule, $DataFieldLabelAndValuesRef) = @_;
 314   my(@ColValues);
 315 
 316   @ColValues = ();
 317   if ($OptionsInfo{DataFieldsMode} =~ /^CompoundID$/i) {
 318     push @ColValues, SetupCmpdIDForOutputFiles($CmpdCount, $Molecule, $DataFieldLabelAndValuesRef);
 319   }
 320   elsif ($OptionsInfo{DataFieldsMode} =~ /^All$/i) {
 321     @ColValues = map { exists $DataFieldLabelAndValuesRef->{$_} ? $DataFieldLabelAndValuesRef->{$_} : ''} @{$SDFilesInfo{AllDataFieldsRef}[$FileIndex]};
 322   }
 323   elsif ($OptionsInfo{DataFieldsMode} =~ /^Common$/i) {
 324     @ColValues = map { exists $DataFieldLabelAndValuesRef->{$_} ? $DataFieldLabelAndValuesRef->{$_} : ''} @{$SDFilesInfo{CommonDataFieldsRef}[$FileIndex]};
 325   }
 326   elsif ($OptionsInfo{DataFieldsMode} =~ /^Specify$/i) {
 327     @ColValues = map { exists $DataFieldLabelAndValuesRef->{$_} ? $DataFieldLabelAndValuesRef->{$_} : ''} @{$OptionsInfo{SpecifiedDataFields}};
 328   }
 329 
 330   return \@ColValues;
 331 }
 332 
 333 # Generate compound ID for FP and FPText output files..
 334 #
 335 sub SetupCmpdIDForOutputFiles {
 336   my($CmpdCount, $Molecule, $DataFieldLabelAndValuesRef) = @_;
 337   my($CmpdID);
 338 
 339   $CmpdID = '';
 340   if ($OptionsInfo{CompoundIDMode} =~ /^MolNameOrLabelPrefix$/i) {
 341     my($MolName);
 342     $MolName = $Molecule->GetName();
 343     $CmpdID = $MolName ? $MolName : "$OptionsInfo{CompoundID}${CmpdCount}";
 344   }
 345   elsif ($OptionsInfo{CompoundIDMode} =~ /^LabelPrefix$/i) {
 346     $CmpdID = "$OptionsInfo{CompoundID}${CmpdCount}";
 347   }
 348   elsif ($OptionsInfo{CompoundIDMode} =~ /^DataField$/i) {
 349     my($SpecifiedDataField);
 350     $SpecifiedDataField = $OptionsInfo{CompoundID};
 351     $CmpdID = exists $DataFieldLabelAndValuesRef->{$SpecifiedDataField} ? $DataFieldLabelAndValuesRef->{$SpecifiedDataField} : '';
 352   }
 353   elsif ($OptionsInfo{CompoundIDMode} =~ /^MolName$/i) {
 354     $CmpdID = $Molecule->GetName();
 355   }
 356   return $CmpdID;
 357 }
 358 
 359 # Generate fingerprints for molecule...
 360 #
 361 sub GenerateMoleculeFingerprints {
 362   my($Molecule) = @_;
 363   my($ExtendedConnectivityFingerprints);
 364 
 365   if ($OptionsInfo{KeepLargestComponent}) {
 366     $Molecule->KeepLargestComponent();
 367   }
 368   if (!$Molecule->DetectRings()) {
 369     return undef;
 370   }
 371   $Molecule->SetAromaticityModel($OptionsInfo{AromaticityModel});
 372   $Molecule->DetectAromaticity();
 373 
 374   $ExtendedConnectivityFingerprints = undef;
 375   if ($OptionsInfo{Mode} =~ /^(ExtendedConnectivity|ExtendedConnectivityCount)$/i ) {
 376     $ExtendedConnectivityFingerprints = new Fingerprints::ExtendedConnectivityFingerprints('Type' => $OptionsInfo{Mode}, 'Molecule' => $Molecule, 'NeighborhoodRadius' => $OptionsInfo{NeighborhoodRadius}, 'AtomIdentifierType' => $OptionsInfo{AtomIdentifierType});
 377   }
 378   elsif ($OptionsInfo{Mode} =~ /^ExtendedConnectivityBits$/i) {
 379     $ExtendedConnectivityFingerprints = new Fingerprints::ExtendedConnectivityFingerprints('Type' => $OptionsInfo{Mode}, 'Molecule' => $Molecule, 'NeighborhoodRadius' => $OptionsInfo{NeighborhoodRadius}, 'AtomIdentifierType' => $OptionsInfo{AtomIdentifierType}, 'Size' => $OptionsInfo{Size}, 'UsePerlCoreRandom' => $OptionsInfo{UsePerlCoreRandom});
 380   }
 381   else {
 382     die "Error: The value specified, $Options{mode}, for option \"-m, --mode\" is not valid. Allowed values: ExtendedConnectivity,  ExtendedConnectivityCount or ExtendedConnectivityBits\n";
 383   }
 384   SetAtomIdentifierTypeValuesToUse($ExtendedConnectivityFingerprints);
 385 
 386   # Generate fingerprints...
 387   $ExtendedConnectivityFingerprints->GenerateFingerprints();
 388 
 389   # Make sure fingerprints generation is successful...
 390   if (!$ExtendedConnectivityFingerprints->IsFingerprintsGenerationSuccessful()) {
 391     return undef;
 392   }
 393 
 394   return $ExtendedConnectivityFingerprints;
 395 }
 396 
 397 # Set atom identifier type to use for generating fingerprints...
 398 #
 399 sub SetAtomIdentifierTypeValuesToUse {
 400   my($ExtendedConnectivityFingerprints) = @_;
 401 
 402   if ($OptionsInfo{AtomIdentifierType} =~ /^AtomicInvariantsAtomTypes$/i) {
 403     $ExtendedConnectivityFingerprints->SetAtomicInvariantsToUse(\@{$OptionsInfo{AtomicInvariantsToUse}});
 404   }
 405   elsif ($OptionsInfo{AtomIdentifierType} =~ /^FunctionalClassAtomTypes$/i) {
 406     $ExtendedConnectivityFingerprints->SetFunctionalClassesToUse(\@{$OptionsInfo{FunctionalClassesToUse}});
 407   }
 408   elsif ($OptionsInfo{AtomIdentifierType} =~ /^(DREIDINGAtomTypes|EStateAtomTypes|MMFF94AtomTypes|SLogPAtomTypes|SYBYLAtomTypes|TPSAAtomTypes|UFFAtomTypes)$/i) {
 409     # Nothing to do for now...
 410   }
 411   else {
 412     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";
 413   }
 414 }
 415 
 416 # Retrieve information about SD files...
 417 #
 418 sub RetrieveSDFilesInfo {
 419   my($SDFile, $Index, $FileDir, $FileExt, $FileName, $OutFileRoot, $TextOutFileExt, $SDOutFileExt, $FPOutFileExt, $NewSDFileName, $NewFPFileName, $NewTextFileName, $CheckDataField, $CollectDataFields, $AllDataFieldsRef, $CommonDataFieldsRef);
 420 
 421   %SDFilesInfo = ();
 422   @{$SDFilesInfo{FileOkay}} = ();
 423   @{$SDFilesInfo{OutFileRoot}} = ();
 424   @{$SDFilesInfo{SDOutFileNames}} = ();
 425   @{$SDFilesInfo{FPOutFileNames}} = ();
 426   @{$SDFilesInfo{TextOutFileNames}} = ();
 427   @{$SDFilesInfo{AllDataFieldsRef}} = ();
 428   @{$SDFilesInfo{CommonDataFieldsRef}} = ();
 429 
 430   $CheckDataField = ($OptionsInfo{TextOutput} && ($OptionsInfo{DataFieldsMode} =~ /^CompoundID$/i) && ($OptionsInfo{CompoundIDMode} =~ /^DataField$/i)) ? 1 : 0;
 431   $CollectDataFields = ($OptionsInfo{TextOutput} && ($OptionsInfo{DataFieldsMode} =~ /^(All|Common)$/i)) ? 1 : 0;
 432 
 433   FILELIST: for $Index (0 .. $#SDFilesList) {
 434     $SDFile = $SDFilesList[$Index];
 435 
 436     $SDFilesInfo{FileOkay}[$Index] = 0;
 437     $SDFilesInfo{OutFileRoot}[$Index] = '';
 438     $SDFilesInfo{SDOutFileNames}[$Index] = '';
 439     $SDFilesInfo{FPOutFileNames}[$Index] = '';
 440     $SDFilesInfo{TextOutFileNames}[$Index] = '';
 441 
 442     $SDFile = $SDFilesList[$Index];
 443     if (!(-e $SDFile)) {
 444       warn "Warning: Ignoring file $SDFile: It doesn't exist\n";
 445       next FILELIST;
 446     }
 447     if (!CheckFileType($SDFile, "sd sdf")) {
 448       warn "Warning: Ignoring file $SDFile: It's not a SD file\n";
 449       next FILELIST;
 450     }
 451 
 452     if ($CheckDataField) {
 453       # Make sure data field exists in SD file..
 454       my($CmpdString, $SpecifiedDataField, @CmpdLines, %DataFieldValues);
 455 
 456       @CmpdLines = ();
 457       open SDFILE, "$SDFile" or die "Error: Couldn't open $SDFile: $! \n";
 458       $CmpdString = ReadCmpdString(\*SDFILE);
 459       close SDFILE;
 460       @CmpdLines = split "\n", $CmpdString;
 461       %DataFieldValues = GetCmpdDataHeaderLabelsAndValues(\@CmpdLines);
 462       $SpecifiedDataField = $OptionsInfo{CompoundID};
 463       if (!exists $DataFieldValues{$SpecifiedDataField}) {
 464         warn "Warning: Ignoring file $SDFile: Data field value, $SpecifiedDataField, using  \"--CompoundID\" option in \"DataField\" \"--CompoundIDMode\" doesn't exist\n";
 465         next FILELIST;
 466       }
 467     }
 468 
 469     $AllDataFieldsRef = '';
 470     $CommonDataFieldsRef = '';
 471     if ($CollectDataFields) {
 472       my($CmpdCount);
 473       open SDFILE, "$SDFile" or die "Error: Couldn't open $SDFile: $! \n";
 474       ($CmpdCount, $AllDataFieldsRef, $CommonDataFieldsRef) = GetAllAndCommonCmpdDataHeaderLabels(\*SDFILE);
 475       close SDFILE;
 476     }
 477 
 478     # Setup output file names...
 479     $FileDir = ""; $FileName = ""; $FileExt = "";
 480     ($FileDir, $FileName, $FileExt) = ParseFileName($SDFile);
 481 
 482     $TextOutFileExt = "csv";
 483     if ($Options{outdelim} =~ /^tab$/i) {
 484       $TextOutFileExt = "tsv";
 485     }
 486     $SDOutFileExt = $FileExt;
 487     $FPOutFileExt = "fpf";
 488 
 489     if ($OptionsInfo{OutFileRoot} && (@SDFilesList == 1)) {
 490       my ($RootFileDir, $RootFileName, $RootFileExt) = ParseFileName($OptionsInfo{OutFileRoot});
 491       if ($RootFileName && $RootFileExt) {
 492         $FileName = $RootFileName;
 493       }
 494       else {
 495         $FileName = $OptionsInfo{OutFileRoot};
 496       }
 497       $OutFileRoot = $FileName;
 498     }
 499     else {
 500       $OutFileRoot = "${FileName}ExtendedConnectivityFP";
 501     }
 502 
 503     $NewSDFileName = "${OutFileRoot}.${SDOutFileExt}";
 504     $NewFPFileName = "${OutFileRoot}.${FPOutFileExt}";
 505     $NewTextFileName = "${OutFileRoot}.${TextOutFileExt}";
 506 
 507     if ($OptionsInfo{SDOutput}) {
 508       if ($SDFile =~ /$NewSDFileName/i) {
 509         warn "Warning: Ignoring input file $SDFile: Same output, $NewSDFileName, and input file names.\n";
 510         print "Specify a different name using \"-r --root\" option or use default name.\n";
 511         next FILELIST;
 512       }
 513     }
 514 
 515     if (!$OptionsInfo{OverwriteFiles}) {
 516       # Check SD and text outout files...
 517       if ($OptionsInfo{SDOutput}) {
 518         if (-e $NewSDFileName) {
 519           warn "Warning: Ignoring file $SDFile: The file $NewSDFileName already exists\n";
 520           next FILELIST;
 521         }
 522       }
 523       if ($OptionsInfo{FPOutput}) {
 524         if (-e $NewFPFileName) {
 525           warn "Warning: Ignoring file $SDFile: The file $NewFPFileName already exists\n";
 526           next FILELIST;
 527         }
 528       }
 529       if ($OptionsInfo{TextOutput}) {
 530         if (-e $NewTextFileName) {
 531           warn "Warning: Ignoring file $SDFile: The file $NewTextFileName already exists\n";
 532           next FILELIST;
 533         }
 534       }
 535     }
 536 
 537     $SDFilesInfo{FileOkay}[$Index] = 1;
 538 
 539     $SDFilesInfo{OutFileRoot}[$Index] = $OutFileRoot;
 540     $SDFilesInfo{SDOutFileNames}[$Index] = $NewSDFileName;
 541     $SDFilesInfo{FPOutFileNames}[$Index] = $NewFPFileName;
 542     $SDFilesInfo{TextOutFileNames}[$Index] = $NewTextFileName;
 543 
 544     $SDFilesInfo{AllDataFieldsRef}[$Index] = $AllDataFieldsRef;
 545     $SDFilesInfo{CommonDataFieldsRef}[$Index] = $CommonDataFieldsRef;
 546   }
 547 }
 548 
 549 # Process option values...
 550 sub ProcessOptions {
 551   %OptionsInfo = ();
 552 
 553   ProcessAtomIdentifierTypeOptions();
 554 
 555   $OptionsInfo{AromaticityModel} = $Options{aromaticitymodel};
 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 (!AtomTypes::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 (!AtomTypes::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{aromaticitymodel} = 'MayaChemToolsAromaticityModel';
 704 
 705   $Options{atomidentifiertype} = 'AtomicInvariantsAtomTypes';
 706   $Options{atomicinvariantstouse} = 'AS,X,BO,H,FC,MN';
 707   $Options{functionalclassestouse} = 'HBD,HBA,PI,NI,Ar,Hal';
 708 
 709   $Options{bitsorder} = 'Ascending';
 710   $Options{bitstringformat} = 'HexadecimalString';
 711 
 712   $Options{compoundidmode} = 'LabelPrefix';
 713   $Options{compoundidlabel} = 'CompoundID';
 714   $Options{datafieldsmode} = 'CompoundID';
 715 
 716   $Options{filter} = 'Yes';
 717 
 718   $Options{keeplargestcomponent} = 'Yes';
 719 
 720   $Options{mode} = 'ExtendedConnectivity';
 721 
 722   $Options{neighborhoodradius} = 2;
 723 
 724   $Options{useperlcorerandom} = 'yes';
 725 
 726   $Options{output} = 'text';
 727   $Options{outdelim} = 'comma';
 728   $Options{quote} = 'yes';
 729 
 730   $Options{size} = 1024;
 731 
 732   $Options{vectorstringformat} = '';
 733 
 734   if (!GetOptions(\%Options, "aromaticitymodel=s", "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")) {
 735     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";
 736   }
 737   if ($Options{workingdir}) {
 738     if (! -d $Options{workingdir}) {
 739       die "Error: The value specified, $Options{workingdir}, for option \"-w --workingdir\" is not a directory name.\n";
 740     }
 741     chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n";
 742   }
 743   if (!Molecule::IsSupportedAromaticityModel($Options{aromaticitymodel})) {
 744     my(@SupportedModels) = Molecule::GetSupportedAromaticityModels();
 745     die "Error: The value specified, $Options{aromaticitymodel}, for option \"--AromaticityModel\" is not valid. Supported aromaticity models in current release of MayaChemTools: @SupportedModels\n";
 746   }
 747   if ($Options{atomidentifiertype} !~ /^(AtomicInvariantsAtomTypes|FunctionalClassAtomTypes|DREIDINGAtomTypes|EStateAtomTypes|MMFF94AtomTypes|SLogPAtomTypes|SYBYLAtomTypes|TPSAAtomTypes|UFFAtomTypes)$/i) {
 748     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";
 749   }
 750   if ($Options{bitsorder} !~ /^(Ascending|Descending)$/i) {
 751     die "Error: The value specified, $Options{bitsorder}, for option \"--BitsOrder\" is not valid. Allowed values: Ascending or Descending\n";
 752   }
 753   if ($Options{bitstringformat} !~ /^(BinaryString|HexadecimalString)$/i) {
 754     die "Error: The value specified, $Options{bitstringformat}, for option \"-b, --bitstringformat\" is not valid. Allowed values: BinaryString or HexadecimalString\n";
 755   }
 756   if ($Options{compoundidmode} !~ /^(DataField|MolName|LabelPrefix|MolNameOrLabelPrefix)$/i) {
 757     die "Error: The value specified, $Options{compoundidmode}, for option \"--CompoundIDMode\" is not valid. Allowed values: DataField, MolName, LabelPrefix or MolNameOrLabelPrefix\n";
 758   }
 759   if ($Options{datafieldsmode} !~ /^(All|Common|Specify|CompoundID)$/i) {
 760     die "Error: The value specified, $Options{datafieldsmode}, for option \"-d, --DataFieldsMode\" is not valid. Allowed values: All, Common, Specify or CompoundID\n";
 761   }
 762   if ($Options{filter} !~ /^(Yes|No)$/i) {
 763     die "Error: The value specified, $Options{filter}, for option \"-f, --Filter\" is not valid. Allowed values: Yes or No\n";
 764   }
 765   if ($Options{keeplargestcomponent} !~ /^(Yes|No)$/i) {
 766     die "Error: The value specified, $Options{keeplargestcomponent}, for option \"-k, --KeepLargestComponent\" is not valid. Allowed values: Yes or No\n";
 767   }
 768   if ($Options{mode} !~ /^(ExtendedConnectivity|ExtendedConnectivityCount|ExtendedConnectivityBits)$/i) {
 769     die "Error: The value specified, $Options{mode}, for option \"-m, --mode\" is not valid. Allowed values: ExtendedConnectivity, ExtendedConnecticityCount, or ExtendedConnectivityBits\n";
 770   }
 771   if (!(IsInteger($Options{neighborhoodradius}) && ($Options{neighborhoodradius} >= 0))) {
 772     die "Error: The value specified, $Options{neighborhoodradius}, for option \"-n, --NeighborhoodRadius\" is not valid. Allowed values: >= 0 \n";
 773   }
 774   if ($Options{output} !~ /^(SD|FP|text|all)$/i) {
 775     die "Error: The value specified, $Options{output}, for option \"--output\" is not valid. Allowed values: SD, FP, text, or all\n";
 776   }
 777   if ($Options{outdelim} !~ /^(comma|semicolon|tab)$/i) {
 778     die "Error: The value specified, $Options{outdelim}, for option \"--outdelim\" is not valid. Allowed values: comma, tab, or semicolon\n";
 779   }
 780   if ($Options{quote} !~ /^(Yes|No)$/i) {
 781     die "Error: The value specified, $Options{quote}, for option \"-q --quote\" is not valid. Allowed values: Yes or No\n";
 782   }
 783   if (!IsPositiveInteger($Options{size})) {
 784     die "Error: The value specified, $Options{size}, for option \"-s, --size\" is not valid. Allowed values: > 0 \n";
 785   }
 786   if ($Options{outdelim} =~ /semicolon/i && $Options{quote} =~ /^No$/i) {
 787     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";
 788   }
 789   if ($Options{useperlcorerandom} !~ /^(Yes|No)$/i) {
 790     die "Error: The value specified, $Options{useperlcorerandom}, for option \"--UsePerlCoreRandom\" is not valid. Allowed values: Yes or No\n";
 791   }
 792   if ($Options{vectorstringformat} && $Options{vectorstringformat} !~ /^(ValuesString|IDsAndValuesString|IDsAndValuesPairsString|ValuesAndIDsString|ValuesAndIDsPairsString)$/i) {
 793     die "Error: The value specified, $Options{vectorstringformat}, for option \"-v, --VectorStringFormat\" is not valid. Allowed values: ValuesString, IDsAndValuesString, IDsAndValuesPairsString, ValuesAndIDsString or ValuesAndIDsPairsString\n";
 794   }
 795 }
 796