MayaChemTools

   1 #!/usr/bin/perl -w
   2 #
   3 # File: ModifyTextFilesFormat.pl
   4 # Author: Manish Sud <msud@san.rr.com>
   5 #
   6 # Copyright (C) 2019 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 Text::ParseWords;
  31 use Benchmark;
  32 use FileUtil;
  33 use TextUtil;
  34 
  35 my($ScriptName, %Options, $StartTime, $EndTime, $TotalTime);
  36 
  37 # Autoflush STDOUT
  38 $| = 1;
  39 
  40 # Starting message...
  41 $ScriptName = basename $0;
  42 print "\n$ScriptName:Starting...\n\n";
  43 $StartTime = new Benchmark;
  44 
  45 # Get the options and setup script...
  46 SetupScriptUsage();
  47 if ($Options{help} || @ARGV < 1) {
  48   die GetUsageFromPod("$FindBin::Bin/$ScriptName");
  49 }
  50 
  51 my(@TextFilesList);
  52 @TextFilesList = ExpandFileNames(\@ARGV, "csv tsv");
  53 
  54 # Process options...
  55 print "Processing options...\n";
  56 my(%OptionsInfo);
  57 ProcessOptions();
  58 
  59 print "Checking input text file(s)...\n";
  60 my(%TextFilesInfo);
  61 RetrieveTextFilesInfo();
  62 
  63 # Generate output files...
  64 my($FileIndex);
  65 if (@TextFilesList > 1) {
  66   print "\nProcessing text files...\n";
  67 }
  68 for $FileIndex (0 .. $#TextFilesList) {
  69   if ($TextFilesInfo{FileOkay}[$FileIndex]) {
  70     print "\nProcessing file $TextFilesList[$FileIndex]...\n";
  71     ModifyTextFileFormat($FileIndex);
  72   }
  73 }
  74 print "\n$ScriptName:Done...\n\n";
  75 
  76 $EndTime = new Benchmark;
  77 $TotalTime = timediff ($EndTime, $StartTime);
  78 print "Total time: ", timestr($TotalTime), "\n";
  79 
  80 ###############################################################################
  81 
  82 # Modify text file format...
  83 sub ModifyTextFileFormat {
  84   my($Index) = @_;
  85   my($TextFile, $NewTextFile, $InDelim, $Line, @Words);
  86 
  87   $TextFile = $TextFilesList[$Index];
  88 
  89   $NewTextFile = $TextFilesInfo{OutFile}[$Index];
  90   $InDelim = $TextFilesInfo{InDelim}[$Index];
  91 
  92   print "Generating new $NewTextFile file...\n";
  93 
  94   open NEWTEXTFILE, ">$NewTextFile" or die "Error: Can't open $NewTextFile !$ \n";
  95   open TEXTFILE, "$TextFile" or die "Error: Can't open $TextFile: $! \n";
  96 
  97   while ($Line = GetTextLine(\*TEXTFILE)) {
  98     @Words = quotewords($InDelim, 0, $Line);
  99     $Line = JoinWords(\@Words, $OptionsInfo{OutDelim}, $OptionsInfo{OutQuote});
 100     print NEWTEXTFILE "$Line\n";
 101   }
 102 
 103   close NEWTEXTFILE;
 104   close TEXTFILE;
 105 }
 106 
 107 # Retrieve information about input text files...
 108 sub RetrieveTextFilesInfo {
 109   my($Index, $TextFile, $NewTextFile, $FileDir, $FileName, $FileExt, $InDelim, $OutFileExt);
 110 
 111   %TextFilesInfo = ();
 112   @{$TextFilesInfo{FileOkay}} = ();
 113   @{$TextFilesInfo{InDelim}} = ();
 114   @{$TextFilesInfo{OutFile}} = ();
 115 
 116   FILELIST: for $Index (0 .. $#TextFilesList) {
 117     $TextFile = $TextFilesList[$Index];
 118 
 119     $TextFilesInfo{FileOkay}[$Index] = 0;
 120     $TextFilesInfo{InDelim}[$Index] = "";
 121     $TextFilesInfo{OutFile}[$Index] = "";
 122 
 123     if (!(-e $TextFile)) {
 124       warn "Warning: Ignoring file $TextFile: It doesn't exist\n";
 125       next FILELIST;
 126     }
 127     if (!CheckFileType($TextFile, "csv tsv")) {
 128       warn "Warning: Ignoring file $TextFile: It's not a csv or tsv file\n";
 129       next FILELIST;
 130     }
 131     if (!open TEXTFILE, "$TextFile") {
 132       warn "Warning: Ignoring file $TextFile: Couldn't open it: $! \n";
 133       next FILELIST;
 134     }
 135     close TEXTFILE;
 136 
 137     ($FileDir, $FileName, $FileExt) = ParseFileName($TextFile);
 138 
 139     if ($FileExt =~ /^tsv$/i) {
 140       $InDelim = "\t";
 141     }
 142     else {
 143       $InDelim = "\,";
 144       if ($OptionsInfo{InDelim} !~ /^(comma|semicolon)$/i) {
 145         warn "Warning: Ignoring file $TextFile: The value specified, $OptionsInfo{InDelim}, for option \"--indelim\" is not valid for csv files\n";
 146         next FILELIST;
 147       }
 148       if ($OptionsInfo{InDelim} =~ /^semicolon$/i) {
 149         $InDelim = "\;";
 150       }
 151     }
 152 
 153     if (lc($InDelim) eq lc($OptionsInfo{OutDelim})) {
 154       warn "Warning: Ignoring file $TextFile: The value specified, $Options{outdelim}, for option \"--outdelim\" is same as input delimiter\n";
 155       next FILELIST;
 156     }
 157 
 158     $OutFileExt = ($Options{outdelim} =~ /^tab$/i ) ? "tsv" : "csv";
 159 
 160     $NewTextFile = $FileName;
 161     if ($OptionsInfo{OutFileRoot} && (@TextFilesList == 1)) {
 162       my ($RootFileDir, $RootFileName, $RootFileExt) = ParseFileName($OptionsInfo{OutFileRoot});
 163       if ($RootFileName && $RootFileExt) {
 164         $NewTextFile = $RootFileName;
 165       }
 166       else {
 167         $NewTextFile = $OptionsInfo{OutFileRoot};
 168       }
 169       $NewTextFile .= ".$OutFileExt";
 170     }
 171     else {
 172       $NewTextFile .= "FormatModified" . ".$OutFileExt";
 173     }
 174 
 175     if (!$OptionsInfo{Overwrite}) {
 176       if (-e $NewTextFile) {
 177         warn "Warning: Ignoring file $TextFile: New Text file, $NewTextFile, already exists\n";
 178         next FILELIST;
 179       }
 180     }
 181 
 182     $TextFilesInfo{FileOkay}[$Index] = 1;
 183     $TextFilesInfo{InDelim}[$Index] = $InDelim;
 184     $TextFilesInfo{OutFile}[$Index] = $NewTextFile;
 185   }
 186 
 187 }
 188 
 189 # Process option values...
 190 sub ProcessOptions {
 191   %OptionsInfo = ();
 192 
 193   $OptionsInfo{InDelim} = $Options{indelim};
 194 
 195   $OptionsInfo{OutFileRoot} = $Options{root} ? $Options{root} : undef;
 196   $OptionsInfo{Overwrite} = $Options{overwrite} ? $Options{overwrite} : undef;
 197 
 198   $OptionsInfo{OutDelim} = ($Options{outdelim} =~ /^tab$/i ) ? "\t" : (($Options{outdelim} =~ /^semicolon$/i) ? "\;" : "\,");
 199   $OptionsInfo{OutQuote} = ($Options{quote} =~ /^yes$/i) ? 1 : 0;
 200 
 201 }
 202 
 203 # Setup script usage  and retrieve command line arguments specified using various options...
 204 sub SetupScriptUsage {
 205 
 206   # Retrieve all the options...
 207   %Options = ();
 208 
 209   $Options{indelim} = "comma";
 210   $Options{outdelim} = "tab";
 211   $Options{quote} = "yes";
 212 
 213   if (!GetOptions(\%Options, "help|h", "indelim=s", "outdelim=s", "overwrite|o", "quote|q=s", "root|r=s", "workingdir|w=s")) {
 214     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";
 215   }
 216   if ($Options{workingdir}) {
 217     if (! -d $Options{workingdir}) {
 218       die "Error: The value specified, $Options{workingdir},  for option \"-w --workingdir\" is not a directory name.\n";
 219     }
 220     chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n";
 221   }
 222   if ($Options{indelim} !~ /^(comma|semicolon)$/i) {
 223     die "Error: The value specified, $Options{indelim}, for option \"--indelim\" is not valid. Allowed values: comma or semicolon\n";
 224   }
 225   if ($Options{outdelim} !~ /^(comma|semicolon|tab)$/i) {
 226     die "Error: The value specified, $Options{outdelim}, for option \"--outdelim\" is not valid. Allowed values: comma, tab, or semicolon\n";
 227   }
 228   if ($Options{quote} !~ /^(yes|no)$/i) {
 229     die "Error: The value specified, $Options{quote}, for option \"-q --quote\" is not valid. Allowed values: yes or no\n";
 230   }
 231 }
 232