1 #!/usr/bin/perl -w 2 # 3 # $RCSfile: AtomTypesFingerprints.pl,v $ 4 # $Date: 2015/02/28 20:46:19 $ 5 # $Revision: 1.25 $ 6 # 7 # Author: Manish Sud <msud@san.rr.com> 8 # 9 # Copyright (C) 2015 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::AtomTypesFingerprints; 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 GenerateAtomTypesFingerprints($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 GenerateAtomTypesFingerprints { 97 my($FileIndex) = @_; 98 my($CmpdCount, $IgnoredCmpdCount, $SDFile, $MoleculeFileIO, $Molecule, $AtomTypesFingerprints, $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 $AtomTypesFingerprints = GenerateMoleculeFingerprints($Molecule); 124 if (!$AtomTypesFingerprints) { 125 $IgnoredCmpdCount++; 126 ProcessIgnoredCompound('FingerprintsGenerationFailed', $CmpdCount, $Molecule); 127 next COMPOUND; 128 } 129 130 WriteDataToOutputFiles($FileIndex, $CmpdCount, $Molecule, $AtomTypesFingerprints, $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} =~ /^AtomTypesBits$/i) { 221 %FingerprintsFileIOParams = ('Mode' => 'Write', 'Overwrite' => $OptionsInfo{OverwriteFiles}, 'FingerprintsStringMode' => 'FingerprintsBitVectorString', 'BitStringFormat' => $OptionsInfo{BitStringFormat}, 'BitsOrder' => $OptionsInfo{BitsOrder}); 222 } 223 elsif ($OptionsInfo{Mode} =~ /^AtomTypesCount$/i) { 224 %FingerprintsFileIOParams = ('Mode' => 'Write', 'Overwrite' => $OptionsInfo{OverwriteFiles}, 'FingerprintsStringMode' => 'FingerprintsVectorString', 'VectorStringFormat' => $OptionsInfo{VectorStringFormat}); 225 } 226 227 if ($OptionsInfo{SDOutput}) { 228 $NewFPSDFile = $SDFilesInfo{SDOutFileNames}[$FileIndex]; 229 print "Generating SD file $NewFPSDFile...\n"; 230 $NewFPSDFileIO = new FileIO::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 FileIO::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 FileIO::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, $AtomTypesFingerprints, $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($AtomTypesFingerprints, $CmpdString); 271 } 272 273 if ($NewFPTextFileIO) { 274 my($ColValuesRef); 275 276 $ColValuesRef = SetupFPTextFileCoulmnValues($FileIndex, $CmpdCount, $Molecule, $DataFieldLabelAndValuesRef); 277 $NewFPTextFileIO->WriteFingerprints($AtomTypesFingerprints, $ColValuesRef); 278 } 279 280 if ($NewFPFileIO) { 281 my($CompoundID); 282 283 $CompoundID = SetupCmpdIDForOutputFiles($CmpdCount, $Molecule, $DataFieldLabelAndValuesRef); 284 $NewFPFileIO->WriteFingerprints($AtomTypesFingerprints, $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($AtomTypesFingerprints); 367 368 if ($OptionsInfo{KeepLargestComponent}) { 369 $Molecule->KeepLargestComponent(); 370 } 371 if (!$Molecule->DetectRings()) { 372 return undef; 373 } 374 $Molecule->SetAromaticityModel($OptionsInfo{AromaticityModel}); 375 $Molecule->DetectAromaticity(); 376 377 $AtomTypesFingerprints = undef; 378 if ($OptionsInfo{Mode} =~ /^AtomTypesCount$/i) { 379 $AtomTypesFingerprints = new Fingerprints::AtomTypesFingerprints('Molecule' => $Molecule, 'Type' => 'AtomTypesCount', 'AtomIdentifierType' => $OptionsInfo{AtomIdentifierType}, 'AtomTypesSetToUse' => $OptionsInfo{AtomTypesSetToUse}, 'IgnoreHydrogens' => $OptionsInfo{IgnoreHydrogens}); 380 381 } 382 elsif ($OptionsInfo{Mode} =~ /^AtomTypesBits$/i) { 383 $AtomTypesFingerprints = new Fingerprints::AtomTypesFingerprints('Molecule' => $Molecule, 'Type' => 'AtomTypesBits', 'AtomIdentifierType' => $OptionsInfo{AtomIdentifierType}, 'AtomTypesSetToUse' => 'FixedSize', 'IgnoreHydrogens' => $OptionsInfo{IgnoreHydrogens}); 384 } 385 else { 386 die "Error: The value specified, $Options{mode}, for option \"-m, --mode\" is not valid. Allowed values: AtomTypesCount or AtomTypesBits\n"; 387 } 388 389 SetAtomIdentifierTypeValuesToUse($AtomTypesFingerprints); 390 391 # Generate atom types fingerprints... 392 $AtomTypesFingerprints->GenerateFingerprints(); 393 394 # Make sure atom types fingerprints generation is successful... 395 if (!$AtomTypesFingerprints->IsFingerprintsGenerationSuccessful()) { 396 return undef; 397 } 398 399 return $AtomTypesFingerprints; 400 } 401 402 # Set atom identifier type to use for generating fingerprints... 403 # 404 sub SetAtomIdentifierTypeValuesToUse { 405 my($AtomTypesFingerprints) = @_; 406 407 if ($OptionsInfo{AtomIdentifierType} =~ /^AtomicInvariantsAtomTypes$/i) { 408 $AtomTypesFingerprints->SetAtomicInvariantsToUse(\@{$OptionsInfo{AtomicInvariantsToUse}}); 409 } 410 elsif ($OptionsInfo{AtomIdentifierType} =~ /^FunctionalClassAtomTypes$/i) { 411 $AtomTypesFingerprints->SetFunctionalClassesToUse(\@{$OptionsInfo{FunctionalClassesToUse}}); 412 } 413 elsif ($OptionsInfo{AtomIdentifierType} =~ /^(DREIDINGAtomTypes|EStateAtomTypes|MMFF94AtomTypes|SLogPAtomTypes|SYBYLAtomTypes|TPSAAtomTypes|UFFAtomTypes)$/i) { 414 # Nothing to do for now... 415 } 416 else { 417 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"; 418 } 419 } 420 421 # Retrieve information about SD files... 422 # 423 sub RetrieveSDFilesInfo { 424 my($SDFile, $Index, $FileDir, $FileExt, $FileName, $OutFileRoot, $TextOutFileExt, $SDOutFileExt, $FPOutFileExt, $NewSDFileName, $NewFPFileName, $NewTextFileName, $CheckDataField, $CollectDataFields, $AllDataFieldsRef, $CommonDataFieldsRef); 425 426 %SDFilesInfo = (); 427 @{$SDFilesInfo{FileOkay}} = (); 428 @{$SDFilesInfo{OutFileRoot}} = (); 429 @{$SDFilesInfo{SDOutFileNames}} = (); 430 @{$SDFilesInfo{FPOutFileNames}} = (); 431 @{$SDFilesInfo{TextOutFileNames}} = (); 432 @{$SDFilesInfo{AllDataFieldsRef}} = (); 433 @{$SDFilesInfo{CommonDataFieldsRef}} = (); 434 435 $CheckDataField = ($OptionsInfo{TextOutput} && ($OptionsInfo{DataFieldsMode} =~ /^CompoundID$/i) && ($OptionsInfo{CompoundIDMode} =~ /^DataField$/i)) ? 1 : 0; 436 $CollectDataFields = ($OptionsInfo{TextOutput} && ($OptionsInfo{DataFieldsMode} =~ /^(All|Common)$/i)) ? 1 : 0; 437 438 FILELIST: for $Index (0 .. $#SDFilesList) { 439 $SDFile = $SDFilesList[$Index]; 440 441 $SDFilesInfo{FileOkay}[$Index] = 0; 442 $SDFilesInfo{OutFileRoot}[$Index] = ''; 443 $SDFilesInfo{SDOutFileNames}[$Index] = ''; 444 $SDFilesInfo{FPOutFileNames}[$Index] = ''; 445 $SDFilesInfo{TextOutFileNames}[$Index] = ''; 446 447 $SDFile = $SDFilesList[$Index]; 448 if (!(-e $SDFile)) { 449 warn "Warning: Ignoring file $SDFile: It doesn't exist\n"; 450 next FILELIST; 451 } 452 if (!CheckFileType($SDFile, "sd sdf")) { 453 warn "Warning: Ignoring file $SDFile: It's not a SD file\n"; 454 next FILELIST; 455 } 456 457 if ($CheckDataField) { 458 # Make sure data field exists in SD file.. 459 my($CmpdString, $SpecifiedDataField, @CmpdLines, %DataFieldValues); 460 461 @CmpdLines = (); 462 open SDFILE, "$SDFile" or die "Error: Couldn't open $SDFile: $! \n"; 463 $CmpdString = ReadCmpdString(\*SDFILE); 464 close SDFILE; 465 @CmpdLines = split "\n", $CmpdString; 466 %DataFieldValues = GetCmpdDataHeaderLabelsAndValues(\@CmpdLines); 467 $SpecifiedDataField = $OptionsInfo{CompoundID}; 468 if (!exists $DataFieldValues{$SpecifiedDataField}) { 469 warn "Warning: Ignoring file $SDFile: Data field value, $SpecifiedDataField, using \"--CompoundID\" option in \"DataField\" \"--CompoundIDMode\" doesn't exist\n"; 470 next FILELIST; 471 } 472 } 473 474 $AllDataFieldsRef = ''; 475 $CommonDataFieldsRef = ''; 476 if ($CollectDataFields) { 477 my($CmpdCount); 478 open SDFILE, "$SDFile" or die "Error: Couldn't open $SDFile: $! \n"; 479 ($CmpdCount, $AllDataFieldsRef, $CommonDataFieldsRef) = GetAllAndCommonCmpdDataHeaderLabels(\*SDFILE); 480 close SDFILE; 481 } 482 483 # Setup output file names... 484 $FileDir = ""; $FileName = ""; $FileExt = ""; 485 ($FileDir, $FileName, $FileExt) = ParseFileName($SDFile); 486 487 $TextOutFileExt = "csv"; 488 if ($Options{outdelim} =~ /^tab$/i) { 489 $TextOutFileExt = "tsv"; 490 } 491 $SDOutFileExt = $FileExt; 492 $FPOutFileExt = "fpf"; 493 494 if ($OptionsInfo{OutFileRoot} && (@SDFilesList == 1)) { 495 my ($RootFileDir, $RootFileName, $RootFileExt) = ParseFileName($OptionsInfo{OutFileRoot}); 496 if ($RootFileName && $RootFileExt) { 497 $FileName = $RootFileName; 498 } 499 else { 500 $FileName = $OptionsInfo{OutFileRoot}; 501 } 502 $OutFileRoot = $FileName; 503 } 504 else { 505 $OutFileRoot = $FileName . 'AtomTypesFP'; 506 } 507 508 $NewSDFileName = "${OutFileRoot}.${SDOutFileExt}"; 509 $NewFPFileName = "${OutFileRoot}.${FPOutFileExt}"; 510 $NewTextFileName = "${OutFileRoot}.${TextOutFileExt}"; 511 512 if ($OptionsInfo{SDOutput}) { 513 if ($SDFile =~ /$NewSDFileName/i) { 514 warn "Warning: Ignoring input file $SDFile: Same output, $NewSDFileName, and input file names.\n"; 515 print "Specify a different name using \"-r --root\" option or use default name.\n"; 516 next FILELIST; 517 } 518 } 519 520 if (!$OptionsInfo{OverwriteFiles}) { 521 # Check SD and text outout files... 522 if ($OptionsInfo{SDOutput}) { 523 if (-e $NewSDFileName) { 524 warn "Warning: Ignoring file $SDFile: The file $NewSDFileName already exists\n"; 525 next FILELIST; 526 } 527 } 528 if ($OptionsInfo{FPOutput}) { 529 if (-e $NewFPFileName) { 530 warn "Warning: Ignoring file $SDFile: The file $NewFPFileName already exists\n"; 531 next FILELIST; 532 } 533 } 534 if ($OptionsInfo{TextOutput}) { 535 if (-e $NewTextFileName) { 536 warn "Warning: Ignoring file $SDFile: The file $NewTextFileName already exists\n"; 537 next FILELIST; 538 } 539 } 540 } 541 542 $SDFilesInfo{FileOkay}[$Index] = 1; 543 544 $SDFilesInfo{OutFileRoot}[$Index] = $OutFileRoot; 545 $SDFilesInfo{SDOutFileNames}[$Index] = $NewSDFileName; 546 $SDFilesInfo{FPOutFileNames}[$Index] = $NewFPFileName; 547 $SDFilesInfo{TextOutFileNames}[$Index] = $NewTextFileName; 548 549 $SDFilesInfo{AllDataFieldsRef}[$Index] = $AllDataFieldsRef; 550 $SDFilesInfo{CommonDataFieldsRef}[$Index] = $CommonDataFieldsRef; 551 } 552 } 553 554 # Process option values... 555 sub ProcessOptions { 556 %OptionsInfo = (); 557 558 $OptionsInfo{Mode} = $Options{mode}; 559 $OptionsInfo{AromaticityModel} = $Options{aromaticitymodel}; 560 561 ProcessAtomIdentifierTypeOptions(); 562 563 my($AtomTypesSetToUse); 564 $AtomTypesSetToUse = ''; 565 if ($Options{mode} =~ /^AtomTypesBits$/i) { 566 if ($Options{atomtypessettouse} && $Options{atomtypessettouse} !~ /^FixedSize$/) { 567 die "Error: The value specified, $Options{atomtypessettouse}, for option \"-e, --AtomTypesSetToUse\" is not valid. Allowed values for AtomTypesBits of \"-m, --mode\" option: FixedSize\n"; 568 } 569 $AtomTypesSetToUse = 'FixedSize'; 570 } 571 else { 572 if ($Options{atomidentifiertype} =~ /^(AtomicInvariantsAtomTypes|FunctionalClassAtomTypes)$/i && $Options{atomtypessettouse} =~ /^FixedSize$/) { 573 die "Error: The value specified, $Options{atomtypessettouse}, for option \"-e, --AtomTypesSetToUse\" is not valid during \"AtomicInvariantsAtomTypes or FunctionalClassAtomTypes\" value of \"-a, --AtomIdentifierType\". Allowed values: ArbitrarySize\n"; 574 } 575 if ($Options{atomidentifiertype} =~ /^TPSAAtomTypes$/i && $Options{atomtypessettouse} =~ /^ArbitrarySize$/) { 576 die "Error: The value specified, $Options{atomtypessettouse}, for option \"-e, --AtomTypesSetToUse\" is not valid during \"TPSAAtomTypes\" value of \"-a, --AtomIdentifierType\". Allowed values: FixedSize\n"; 577 } 578 $AtomTypesSetToUse = $Options{atomtypessettouse} ? $Options{atomtypessettouse} : 'ArbitrarySize'; 579 } 580 $OptionsInfo{AtomTypesSetToUse} = $AtomTypesSetToUse; 581 582 $OptionsInfo{BitsOrder} = $Options{bitsorder}; 583 $OptionsInfo{BitStringFormat} = $Options{bitstringformat}; 584 585 $OptionsInfo{CompoundIDMode} = $Options{compoundidmode}; 586 $OptionsInfo{CompoundIDLabel} = $Options{compoundidlabel}; 587 $OptionsInfo{DataFieldsMode} = $Options{datafieldsmode}; 588 589 my(@SpecifiedDataFields); 590 @SpecifiedDataFields = (); 591 592 @{$OptionsInfo{SpecifiedDataFields}} = (); 593 $OptionsInfo{CompoundID} = ''; 594 595 if ($Options{datafieldsmode} =~ /^CompoundID$/i) { 596 if ($Options{compoundidmode} =~ /^DataField$/i) { 597 if (!$Options{compoundid}) { 598 die "Error: You must specify a value for \"--CompoundID\" option in \"DataField\" \"--CompoundIDMode\". \n"; 599 } 600 $OptionsInfo{CompoundID} = $Options{compoundid}; 601 } 602 elsif ($Options{compoundidmode} =~ /^(LabelPrefix|MolNameOrLabelPrefix)$/i) { 603 $OptionsInfo{CompoundID} = $Options{compoundid} ? $Options{compoundid} : 'Cmpd'; 604 } 605 } 606 elsif ($Options{datafieldsmode} =~ /^Specify$/i) { 607 if (!$Options{datafields}) { 608 die "Error: You must specify a value for \"--DataFields\" option in \"Specify\" \"-d, --DataFieldsMode\". \n"; 609 } 610 @SpecifiedDataFields = split /\,/, $Options{datafields}; 611 push @{$OptionsInfo{SpecifiedDataFields}}, @SpecifiedDataFields; 612 } 613 614 $OptionsInfo{IgnoreHydrogens} = ($Options{ignorehydrogens} =~ /^Yes$/i) ? 1 : 0; 615 616 $OptionsInfo{FingerprintsLabel} = $Options{fingerprintslabel} ? $Options{fingerprintslabel} : 'AtomTypesFingerprints'; 617 618 $OptionsInfo{Filter} = ($Options{filter} =~ /^Yes$/i) ? 1 : 0; 619 620 if ($Options{fingerprintslabelmode} =~ /^FingerprintsLabelWithIDs$/) { 621 if ($Options{mode} =~ /^(AtomTypesCount)$/i && $Options{atomtypessettouse} =~ /^FixedSize$/i) { 622 # Append atom types to the fingerprints label... 623 my($FixedSizeAtomTypesSetRef); 624 $FixedSizeAtomTypesSetRef = GetFixedSizeAtomTypesSet(); 625 626 $OptionsInfo{FingerprintsLabel} .= "; AtomTypes: " . TextUtil::JoinWords($FixedSizeAtomTypesSetRef, " ", 0); 627 } 628 } 629 $OptionsInfo{FingerprintsLabelMode} = $Options{fingerprintslabelmode}; 630 631 $OptionsInfo{KeepLargestComponent} = ($Options{keeplargestcomponent} =~ /^Yes$/i) ? 1 : 0; 632 633 $OptionsInfo{Output} = $Options{output}; 634 $OptionsInfo{SDOutput} = ($Options{output} =~ /^(SD|All)$/i) ? 1 : 0; 635 $OptionsInfo{FPOutput} = ($Options{output} =~ /^(FP|All)$/i) ? 1 : 0; 636 $OptionsInfo{TextOutput} = ($Options{output} =~ /^(Text|All)$/i) ? 1 : 0; 637 638 $OptionsInfo{OutDelim} = $Options{outdelim}; 639 $OptionsInfo{OutQuote} = ($Options{quote} =~ /^Yes$/i) ? 1 : 0; 640 641 $OptionsInfo{OverwriteFiles} = $Options{overwrite} ? 1 : 0; 642 $OptionsInfo{OutFileRoot} = $Options{root} ? $Options{root} : 0; 643 644 # Setup default vector string format... 645 my($VectorStringFormat); 646 $VectorStringFormat = ''; 647 if ($Options{vectorstringformat}) { 648 $VectorStringFormat = $Options{vectorstringformat}; 649 } 650 else { 651 $VectorStringFormat = ($Options{atomtypessettouse} =~ /^FixedSize$/) ? "ValuesString" : "IDsAndValuesString"; 652 } 653 $OptionsInfo{VectorStringFormat} = $VectorStringFormat; 654 } 655 656 # Process atom identifier type and related options... 657 # 658 sub ProcessAtomIdentifierTypeOptions { 659 660 $OptionsInfo{AtomIdentifierType} = $Options{atomidentifiertype}; 661 662 if ($Options{atomidentifiertype} =~ /^AtomicInvariantsAtomTypes$/i) { 663 ProcessAtomicInvariantsToUseOption(); 664 } 665 elsif ($Options{atomidentifiertype} =~ /^FunctionalClassAtomTypes$/i) { 666 ProcessFunctionalClassesToUse(); 667 } 668 elsif ($OptionsInfo{AtomIdentifierType} =~ /^(DREIDINGAtomTypes|EStateAtomTypes|MMFF94AtomTypes|SLogPAtomTypes|SYBYLAtomTypes|TPSAAtomTypes|UFFAtomTypes)$/i) { 669 # Nothing to do for now... 670 } 671 else { 672 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"; 673 } 674 } 675 676 # Process specified atomic invariants to use... 677 # 678 sub ProcessAtomicInvariantsToUseOption { 679 my($AtomicInvariant, $AtomSymbolSpecified, @AtomicInvariantsWords); 680 681 @{$OptionsInfo{AtomicInvariantsToUse}} = (); 682 if (IsEmpty($Options{atomicinvariantstouse})) { 683 die "Error: Atomic invariants value specified using \"--AtomicInvariantsToUse\" option is empty\n"; 684 } 685 $AtomSymbolSpecified = 0; 686 @AtomicInvariantsWords = split /\,/, $Options{atomicinvariantstouse}; 687 for $AtomicInvariant (@AtomicInvariantsWords) { 688 if (!AtomTypes::AtomicInvariantsAtomTypes::IsAtomicInvariantAvailable($AtomicInvariant)) { 689 die "Error: Atomic invariant specified, $AtomicInvariant, using \"--AtomicInvariantsToUse\" option is not valid...\n "; 690 } 691 if ($AtomicInvariant =~ /^(AS|AtomSymbol)$/i) { 692 $AtomSymbolSpecified = 1; 693 } 694 push @{$OptionsInfo{AtomicInvariantsToUse}}, $AtomicInvariant; 695 } 696 if (!$AtomSymbolSpecified) { 697 die "Error: Atomic invariant, AS or AtomSymbol, must be specified as using \"--AtomicInvariantsToUse\" option...\n "; 698 } 699 } 700 701 # Process specified functional classes invariants to use... 702 # 703 sub ProcessFunctionalClassesToUse { 704 my($FunctionalClass, @FunctionalClassesToUseWords); 705 706 @{$OptionsInfo{FunctionalClassesToUse}} = (); 707 if (IsEmpty($Options{functionalclassestouse})) { 708 die "Error: Functional classes value specified using \"--FunctionalClassesToUse\" option is empty\n"; 709 } 710 @FunctionalClassesToUseWords = split /\,/, $Options{functionalclassestouse}; 711 for $FunctionalClass (@FunctionalClassesToUseWords) { 712 if (!AtomTypes::FunctionalClassAtomTypes::IsFunctionalClassAvailable($FunctionalClass)) { 713 die "Error: Functional class specified, $FunctionalClass, using \"--FunctionalClassesToUse\" option is not valid...\n "; 714 } 715 push @{$OptionsInfo{FunctionalClassesToUse}}, $FunctionalClass; 716 } 717 } 718 719 # Get fixed size atom types set... 720 # 721 sub GetFixedSizeAtomTypesSet { 722 my($AtomTypesRef); 723 724 $AtomTypesRef = undef; 725 726 IDENTIFIERTYPE: { 727 if ($OptionsInfo{AtomIdentifierType} =~ /^DREIDINGAtomTypes$/i) { 728 $AtomTypesRef = $OptionsInfo{IgnoreHydrogens} ? DREIDINGAtomTypes::GetAllPossibleDREIDINGNonHydrogenAtomTypes() : DREIDINGAtomTypes::GetAllPossibleDREIDINGAtomTypes(); 729 last IDENTIFIERTYPE; 730 } 731 732 if ($OptionsInfo{AtomIdentifierType} =~ /^EStateAtomTypes$/i) { 733 $AtomTypesRef = $OptionsInfo{IgnoreHydrogens} ? EStateAtomTypes::GetAllPossibleEStateNonHydrogenAtomTypes() : EStateAtomTypes::GetAllPossibleEStateAtomTypes(); 734 last IDENTIFIERTYPE; 735 } 736 737 if ($OptionsInfo{AtomIdentifierType} =~ /^MMFF94AtomTypes$/i) { 738 $AtomTypesRef = $OptionsInfo{IgnoreHydrogens} ? MMFF94AtomTypes::GetAllPossibleMMFF94NonHydrogenAtomTypes() : MMFF94AtomTypes::GetAllPossibleMMFF94AtomTypes(); 739 last IDENTIFIERTYPE; 740 } 741 742 if ($OptionsInfo{AtomIdentifierType} =~ /^SLogPAtomTypes$/i) { 743 $AtomTypesRef = $OptionsInfo{IgnoreHydrogens} ? SLogPAtomTypes::GetAllPossibleSLogPNonHydrogenAtomTypes() : SLogPAtomTypes::GetAllPossibleSLogPAtomTypes(); 744 last IDENTIFIERTYPE; 745 } 746 747 if ($OptionsInfo{AtomIdentifierType} =~ /^SYBYLAtomTypes$/i) { 748 $AtomTypesRef = $OptionsInfo{IgnoreHydrogens} ? SYBYLAtomTypes::GetAllPossibleSYBYLNonHydrogenAtomTypes() : SYBYLAtomTypes::GetAllPossibleSYBYLAtomTypes(); 749 last IDENTIFIERTYPE; 750 } 751 752 if ($OptionsInfo{AtomIdentifierType} =~ /^TPSAAtomTypes$/i) { 753 $AtomTypesRef = TPSAAtomTypes::GetAllPossibleTPSAAtomTypes(); 754 last IDENTIFIERTYPE; 755 } 756 757 if ($OptionsInfo{AtomIdentifierType} =~ /^UFFAtomTypes$/i) { 758 $AtomTypesRef = $OptionsInfo{IgnoreHydrogens} ? UFFAtomTypes::GetAllPossibleUFFNonHydrogenAtomTypes() : UFFAtomTypes::GetAllPossibleUFFAtomTypes(); 759 last IDENTIFIERTYPE; 760 } 761 die "Error: GetFixedSizeAtomTypesSet: Atom types set for atom indentifier type, $OptionsInfo{AtomIdentifierType}, is not available..."; 762 } 763 764 return $AtomTypesRef; 765 } 766 767 # Setup script usage and retrieve command line arguments specified using various options... 768 sub SetupScriptUsage { 769 770 # Retrieve all the options... 771 %Options = (); 772 773 $Options{aromaticitymodel} = 'MayaChemToolsAromaticityModel'; 774 775 $Options{atomidentifiertype} = 'AtomicInvariantsAtomTypes'; 776 $Options{atomicinvariantstouse} = 'AS,X,BO,H,FC'; 777 $Options{functionalclassestouse} = 'HBD,HBA,PI,NI,Ar,Hal'; 778 779 $Options{atomtypessettouse} = 'ArbitrarySize'; 780 781 $Options{bitsorder} = 'Ascending'; 782 $Options{bitstringformat} = 'BinaryString'; 783 784 $Options{compoundidmode} = 'LabelPrefix'; 785 $Options{compoundidlabel} = 'CompoundID'; 786 $Options{datafieldsmode} = 'CompoundID'; 787 788 $Options{filter} = 'Yes'; 789 790 $Options{fingerprintslabelmode} = 'FingerprintsLabelOnly'; 791 $Options{keeplargestcomponent} = 'Yes'; 792 793 $Options{mode} = 'AtomTypesCount'; 794 795 $Options{ignorehydrogens} = 'Yes'; 796 797 $Options{quote} = 'yes'; 798 799 $Options{output} = 'text'; 800 $Options{outdelim} = 'comma'; 801 $Options{quote} = 'yes'; 802 803 $Options{vectorstringformat} = ''; 804 805 if (!GetOptions(\%Options, "aromaticitymodel=s", "atomidentifiertype|a=s", "atomicinvariantstouse=s", "functionalclassestouse=s", "atomtypessettouse|e=s", "bitsorder=s", "bitstringformat|b=s", "compoundid=s", "compoundidlabel=s", "compoundidmode=s", "datafields=s", "datafieldsmode|d=s", "filter|f=s", "fingerprintslabelmode=s", "fingerprintslabel=s", "help|h", "ignorehydrogens|i=s", "keeplargestcomponent|k=s", "mode|m=s", "outdelim=s", "output=s", "overwrite|o", "quote|q=s", "root|r=s", "vectorstringformat|v=s", "workingdir|w=s")) { 806 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"; 807 } 808 if ($Options{workingdir}) { 809 if (! -d $Options{workingdir}) { 810 die "Error: The value specified, $Options{workingdir}, for option \"-w --workingdir\" is not a directory name.\n"; 811 } 812 chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n"; 813 } 814 if (!Molecule::IsSupportedAromaticityModel($Options{aromaticitymodel})) { 815 my(@SupportedModels) = Molecule::GetSupportedAromaticityModels(); 816 die "Error: The value specified, $Options{aromaticitymodel}, for option \"--AromaticityModel\" is not valid. Supported aromaticity models in current release of MayaChemTools: @SupportedModels\n"; 817 } 818 if ($Options{atomidentifiertype} !~ /^(AtomicInvariantsAtomTypes|DREIDINGAtomTypes|EStateAtomTypes|FunctionalClassAtomTypes|MMFF94AtomTypes|SLogPAtomTypes|SYBYLAtomTypes|TPSAAtomTypes|UFFAtomTypes)$/i) { 819 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"; 820 } 821 if ($Options{atomtypessettouse} && $Options{atomtypessettouse} !~ /^(ArbitrarySize|FixedSize)$/) { 822 die "Error: The value specified, $Options{atomtypessettouse}, for option \"--AtomTypesSetToUse\" is not valid. Allowed values: ArbitrarySize or FixedSize\n"; 823 } 824 if ($Options{bitsorder} !~ /^(Ascending|Descending)$/i) { 825 die "Error: The value specified, $Options{bitsorder}, for option \"--BitsOrder\" is not valid. Allowed values: Ascending or Descending\n"; 826 } 827 if ($Options{bitstringformat} !~ /^(BinaryString|HexadecimalString)$/i) { 828 die "Error: The value specified, $Options{bitstringformat}, for option \"-b, --bitstringformat\" is not valid. Allowed values: BinaryString or HexadecimalString\n"; 829 } 830 if ($Options{compoundidmode} !~ /^(DataField|MolName|LabelPrefix|MolNameOrLabelPrefix)$/i) { 831 die "Error: The value specified, $Options{compoundidmode}, for option \"--CompoundIDMode\" is not valid. Allowed values: DataField, MolName, LabelPrefix or MolNameOrLabelPrefix\n"; 832 } 833 if ($Options{datafieldsmode} !~ /^(All|Common|Specify|CompoundID)$/i) { 834 die "Error: The value specified, $Options{datafieldsmode}, for option \"-d, --DataFieldsMode\" is not valid. Allowed values: All, Common, Specify or CompoundID\n"; 835 } 836 if ($Options{filter} !~ /^(Yes|No)$/i) { 837 die "Error: The value specified, $Options{filter}, for option \"-f, --Filter\" is not valid. Allowed values: Yes or No\n"; 838 } 839 if ($Options{fingerprintslabelmode} !~ /^(FingerprintsLabelOnly|FingerprintsLabelWithIDs)$/i) { 840 die "Error: The value specified, $Options{fingerprintslabelmode}, for option \"--FingerprintsLabelMode\" is not valid. Allowed values: FingerprintsLabelOnly or FingerprintsLabelWithIDs\n"; 841 } 842 if ($Options{ignorehydrogens} !~ /^(Yes|No)$/i) { 843 die "Error: The value specified, $Options{ignorehydrogens}, for option \"-i, --IgnoreHydrogens\" is not valid. Allowed values: Yes or No\n"; 844 } 845 if ($Options{keeplargestcomponent} !~ /^(Yes|No)$/i) { 846 die "Error: The value specified, $Options{keeplargestcomponent}, for option \"-k, --KeepLargestComponent\" is not valid. Allowed values: Yes or No\n"; 847 } 848 if ($Options{mode} !~ /^(AtomTypesCount|AtomTypesBits)$/i) { 849 die "Error: The value specified, $Options{mode}, for option \"-m, --mode\" is not valid. Allowed values: AtomTypesCount, or AtomTypesBits\n"; 850 } 851 if ($Options{output} !~ /^(SD|FP|text|all)$/i) { 852 die "Error: The value specified, $Options{output}, for option \"--output\" is not valid. Allowed values: SD, FP, text, or all\n"; 853 } 854 if ($Options{outdelim} !~ /^(comma|semicolon|tab)$/i) { 855 die "Error: The value specified, $Options{outdelim}, for option \"--outdelim\" is not valid. Allowed values: comma, tab, or semicolon\n"; 856 } 857 if ($Options{quote} !~ /^(Yes|No)$/i) { 858 die "Error: The value specified, $Options{quote}, for option \"-q --quote\" is not valid. Allowed values: Yes or No\n"; 859 } 860 if ($Options{outdelim} =~ /semicolon/i && $Options{quote} =~ /^No$/i) { 861 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"; 862 } 863 if ($Options{vectorstringformat} && $Options{vectorstringformat} !~ /^(ValuesString|IDsAndValuesString|IDsAndValuesPairsString|ValuesAndIDsString|ValuesAndIDsPairsString)$/i) { 864 die "Error: The value specified, $Options{vectorstringformat}, for option \"-v, --VectorStringFormat\" is not valid. Allowed values: ValuesString, IDsAndValuesString, IDsAndValuesPairsString, ValuesAndIDsString or ValuesAndIDsPairsString\n"; 865 } 866 } 867