-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfa-filter-size.pl
executable file
·59 lines (46 loc) · 1.55 KB
/
fa-filter-size.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
#!/usr/bin/env perl
use strict;
use Bio::SeqIO;
my(@Options, $verbose, $minsize, $maxsize);
setOptions();
my $in = Bio::SeqIO->new(-fh=>\*ARGV, -format=>'Fasta');
my $out = Bio::SeqIO->new(-fh=>\*STDOUT, -format=>'Fasta');
my $nread=0;
my $nwrote=0;
while (my $seq = $in->next_seq) {
$nread++;
if ($seq->length >= $minsize and $seq->length <= $maxsize) {
$out->write_seq($seq);
$nwrote++;
}
print STDERR "\rWrote $nwrote/$nread" if $nread%1000==0;
}
print STDERR "\rRead $nread sequences, wrote $nwrote, skipped ", $nread-$nwrote,".\n";
#----------------------------------------------------------------------
# Option setting routines
sub setOptions {
use Getopt::Long;
@Options = (
{OPT=>"help", VAR=>\&usage, DESC=>"This help"},
{OPT=>"verbose!", VAR=>\$verbose, DEFAULT=>0, DESC=>"Verbose"},
{OPT=>"minsize=i", VAR=>\$minsize, DEFAULT=>0, DESC=>"Minimum sequence length"},
{OPT=>"maxsize=i", VAR=>\$maxsize, DEFAULT=>1E10, DESC=>"Maximum sequence length"},
);
#(!@ARGV) && (usage());
&GetOptions(map {$_->{OPT}, $_->{VAR}} @Options) || usage();
# Now setup default values.
foreach (@Options) {
if (defined($_->{DEFAULT}) && !defined(${$_->{VAR}})) {
${$_->{VAR}} = $_->{DEFAULT};
}
}
}
sub usage {
print "Usage: $0 [options] all.fasta > some.fasta\n";
foreach (@Options) {
printf " --%-13s %s%s.\n",$_->{OPT},$_->{DESC},
defined($_->{DEFAULT}) ? " (default '$_->{DEFAULT}')" : "";
}
exit(1);
}
#----------------------------------------------------------------------