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