MayaChemTools

   1 #!/usr/bin/perl -w
   2 #
   3 # $RCSfile: MolFilesToSD.pl,v $
   4 # $Date: 2008/01/30 21:44:50 $
   5 # $Revision: 1.24 $
   6 #
   7 # Author: Manish Sud <msud@san.rr.com>
   8 #
   9 # Copyright (C) 2004-2008 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 5.006;
  30 use strict;
  31 use FindBin; use lib "$FindBin::Bin/../lib";
  32 use Getopt::Long;
  33 use File::Basename;
  34 use Text::ParseWords;
  35 use Benchmark;
  36 use SDFileUtil;
  37 use FileUtil;
  38 use TextUtil;
  39 
  40 my($ScriptName, %Options, $StartTime, $EndTime, $TotalTime);
  41 
  42 # Autoflush STDOUT
  43 $| = 1;
  44 
  45 # Starting message...
  46 $ScriptName = basename $0;
  47 print "\n$ScriptName:Starting...\n\n";
  48 $StartTime = new Benchmark;
  49 
  50 my($MOLFile, @MOLFilesList, $SDFile, $Index, $FileDir, $FileName, $FileExt, $AddMolNameLine, $AddDataField, $UseFilePrefix, $FileCount, $MolNameLine, $CmpdID);
  51 
  52 # Get the options and setup script...
  53 SetupScriptUsage();
  54 if ($Options{help} || @ARGV < 1) {
  55   die GetUsageFromPod("$FindBin::Bin/$ScriptName");
  56 }
  57 
  58 @MOLFilesList = ExpandFileNames(\@ARGV, "mol");
  59 
  60 if ($Options{root}) {
  61   $FileDir = ""; $FileName = ""; $FileExt = "";
  62   ($FileDir, $FileName, $FileExt) = ParseFileName($Options{root});
  63   if ($FileName && $FileExt) {
  64     $SDFile = $FileName;
  65   }
  66   else {
  67       $SDFile =  $Options{root};
  68   }
  69   $SDFile .=  ".sdf";
  70 }
  71 else {
  72   $FileDir = ""; $FileName = ""; $FileExt = "";
  73   ($FileDir, $FileName, $FileExt) = ParseFileName($MOLFilesList[0]);
  74   $SDFile = $FileName . "1To" . @MOLFilesList . ".sdf";
  75 }
  76 if (!$Options{overwrite}) {
  77   if (-e $SDFile) {
  78     die "Error: The file $SDFile already exists.\n";
  79   }
  80 }
  81 print "Generating SD file $SDFile...\n";
  82 
  83 open SDFILE, ">$SDFile" or die "Error: Can't open $SDFile: $! \n";
  84 $FileCount = 0;
  85 FILELIST: for $Index (0 .. $#MOLFilesList) {
  86   $MOLFile = $MOLFilesList[$Index];
  87   $FileCount++;
  88   if (@MOLFilesList > 1) {
  89     print "\nProcessing file $MOLFile...\n";
  90   }
  91   else {
  92     print "Processing file $MOLFile...\n"
  93   }
  94   if (!(-e $MOLFile)) {
  95     warn "Warning: Ignoring file $MOLFile: It doesn't exist\n";
  96     next FILELIST;
  97   }
  98   if (!CheckFileType($MOLFile, "mol")) {
  99     warn "Warning: Ignoring file $MOLFile: It's not a MDLMOL file\n";
 100     next FILELIST;
 101   }
 102   if (!open MOLFILE, "$MOLFile") {
 103     warn "Warning: Ignoring file $MOLFile: Couldn't open it: $! \n";
 104     next FILELIST;
 105   }
 106   if ($AddMolNameLine || $AddDataField) {
 107     $MolNameLine = <MOLFILE>;
 108     if ($UseFilePrefix) {
 109       ($FileDir, $FileName, $FileExt) = ParseFileName($MOLFile);
 110       $CmpdID = $FileName;
 111     }
 112     else {
 113       $CmpdID = $Options{compoundid} . "$FileCount";
 114     }
 115     if ($AddMolNameLine) {
 116       print SDFILE "$CmpdID\n";
 117     }
 118     else {
 119       $MolNameLine =~ s/(\r\n)|(\r)/\n/g;
 120       print SDFILE $MolNameLine;
 121     }
 122     while (<MOLFILE>) {
 123       s/(\r\n)|(\r)/\n/g;
 124       print SDFILE;
 125     }
 126     if ($AddDataField) {
 127       print SDFILE ">  <$Options{datafieldlabel}>\n${CmpdID}\n";
 128     }
 129   }
 130   else {
 131     while (<MOLFILE>) {
 132       s/(\r\n)|(\r)/\n/g;
 133       print SDFILE;
 134     }
 135   }
 136   print SDFILE "\n\$\$\$\$\n";
 137   close MOLFILE;
 138 }
 139 close SDFILE;
 140 print "$ScriptName:Done...\n\n";
 141 
 142 $EndTime = new Benchmark;
 143 $TotalTime = timediff ($EndTime, $StartTime);
 144 print "Total time: ", timestr($TotalTime), "\n";
 145 
 146 ###############################################################################
 147 
 148 # Setup script usage  and retrieve command line arguments specified using various options...
 149 sub SetupScriptUsage {
 150 
 151   # Retrieve all the options...
 152   %Options = ();
 153   $Options{compoundid} = "Cmpd";
 154   $Options{datafieldlabel} = "Cmpd_ID";
 155   $Options{mode} = "none";
 156 
 157   if (!GetOptions(\%Options, "compoundid|c=s", "datafieldlabel|d=s", "help|h", "mode|m=s", "overwrite|o", "root|r=s", "workingdir|w=s")) {
 158     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";
 159   }
 160   if ($Options{workingdir}) {
 161     if (! -d $Options{workingdir}) {
 162       die "Error: The value specified, $Options{workingdir}, for option \"-w --workingdir\" is not a directory name.\n";
 163     }
 164     chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n";
 165   }
 166   if ($Options{mode} !~ /^(molnameline|datafield|both|none)$/i ) {
 167     die "Error: The value specified, $Options{mode}, for option \"-m --mode\" is not valid. Allowed values: molnameline, datafield, both, or none\n";
 168   }
 169   $AddMolNameLine = ($Options{mode} =~ /^(molnameline|both)$/i) ? 1 : 0;
 170   $AddDataField = ($Options{mode} =~ /^(datafield|both)$/i) ? 1 : 0;
 171   $UseFilePrefix = ($Options{compoundid} =~ /^usefileprefix$/i) ? 1 : 0;
 172 }
 173