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