1 #!/usr/bin/perl -w 2 # 3 # $RCSfile: ModifyTextFilesFormat.pl,v $ 4 # $Date: 2008/01/30 21:44:49 $ 5 # $Revision: 1.20 $ 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 TextUtil; 38 39 my($ScriptName, %Options, $StartTime, $EndTime, $TotalTime); 40 my($TextFile, @TextFilesList, $Index, $OutQuote, $InDelim, $OutDelim, $FileDir, $FileName, $FileExt, $NewTextFileName, $Line, @Words); 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 @TextFilesList = ExpandFileNames(\@ARGV, "csv tsv"); 57 58 if (@TextFilesList > 1) { 59 print "Processing Text files...\n"; 60 } 61 FILELIST: for $Index (0 .. $#TextFilesList) { 62 $TextFile = $TextFilesList[$Index]; 63 if (@TextFilesList > 1) { 64 print "\nProcessing file $TextFile...\n"; 65 } 66 else { 67 print "Processing file $TextFile...\n" 68 } 69 if (!(-e $TextFile)) { 70 warn "Warning: Ignoring file $TextFile: It doesn't exist\n"; 71 next FILELIST; 72 } 73 if (!CheckFileType($TextFile, "csv tsv")) { 74 warn "Warning: Ignoring file $TextFile: It's not a text file\n"; 75 next FILELIST; 76 } 77 ($FileDir, $FileName, $FileExt) = ParseFileName($TextFile); 78 if ($FileExt =~ /^tsv$/i) { 79 $InDelim = "\t"; 80 } 81 else { 82 $InDelim = "\,"; 83 if ($Options{indelim} !~ /^(comma|semicolon)$/i) { 84 warn "Warning: Ignoring file $TextFile: The value specified, $Options{indelim}, for option \"--indelim\" is not valid for csv files\n"; 85 next FILELIST; 86 } 87 if ($Options{indelim} =~ /^semicolon$/i) { 88 $InDelim = "\;"; 89 } 90 } 91 if (lc($InDelim) eq lc($OutDelim)) { 92 warn "Warning: Ignoring file $TextFile: The value specified, $Options{outdelim}, for option \"--outdelim\" is same as input delimiter\n"; 93 next FILELIST; 94 } 95 if (!open TEXTFILE, "$TextFile") { 96 warn "Warning: Ignoring file $TextFile: Couldn't open it: $! \n"; 97 next FILELIST; 98 } 99 close TEXTFILE; 100 $FileDir = ""; $FileName = ""; $FileExt = ""; 101 ($FileDir, $FileName, $FileExt) = ParseFileName($TextFile); 102 $FileExt = "csv"; 103 if ($Options{outdelim} =~ /^tab$/i) { 104 $FileExt = "tsv"; 105 } 106 $NewTextFileName = $FileName; 107 if ($Options{root} && (@TextFilesList == 1)) { 108 my ($RootFileDir, $RootFileName, $RootFileExt) = ParseFileName($Options{root}); 109 if ($RootFileName && $RootFileExt) { 110 $NewTextFileName = $RootFileName; 111 } 112 else { 113 $NewTextFileName = $Options{root}; 114 } 115 $NewTextFileName .= ".$FileExt"; 116 } 117 else { 118 $NewTextFileName .= "FormatModified" . ".$FileExt"; 119 } 120 if (!$Options{overwrite}) { 121 if (-e $NewTextFileName) { 122 warn "Warning: Ignoring file $TextFile: New Text file, $NewTextFileName, already exists\n"; 123 next FILELIST; 124 } 125 } 126 open NEWTEXTFILE, ">$NewTextFileName" or die "Error: Can't open $NewTextFileName !$ \n"; 127 print "Generating $NewTextFileName file\n"; 128 open TEXTFILE, "$TextFile" or die "Error: Can't open $TextFile: $! \n"; 129 while ($Line = GetTextLine(\*TEXTFILE)) { 130 @Words = quotewords($InDelim, 0, $Line); 131 $Line = JoinWords(\@Words, $OutDelim, $OutQuote); 132 print NEWTEXTFILE "$Line\n"; 133 } 134 close NEWTEXTFILE; 135 close TEXTFILE; 136 } 137 138 print "$ScriptName:Done...\n\n"; 139 140 $EndTime = new Benchmark; 141 $TotalTime = timediff ($EndTime, $StartTime); 142 print "Total time: ", timestr($TotalTime), "\n"; 143 144 ############################################################################### 145 146 # Setup script usage and retrieve command line arguments specified using various options... 147 sub SetupScriptUsage { 148 149 # Retrieve all the options... 150 %Options = (); 151 $Options{indelim} = "comma"; 152 $Options{outdelim} = "tab"; 153 $Options{quote} = "yes"; 154 if (!GetOptions(\%Options, "help|h", "indelim=s", "outdelim=s", "overwrite|o", "quote|q=s", "root|r=s", "workingdir|w=s")) { 155 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"; 156 } 157 if ($Options{workingdir}) { 158 if (! -d $Options{workingdir}) { 159 die "Error: The value specified, $Options{workingdir}, for option \"-w --workingdir\" is not a directory name.\n"; 160 } 161 chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n"; 162 } 163 if ($Options{indelim} !~ /^(comma|semicolon)$/i) { 164 die "Error: The value specified, $Options{indelim}, for option \"--indelim\" is not valid. Allowed values: comma or semicolon\n"; 165 } 166 if ($Options{outdelim} !~ /^(comma|semicolon|tab)$/i) { 167 die "Error: The value specified, $Options{outdelim}, for option \"--outdelim\" is not valid. Allowed values: comma, tab, or semicolon\n"; 168 } 169 if ($Options{quote} !~ /^(yes|no)$/i) { 170 die "Error: The value specified, $Options{quote}, for option \"-q --quote\" is not valid. Allowed values: yes or no\n"; 171 } 172 $OutDelim = ($Options{outdelim} =~ /^tab$/i ) ? "\t" : (($Options{outdelim} =~ /^semicolon$/i) ? "\;" : "\,"); 173 $OutQuote = ($Options{quote} =~ /^yes$/i) ? 1 : 0; 174 } 175