File Coverage

blib/lib/Proch/N50.pm
Criterion Covered Total %
statement 95 103 92.2
branch 19 28 67.8
condition 14 24 62.5
subroutine 11 11 100.0
pod 3 3 100.0
total 142 169 84.6


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   4231 use 5.012;
  8         68  
5 8     8   40 use warnings;
  8         18  
  8         448  
6             my $opt_digits = 2;
7             $Proch::N50::VERSION = '1.5.7';
8 8     8   50 use File::Spec;
  8         22  
  8         223  
9 8     8   6134 use JSON::PP;
  8         147117  
  8         628  
10 8     8   3801 use FASTX::Reader;
  8         200429  
  8         399  
11 8     8   72 use File::Basename;
  8         16  
  8         438  
12 8     8   53 use Exporter qw(import);
  8         18  
  8         6666  
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 1665 my ( $file, $wantJSON, $customN ) = @_;
23 11 0 0     46 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         27 my $answer;
27 11         39 $answer->{status} = 1;
28 11         28 $answer->{N50} = undef;
29              
30             # Check file existence
31             # uncoverable condition right
32 11 100 66     196 if ( !-e "$file" and $file ne '-' ) {
33 3         10 $answer->{status} = 0;
34 3         12 $answer->{message} = "Unable to find <$file>";
35             }
36              
37             # Return failed status if file not found or not readable
38 11 100       47 if ( $answer->{status} == 0 ) {
39 3         9 return $answer;
40             }
41              
42             # my @aux = undef;
43 8         15 my $Reader;
44 8 50       29 if ($file ne '-') {
45 8         87 $Reader = FASTX::Reader->new({ filename => "$file" });
46             } else {
47 0         0 $Reader = FASTX::Reader->new({ filename => '{{STDIN}}' });
48             }
49 8         2415 my %sizes;
50 8         30 my ( $n, $slen ) = ( 0, 0 );
51              
52             # Parse FASTA/FASTQ file
53 8         65 while ( my $seq = $Reader->getRead() ) {
54 48         3190 ++$n;
55 48         106 my $size = length($seq->{seq});
56 48         69 $slen += $size;
57 48         196 $sizes{$size}++;
58             }
59              
60 8         255 my ($n50, $min, $max, $auN, $n75, $n90, $nx);
61 8 50       30 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         29 ($n50, $min, $max, $auN, $n75, $n90, $nx) = _n50fromHash( \%sizes, $slen, $customN );
68             }
69 8         510 my $basename = basename($file);
70              
71 8         36 $answer->{N50} = $n50 + 0;
72 8         24 $answer->{N75} = $n75 + 0;
73 8         29 $answer->{N90} = $n90 + 0;
74 8 50       35 if (defined $customN) {
75 0         0 $answer->{Ne} = $nx + 0;
76 0         0 $answer->{"N$customN"} = $nx + 0;
77             }
78              
79 8         105 $answer->{auN} = sprintf("%.${opt_digits}f", $auN + 0);
80 8         29 $answer->{min} = $min + 0;
81 8         17 $answer->{max} = $max + 0;
82 8         33 $answer->{seqs} = $n;
83 8         20 $answer->{size} = $slen;
84 8         27 $answer->{filename} = $basename;
85 8         289 $answer->{dirname} = dirname($file);
86 8         317 $answer->{path } = File::Spec->rel2abs(dirname($file));
87              
88             # If JSON is required return JSON
89 8 100 66     74 if ( defined $wantJSON and $wantJSON ) {
90              
91 5         40 my $json = JSON::PP->new->ascii->pretty->allow_nonref;
92 5         842 my $pretty_printed = $json->encode( $answer );
93 5         4169 $answer->{json} = $pretty_printed;
94              
95             }
96 8         211 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   25 my ( $hash_ref, $total_size, $custom_n ) = @_;
106 8         16 my $progressive_sum = 0;
107 8         13 my $auN = 0;
108 8         18 my $n50 = undef;
109 8         15 my $n75 = undef;
110 8         12 my $n90 = undef;
111 8         15 my $nx = undef;
112 8         17 my @sorted_keys = sort { $a <=> $b } keys %{$hash_ref};
  36         121  
  8         47  
113              
114             # Added in v. 0.039
115 8         35 my $max = $sorted_keys[-1];
116 8         21 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         29 foreach my $s ( @sorted_keys ) {
122 30         48 my $ctgs_length = $s * ${$hash_ref}{$s};
  30         62  
123 30         43 $progressive_sum += $ctgs_length;
124              
125              
126 30         74 $auN += ( $ctgs_length ) * ( $ctgs_length / $total_size);
127              
128 30 100 66     159 if ( !$n50 and $progressive_sum > ( $total_size * ((100 - 50) / 100) ) ) {
129 8         27 $n50 = $s;
130             }
131              
132 30 100 100     109 if ( !$n75 and $progressive_sum > ( $total_size * ((100 - 75) / 100) ) ) {
133 8         26 $n75 = $s;
134             }
135 30 100 100     105 if ( !$n90 and $progressive_sum > ( $total_size * ((100 - 90) / 100) ) ) {
136 8         19 $n90 = $s;
137             }
138 30 50 33     106 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         59 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 322 my ($file) = @_;
150 3         10 my $stats = getStats($file);
151              
152             # Verify status and return
153             # uncoverable branch false
154 3 50       19 if ( $stats->{status} ) {
155 3         18 return $stats->{N50};
156             } else {
157 0         0 return 0;
158             }
159             }
160              
161             sub jsonStats {
162 2     2 1 5410 my ($file) = @_;
163 2         6 my $stats = getStats($file, 'JSON');
164              
165             # Return JSON object if getStats() was able to reduce one
166             # uncoverable branch false
167 2 100       36 if (defined $stats->{json}) {
168             return $stats->{json}
169 1         7 } else {
170             # Return otherwise
171 1         5 return ;
172             }
173             }
174              
175              
176             1;
177              
178             __END__