line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Proch::N50; |
2
|
|
|
|
|
|
|
#ABSTRACT: a small module to calculate N50 (total size, and total number of sequences) for a FASTA or FASTQ file. It's easy to install, with minimal dependencies. |
3
|
|
|
|
|
|
|
|
4
|
8
|
|
|
8
|
|
4376
|
use 5.012; |
|
8
|
|
|
|
|
67
|
|
5
|
8
|
|
|
8
|
|
41
|
use warnings; |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
491
|
|
6
|
|
|
|
|
|
|
my $opt_digits = 2; |
7
|
|
|
|
|
|
|
$Proch::N50::VERSION = '1.5.6'; |
8
|
8
|
|
|
8
|
|
51
|
use File::Spec; |
|
8
|
|
|
|
|
22
|
|
|
8
|
|
|
|
|
194
|
|
9
|
8
|
|
|
8
|
|
5952
|
use JSON::PP; |
|
8
|
|
|
|
|
149892
|
|
|
8
|
|
|
|
|
628
|
|
10
|
8
|
|
|
8
|
|
3703
|
use FASTX::Reader; |
|
8
|
|
|
|
|
185963
|
|
|
8
|
|
|
|
|
389
|
|
11
|
8
|
|
|
8
|
|
62
|
use File::Basename; |
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
426
|
|
12
|
8
|
|
|
8
|
|
49
|
use Exporter qw(import); |
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
7056
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our @EXPORT = qw(getStats getN50 jsonStats); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub getStats { |
17
|
|
|
|
|
|
|
# Parses a FASTA/FASTQ file and returns stats |
18
|
|
|
|
|
|
|
# Parameters: |
19
|
|
|
|
|
|
|
# * filename (Str) |
20
|
|
|
|
|
|
|
# * Also return JSON string (Bool) |
21
|
|
|
|
|
|
|
|
22
|
11
|
|
|
11
|
1
|
2687
|
my ( $file, $wantJSON, $customN ) = @_; |
23
|
11
|
0
|
0
|
|
|
47
|
if (defined $customN and ($customN > 100 or $customN < 0) ) { |
|
|
|
33
|
|
|
|
|
24
|
0
|
|
|
|
|
0
|
die "[Proch::N50] Custom value must be 0 < x < 100\n"; |
25
|
|
|
|
|
|
|
} |
26
|
11
|
|
|
|
|
19
|
my $answer; |
27
|
11
|
|
|
|
|
38
|
$answer->{status} = 1; |
28
|
11
|
|
|
|
|
29
|
$answer->{N50} = undef; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Check file existence |
31
|
|
|
|
|
|
|
# uncoverable condition right |
32
|
11
|
100
|
66
|
|
|
204
|
if ( !-e "$file" and $file ne '-' ) { |
33
|
3
|
|
|
|
|
9
|
$answer->{status} = 0; |
34
|
3
|
|
|
|
|
20
|
$answer->{message} = "Unable to find <$file>"; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# Return failed status if file not found or not readable |
40
|
11
|
100
|
|
|
|
52
|
if ( $answer->{status} == 0 ) { |
41
|
3
|
|
|
|
|
12
|
return $answer; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
##my @aux = undef; |
45
|
8
|
|
|
|
|
15
|
my $Reader; |
46
|
8
|
50
|
|
|
|
33
|
if ($file ne '-') { |
47
|
8
|
|
|
|
|
109
|
$Reader = FASTX::Reader->new({ filename => "$file" }); |
48
|
|
|
|
|
|
|
} else { |
49
|
0
|
|
|
|
|
0
|
$Reader = FASTX::Reader->new({ filename => '{{STDIN}}' }); |
50
|
|
|
|
|
|
|
} |
51
|
8
|
|
|
|
|
2510
|
my %sizes; |
52
|
8
|
|
|
|
|
28
|
my ( $n, $slen ) = ( 0, 0 ); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Parse FASTA/FASTQ file |
55
|
8
|
|
|
|
|
71
|
while ( my $seq = $Reader->getRead() ) { |
56
|
48
|
|
|
|
|
3196
|
++$n; |
57
|
48
|
|
|
|
|
119
|
my $size = length($seq->{seq}); |
58
|
48
|
|
|
|
|
80
|
$slen += $size; |
59
|
48
|
|
|
|
|
196
|
$sizes{$size}++; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
8
|
|
|
|
|
288
|
my ($n50, $min, $max, $auN, $n75, $n90, $nx); |
63
|
8
|
50
|
|
|
|
38
|
unless ($n) { |
64
|
0
|
|
|
|
|
0
|
($n50, $min, $max, $auN, $n75, $n90, $nx) = |
65
|
|
|
|
|
|
|
( 0, 0, 0, 0, 0, 0, 0); |
66
|
0
|
|
|
|
|
0
|
say STDERR "[n50] WARNING: Not a sequence file: $file"; |
67
|
|
|
|
|
|
|
} else { |
68
|
|
|
|
|
|
|
# Invokes core _n50fromHash() routine |
69
|
8
|
|
|
|
|
34
|
($n50, $min, $max, $auN, $n75, $n90, $nx) = _n50fromHash( \%sizes, $slen, $customN ); |
70
|
|
|
|
|
|
|
} |
71
|
8
|
|
|
|
|
561
|
my $basename = basename($file); |
72
|
|
|
|
|
|
|
|
73
|
8
|
|
|
|
|
33
|
$answer->{N50} = $n50 + 0; |
74
|
8
|
|
|
|
|
22
|
$answer->{N75} = $n75 + 0; |
75
|
8
|
|
|
|
|
22
|
$answer->{N90} = $n90 + 0; |
76
|
8
|
50
|
|
|
|
66
|
if (defined $customN) { |
77
|
0
|
|
|
|
|
0
|
$answer->{Ne} = $nx + 0; |
78
|
0
|
|
|
|
|
0
|
$answer->{"N$customN"} = $nx + 0; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
8
|
|
|
|
|
126
|
$answer->{auN} = sprintf("%.${opt_digits}f", $auN + 0); |
82
|
8
|
|
|
|
|
39
|
$answer->{min} = $min + 0; |
83
|
8
|
|
|
|
|
32
|
$answer->{max} = $max + 0; |
84
|
8
|
|
|
|
|
37
|
$answer->{seqs} = $n; |
85
|
8
|
|
|
|
|
20
|
$answer->{size} = $slen; |
86
|
8
|
|
|
|
|
18
|
$answer->{filename} = $basename; |
87
|
8
|
|
|
|
|
330
|
$answer->{dirname} = dirname($file); |
88
|
8
|
|
|
|
|
330
|
$answer->{path } = File::Spec->rel2abs(dirname($file)); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# If JSON is required return JSON |
91
|
8
|
100
|
66
|
|
|
55
|
if ( defined $wantJSON and $wantJSON ) { |
92
|
|
|
|
|
|
|
|
93
|
5
|
|
|
|
|
108
|
my $json = JSON::PP->new->ascii->pretty->allow_nonref; |
94
|
5
|
|
|
|
|
889
|
my $pretty_printed = $json->encode( $answer ); |
95
|
5
|
|
|
|
|
4138
|
$answer->{json} = $pretty_printed; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
} |
98
|
8
|
|
|
|
|
230
|
return $answer; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub _n50fromHash { |
102
|
|
|
|
|
|
|
# _n50fromHash(): calculate stats from hash of lengths |
103
|
|
|
|
|
|
|
# |
104
|
|
|
|
|
|
|
# Parameters: |
105
|
|
|
|
|
|
|
# * A hash of key={contig_length} and value={no_contigs} |
106
|
|
|
|
|
|
|
# * Sum of all contigs sizes |
107
|
8
|
|
|
8
|
|
23
|
my ( $hash_ref, $total_size, $custom_n ) = @_; |
108
|
8
|
|
|
|
|
15
|
my $progressive_sum = 0; |
109
|
8
|
|
|
|
|
14
|
my $auN = 0; |
110
|
8
|
|
|
|
|
14
|
my $n50 = undef; |
111
|
8
|
|
|
|
|
15
|
my $n75 = undef; |
112
|
8
|
|
|
|
|
13
|
my $n90 = undef; |
113
|
8
|
|
|
|
|
15
|
my $nx = undef; |
114
|
8
|
|
|
|
|
14
|
my @sorted_keys = sort { $a <=> $b } keys %{$hash_ref}; |
|
34
|
|
|
|
|
104
|
|
|
8
|
|
|
|
|
47
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# Added in v. 0.039 |
117
|
8
|
|
|
|
|
31
|
my $max = $sorted_keys[-1]; |
118
|
8
|
|
|
|
|
15
|
my $min = $sorted_keys[0] ; |
119
|
|
|
|
|
|
|
# N50 definition: https://en.wikipedia.org/wiki/N50_statistic |
120
|
|
|
|
|
|
|
# Was '>=' in my original implementation of N50. Now complies with 'seqkit' |
121
|
|
|
|
|
|
|
# N50 Calculation |
122
|
|
|
|
|
|
|
|
123
|
8
|
|
|
|
|
28
|
foreach my $s ( @sorted_keys ) { |
124
|
30
|
|
|
|
|
57
|
my $ctgs_length = $s * ${$hash_ref}{$s}; |
|
30
|
|
|
|
|
82
|
|
125
|
30
|
|
|
|
|
44
|
$progressive_sum += $ctgs_length; |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
128
|
30
|
|
|
|
|
55
|
$auN += ( $ctgs_length ) * ( $ctgs_length / $total_size); |
129
|
|
|
|
|
|
|
|
130
|
30
|
100
|
66
|
|
|
133
|
if ( !$n50 and $progressive_sum > ( $total_size * ((100 - 50) / 100) ) ) { |
131
|
8
|
|
|
|
|
18
|
$n50 = $s; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
30
|
100
|
100
|
|
|
101
|
if ( !$n75 and $progressive_sum > ( $total_size * ((100 - 75) / 100) ) ) { |
135
|
8
|
|
|
|
|
15
|
$n75 = $s; |
136
|
|
|
|
|
|
|
} |
137
|
30
|
100
|
100
|
|
|
105
|
if ( !$n90 and $progressive_sum > ( $total_size * ((100 - 90) / 100) ) ) { |
138
|
8
|
|
|
|
|
31
|
$n90 = $s; |
139
|
|
|
|
|
|
|
} |
140
|
30
|
50
|
33
|
|
|
107
|
if ( !$nx and defined $custom_n) { |
141
|
0
|
0
|
|
|
|
0
|
$nx = $s if ( $progressive_sum > ( $total_size * ((100 - $custom_n) / 100) )); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} |
144
|
8
|
|
|
|
|
45
|
return ($n50, $min, $max, $auN, $n75, $n90, $nx); |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub getN50 { |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# Invokes the full getStats returning N50 or 0 in case of error; |
151
|
3
|
|
|
3
|
1
|
326
|
my ($file) = @_; |
152
|
3
|
|
|
|
|
8
|
my $stats = getStats($file); |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# Verify status and return |
155
|
|
|
|
|
|
|
# uncoverable branch false |
156
|
3
|
50
|
|
|
|
17
|
if ( $stats->{status} ) { |
157
|
3
|
|
|
|
|
15
|
return $stats->{N50}; |
158
|
|
|
|
|
|
|
} else { |
159
|
0
|
|
|
|
|
0
|
return 0; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub jsonStats { |
164
|
2
|
|
|
2
|
1
|
7177
|
my ($file) = @_; |
165
|
2
|
|
|
|
|
8
|
my $stats = getStats($file, 'JSON'); |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# Return JSON object if getStats() was able to reduce one |
168
|
|
|
|
|
|
|
# uncoverable branch false |
169
|
2
|
100
|
|
|
|
21
|
if (defined $stats->{json}) { |
170
|
|
|
|
|
|
|
return $stats->{json} |
171
|
1
|
|
|
|
|
6
|
} else { |
172
|
|
|
|
|
|
|
# Return otherwise |
173
|
1
|
|
|
|
|
4
|
return ; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
1; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
__END__ |