MayaChemTools

   1 #!/usr/bin/perl -w
   2 #
   3 # $RCSfile: SortSDFiles.pl,v $
   4 # $Date: 2008/01/30 21:45:03 $
   5 # $Revision: 1.13 $
   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 FileUtil;
  37 use SDFileUtil;
  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 # Get the options and setup script...
  51 SetupScriptUsage();
  52 if ($Options{help} || @ARGV < 1) {
  53   die GetUsageFromPod("$FindBin::Bin/$ScriptName");
  54 }
  55 
  56 my(@SDFilesList);
  57 @SDFilesList = ExpandFileNames(\@ARGV, "sdf sd");
  58 
  59 my($SpecifiedDataFieldName, $DetailLevel);
  60 ProcessOptions();
  61 
  62 print "Checking input SD file(s)...\n";
  63 my(@SDFilesOkay, @SDFilesOutFile, @SDFilesKeyDataFieldName);
  64 RetrieveSDFilesInfo();
  65 
  66 # Generate output files...
  67 my($Index, $SDFile);
  68 if (@SDFilesList > 1) {
  69   print "Processing SD file(s)...\n";
  70 }
  71 for $Index (0 .. $#SDFilesList) {
  72   if ($SDFilesOkay[$Index]) {
  73     $SDFile = $SDFilesList[$Index];
  74     if (@SDFilesList > 1) {
  75       print "\nProcessing file $SDFile...\n";
  76     }
  77     else {
  78       print "Processing file $SDFile...\n"
  79     }
  80     SortSDFile($Index);
  81   }
  82 }
  83 
  84 print "$ScriptName:Done...\n\n";
  85 
  86 $EndTime = new Benchmark;
  87 $TotalTime = timediff ($EndTime, $StartTime);
  88 print "Total time: ", timestr($TotalTime), "\n";
  89 
  90 ###############################################################################
  91 
  92 # Process option values...
  93 sub ProcessOptions {
  94   $DetailLevel = $Options{detail};
  95 
  96   $SpecifiedDataFieldName = "";
  97   if (defined $Options{key}) {
  98     $SpecifiedDataFieldName = $Options{key};
  99   }
 100 }
 101 
 102 # Retrieve information about input SD files...
 103 sub RetrieveSDFilesInfo {
 104   my($Index, $SDFile, $FileDir, $FileName, $FileExt, $OutFileRoot,  $OutFile, $DataFieldName);
 105 
 106   @SDFilesOkay = ();
 107   @SDFilesOutFile = ();
 108   @SDFilesKeyDataFieldName = ();
 109 
 110  FILELIST: for $Index (0 .. $#SDFilesList) {
 111     $SDFile = $SDFilesList[$Index];
 112     $SDFilesOkay[$Index] = 0;
 113     $SDFilesOutFile[$Index] = "";
 114     $SDFilesKeyDataFieldName[$Index] = "";
 115     if (!(-e $SDFile)) {
 116       warn "Warning: Ignoring file $SDFile: It doesn't exist\n";
 117       next FILELIST;
 118     }
 119     if (!CheckFileType($SDFile, "sd sdf")) {
 120       warn "Warning: Ignoring file $SDFile: It's not a SD file\n";
 121       next FILELIST;
 122     }
 123     $FileDir = ""; $FileName = ""; $FileExt = "";
 124     ($FileDir, $FileName, $FileExt) = ParseFileName($SDFile);
 125     if ($Options{root} && (@SDFilesList == 1)) {
 126       my ($RootFileDir, $RootFileName, $RootFileExt) = ParseFileName($Options{root});
 127       if ($RootFileName && $RootFileExt) {
 128 	$FileName = $RootFileName;
 129       }
 130       else {
 131 	$FileName = $Options{root};
 132       }
 133       $OutFileRoot = $FileName;
 134     }
 135     else {
 136       $OutFileRoot = $FileName . "SortedByDataField";
 137     }
 138 
 139     $OutFile = $OutFileRoot . ".$FileExt";
 140     if (lc($OutFile) eq lc($SDFile)) {
 141       warn "Warning: Ignoring file $SDFile:Output file name, $OutFile, is same as input SD file name, $SDFile\n";
 142       next FILELIST;
 143     }
 144     if (!$Options{overwrite}) {
 145       if (-e $OutFile) {
 146 	warn "Warning: Ignoring file $SDFile: The file $OutFile already exists\n";
 147 	next FILELIST;
 148       }
 149     }
 150     # Setup data field name...
 151     if ($SpecifiedDataFieldName) {
 152       $DataFieldName = $SpecifiedDataFieldName;
 153     }
 154     else {
 155       my($CmpdString, @CmpdLines, @DataFieldNames);
 156       @DataFieldNames = ();
 157       if (!open(SDFILE, "$SDFile")) {
 158 	warn "Warning: Ignoring file $SDFile: Couldn't open it: $! \n";
 159 	next FILELIST;
 160       }
 161       $CmpdString = ReadCmpdString(\*SDFILE);
 162       close SDFILE;
 163 
 164       @CmpdLines = split "\n", $CmpdString;
 165       @DataFieldNames = GetCmpdDataHeaderLabels(\@CmpdLines);
 166       $DataFieldName = $DataFieldNames[0];
 167     }
 168 
 169     $SDFilesOkay[$Index] = 1;
 170     $SDFilesOutFile[$Index] = "$OutFile";
 171     $SDFilesKeyDataFieldName[$Index] = $DataFieldName;
 172   }
 173 }
 174 
 175 # Sort it out...
 176 sub SortSDFile {
 177   my($Index) = @_;
 178   my($SDFile, $NewSDFile, $KeyDataFieldName);
 179 
 180   $SDFile = $SDFilesList[$Index];
 181   $NewSDFile = $SDFilesOutFile[$Index];
 182   $KeyDataFieldName = $SDFilesKeyDataFieldName[$Index];
 183 
 184   print "Generating new SD file $NewSDFile...\n";
 185   open NEWSDFILE, ">$NewSDFile" or die "Error: Couldn't open $NewSDFile: $! \n";
 186   open SDFILE, "$SDFile" or die "Error: Can't open $SDFile: $! \n";
 187 
 188   # Go over all compound records and store 'em using key value as hash...
 189   my(%KeyToCompundRecordsMap, @InvalidCompoundRecords, $CmpdCount, $CmpdString, @CmpdLines, %DataFieldValues, $KeyDataFieldValue);
 190   %KeyToCompundRecordsMap = ();
 191   @InvalidCompoundRecords = ();
 192   $CmpdCount = 0;
 193 
 194   COMPOUND: while ($CmpdString = ReadCmpdString(\*SDFILE)) {
 195       $CmpdCount++;
 196       @CmpdLines = split "\n", $CmpdString;
 197       %DataFieldValues = GetCmpdDataHeaderLabelsAndValues(\@CmpdLines);
 198       #Make sure data field value is okay...
 199       if (!(IsNotEmpty($DataFieldValues{$KeyDataFieldName}) && ($DataFieldValues{$KeyDataFieldName} !~ /\n/))) {
 200 	push @InvalidCompoundRecords, $CmpdString;
 201 	if ($DetailLevel >= 3 ) {
 202 	  print "Ignoring compound record $CmpdCount: Contains empty value for key data field $KeyDataFieldName :\n $CmpdString\n\n";
 203 	}
 204 	elsif ($DetailLevel >= 2) {
 205 	  print "Ignoring compound record $CmpdCount: Contains empty value for key data field $KeyDataFieldName...\n";
 206 	}
 207 	next COMPOUND;
 208       }
 209       $KeyDataFieldValue = $DataFieldValues{$KeyDataFieldName};
 210       if ($Options{keydata} =~ /^numeric$/i) {
 211 	if (!IsFloat($KeyDataFieldValue)) {
 212 	  push @InvalidCompoundRecords, $CmpdString;
 213 	  if ($DetailLevel >= 3 ) {
 214 	    print "Ignoring compound record $CmpdCount: Contains non-numerical value for key data field $KeyDataFieldName :\n $CmpdString\n\n";
 215 	  }
 216 	  elsif ($DetailLevel >= 2) {
 217 	    print "Ignoring compound record $CmpdCount: Contains non-numerical value for key data field $KeyDataFieldName...\n";
 218 	  }
 219 	  next COMPOUND;
 220 	}
 221       }
 222       if (exists($KeyToCompundRecordsMap{$KeyDataFieldValue})) {
 223 	# Append to existing coompund data...
 224 	$KeyToCompundRecordsMap{$KeyDataFieldValue} .= "\n" . $CmpdString;
 225       }
 226       else {
 227 	$KeyToCompundRecordsMap{$KeyDataFieldValue} = $CmpdString;
 228       }
 229   }
 230 
 231   if ($Options{sort} =~ /^ascending$/i) {
 232     if ($Options{keydata} =~ /^alphanumeric$/i) {
 233       for $KeyDataFieldValue (sort { lc($a) cmp lc($b) } keys %KeyToCompundRecordsMap ) {
 234 	print NEWSDFILE "$KeyToCompundRecordsMap{$KeyDataFieldValue}\n";
 235       }
 236     }
 237     else {
 238       for $KeyDataFieldValue (sort { $a <=> $b } keys %KeyToCompundRecordsMap ) {
 239 	print NEWSDFILE "$KeyToCompundRecordsMap{$KeyDataFieldValue}\n";
 240       }
 241     }
 242   }
 243   else {
 244     if ($Options{keydata} =~ /^alphanumeric$/i) {
 245       for $KeyDataFieldValue (sort { lc($b) cmp lc($a) } keys %KeyToCompundRecordsMap ) {
 246 	print NEWSDFILE "$KeyToCompundRecordsMap{$KeyDataFieldValue}\n";
 247       }
 248     }
 249     else {
 250       for $KeyDataFieldValue (sort { $b <=> $a } keys %KeyToCompundRecordsMap ) {
 251 	print NEWSDFILE "$KeyToCompundRecordsMap{$KeyDataFieldValue}\n";
 252       }
 253     }
 254   }
 255   # Append the records containing data not appropriate for sorting...
 256   if (@InvalidCompoundRecords) {
 257     print "Placing ", scalar(@InvalidCompoundRecords)," compound record(s) with invalid data field key data the end...\n";
 258     for $CmpdString (@InvalidCompoundRecords) {
 259       print NEWSDFILE "$CmpdString\n";
 260     }
 261   }
 262   close NEWSDFILE;
 263   close SDFILE;
 264 }
 265 
 266 # Setup script usage  and retrieve command line arguments specified using various options...
 267 sub SetupScriptUsage {
 268 
 269   # Retrieve all the options...
 270   %Options = ();
 271   $Options{detail} = 1;
 272   $Options{sort} = "ascending";
 273   $Options{keydata} = "numeric";
 274   if (!GetOptions(\%Options, "detail|d=i", "help|h",  "key|k=s", "keydata=s", "overwrite|o", "root|r=s", "sort|s=s", "workingdir|w=s")) {
 275     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";
 276   }
 277   if ($Options{workingdir}) {
 278     if (! -d $Options{workingdir}) {
 279       die "Error: The value specified, $Options{workingdir}, for option \"-w --workingdir\" is not a directory name.\n";
 280     }
 281     chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n";
 282   }
 283   if ($Options{keydata} !~ /^(numeric|alphanumeric)$/i) {
 284     die "Error: The value specified, $Options{keydata}, for option \"--keydata\" is not valid. Allowed values: numeric or alphanumeric\n";
 285   }
 286   if ($Options{sort} !~ /^(ascending|descending)$/i) {
 287     die "Error: The value specified, $Options{sort}, for option \"-s --sort\" is not valid. Allowed values: ascending or descending\n";
 288   }
 289   if (!IsPositiveInteger($Options{detail})) {
 290     die "Error: The value specified, $Options{detail}, for option \"-d --detail\" is not valid. Allowed values: > 0\n";
 291   }
 292 }
 293