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