-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrep.pl
executable file
·1329 lines (1136 loc) · 44.1 KB
/
grep.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
#Grep (for perl patterns)
#Generated using perl_script_template.pl 1.8
#Robert W. Leach
#10/8/2004
#Los Alamos National Laboratory
#Copyright 2004
#To-do:
#1. Accumulate matched lines in output files that have an extension that uses
# the matched string. That way if I did grep.pl -p 'chr\d+'
# --split-accumulate, I'd end up with files named 'whatever.chr1,
# whatever.chr2, ...'.
#2. Tried matching a fastq file using -c '^\@[^\n]*\n[^\n]*\n\+' and it did not
# split the records. This is a bug. Fix it.
markTime();
my $software_version_number = '1.14';
##
## Start Main
##
use strict;
use Getopt::Long;
#Declare variables
my($verbose,
$quiet,
$split,
$help,
@input_files,
$version,
$DEBUG,
$outfile_suffix,
$current_output_file,
$pattern,
$mark_matched,
$match_marker,
$no_match_marker,
$count_pattern,
$fasta_grep,
$blast_query_grep,
$blast_subject_grep,
$blast_aln_grep,
$everything_else,
$combine_with_and,
$enforce_order,
$show_files,
$case_insensitive);
my $split_group_size = 0;
my $pattern_files = [];
my $file_patterns = [];
my $before = 0;
my $after = 0;
#If there are no arguments and no files directed or piped in
if(scalar(@ARGV) == 0 && (-t STDIN || eof(STDIN)))
{
usage();
exit(0);
}
#Get the input options
GetOptions('p|perl-pattern=s' => \$pattern, #REQUIRED
'b|lines-before=s' => \$before, #OPTIONAL [0]
'a|lines-after=s' => \$after, #OPTIONAL [0]
'i|case-insensitive!' => \$case_insensitive, #OPTIONAL [Off]
'm|mark-matched-lines!' => \$mark_matched, #OPTIONAL [Off]
'k|match-marker=s' => \$match_marker, #OPTIONAL [*]
'u|no-match-marker=s' => \$no_match_marker, #OPTIONAL [undef]
's|show-file-names!' => \$show_files, #OPTIONAL [Off]
'c|count-pattern|r|record-pattern=s' => #OPTIONAL [undef]
\$count_pattern,
'f|pattern-files=s' => sub {push(@$pattern_files, #OPTIONAL
sglob($_[1]))}, #[undef]
'n|combine-with-AND!' => \$combine_with_and, #OPTIONAL [Off]
'e|enforce-order!' => \$enforce_order, #OPTIONAL [Off]
'v|everything-else!' => \$everything_else, #OPTIONAL [Off]
't|fasta-grep!' => \$fasta_grep, #OPTIONAL [Off]
'bq|blast-query-grep!' => \$blast_query_grep, #OPTIONAL [Off]
'bs|blast-subject-grep!' => \$blast_subject_grep, #OPTIONAL [Off]
'ba|blast-alnmt-grep!' => \$blast_aln_grep, #OPTIONAL [Off]
'D|debug!' => \$DEBUG, #OPTIONAL [Off]
'version!' => \$version, #OPTIONAL [Off]
'verbose!' => \$verbose, #OPTIONAL [Off]
'q|quiet!' => \$quiet, #OPTIONAL [Off]
'h|help!' => \$help, #OPTIONAL [Off]
'o|outfile-suffix=s' => \$outfile_suffix, #OPTIONAL [undef]
'split!' => \$split, #OPTIONAL [Off]
'split-group-size=s' => \$split_group_size, #OPTIONAL [0]
'<>' => sub {push(@input_files, #REQUIRED STDIN
sglob($_[0]))}, #acceptable
);
#Print the argument settings if in debug mode
if($DEBUG)
{debug("Debug mode on.")}
#If the user has asked for help, call the help subroutine
if($help)
{
help();
exit(0);
}
#If the user has asked for the software version, print it
if($version)
{
print($software_version_number,"\n");
exit(0);
}
#Check validity of verbosity options
if($verbose && $quiet)
{
error("You cannot supply verbose and quiet flags at the same time.");
exit(2);
}
#Put standard input into the input_files array if there is standard input
if(!(-t STDIN || eof(STDIN)))
{
#Clear out the outfile suffix if there is input on STDIN
undef($outfile_suffix) if(scalar(@input_files) == 0);
push(@input_files,'-');
}
#Make sure there is input
if(scalar(@input_files) == 0)
{
error("No input files detected.") unless($quiet);
usage();
exit(1);
}
if($split_group_size !~ /^\d+$/)
{
warning("Invalid --split-group-size: [$split_group_size]. It must ",
"be an integer greater than 0. Flattening.");
$split_group_size = int($split_group_size);
}
if($split_group_size < 0)
{
warning("Invalid --split-group-size: [$split_group_size]. It must ",
"be an integer greater than 0. Changing to 1.");
$split_group_size = 1;
}
if($split && !$split_group_size)
{$split_group_size = 1}
elsif($split_group_size && !$split)
{$split = 1}
#If no patterns were supplied and we're not splitting (every line)
if(!defined($pattern) &&
!defined($split) &&
(scalar(@$pattern_files) == 0 ||
scalar(grep {!(-r $_)} @$pattern_files) ||
scalar(grep {-B $_} @$pattern_files)))
{
error(((scalar(@$pattern_files) &&
scalar(grep {!(-r $_)} @$pattern_files)) ?
"Pattern files: [" . join(',',grep {!(-r $_)} @$pattern_files) .
"] are not readable." :
((scalar(@$pattern_files) &&
scalar(grep {-B $_} @$pattern_files)) ?
"Pattern files: [" . join(',',grep {-B $_} @$pattern_files) .
"] are binary files." :
"No pattern submitted."))) unless($quiet);
usage();
exit(1);
}
#If we've been asked to split the records and no pattern was given, create a
#pattern that will match (and thus split) every line
if(!defined($pattern) &&
defined($split) &&
scalar(@$pattern_files) == 0)
{$pattern = ''}
$pattern = ($case_insensitive ? '(?i)' : '') . $pattern
unless(scalar(@$pattern_files));
if((defined($match_marker) && $match_marker ne '') ||
(defined($no_match_marker) && $no_match_marker ne ''))
{$mark_matched = 1}
if($mark_matched &&
(!defined($match_marker) || $match_marker eq '') &&
(!defined($match_marker) &&
(!defined($no_match_marker) || $no_match_marker eq '')))
{$match_marker = '*'}
if($fasta_grep && defined($count_pattern) && $count_pattern ne '')
{
error("You can't use the -t and -c options at the same time. These are ",
"conflicting flags.") unless($quiet);
usage();
exit(1);
}
if($fasta_grep)
{$count_pattern = '^\s*>'}
my $c_ok = 0;
if($blast_query_grep && $fasta_grep)
{
error("You can't use the --bq and -t options at the same time. These ",
"are conflicting flags.") unless($quiet);
usage();
exit(1);
}
if($blast_query_grep && defined($count_pattern) && $count_pattern ne '')
{
error("You can't use the --bq and -c options at the same time. These ",
"are conflicting flags.") unless($quiet);
usage();
exit(1);
}
if($blast_query_grep)
{
$count_pattern = '^(?:\S*BLAST|Query=|\A\s+Database:)';
$c_ok = 1;
}
if($blast_subject_grep && $fasta_grep)
{
error("You can't use the --bs and -t options at the same time. These ",
"are conflicting flags.") unless($quiet);
usage();
exit(1);
}
if($blast_subject_grep && !$c_ok && defined($count_pattern) &&
$count_pattern ne '')
{
error("You can't use the --bs and -c options at the same time. These ",
"are conflicting flags.") unless($quiet);
usage();
exit(1);
}
if($blast_subject_grep)
{
$count_pattern = '^(?:\S*BLAST|Query=|\A\s+Database:|>)';
$c_ok = 1;
}
if($blast_aln_grep && $fasta_grep)
{
error("You can't use the --ba and -t options at the same time. These ",
"are conflicting flags.") unless($quiet);
usage();
exit(1);
}
if($blast_aln_grep && !$c_ok && defined($count_pattern) &&
$count_pattern ne '')
{
error("You can't use the --ba and -c options at the same time. These ",
"are conflicting flags.") unless($quiet);
usage();
exit(1);
}
if($blast_aln_grep)
{$count_pattern = '(?:\A\S*BLAST|\AQuery=|\A\s+Database:|\A>|Score =)'}
if(scalar(@$pattern_files) == 0 &&
($enforce_order || $combine_with_and) && !$quiet)
{warning("The -n and -e flags only work when a pattern file (-f) is used.")}
my $file_by_file = (scalar(@$pattern_files) == scalar(@input_files));
if($verbose && scalar(@$pattern_files))
{print STDERR ("PATTERN FILE MODE: ",
($file_by_file ? 'FILE BY FILE' :
'ALL PATTERN FILES COMBINED COMPARED TO EACH INPUT FILE'),
"\n")}
#Create a pattern from the pattern file
if(scalar(@$pattern_files))
{
if(defined($pattern) && !$quiet)
{warning("Ignoring supplied pattern: [$pattern] because you ",
"supplied pattern files: [",join(',',@$pattern_files),"].\n")}
#Ignore any pattern provided
$pattern = ($case_insensitive ? '(?i)' : '');
#For each input file of the same type
foreach my $pattern_file (@$pattern_files)
{
if($verbose)
{print STDERR ("Opening input file: [$pattern_file].\n")}
#Open the input file
unless(open(INPUT,$pattern_file))
{
#Report an error and iterate if there was an error
error("Unable to open input file: [$pattern_file]\n$!")
unless($quiet);
next;
}
#Keep track of input file's line number
my $line_num = 1;
$pattern = ($case_insensitive ? '(?i)' : '')
if(scalar(@$pattern_files) == scalar(@input_files));
$pattern .= '(?=.*' if(!$enforce_order && $combine_with_and);
$pattern .= join(($enforce_order ? '.*?' :
($combine_with_and ? ')(?=.*?' : '|')),
grep {$_ !~ /^(?:\s*\#.*)$/ && /./}
map {chomp;$_}
getLine(\*INPUT,$quiet));
$pattern .= ')' if(!$enforce_order && $combine_with_and);
if($verbose)
{print STDERR ("\n[$pattern_file] Done. Closing File. Time ",
"taken: [",
scalar(markTime()),
" Seconds]\n")}
close(INPUT);
if($file_by_file)
{push(@$file_patterns,$pattern)}
}
}
#For each input file of the same type
foreach my $input_file (@input_files)
{
if($file_by_file)
{$pattern = shift(@$file_patterns)}
#Open and select the next output file if an output file name suffix has
#been defined
if(defined($outfile_suffix) && !$split)
{
#Set the current output file name
$current_output_file = $input_file . $outfile_suffix;
if($verbose)
{print STDERR ("Opening output file: [$current_output_file].\n")}
#Open the output file
unless(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$!")
unless($quiet);
next;
}
#Select the output file handle
select(OUTPUT);
}
if($verbose)
{print STDERR ("Opening input file: [",
($input_file eq '-' ? 'STDIN' : $input_file),
"].\n")}
if($verbose && $file_by_file)
{print STDERR ("Using pattern file: [",shift(@$pattern_files),"].\n")}
if($pattern eq '' && !$split && !$quiet)
{warning("Your pattern: [$pattern] is empty. Every line will match.")}
if($verbose)
{
if(defined($count_pattern))
{print STDERR ("RECORD PATTERN (-c): [$count_pattern]\n")}
print STDERR ("PATTERN: ",
($everything_else ?
'EVERYTHING EXCEPT (-v activated): ' : ''),
"[$pattern]\n");
}
#Open the input file
unless(open(INPUT,$input_file))
{
#Report an error and iterate if there was an error
error("Unable to open input file: [$input_file]\n$!") unless($quiet);
next;
}
#Keep track of input file's line number
my $line_num = 1;
#This is a decrementing counter that determines when to stop printing after
#a matched line. It is set every time there's a match to $after.
my $still = 0;
#This is the buffer needed for the lines printed before the matched line
#When a match is encountered, the buffer is printed and emptied
my @lines = ();
my $blast_header = '';
my $blast_query_header = '';
my $blast_subject_header = '';
my $blast_footer = '';
my $num_matches = 0;
my $num_nonmatches = 0;
#For each line in the current input file
while(defined($count_pattern) ?
getCountedLine($count_pattern,\*INPUT) : getLine(\*INPUT))
{
#Record & skip 'header' records in blast mode unless flags provided
if($blast_query_grep || $blast_subject_grep || $blast_aln_grep)
{
debug("Recording blast headers");
if(/^\S*BLAST/)
{debug("Got top blast header");$blast_header = $_}
if($blast_subject_grep && /^>/ && $blast_aln_grep)
{debug("Got subject blast header");$blast_subject_header = $_}
if($blast_query_grep && /^Query=/ &&
($blast_subject_grep || $blast_aln_grep))
{debug("Got query blast header");$blast_query_header = $_}
if(/^\s+Database:/)
{debug("Got blast footer");$blast_footer = $_}
next if(/BLAST/);
next if(/^Query=/ && ($blast_aln_grep || $blast_subject_grep));
next if(/^>/ && $blast_aln_grep);
if($blast_footer)
{
print($blast_footer);
$blast_footer = '';
next;
}
# if($blast_query_grep && $blast_subject_grep && $blast_aln_grep)
# {$_ = $blast_query_header . $blast_subject_header . $_}
# elsif($blast_subject_grep && $blast_aln_grep)
# {$_ = $blast_subject_header . $_}
# elsif($blast_query_grep &&
# ($blast_aln_grep || $blast_subject_grep))
# {$_ = $blast_query_header . $_}
}
if($verbose && $outfile_suffix)
{print STDERR ("Reading ",
(defined($count_pattern) ? 'record' : 'line'),
" $line_num\r")}
if(scalar(@lines) == 0 || $still == 0)
{
#Push onto the buffer
push(@lines,$_);
}
debug("Pattern matching");
#If we've matched the pattern
if(($everything_else ? ($_ !~ /$pattern/s) : (/$pattern/s)))
{
$num_matches++;
debug("Matched");
#Open and select the next output file if split is true (and we've
#already filled the previous file to size $split_group_size)
#This overrides the previous output file selected by outfile suffix
#alone
if($split && ($num_matches - 1) % $split_group_size == 0)
{
#Close the last output file if one has been opened
if($num_matches > 1)
{
#Select standard out
select(STDOUT);
#Close the output file handle
close(OUTPUT);
}
#Set the current output file name
$current_output_file = $input_file . ".$num_matches";
$current_output_file .= $outfile_suffix
if(defined($outfile_suffix));
if($verbose)
{print STDERR ("Opening output file: [$current_output_file]",
".\n")}
#Open the output file
unless(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$!")
unless($quiet);
next;
}
#Select the output file handle
select(OUTPUT);
}
#Take care of blast parsing: print header stuff
if($blast_query_grep || $blast_subject_grep || $blast_aln_grep)
{
debug("Checking blast headers");
if($blast_header &&
$blast_query_header &&
$blast_subject_header)
{
debug("Applying all blast headers");
$lines[0] = $blast_header . $blast_query_header .
$blast_subject_header . $lines[0];
}
elsif($blast_query_header &&
$blast_subject_header)
{
debug("Applying query and subject blast headers");
$lines[0] = $blast_query_header . $blast_subject_header .
$lines[0];
}
elsif($blast_subject_header)
{
debug("Applying subject blast headers");
$lines[0] = $blast_subject_header . $lines[0];
}
$blast_header = '';
$blast_query_header = '';
$blast_subject_header = '';
}
if(scalar(@lines) > 1 && $still == 0)
{
my(@new_unmatched_lines);
foreach my $unmatched_line (@lines[0..(scalar(@lines)-2)])
{
push(@new_unmatched_lines,
split(/(?<=\n)/,$unmatched_line));
pop(@new_unmatched_lines)
if($new_unmatched_lines[-1] !~ /\S/);
}
print(($mark_matched ?
(!defined($no_match_marker) ?
' ' x length($match_marker) :
$no_match_marker) . ' ' :
'') .
($show_files ?
($input_file eq '-' ?
'STDIN' :
$input_file) . ':' :
''),
join('' .
($mark_matched ?
(!defined($no_match_marker) ?
' ' x length($match_marker) :
$no_match_marker) . ' ' : '') .
($show_files ?
($input_file eq '-' ?
'STDIN' :
$input_file) . ':' :
''),
@new_unmatched_lines));
}
my(@new_matched_lines);
push(@new_matched_lines,split(/(?<=\n)/,$lines[-1]));
#I think this was removing blank lines at the end of records when using -r
#I don't know why it was originally coded this way, so I'm just commenting it
#out in case I find that it starts inserting lines...
# pop(@new_matched_lines)
# if(scalar(@new_matched_lines) &&
# (!defined($new_matched_lines[-1]) ||
# $new_matched_lines[-1] !~ /\S/));
pop(@new_matched_lines)
if(scalar(@new_matched_lines) &&
!defined($new_matched_lines[-1]));
foreach my $new_matched_line (@new_matched_lines)
{
print(($mark_matched ?
(($everything_else ?
($new_matched_line !~ /$pattern/) :
($new_matched_line =~ /$pattern/)) ?
(!defined($match_marker) || $match_marker eq '' ?
' ' x length($no_match_marker) :
$match_marker) :
(!defined($no_match_marker) || $no_match_marker eq '' ?
' ' x length($match_marker) :
$no_match_marker)) . ' ' :
''),
($show_files ?
($input_file eq '-' ?
'STDIN' :
$input_file) . ':' :
''),
$new_matched_line);
}
@lines = ();
$still = $after;
}
#Else if we're not counting specific lines and we're still printing
#lines after a match OR we are counting specific lines and we're still
#printing lines after a match (including those that aren't counted
#after a counted line)
elsif($still > 0)
{
$num_nonmatches++;
debug("Still matching");
#Decrement still
$still--;
#Only print the line if still is greater than 0
if($still >= 0)
{
print(($mark_matched ?
(!defined($no_match_marker) ?
' ' x length($match_marker) :
$no_match_marker) . ' ' : '') .
($show_files ?
($input_file eq '-' ?
'STDIN' :
$input_file) . ':' :
''),
$_);
@lines = ();
}
}
else
{$num_nonmatches++;debug("Didn't match")}
#Trim the buffer if it's gotten too large
if(scalar(@lines) == $before + 1)
{shift(@lines)}
$line_num++;
}
if($verbose)
{print STDERR ("\n$input_file Results: $num_matches MATCHES ",
"$num_nonmatches NON-MATCHES\n",
"\n[",
($input_file eq '-' ? 'STDIN' : $input_file),
'] Done. Closing File. Time taken: [',
scalar(markTime()),
" Seconds]\n")}
close(INPUT);
#If an output file name suffix is set
if((defined($outfile_suffix) && !$split) || ($split && $num_matches > 0))
{
#Select standard out
select(STDOUT);
#Close the output file handle
close(OUTPUT);
}
}
#Report the stop time if verbose mode is on
if($verbose)
{print STDERR ("Running Time: ",scalar(markTime(0))," Seconds\n")}
##
## End Main
##
##
## Subroutines
##
sub help
{
my $script = $0;
$script =~ s/^.*\/([^\/]+)$/$1/;
#Print a description of this program
print << "end_print";
$script
Copyright 2004
Robert W. Leach
2/4/2005
Bioscience Division
Bioinformatics and Computational Biology Group, B5
Biodefense Informatics Team
MS M888
Los Alamos National Laboratory
Los Alamos, NM 87545
robleach\@lanl.gov
* WHAT IS THIS: This is a perl version of grep. It doesn\'t claim to be as
fast as the unix version of grep, but it has a few nice
features not offered in the unix version. It was made in order
to be able to see lines before and after the line that was
matched.
* INPUT FORMAT: It\'s pretty much the same as the unix version of grep, except
the pattern needs a flag (-p). Also, since it\'s a perl
pattern, characters have different meanings. be careful to
escape your \'\$\' and \'\@\' signs!
* OUTPUT FORMAT: Again it\'s pretty much the same as the unix version of grep,
except there\'s an option to flag the line with the matching
pattern with any desired string. You must also specify if you
want matched lines prepended with the file name they were
found in.
end_print
return(0);
}
sub usage
{
my $script = $0;
$script =~ s/^.*\/([^\/]+)$/$1/;
print << "end_print";
USAGE1: $script input_files -p perl_regular_expression [-b number] [-a number] [-m] [-k match_marker_string] [-u no_match_marker_string] [-s] [-f "*.pattern_files"] [-n] [-e] [-c perl_regular_expression] [-v] [-t] [-o .suffix] [--split] [--verbose] [-q] [-h] [--version] [-D]
USAGE2: $script -p perl_regular_expression [-b number] [-a number] [-m] [-k match_marker_string] [-u no_match_marker_string] [-s] [-f "*.pattern_files"] [-n] [-e] [-c perl_regular_expression] [-v] [-t] [--split] [--verbose] [-h] [--version] [-q] [-D] < input_file
REQUIRED Input file(s). Standard input via
redirection is acceptable. There is no flag
required for input files.
-p|--perl-pattern REQUIRED Perl regular expression to look for in the
input file(s). Optional if --split or -f is
supplied.
-b|--lines-before OPTIONAL [0] Number of lines to display above a
matched line.
-a|--lines-after OPTIONAL [0] Number of lines to display below a
matched line.
-m|--mark-matched- OPTIONAL [Off] Mark matched lines (to make them
lines easier to identify).
-k|--match-marker OPTIONAL [*] This is the marker used by the -m flag
(described above). Supplying a value here
turns on the -m flag automatically.
-u|--no-match-marker OPTIONAL [] When the -m flag is on, this is the
string used to mark unmatched lines.
Supplying a value here turns on the -m flag
automatically.
-s|--show-file-names OPTIONAL [Off] Show file names where the pattern was
matched.
-c|-r| OPTIONAL [] This is basically a record delimiter
--count-pattern| pattern. This allows you to output an
--record-pattern entire record if the record matches the
pattern supplied in -p (multiple line
patterns allowed). Records are split at the
beginning of the line that matches this
pattern.
-f|--pattern-files REQUIRED This parameter is OPTIONAL if -p or --split
is provided. If both -p and -f are
provided, the -p flag is ignored. Each line
of the pattern file is concatenated into one
pattern. Each line is joined in the final
pattern with an "OR" matching behavior by
default. See -n to use "AND".
-n|--combine-with- OPTIONAL [Off] This parameter only works when -f is
AND used and is otherwise ignored. This
parameter changes the joining behavior of
the patterns from a file. A line in the
"grepped" files matches only if every
pattern from the pattern is found on the
line (i.e. not if only one of the patterns
matches as is the default behavior).
-e|--enforce-order OPTIONAL [Off] This parameter only works when the -f
flag is used and is otherwise ignored. This
parameter changes the joining behavior of
the patterns from a file. A line in the
"grepped" files matches only if every
pattern from the pattern is found on the
line in the order they occur in the pattern
file. This flag has the same effect as the
-n flag, but with the added order
restriction.
-v|--everything-else OPTIONAL [Off] This parameter reverses the matching
behavior of the perl pattern. Lines that
match and lines that don\'t match are
reversed. In other words, the behavior is
the same as the -v option of the unix
version of grep.
-t|--fasta-grep OPTIONAL [Off] This flag causes fasta records to be
counted as one line (but it doesn\'t allow
you to pattern match past hard returns -
it\'s mainly for matching deflines). This
flag cannot be used with the -c, -bq, -bs,
or -ba options.
--bq|--blast-query- OPTIONAL [Off] This flag causes blast records (per
grep query sequence) to be counted as one line
(but it doesn\'t allow you to pattern match
past hard returns). This flag cannot be
used with the -c or -t options. If supplied
with the --bs or --ba options, some records
will contain only the query header
information. The query header information
is not pattern matched (by the -p pattern)
if either or both of the other blast options
is supplied.
--bs|--blast- OPTIONAL [Off] This flag causes blast records (per
subject-grep subject sequence) to be counted as one line
(but it doesn't allow you to pattern match
past hard returns). This flag cannot be
used with the -c or -t options. If supplied
with the --ba option, some records will
contain only the subject header information.
The query and subject header information is
not pattern matched (by the -p pattern) if
the --ba option is supplied.
--ba|--blast-alnmt- OPTIONAL [Off] This flag causes blast records (per
grep alignment) to be counted as one line (but it
doesn\'t allow you to pattern match past
hard returns). This flag cannot be used
with the -c or -t options. If this flag is
used without the --bq or --bs options, no
records will contain query or subject header
information. If this option is supplied,
the query and subject header information is
not pattern matched (by the -p pattern).
-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
inactivates this option and sends output to
standard out.
--split OPTIONAL [Off] This will cause each matched line/
record to go into a different output file
with a numbered suffix indicating the
number of the match (e.g. the first match
will go into input_file.1). If supplied
with -o, the number will appear before the
supplied suffix (e.g. the first match will
go into input_file.1supplied_suffix). If no
pattern(s) is supplied, every record (see
-c) will match.
--split-group-size OPTIONAL [0] Instead of splitting each line/record
into a new file (see --split), this option
will allow you to specify how many matched
lines/records should go into each output
file before stating a new one. 0 indicates
that no split will be performed. When
supplied and non-zero, --split is turned on
automatically. If not supplied and --split
is supplied, this value is automatically
changed to 1.
--verbose OPTIONAL [Off] Verbose mode.
-q|--quiet OPTIONAL [Off] Quiet mode. Turns off warnings and
errors. Cannot be used with the verbose
flag.
-h|--help OPTIONAL [Off] Help. Use this option to see sample
input files and an explanation.
--version OPTIONAL [Off] Print software version and quit.
-D|--debug OPTIONAL [Off] Debug mode.
end_print
return(0);
}
##
## Subroutine that prints errors with a leading program identifier
##
sub error
{
#Gather and concatenate the error message and split on hard returns
my @error_message = split("\n",join('',@_));
pop(@error_message) if($error_message[-1] !~ /\S/);
my $script = $0;
$script =~ s/^.*\/([^\/]+)$/$1/;
#Assign the values from the calling subroutine
#but if called from main, assign the values from main
my($junk1,$junk2,$line_num,$calling_sub);
(($junk1,$junk2,$line_num,$calling_sub)) = caller(1) ||
(($junk1,$junk2,$line_num) = caller());
#Edit the calling subroutine string
$calling_sub =~ s/^.*?::(.+)$/$1:/ if(defined($calling_sub));
#Put location information at the beginning of each line of the message
foreach my $line (@error_message)
{
print STDERR ("ERROR:$script:",
($line_num ?
"LINE$line_num:" : "REFERENCED_SUBROUTINE:"),
(defined($calling_sub) ? $calling_sub : ''),
' ')
if($line =~ /\S/);
print STDERR ($line,"\n");
}
#Return success
return(0);
}
##
## Subroutine that prints warnings with a leader string
##
sub warning
{
#Gather and concatenate the warning message and split on hard returns
my @warning_message = split("\n",join('',@_));
pop(@warning_message) if($warning_message[-1] !~ /\S/);
#Put leader string at the beginning of each line of the message
foreach my $line (@warning_message)
{print STDERR (($line =~ /\S/ ? 'WARNING: ' : ''),$line,"\n")}
#Return success
return(0);
}
##
## Subroutine that gets a line of input and accounts for carriage returns that
## many different platforms use instead of hard returns. Note, it uses a
## global array reference variable ($infile_line_buffer).
##
sub getLineOLDOLD
{
my $file_handle = $_[0];
#Set a global array variable if not already set
$main::infile_line_buffer = {} if(!defined($main::infile_line_buffer));
if(!exists($main::infile_line_buffer->{$file_handle}))
{$main::infile_line_buffer->{$file_handle} = []}
#If this sub was called in array context
if(wantarray)
{
#Check to see if this file handle has anything remaining in its buffer
#and if so return it with the rest
if(scalar(@{$main::infile_line_buffer->{$file_handle}}) > 0)
{return(@{$main::infile_line_buffer->{$file_handle}},
map {split("\r",$_)} <$file_handle>)}
#Otherwise return everything else
return(map {split("\r",$_)} <$file_handle>);
}
#If the file handle's buffer is empty, put more on
if(scalar(@{$main::infile_line_buffer->{$file_handle}}) == 0)
{@{$main::infile_line_buffer->{$file_handle}} = split("\r",
<$file_handle>)}
#Shift off and return the first thing in the buffer for this file handle
return($_ = shift(@{$main::infile_line_buffer->{$file_handle}}));
}
##
## Subroutine that gets a line of input and accounts for carriage returns that
## many different platforms use instead of hard returns. Note, it uses a
## global array reference variable ($infile_line_buffer) to keep track of
## buffered lines from multiple file handles.
##
sub getLineOLD