MayaChemTools

   1 #!/usr/bin/perl -w
   2 #
   3 # $RCSfile: JoinSDFiles.pl,v $
   4 # $Date: 2008/01/30 21:44:48 $
   5 # $Revision: 1.21 $
   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 Benchmark;
  35 use SDFileUtil;
  36 use FileUtil;
  37 
  38 my($ScriptName, %Options, $StartTime, $EndTime, $TotalTime);
  39 my($SDFile, @SDFilesList, $NewSDFile, $Index, $FileDir, $FileName, $FileExt);
  40 
  41 # Autoflush STDOUT
  42 $| = 1;
  43 
  44 # Starting message...
  45 $ScriptName = basename $0;
  46 print "\n$ScriptName:Starting...\n\n";
  47 $StartTime = new Benchmark;
  48 
  49 # Get the options and setup script...
  50 SetupScriptUsage();
  51 if ($Options{help} || @ARGV < 1) {
  52   die GetUsageFromPod("$FindBin::Bin/$ScriptName");
  53 }
  54 
  55 @SDFilesList = ExpandFileNames(\@ARGV, "sdf sd");
  56 
  57 if (@SDFilesList == 1) {
  58   die "Error: Specify more than one SD file.\n";
  59 }
  60 
  61 if ($Options{root}) {
  62   $FileDir = ""; $FileName = ""; $FileExt = "";
  63   ($FileDir, $FileName, $FileExt) = ParseFileName($Options{root});
  64   if ($FileName && $FileExt) {
  65     $NewSDFile = $FileName . "." . $FileExt;
  66   }
  67   else {
  68       $NewSDFile =  $Options{root} . ".sdf";
  69   }
  70 }
  71 else {
  72   $FileDir = ""; $FileName = ""; $FileExt = "";
  73   ($FileDir, $FileName, $FileExt) = ParseFileName($SDFilesList[0]);
  74   $NewSDFile = $FileName . "1To" . @SDFilesList . "Joined.sdf";
  75 }
  76 if (!$Options{overwrite}) {
  77   if (-e $NewSDFile) {
  78     die "Error: The file $NewSDFile already exists.\n";
  79   }
  80 }
  81 if ($Options{root}) {
  82   for $Index (0 .. $#SDFilesList) {
  83     if (lc($NewSDFile) eq lc($SDFilesList[$Index])) {
  84       die "Error: Output filename, $NewSDFile, is similar to a input file name.\nSpecify a different name using \"-r --root\" option or use default name.\n";
  85     }
  86   }
  87 }
  88 print "Generating new SD file $NewSDFile...\n";
  89 
  90 open NEWSDFILE, ">$NewSDFile" or die "Error: Couldn't open $NewSDFile: $! \n";
  91 FILELIST: for $Index (0 .. $#SDFilesList) {
  92   $SDFile = $SDFilesList[$Index];
  93   print "\nProcessing file $SDFile...\n";
  94   if (!(-e $SDFile)) {
  95     warn "Warning: Ignoring file $SDFile: It doesn't exist\n";
  96     next FILELIST;
  97   }
  98   if (!CheckFileType($SDFile, "sd sdf")) {
  99     warn "Warning: Ignoring file $SDFile: It's not a SD file\n";
 100     next FILELIST;
 101   }
 102   if (!open SDFILE, "$SDFile") {
 103     warn "Warning: Ignoring file $SDFile: Couldn't open it: $! \n";
 104     next FILELIST;
 105   }
 106   while (<SDFILE>) {
 107     s/(\r\n)|(\r)/\n/g;
 108     print NEWSDFILE;
 109   }
 110   close SDFILE;
 111 }
 112 close NEWSDFILE;
 113 
 114 print "$ScriptName:Done...\n\n";
 115 
 116 $EndTime = new Benchmark;
 117 $TotalTime = timediff ($EndTime, $StartTime);
 118 print "Total time: ", timestr($TotalTime), "\n";
 119 
 120 ###############################################################################
 121 
 122 # Setup script usage  and retrieve command line arguments specified using various options...
 123 sub SetupScriptUsage {
 124 
 125   # Retrieve all the options...
 126   %Options = ();
 127   if (!GetOptions(\%Options, "help|h", "overwrite|o", "root|r=s", "workingdir|w=s")) {
 128     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";
 129   }
 130   if ($Options{workingdir}) {
 131     if (! -d $Options{workingdir}) {
 132       die "Error: The value specified, $Options{workingdir}, for option \"-w --workingdir\" is not a directory name.\n";
 133     }
 134     chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n";
 135   }
 136 }
 137