Repository navigation
Expand file tree
/
Copy pathget_seq_len_stats.pl
More file actions
executable file
·95 lines (84 loc) · 2.46 KB
/
Copy pathget_seq_len_stats.pl
File metadata and controls
executable file
·95 lines (84 loc) · 2.46 KB
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
#!/usr/bin/perl -w
# USAGE: get_seq_len_stats.pl FILENAME.fasta ['yes' | 'no']
# NOTE: Summary statistics are calculated by default. To suppress the summary file,
# append the above command with "no". Summary statistics requires the
# Statistics::Descriptive perl module.
use strict;
my $seqfile = $ARGV[0];
unless (-e $seqfile) {die "File \'$seqfile\' does not exist.\nUSAGE: get_seq_len_stats.pl FILENAME.fasta ['yes' | 'no']";}
my $full_stats = $ARGV[1] || "yes";
my ($id,$seq) = ("","");
my @len;
open IN, "< $seqfile";
open OUT, "> $seqfile.len.txt";
while (<IN>){
chomp;
if (/>(\S+)/){
# set the previous info
unless ($id eq ""){
my $len = length($seq);
push (@len,$len) if ($full_stats eq "yes");
print OUT "$id\t$len\n";
}
$id = $1;
$seq = "";
} else {
$seq .= $_;
}
}
# deal with the last squence
my $len = length($seq);
push (@len,$len) if ($full_stats eq "yes");
print OUT "$id\t$len\n";
close OUT;
# Calculate the summary stats
if ($full_stats eq "yes"){
use Statistics::Descriptive;
my $stat = Statistics::Descriptive::Full->new();
$stat->add_data(@len);
my $seqcount = $stat->count();
my $mean = sprintf("%.2f", $stat->mean());
my $median = sprintf("%.2f", $stat->median());
my $variance = sprintf("%.2f", $stat->variance());
my $stdev = sprintf("%.2f", $stat->standard_deviation());
my $mode = sprintf("%.2f", $stat->mode());
my $min = sprintf("%.2f", $stat->min());
my $max = sprintf("%.2f", $stat->max());
my $sum = sprintf("%.0f", $stat->sum());
my $mb = sprintf("%.2f", $sum/1000000);
# Get the N50 length
my @sorted_len = sort {$b <=> $a} @len;
my $cumulative = 0;
my $count = 0;
while ($cumulative <= ($sum * .5) ){
$cumulative += shift @sorted_len;
$count++;
}
my $L50 = shift @sorted_len;
my $N50 = $count;
$cumulative += $L50;
$count++;
# Get the N90 length
while ($cumulative <= ($sum * .90) ){
$cumulative += shift @sorted_len;
$count++;
}
my $L90 = shift @sorted_len;
my $N90 = $count;
$cumulative += $L90;
$count++;
# Get the N95 length
while ($cumulative <= ($sum * .95) ){
$cumulative += shift @sorted_len;
$count++;
}
my $L95 = shift @sorted_len;
my $N95 = $count;
$cumulative += $L95;
$count++;
open OUT, ">$seqfile.seq_stats.txt";
print OUT "SeqFile\tSeqCount\tMean_len\tStDev\tMedian\tMode\tN50\tL50\tN90\tL90\tN95\tL95\tMin\tMax\tSum\tMbp\n";
print OUT "$seqfile\t$seqcount\t$mean\t$stdev\t$median\t$mode\t$N50\t$L50\t$N90\t$L90\t$N95\t$L95\t$min\t$max\t$sum\t$mb\n";
close OUT;
}
exit;