-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_fasta_w_min_number.pl
executable file
·57 lines (48 loc) · 1.37 KB
/
get_fasta_w_min_number.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
#!/usr/bin/perl
# get_fasta_w_min_number.pl INDIR OUTDIR MINIMUM_SEQS
# script goes through FASTA files in INDIR and prints the file
# to OUTDIR if it has at least MINIMUM_SEQS
use strict;
use warnings;
MAIN: {
my $indir = $ARGV[0] || usage();
my $outdir = $ARGV[1] || usage();
my $min = $ARGV[2] || usage();
my $count = 0;
check_outdir($outdir);
opendir DIR, $indir or die "cannot open $indir:$!";
my @files = readdir DIR;
foreach my $f (@files) {
open IN, "$indir/$f" or die "cannot open $indir/$f:$!";
my $count = 0;
my $seqs = '';
while (my $line = <IN>) {
$seqs .= $line;
$count++ if ($line =~ m/^>/);
}
write_seqs($outdir,$f,$seqs) if ($count >= $min);
}
}
sub check_outdir {
my $outdir = shift;
if (-d $outdir) {
opendir OUTDIR, $outdir or die "cannot read $outdir:$!";
my @existing = grep {!/^\.\.?$/} readdir OUTDIR;
foreach my $e (@existing) {
warn "warning: $outdir exists and includes $e\n";
}
} else {
mkdir $outdir or die "cannot open $outdir";
}
}
sub write_seqs {
my $dir = shift;
my $file = shift;
my $seqs = shift;
open OUT, ">$dir/$file" or die "cannot open >$dir/$file:$!";
print OUT $seqs;
close OUT;
}
sub usage {
die "usage: $0 INDIR OUTDIR MINIMUM_SEQS\n";
}