-
Notifications
You must be signed in to change notification settings - Fork 15
/
fastx-length.pl
executable file
·131 lines (118 loc) · 3.28 KB
/
fastx-length.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
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long qw(:config auto_help pass_through);
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
sub SIConvert{
my ($val) = @_;
my @largePrefixes = ("k", "M", "G");
my $pID = 0;
my $prefix = "";
my $changed=0;
while(($val > 1000) && ($pID < $#largePrefixes)){
$prefix = $largePrefixes[$pID];
$val /= 1000;
$pID++;
}
return(sprintf("%.12g %s", $val, $prefix));
}
my $showMD5 = 0; # false
my $showFName = 0; # false
GetOptions("md5!" => \$showMD5, "fname!" => \$showFName, ) or
die("Error in command line arguments");
if($showMD5){
use Digest::MD5 qw(md5_hex); ## only attempt to load if md5 is requested
}
# use stdin if no files supplied
if(!@ARGV){
@ARGV = '-' unless (-t STDIN);
}
my @lengths = ();
my $inQual = 0; # false
my $seqID = "";
my $qualID = "";
my $seq = "";
my $qual = "";
my $seqFName = "";
foreach my $file (@ARGV) {
# This little gunzip dance makes sure the script can handle both
# gzip-compressed and uncompressed input, regardless of whether
# or not it is piped
my $z = new IO::Uncompress::Gunzip($file, "transparent", 1)
or die "gunzip failed: $GunzipError\n";
while(<$z>){
chomp;chomp;chomp;
if (!$inQual) {
if (/^(>|@)((.+?)( .*?\s*)?)$/) {
my $newSeqID = $2;
my $newShortID = $3;
if ($seqID) {
print(length($seq));
if($showMD5){
print(" ".md5_hex($seq));
}
print(" ${seqID}");
if($showFName){
print(" ".$seqFName);
}
print("\n");
push(@lengths, length($seq));
}
$seq = "";
$qual = "";
$seqID = $newSeqID;
$seqFName = $file;
} elsif (/^\+(.*)$/) {
$inQual = 1; # true
$qualID = $1;
} else {
$seq .= $_;
}
} else {
$qual .= $_;
if (length($qual) >= length($seq)) {
$inQual = 0; # false
}
}
}
}
if ($seqID) {
print(length($seq));
if($showMD5){
print(" ".md5_hex($seq));
}
print(" ${seqID}");
if($showFName){
print(" ".$seqFName);
}
print("\n");
push(@lengths, length($seq));
}
## calculate statistics
@lengths = sort {$b <=> $a} (@lengths);
my $sum = 0;
my @cumLengths = map {$sum += $_} (@lengths);
my $L50LengthNum = 0;
while($cumLengths[$L50LengthNum] < ($sum * 0.5)){
$L50LengthNum++;
}
my $L90LengthNum = 0;
while($cumLengths[$L90LengthNum] < ($sum * 0.9)){
$L90LengthNum++;
}
my $L10LengthNum = 0;
while($cumLengths[$L10LengthNum] < ($sum * 0.1)){
$L10LengthNum++;
}
printf(STDERR "Total sequences: %d\n", scalar(@lengths));
printf(STDERR "Total length: %sb\n", SIConvert($sum));
printf(STDERR "Longest sequence: %sb\n", SIConvert($lengths[0]));
printf(STDERR "Shortest sequence: %sb\n", SIConvert($lengths[$#lengths]));
printf(STDERR "Mean Length: %sb\n", SIConvert(sprintf("%d", ($sum) / scalar(@lengths))));
printf(STDERR "Median Length: %sb\n", SIConvert($lengths[$#lengths / 2]));
printf(STDERR "N10: %d sequences; L10: %sb\n",
($L10LengthNum+1), SIConvert($lengths[$L10LengthNum]));
printf(STDERR "N50: %d sequences; L50: %sb\n",
($L50LengthNum+1), SIConvert($lengths[$L50LengthNum]));
printf(STDERR "N90: %d sequences; L90: %sb\n",
($L90LengthNum+1), SIConvert($lengths[$L90LengthNum]));