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