-
Notifications
You must be signed in to change notification settings - Fork 16
/
fasta2collapse.pl
executable file
·59 lines (37 loc) · 1.11 KB
/
fasta2collapse.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
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use Carp;
use File::Basename;
use MyConfig;
my $verbose = 0;
my $prog = basename ($0);
my $tmpDir = getDefaultCache ($prog);
GetOptions (
"tmp-dir:s"=>\$tmpDir,
"v|verbose"=>\$verbose);
if (@ARGV != 2)
{
print "collapse exact duplicate sequences\n";
print "Usage: $prog [options] <in.fa> <out.fa>\n";
print "options:\n";
print " --tmp-dir [string]: temp dir ($tmpDir)\n";
print " -v : verbose\n";
exit (1);
}
Carp::croak "$tmpDir already exists\n" if -d $tmpDir;
system ("mkdir $tmpDir");
my ($inFastaFile, $outFastaFile) = @ARGV;
my $tmpHeaderFile = "$tmpDir/header";
my $tmpSeqFile = "$tmpDir/seq";
my $cmd = "grep \\> $inFastaFile | cut -d \" \" -f 1 > $tmpHeaderFile";
print $cmd, "\n" if $verbose;
system ($cmd);
$cmd = "grep -v \\> $inFastaFile > $tmpSeqFile";
print $cmd, "\n" if $verbose;
system ($cmd);
$cmd = "paste $tmpHeaderFile $tmpSeqFile | sort -T $tmpDir -k 2 | uniq -f 1 -c | awk '{print \$2\"#\"\$1\"\\n\"\$3}' > $outFastaFile";
print $cmd, "\n" if $verbose;
system ($cmd);
system ("rm -rf $tmpDir");