MayaChemTools

   1 #!/usr/bin/perl -w
   2 #
   3 # File: JoinSDFiles.pl
   4 # Author: Manish Sud <msud@san.rr.com>
   5 #
   6 # Copyright (C) 2024 Manish Sud. All rights reserved.
   7 #
   8 # This file is part of MayaChemTools.
   9 #
  10 # MayaChemTools is free software; you can redistribute it and/or modify it under
  11 # the terms of the GNU Lesser General Public License as published by the Free
  12 # Software Foundation; either version 3 of the License, or (at your option) any
  13 # later version.
  14 #
  15 # MayaChemTools is distributed in the hope that it will be useful, but without
  16 # any warranty; without even the implied warranty of merchantability of fitness
  17 # for a particular purpose.  See the GNU Lesser General Public License for more
  18 # details.
  19 #
  20 # You should have received a copy of the GNU Lesser General Public License
  21 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or
  22 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330,
  23 # Boston, MA, 02111-1307, USA.
  24 #
  25 
  26 use strict;
  27 use FindBin; use lib "$FindBin::Bin/../lib";
  28 use Getopt::Long;
  29 use File::Basename;
  30 use Benchmark;
  31 use SDFileUtil;
  32 use FileUtil;
  33 
  34 my($ScriptName, %Options, $StartTime, $EndTime, $TotalTime);
  35 
  36 # Autoflush STDOUT
  37 $| = 1;
  38 
  39 # Starting message...
  40 $ScriptName = basename $0;
  41 print "\n$ScriptName:Starting...\n\n";
  42 $StartTime = new Benchmark;
  43 
  44 # Get the options and setup script...
  45 SetupScriptUsage();
  46 if ($Options{help} || @ARGV < 1) {
  47   die GetUsageFromPod("$FindBin::Bin/$ScriptName");
  48 }
  49 
  50 my(@SDFilesList);
  51 @SDFilesList = ExpandFileNames(\@ARGV, "sdf sd");
  52 if (@SDFilesList == 1) {
  53   die "Error: Specify more than one SD file.\n";
  54 }
  55 
  56 # Process options...
  57 print "Processing options...\n";
  58 my(%OptionsInfo);
  59 ProcessOptions();
  60 
  61 # Setup information about input files...
  62 print "Checking input SD files...\n";
  63 my(%SDFilesInfo);
  64 RetrieveSDFilesInfo();
  65 
  66 # Join files...
  67 print "\nGenerating new SD file $OptionsInfo{NewSDFile}...\n";
  68 JoinSDFiles();
  69 
  70 print "\n$ScriptName:Done...\n\n";
  71 
  72 $EndTime = new Benchmark;
  73 $TotalTime = timediff ($EndTime, $StartTime);
  74 print "Total time: ", timestr($TotalTime), "\n";
  75 
  76 ###############################################################################
  77 
  78 # Join all valid SD files...
  79 sub JoinSDFiles {
  80   my($FileIndex, $SDFile, $NewSDFile);
  81 
  82   $NewSDFile = $OptionsInfo{NewSDFile};
  83 
  84   open NEWSDFILE, ">$NewSDFile" or die "Error: Couldn't open $NewSDFile: $! \n";
  85   FILELIST: for $FileIndex (0 .. $#SDFilesList) {
  86     if (!$SDFilesInfo{FileOkay}[$FileIndex]) {
  87       next FILELIST;
  88     }
  89     $SDFile = $SDFilesList[$FileIndex];
  90     print "\nProcessing file $SDFile...\n";
  91 
  92     open SDFILE, "$SDFile" or die "Error: Couldn't open $SDFile: $! \n";
  93     while (<SDFILE>) {
  94       s/(\r\n)|(\r)/\n/g;
  95       print NEWSDFILE;
  96     }
  97     close SDFILE;
  98   }
  99 
 100   close NEWSDFILE;
 101 }
 102 
 103 # Retrieve information about SD files...
 104 sub RetrieveSDFilesInfo {
 105   my($Index, $SDFile);
 106 
 107   %SDFilesInfo = ();
 108   @{$SDFilesInfo{FileOkay}} = ();
 109 
 110   FILELIST: for $Index (0 .. $#SDFilesList) {
 111     $SDFilesInfo{FileOkay}[$Index] = 0;
 112 
 113     $SDFile = $SDFilesList[$Index];
 114     if (!(-e $SDFile)) {
 115       warn "Warning: Ignoring file $SDFile: It doesn't exist\n";
 116       next FILELIST;
 117     }
 118     if (!CheckFileType($SDFile, "sdf sd")) {
 119       warn "Warning: Ignoring file $SDFile: It's not a SD file\n";
 120       next FILELIST;
 121     }
 122     if (! open SDFILE, "$SDFile") {
 123       warn "Warning: Ignoring file $SDFile: Couldn't open it: $! \n";
 124       next FILELIST;
 125     }
 126     close SDFILE;
 127 
 128     $SDFilesInfo{FileOkay}[$Index] = 1;
 129   }
 130 }
 131 
 132 # Process option values...
 133 sub ProcessOptions {
 134   my($FileDir, $FileName, $FileExt, $NewSDFile);
 135 
 136   %OptionsInfo = ();
 137 
 138   $OptionsInfo{OutFileRoot} = $Options{root} ? $Options{root} : undef;
 139   $OptionsInfo{Overwrite} = $Options{overwrite} ? $Options{overwrite} : undef;
 140 
 141   if ($Options{root}) {
 142     $FileDir = ""; $FileName = ""; $FileExt = "";
 143     ($FileDir, $FileName, $FileExt) = ParseFileName($Options{root});
 144     if ($FileName && $FileExt) {
 145       $NewSDFile = $FileName . "." . $FileExt;
 146     }
 147     else {
 148       $NewSDFile =  $Options{root} . ".sdf";
 149     }
 150   }
 151   else {
 152     $FileDir = ""; $FileName = ""; $FileExt = "";
 153     ($FileDir, $FileName, $FileExt) = ParseFileName($SDFilesList[0]);
 154     $NewSDFile = $FileName . "1To" . @SDFilesList . "Joined.sdf";
 155   }
 156 
 157   if (!$Options{overwrite}) {
 158     if (-e $NewSDFile) {
 159       die "Error: The file $NewSDFile already exists.\n";
 160     }
 161   }
 162   if ($Options{root}) {
 163     my($FileIndex);
 164     for $FileIndex (0 .. $#SDFilesList) {
 165       if (lc($NewSDFile) eq lc($SDFilesList[$FileIndex])) {
 166         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";
 167       }
 168     }
 169   }
 170   $OptionsInfo{NewSDFile} = $NewSDFile;
 171 
 172 }
 173 
 174 # Setup script usage  and retrieve command line arguments specified using various options...
 175 sub SetupScriptUsage {
 176 
 177   # Retrieve all the options...
 178   %Options = ();
 179   if (!GetOptions(\%Options, "help|h", "overwrite|o", "root|r=s", "workingdir|w=s")) {
 180     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";
 181   }
 182   if ($Options{workingdir}) {
 183     if (! -d $Options{workingdir}) {
 184       die "Error: The value specified, $Options{workingdir}, for option \"-w --workingdir\" is not a directory name.\n";
 185     }
 186     chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n";
 187   }
 188 }
 189