forked from nylander/grepfasta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grepfasta.pl
executable file
·332 lines (244 loc) · 9.08 KB
/
grepfasta.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
#!/usr/bin/perl
#===============================================================================
#
# FILE: grepfasta.pl
#
# USAGE: grepfasta.pl [--inverse|-v] [-p='search pattern'|-f=<file with search patterns>] fasta_file
#
# DESCRIPTION: Print sequence entry from a fasta file to STDOUT,
# IF search string match in the definition (header) line.
# If the '--inverse' option is used, the program prints
# all sequences except the one for which the match is made.
# Input search pattern as a search string (--search-pattern),
# or if multiple searches are to be made, in a file (--search-file),
# with one search phrase per line.
# Match must be exact.
#
# OPTIONS: See grepfasta.pl -man
# REQUIREMENTS: Pod::Usage
# BUGS: ---
# NOTES: ---
# AUTHOR: Johan A. A. Nylander (JN), <jnylander @ users.sourceforge.net>
# COMPANY: SU
# VERSION: 1.0
# CREATED: 03/11/2010 10:34:48 AM CET
# REVISION: 12/18/2012 05:04:56 PM
#===============================================================================
use warnings;
use strict;
use Getopt::Long;
use Pod::Usage;
use Data::Dumper;
## Global parameters
my $man = 0;
my $help = 0;
my $inverse = 0; # No inverse print by default
my $search_string = q{}; # Search string
my $search_pattern = q{}; # Search pattern, with or without single quotes
my $search_file = q{}; # Search file
my $max = q{}; # Max nr of matches to print/delete
my $DEBUG = 0; # No debug
## Handle arguments
if (@ARGV < 1) {
die "No arguments. Try:\n\n $0 -man\n\n";
}
else {
GetOptions('help|?' => sub { pod2usage(1) },
'man' => sub { pod2usage(-exitstatus => 0, -verbose => 2) },
'v|inverse!' => \$inverse,
'p|search-pattern=s' => \$search_pattern,
'f|search-file=s' => \$search_file,
'debug' => \$DEBUG,
'n|max=i' => \$max,
);#or pod2usage(2)
}
#=== FUNCTION ================================================================
# NAME: trim
# VERSION: 03/11/2010 10:39:38 AM CET
# DESCRIPTION: trim search string
# PARAMETERS: string
# RETURNS: string
# TODO: ???
#===============================================================================
sub trim {
my ($search_pattern) = @_;
print STDERR "In function trim(): in >>$search_pattern<<\n" if $DEBUG;
chomp($search_pattern);
if ( ($search_pattern =~ m/^'/) and ($search_pattern =~ m/'$/) ) {
$search_pattern =~ s/^'//;
$search_pattern =~ s/'$//;
}
print STDERR "In function trim(): return >>$search_pattern<<\n" if $DEBUG;
return($search_pattern);
} # end of trim
#=== FUNCTION ================================================================
# NAME: get_if_match_fasta
# VERSION: 12/18/2012 05:04:42 PM
# DESCRIPTION: print from fasta file if search string match
# PARAMETERS: $search_string, $INFILE_file_name
# RETURNS: prints to STDOUT
# TODO: Debug, match correct?, return?
#===============================================================================
sub get_if_match_fasta {
my ($search, $INFILE_file_name) = @_;
my @search_strings = ();
if ( -e $search ) { # is $search a file?
## Search file
open my $SF, "<", $search or die "could not open file $search : $! \n";
while(<$SF>) {
chomp($_);
next if /^\s*$/;
push(@search_strings, $_);
}
close($SF);
}
else {
push(@search_strings, $search);
}
## Get file name
## check if compressed. Warning, does not handle tar archives (*.tar.gz, *.tar.bz2, *.tgz, etc)
if ($INFILE_file_name =~ /\.gz$/) {
$INFILE_file_name =~ s/(.*\.gz)\s*$/gzip -dc < $1|/;
}
elsif ($INFILE_file_name =~ /\.zip$/) {
$INFILE_file_name =~ s/(.*\.zip)\s*$/gzip -dc < $1|/;
}
elsif ($INFILE_file_name =~ /\.Z$/) {
$INFILE_file_name =~ s/(.*\.Z)\s*$/gzip -dc < $1|/;
}
elsif ($INFILE_file_name =~ /\.bz2$/) {
$INFILE_file_name =~ s/(.*\.bz2)\s*$/bzip2 -dc < $1|/;
}
open(INFILE, $INFILE_file_name)
or die "$0 : failed to open input file '$INFILE_file_name' : $! \n";
## Set some start values
my $found_empty_line = 0;
my $found_fasta_line = 0;
my $found_separator = 0;
my $found_match = 0;
my $counter = 0;
## Read the file
while(<INFILE>) {
my $line = $_;
if ($line =~ /^>/) {
if ($found_match) {
$found_match = 0;
}
$found_separator = 1;
## Check header line for match:
CHECK:
foreach my $string (@search_strings) {
if ($line =~ /\Q$string\E/) {
if ($DEBUG) {
warn "\n >>> search_string $string matches (apparently) on $line(hit return to continue)\n" and getc();
}
$found_match = 1;
$counter++;
last CHECK;
}
}
if ($found_match) {
print STDOUT $line unless $inverse;
}
elsif ($inverse) {
print STDOUT "$line";
}
}
else {
if ($found_match) {
print STDOUT $line unless $inverse;
}
elsif ($inverse) {
print STDOUT $line;
}
}
}
## Close file
close INFILE
or warn "$0 : failed to close input file '$INFILE_file_name' : $!\n";
} # end of get_if_match_fasta
#=== FUNCTION ================================================================
# NAME: "MAIN"
# VERSION: 03/11/2010 10:34:07 AM CET
# DESCRIPTION: ???
# PARAMETERS: ???
# RETURNS: ???
# TODO: ???
#===============================================================================
MAIN:
while (my $INFILE_file_name = shift(@ARGV)) {
if ($search_pattern) {
get_if_match_fasta($search_pattern, $INFILE_file_name);
}
elsif ($search_file) {
get_if_match_fasta($search_file, $INFILE_file_name);
}
else {
print STDERR "Error. No search pattern/file given.\n";
exit(1);
}
}
exit(0);
__END__
#=== POD DOCUMENTATION =======================================================
# VERSION: 03/11/2010 10:18:18 AM CETT
# DESCRIPTION: Documentation
# TODO: ?
#===============================================================================
=pod
=head1 NAME
grepfasta.pl - Get entries from FASTA formatted file based on search in header
=head1 SYNOPSIS
grepfasta.pl [options] file ...
=head1 OPTIONS
=over 8
=item B<-h, -?, --help>
Print a brief help message and exits.
=item B<-m, --man>
Prints the manual page and exits.
=item B<-p, --search-pattern=>I<string>
Supply a search I<string>.
Put separated words (phrases) within single or double quotes (e.g., 'My query').
Match must be exact.
=item B<-f, --search-file=>I<file>
Supply a search I<file> with search strings.
Put several search strings on separate lines.
=item B<-i, --inverse|-v>
Inverse the output, i.e., print all fasta entries except the matching.
=item B<-n, --max=>I<integer> [NOT IMPLEMENTED]
Print/delete maximum I<integer> matches. Default is to print all.
=item B<-d, --debug>
Do some debug printing.
=back
=head1 DESCRIPTION
B<grepfasta.pl> will search for the presence of I<string> in
the header of a FASTA entry, and print the entry to STDOUT if match
(the default), or print all entries in the file except the match (if
B<--inverse> is used).
The match must be exact (i.e., no regular expressions allowed), and
the search is repeated for each fasta header in the file untio EOF.
=head1 USAGE
Examples:
grepfasta.pl --search-pattern='ABC 123' file.fasta > out.fasta
grepfasta.pl -p='ABC 123' file.fasta > out.fasta
grepfasta.pl --search-file=search_file.txt file.fasta > out.fasta
grepfasta.pl -f=search_file.txt file.fasta > out.fasta
grepfasta.pl -p='ABC 123' --inverse file.fasta > out.fasta
grepfasta.pl -p='ABC 123' -v file.fasta > out.fasta
grepfasta.pl -p='ABC 123' file.fasta.gz > out.fasta
=head1 AUTHOR
Written by Johan A. A. Nylander
=head1 DEPENDENCIES
Uses Perl modules Getopt::Long and Pod::Usage
=head1 LICENSE AND COPYRIGHT
Copyright (c) 2009, 2010, 2011, 2012 Johan Nylander. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
http://www.gnu.org/copyleft/gpl.html
=cut