-
Notifications
You must be signed in to change notification settings - Fork 1
/
transpose.pl
executable file
·1502 lines (1289 loc) · 48.6 KB
/
transpose.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
#Generated using perl_script_template.pl 1.43
#Robert W. Leach
#Center for Computational Research
#Copyright 2008
#These variables (in main) are used by getVersion() and usage()
my $software_version_number = '1.3';
my $created_on_date = '7/11/2011';
##
## Start Main
##
use strict;
use Getopt::Long;
use File::Glob ':glob';
#Declare & initialize variables. Provide default values here.
my($outfile_suffix); #Not defined so input can be overwritten
my @input_files = ();
my @outdirs = ();
my $current_output_file = '';
my $help = 0;
my $version = 0;
my $overwrite = 0;
my $noheader = 0;
#These variables (in main) are used by the following subroutines:
#verbose, error, warning, debug, getCommand, quit, and usage
my $preserve_args = [@ARGV]; #Preserve the agruments for getCommand
my $verbose = 0;
my $quiet = 0;
my $DEBUG = 0;
my $ignore_errors = 0;
my $GetOptHash =
{'i|input-file=s' => sub {push(@input_files, #REQUIRED unless <> is
[sglob($_[1])])}, # supplied
'<>' => sub {push(@input_files, #REQUIRED unless -i is
[sglob($_[0])])}, # supplied
'o|outfile-suffix=s' => \$outfile_suffix, #OPTIONAL [undef]
'outdir=s' => sub {push(@outdirs, #OPTIONAL
[sglob($_[1])])},
'force|overwrite' => \$overwrite, #OPTIONAL [Off]
'ignore' => \$ignore_errors, #OPTIONAL [Off]
'verbose:+' => \$verbose, #OPTIONAL [Off]
'quiet' => \$quiet, #OPTIONAL [Off]
'debug:+' => \$DEBUG, #OPTIONAL [Off]
'help' => \$help, #OPTIONAL [Off]
'version' => \$version, #OPTIONAL [Off]
'noheader|no-header' => \$noheader, #OPTIONAL [Off]
};
#If there are no arguments and no files directed or piped in
if(scalar(@ARGV) == 0 && isStandardInputFromTerminal())
{
usage();
quit(0);
}
#Get the input options & catch any errors in option parsing
unless(GetOptions(%$GetOptHash))
{
#Try to guess which arguments GetOptions is complaining about
my @possibly_bad = grep {!(-e $_)} map {@$_} @input_files;
error('Getopt::Long::GetOptions reported an error while parsing the ',
'command line arguments. The error should be above. Please ',
'correct the offending argument(s) and try again.');
usage(1);
quit(-1);
}
#Print the debug mode (it checks the value of the DEBUG global variable)
debug('Debug mode on.') if($DEBUG > 1);
#If the user has asked for help, call the help subroutine
if($help)
{
help();
quit(0);
}
#If the user has asked for the software version, print it
if($version)
{
print(getVersion($verbose),"\n");
quit(0);
}
#Check validity of verbosity options
if($quiet && ($verbose || $DEBUG))
{
$quiet = 0;
error('You cannot supply the quiet and (verbose or debug) flags ',
'together.');
quit(-2);
}
#If standard input has been redirected in
my $outfile_stub = 'STDIN';
if(!isStandardInputFromTerminal())
{
#If there's only one input file detected, use that input file as a stub for
#the output file name construction
if(scalar(grep {$_ ne '-'} map {@$_} @input_files) == 1)
{
$outfile_stub = (grep {$_ ne '-'} map {@$_} @input_files)[0];
@input_files = ();
#If $outfile_suffix has not been supplied, set it to an empty string
#so that the name of the output file will be what they supplied with -i
if(!defined($outfile_suffix))
{
#Only allow this is the supplied the overwite flag
if(-e $outfile_stub && !$overwrite)
{
error("File exists: [$outfile_stub] Use --outfile-suffix ",
'(-o) or --overwrite to continue.');
quit(-3);
}
$outfile_suffix = '';
}
}
#If standard input has been redirected in and there's more than 1 input
#file detected
elsif(scalar(grep {$_ ne '-'} map {@$_} @input_files) > 1)
{
#Warn the user about the naming of the outfile when using STDIN
if(defined($outfile_suffix))
{warning('Input on STDIN detected along with multiple other input ',
'files and an outfile suffix. Your output file for the ',
'input on standard input will be named [',$outfile_stub,
$outfile_suffix,'].')}
}
#Unless the dash was supplied by the user on the command line, push it on
unless(scalar(grep {$_ eq '-'} map {@$_} @input_files))
{
#If there are other input files present, push it
if(scalar(@input_files))
{push(@{$input_files[-1]},'-')}
#Else create a new input file set with it as the only file member
else
{@input_files = (['-'])}
}
}
#Warn users when they turn on verbose and output is to the terminal
#(implied by no outfile suffix checked above) that verbose messages may be
#uncleanly overwritten
if($verbose && !defined($outfile_suffix) &&isStandardOutputToTerminal())
{warning('You have enabled --verbose, but appear to possibly be ',
'outputting to the terminal. Note that verbose messages can ',
'interfere with formatting of terminal output making it ',
'difficult to read. You may want to either turn verbose off, ',
'redirect output to a file, or supply an outfile suffix (-o).')}
#Make sure there is input
if(scalar(@input_files) == 0)
{
error('No input files detected.');
usage(1);
quit(-4);
}
#If output directories have been provided
if(scalar(@outdirs))
{
#If there are the same number of output directory sets as input file sets
if(scalar(@outdirs) == scalar(@input_files))
{
#Unless all the output directory sets contain 1 specified directory
unless(scalar(grep {scalar(@$_) == 1} @outdirs) ==
scalar(@input_files) ||
#Or each output directory set has the same number of directories
#as the corresponding input files
scalar(grep {scalar(@{$outdirs[$_]}) ==
scalar(@{$input_files[$_]})}
(0..$#{@input_files})) == scalar(@input_files))
{
error('The number of --outdir\'s is invalid. You may either ',
'supply 1, 1 per input file set, 1 per input file, or ',
'where all sets are the same size, 1 per input file is a ',
'single input file set. You supplied [',
join(',',map {scalar(@$_)} @outdirs),
'] output directories and [',
join(',',map {scalar(@$_)} @input_files),
'] input files.');
quit(-6);
}
}
elsif(scalar(@outdirs) == 1 && scalar(@{$outdirs[0]}) != 1)
{
#Unless all the input file sets are the same size as the single set of
#output directories
unless(scalar(grep {scalar(@$_) == scalar(@{$outdirs[0]})}
@input_files) == scalar(@input_files))
{
error('The number of --outdir\'s is invalid. You may either ',
'supply 1, 1 per input file set, 1 per input file, or ',
'where all sets are the same size, 1 per input file is a ',
'single input file set. You supplied [',
join(',',map {scalar(@$_)} @outdirs),
'] output directories and [',
join(',',map {scalar(@$_)} @input_files),
'] input files.');
quit(-7);
}
}
}
#Check to make sure previously generated output files won't be over-written
#Note, this does not account for output redirected on the command line
if(!$overwrite && defined($outfile_suffix))
{
my $existing_outfiles = [];
my $set_num = 0;
foreach my $input_file_set (@input_files)
{
my $file_num = 0;
#For each output file *name* (this will contain the input file name's
#path if it was supplied)
foreach my $output_file (map {($_ eq '-' ? $outfile_stub : $_)
. $outfile_suffix}
@$input_file_set)
{
#If at least 1 output directory was supplied
my $outdir = '';
if(scalar(@outdirs))
{
#Eliminate any path strings from the output file name that came
#from the input file supplied
$output_file =~ s/.*\///;
#If there is the same number of output directory sets as input
#file sets
if(scalar(@outdirs) > 1 &&
scalar(@outdirs) == scalar(@input_files))
{
#If there's 1 directory per input file set
if(scalar(@{$outdirs[$set_num]}) == 1)
{
#Each set of input files has 1 output directory
$output_file = $outdirs[$set_num]->[0]
. ($outdirs[$set_num]->[0] =~ /\/$/ ? '' : '/')
. $output_file;
}
#Else there must be the same number of directories
elsif(scalar(@{$outdirs[$set_num]}) ==
scalar(@{$input_files[$set_num]}))
{
#Each input file has its own output directory
$output_file = $outdirs[$set_num]->[$file_num] .
($outdirs[$set_num]->[$file_num] =~ /\/$/ ? '' : '/')
. $output_file;
}
#Else Error
else
{
error("Cannot determine corresponding directory for ",
"$output_file. Will output to current ",
"directory.");
}
}
#There must be only 1 output directory set, so if it's more
#than 1 directory and has the same number of directories as
#each set of input files
elsif(scalar(@{$outdirs[0]}) > 1 &&
scalar(grep {scalar(@{$outdirs[0]}) == scalar(@$_)}
@input_files) == scalar(@input_files))
{
#Each set of input files has the same number of input
#files (guaranteed in code above), so each one in series
#will output to the corresponding directory specified in
#the single output directory set
$output_file = $outdirs[0]->[$file_num]
. ($outdirs[0]->[$file_num] =~ /\/$/ ? '' : '/')
. $output_file;
}
#There must be only 1 output directory set, so if it's more
#than 1 directory and has the same number of directories as the
#number of input file sets
elsif(scalar(@{$outdirs[0]}) > 1 &&
scalar(@{$outdirs[0]}) == scalar(@input_files))
{
#Each file set will output to the corresponding directory
#in the first set of directories in series. Note, if the
#number of input files in each set and the number of sets
#is the same, the default mechanism is for a single set's
#files to go in the various directories in the single set
#of directories. For now, this cannot be overridden.
$output_file = $outdirs[0]->[$set_num]
. ($outdirs[0]->[$set_num] =~ /\/$/ ? '' : '/')
. $output_file;
}
#It must be a single output directory
else
{
#All input files have the same output directory
$output_file = $outdirs[0]->[0]
. ($outdirs[0]->[0] =~ /\/$/ ? '' : '/')
. $output_file;
}
}
$file_num++;
push(@$existing_outfiles,$output_file) if(-e $output_file);
}
$set_num++;
}
if(scalar(@$existing_outfiles))
{
error("Files exist: [@$existing_outfiles]. Use --overwrite to ",
"continue. E.g.:\n",getCommand(1),' --overwrite');
quit(-5);
}
}
#Create the output directories
if(scalar(@outdirs))
{
foreach my $dir_set (@outdirs)
{
foreach my $dir (@$dir_set)
{
if(-e $dir)
{
warning('The --overwrite flag will not empty or delete ',
'existing output directories. If you wish to delete ',
'existing output directories, you must do it ',
'manually.') if($overwrite)
}
else
{mkdir($dir)}
}
}
}
verbose('Run conditions: ',getCommand(1));
#If output is going to STDOUT instead of output files with different extensions
#or if STDOUT was redirected, output run info once
verbose('[STDOUT] Opened for all output.') if(!defined($outfile_suffix));
#Store info. about the run as a comment at the top of the output file if
#STDOUT has been redirected to a file
if(!isStandardOutputToTerminal() && !$noheader)
{print(getVersion(),"\n",
'#',scalar(localtime($^T)),"\n",
'#',getCommand(1),"\n");}
#For each input file set
my $set_num = 0;
foreach my $input_file_set (@input_files)
{
my $file_num = 0;
#For each output file *name* (this will contain the input file name's
#path if it was supplied)
#For each input file
foreach my $input_file (@$input_file_set)
{
my $file_num = 0;
#If an output file name suffix has been defined
if(defined($outfile_suffix))
{
##
## Open and select the next output file
##
#Set the current output file name
$current_output_file = ($input_file eq '-' ?
$outfile_stub : $input_file)
. $outfile_suffix;
}
#If at least 1 output directory was supplied
my $outdir = '';
if(scalar(@outdirs))
{
#Eliminate any path strings from the output file name that came
#from the input file supplied
$current_output_file =~ s/.*\///;
#If there is the same number of output directory sets as input
#file sets
if(scalar(@outdirs) > 1 &&
scalar(@outdirs) == scalar(@input_files))
{
#If there's 1 directory per input file set
if(scalar(@{$outdirs[$set_num]}) == 1)
{
#Each set of input files has 1 output directory
$current_output_file = $outdirs[$set_num]->[0]
. ($outdirs[$set_num]->[0] =~ /\/$/ ? '' : '/')
. $current_output_file;
}
#Else there must be the same number of directories
elsif(scalar(@{$outdirs[$set_num]}) ==
scalar(@{$input_files[$set_num]}))
{
#Each input file has its own output directory
$current_output_file = $outdirs[$set_num]->[$file_num]
. ($outdirs[$set_num]->[$file_num] =~ /\/$/ ? '' : '/')
. $current_output_file;
}
#Else Error
else
{
error("Cannot determine corresponding directory for ",
"$current_output_file. Will output to current ",
"directory.");
}
}
#There must be only 1 output directory set, so if it's more
#than 1 directory and has the same number of directories as
#each set of input files
elsif(scalar(@{$outdirs[0]}) > 1 &&
scalar(grep {scalar(@{$outdirs[0]}) == scalar(@$_)}
@input_files) == scalar(@input_files))
{
#Each set of input files has the same number of input
#files (guaranteed in code above), so each one in series
#will output to the corresponding directory specified in
#the single output directory set
$current_output_file = $outdirs[0]->[$file_num]
. ($outdirs[0]->[$file_num] =~ /\/$/ ? '' : '/')
. $current_output_file;
}
#There must be only 1 output directory set, so if it's more
#than 1 directory and has the same number of directories as the
#number of input file sets
elsif(scalar(@{$outdirs[0]}) > 1 &&
scalar(@{$outdirs[0]}) == scalar(@input_files))
{
#Each file set will output to the corresponding directory
#in the first set of directories in series. Note, if the
#number of input files in each set and the number of sets
#is the same, the default mechanism is for a single set's
#files to go in the various directories in the single set
#of directories. For now, this cannot be overridden.
$current_output_file = $outdirs[0]->[$set_num]
. ($outdirs[0]->[$set_num] =~ /\/$/ ? '' : '/')
. $current_output_file;
}
#It must be a single output directory
else
{
#All input files have the same output directory
$current_output_file = $outdirs[0]->[0]
. ($outdirs[0]->[0] =~ /\/$/ ? '' : '/')
. $current_output_file;
}
}
if(defined($outfile_suffix) || scalar(@outdirs))
{
#Open the output file
if(!open(OUTPUT,">$current_output_file"))
{
#Report an error and iterate if there was an error
error("Unable to open output file: [$current_output_file].\n",
$!);
next;
}
else
{verbose("[$current_output_file] Opened output file.")}
#Select the output file handle
select(OUTPUT);
#Store info about the run as a comment at the top of the output
print(getVersion(),"\n",
'#',scalar(localtime($^T)),"\n",
'#',getCommand(1),"\n") unless($noheader);
}
#Open the input file
if(!open(INPUT,$input_file))
{
#Report an error and iterate if there was an error
error("Unable to open input file: [$input_file].\n$!");
next;
}
else
{verbose('[',($input_file eq '-' ? $outfile_stub : $input_file),'] ',
'Opened input file.')}
my $line_num = 0;
my $verbose_freq = 10;
my @data = ();
my $rows = 0;
my $cols = 0;
my $in_header = 1;
#For each line in the current input file
while(getLine(*INPUT))
{
$line_num++;
verboseOverMe('[',($input_file eq '-' ?
$outfile_stub : $input_file),
"] Reading line: [$line_num].")
unless($line_num % $verbose_freq);
if($_ !~ /^\s*#/ && /\t/)
{$in_header = 0}
elsif($in_header)
{
#Since this is going to be a row with data and we seem to have
#encountered a commented out header row, let's remove the
#comment character and allow this line to go into the @data
#array
if(/\t/ && /^\s*#\S/)
{
s/#//;
$in_header = 0;
}
#Else, let's print the header without including it in the
#transposition and not add it to the @data array
else
{
print;
next;
}
}
chomp;
my(@tmpdata);
push(@tmpdata,split(/\t/,$_));
#If the number of columns has grown, set a new max
if(!defined($cols) || (scalar(@tmpdata) - 1) > $cols)
{
#Prepend the new rows with tabs
if($cols > 0)
{
#Grab the first row
my $dummies = $data[0];
#Remove the contents, leaving tabs
$dummies =~ s/[^\t]+//g;
#Prepend the tabs to the added rows
for(my $i = $cols + 1;$i < scalar(@tmpdata);$i++)
{$data[$i] = $dummies}
}
#Set a new max
$cols = scalar(@tmpdata) - 1;
}
#For each column of the original file, append a tab-delimed value
for(my $i = 0;$i <= $cols;$i++)
{
#Prepend a tab only if the string is non-empty
$data[$i] .= "\t" if(defined($data[$i]));
#Append the next value to this new row if there is a value
if($i < scalar(@tmpdata))
{$data[$i] .= $tmpdata[$i]}
}
}
close(INPUT);
verbose('[',($input_file eq '-' ? $outfile_stub : $input_file),'] ',
'Input file done. Time taken: [',scalar(markTime()),
' Seconds].');
if($cols == 0)
{warning("Only 1 column was found. Please make sure your input ",
"file [$input_file] is using a tab as the column ",
"delimiter.")}
print(join("\n",@data),"\n");
#If an output file name suffix is set
if(defined($outfile_suffix))
{
#Select standard out
select(STDOUT);
#Close the output file handle
close(OUTPUT);
verbose("[$current_output_file] Output file done.");
}
}
}
verbose("[STDOUT] Output done.") if(!defined($outfile_suffix));
#Report the number of errors, warnings, and debugs on STDERR
if(!$quiet && ($verbose ||
$DEBUG ||
defined($main::error_number) ||
defined($main::warning_number)))
{
print STDERR ("\n",'Done. EXIT STATUS: [',
'ERRORS: ',
($main::error_number ? $main::error_number : 0),' ',
'WARNINGS: ',
($main::warning_number ? $main::warning_number : 0),
($DEBUG ?
' DEBUGS: ' .
($main::debug_number ? $main::debug_number : 0) : ''),' ',
'TIME: ',scalar(markTime(0)),"s]\n");
if($main::error_number || $main::warning_number)
{print STDERR ("Scroll up to inspect errors and warnings.\n")}
}
##
## End Main
##
##
## Subroutines
##
##
## This subroutine prints a description of the script and it's input and output
## files.
##
sub help
{
my $script = $0;
my $lmd = localtime((stat($script))[9]);
$script =~ s/^.*\/([^\/]+)$/$1/;
#$software_version_number - global
#$created_on_date - global
$created_on_date = 'UNKNOWN' if($created_on_date eq 'DATE HERE');
#Print a description of this program
print << "end_print";
$script version $software_version_number
Copyright 2008
Robert W. Leach
Created: $created_on_date
Last Modified: $lmd
Center for Computational Research
701 Ellicott Street
Buffalo, NY 14203
rwleach\@ccr.buffalo.edu
* WHAT IS THIS: This script will take a tab-delimited file and transpose it so
that the columns are now the rows and vice-versa. This is the
same as the transpose function in Excell.
* INPUT FORMAT: Tab-delimited text file.
* OUTPUT FORMAT: Tab-delimited text file.
* ADVANCED FILE I/O FEATURES: Sets of input files, each with different output
directories can be supplied. Supply each file
set with an additional -i (or --input-file) flag.
The files will have to have quotes around them so
that they are all associated with the preceding
-i option. Likewise, output directories
(--outdir) can be supplied multiple times in the
same order so that each input file set can be
output into a different directory. If the number
of files in each set is the same, you can supply
all output directories as a single set instead of
each having a separate --outdir flag. Here are
some examples of what you can do:
-i 'a b c' --outdir '1' -i 'd e f' --outdir '2'
1/
a
b
c
2/
d
e
f
-i 'a b c' -i 'd e f' --outdir '1 2 3'
1/
a
d
2/
b
e
3/
c
f
This is the default behavior if the number of
sets and the number of files per set are all
the same. For example, this is what will
happen:
-i 'a b' -i 'd e' --outdir '1 2'
1/
a
d
2/
b
e
NOT this: 1/a,b 2/d,e To do this, you must
supply the --outdir flag for each set, like
this:
-i 'a b' -i 'd e' --outdir '1' --outdir '2'
-i 'a b c' -i 'd e f' --outdir '1 2'
1/
a
b
c
2/
d
e
f
-i 'a b c' --outdir '1 2 3' -i 'd e f' --outdir '4 5 6'
1/
a
2/
b
3/
c
4/
d
5/
e
6/
f
end_print
return(0);
}
##
## This subroutine prints a usage statement in long or short form depending on
## whether "no descriptions" is true.
##
sub usage
{
my $no_descriptions = $_[0];
my $script = $0;
$script =~ s/^.*\/([^\/]+)$/$1/;
#Grab the first version of each option from the global GetOptHash
my $options = '[' .
join('] [',
grep {$_ ne '-i'} #Remove REQUIRED params
map {my $key=$_; #Save the key
$key=~s/\|.*//; #Remove other versions
$key=~s/(\!|=.|:.)$//; #Remove trailing getopt stuff
$key = (length($key) > 1 ? '--' : '-') . $key;} #Add dashes
grep {$_ ne '<>'} #Remove the no-flag parameters
keys(%$GetOptHash)) .
']';
print << "end_print";
USAGE: $script -i "input file(s)" $options
$script $options < input_file
end_print
if($no_descriptions)
{print("`$script` for expanded usage.\n")}
else
{
print << 'end_print';
-i|--input-file* REQUIRED Space-separated input file(s) (or when used
with standard input present: file name stub
used for naming files). Note, -o can be
used to append to what is supplied here to
form new output file names. The script will
expand BSD glob characters such as '*', '?',
and '[...]' (e.g. -i "*.txt *.text"). See
--help for a description of the input file
format. See --help for advanced usage.
*No flag required.
-o|--outfile-suffix OPTIONAL [nothing] This suffix is added to the input
file names to use as output files.
Redirecting a file into this script will
result in the output file name to be "STDIN"
with your suffix appended. See --help for a
description of the output file format.
--outdir OPTIONAL [input file location] Supply a directory to
put output files. When supplied without -o,
the output file names will be the same as
the input file names. See --help for
advanced usage.
--force|--overwrite OPTIONAL Force overwrite of existing output files.
Only used when the -o option is supplied.
--ignore OPTIONAL Ignore critical errors & continue
processing. (Errors will still be
reported.) See --force to not exit when
existing output files are found.
--verbose OPTIONAL Verbose mode. Cannot be used with the quiet
flag. Verbosity level can be increased by
supplying a number (e.g. --verbose 2) or by
supplying the --verbose flag multiple times.
--quiet OPTIONAL Quiet mode. Suppresses warnings and errors.
Cannot be used with the verbose or debug
flags.
--help OPTIONAL Print an explanation of the script and its
input/output files.
--version OPTIONAL Print software version number. If verbose
mode is on, it also prints the template
version used to standard error.
--debug OPTIONAL Debug mode. Adds debug output to STDERR and
prepends trace information to warning and
error messages. Cannot be used with the
--quiet flag. Debug level can be increased
by supplying a number (e.g. --debug 2) or by
supplying the --debug flag multiple times.
--noheader OPTIONAL Suppress commented header output. Without
this option, the script version, date/time,
and command-line information will be printed
at the top of all output files commented
with '#' characters.
end_print
}
return(0);
}
##
## Subroutine that prints formatted verbose messages. Specifying a 1 as the
## first argument prints the message in overwrite mode (meaning subsequence
## verbose, error, warning, or debug messages will overwrite the message
## printed here. However, specifying a hard return as the first character will
## override the status of the last line printed and keep it. Global variables
## keep track of print length so that previous lines can be cleanly
## overwritten.
##
sub verbose
{
return(0) unless($verbose);
#Read in the first argument and determine whether it's part of the message
#or a value for the overwrite flag
my $overwrite_flag = $_[0];
#If a flag was supplied as the first parameter (indicated by a 0 or 1 and
#more than 1 parameter sent in)
if(scalar(@_) > 1 && ($overwrite_flag eq '0' || $overwrite_flag eq '1'))
{shift(@_)}
else
{$overwrite_flag = 0}
# #Ignore the overwrite flag if STDOUT will be mixed in
# $overwrite_flag = 0 if(isStandardOutputToTerminal());
#Read in the message
my $verbose_message = join('',grep {defined($_)} @_);
$overwrite_flag = 1 if(!$overwrite_flag && $verbose_message =~ /\r/);
#Initialize globals if not done already
$main::last_verbose_size = 0 if(!defined($main::last_verbose_size));
$main::last_verbose_state = 0 if(!defined($main::last_verbose_state));
$main::verbose_warning = 0 if(!defined($main::verbose_warning));
#Determine the message length
my($verbose_length);
if($overwrite_flag)
{
$verbose_message =~ s/\r$//;
if(!$main::verbose_warning && $verbose_message =~ /\n|\t/)
{
warning('Hard returns and tabs cause overwrite mode to not work ',
'properly.');
$main::verbose_warning = 1;
}
}
else
{chomp($verbose_message)}
#If this message is not going to be over-written (i.e. we will be printing
#a \n after this verbose message), we can reset verbose_length to 0 which
#will cause $main::last_verbose_size to be 0 the next time this is called
if(!$overwrite_flag)
{$verbose_length = 0}
#If there were \r's in the verbose message submitted (after the last \n)
#Calculate the verbose length as the largest \r-split string
elsif($verbose_message =~ /\r[^\n]*$/)
{
my $tmp_message = $verbose_message;
$tmp_message =~ s/.*\n//;
($verbose_length) = sort {length($b) <=> length($a)}
split(/\r/,$tmp_message);
}
#Otherwise, the verbose_length is the size of the string after the last \n
elsif($verbose_message =~ /([^\n]*)$/)
{$verbose_length = length($1)}
#If the buffer is not being flushed, the verbose output doesn't start with
#a \n, and output is to the terminal, make sure we don't over-write any
#STDOUT output
#NOTE: This will not clean up verbose output over which STDOUT was written.
#It will only ensure verbose output does not over-write STDOUT output
#NOTE: This will also break up STDOUT output that would otherwise be on one
#line, but it's better than over-writing STDOUT output. If STDOUT is going
#to the terminal, it's best to turn verbose off.
if(!$| && $verbose_message !~ /^\n/ && isStandardOutputToTerminal())
{
#The number of characters since the last flush (i.e. since the last \n)
#is the current cursor position minus the cursor position after the
#last flush (thwarted if user prints \r's in STDOUT)
#NOTE:
# tell(STDOUT) = current cursor position
# sysseek(STDOUT,0,1) = cursor position after last flush (or undef)
my $num_chars = sysseek(STDOUT,0,1);
if(defined($num_chars))
{$num_chars = tell(STDOUT) - $num_chars}
else
{$num_chars = 0}
#If there have been characters printed since the last \n, prepend a \n
#to the verbose message so that we do not over-write the user's STDOUT
#output
if($num_chars > 0)
{$verbose_message = "\n$verbose_message"}
}
#Overwrite the previous verbose message by appending spaces just before the
#first hard return in the verbose message IF THE VERBOSE MESSAGE DOESN'T
#BEGIN WITH A HARD RETURN. However note that the length stored as the
#last_verbose_size is the length of the last line printed in this message.
if($verbose_message =~ /^([^\n]*)/ && $main::last_verbose_state &&
$verbose_message !~ /^\n/)
{
my $append = ' ' x ($main::last_verbose_size - length($1));
unless($verbose_message =~ s/\n/$append\n/)
{$verbose_message .= $append}
}
#If you don't want to overwrite the last verbose message in a series of
#overwritten verbose messages, you can begin your verbose message with a
#hard return. This tells verbose() to not overwrite the last line that was
#printed in overwrite mode.
#Print the message to standard error
print STDERR ($verbose_message,
($overwrite_flag ? "\r" : "\n"));
#Record the state
$main::last_verbose_size = $verbose_length;