-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathN50.pl
executable file
·381 lines (302 loc) · 9.63 KB
/
N50.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env perl
#ABSTRACT: A script to calculate N50 from one or multiple FASTA/FASTQ files, or from STDIN.
use v5.12;
use Pod::Usage;
use Term::ANSIColor qw(:constants colorvalid colored);
use Getopt::Long;
use File::Basename;
use JSON::PP;
our $BASE = basename($0);
local $Term::ANSIColor::AUTORESET = 1;
our %program = (
'NAME' => 'FASTx N50 CALCULATOR',
'AUTHOR' => 'Andrea Telatin',
'MAIL' => '[email protected]',
'VERSION' => '1.1',
);
my $opt_separator = "\t";
my $opt_format = 'default';
my %formats = (
'default' => 'Prints only N50 for single file, TSV for multiple files',
'tsv' => 'Tab separated output (file, seqs, total size, N50)',
'full' => 'Not implemented',
'json' => 'JSON (JavaScript Object Notation) output',
'short' => 'Not Implemented',
'csv' => 'Alias for tsv',
'custom' => 'Custom format with --template STRING',
);
my ($opt_help,
$opt_version,
$opt_input,
$opt_verbose,
$opt_debug,
$opt_color,
$opt_nonewline,
$opt_noheader,
$opt_pretty,
$opt_basename,
$opt_template,
);
our $tab = "\t";
our $new = "\n";
my $result = GetOptions(
'f|format=s' => \$opt_format,
's|separator=s' => \$opt_separator,
'p|pretty' => \$opt_pretty,
'n|nonewline' => \$opt_nonewline,
'j|noheader' => \$opt_noheader,
'b|basename' => \$opt_basename,
't|template=s' => \$opt_template,
'c|color' => \$opt_color,
'h|help' => \$opt_help,
'v|version' => \$opt_version,
'd|debug' => \$opt_debug,
);
pod2usage({-exitval => 0, -verbose => 2}) if $opt_help;
version() if defined $opt_version;
our %output_object;
if (defined $opt_format) {
$opt_format = lc($opt_format);
if (!$formats{$opt_format}) {
my @list = sort keys(%formats);
die " FATAL ERROR:\n Output format not valid (--format '$opt_format').\n Use one of the following: " .
join(', ',@list) . ".\n";
}
if ($formats{$opt_format} eq 'Not implemented') {
print STDERR " WARNING: Format '$opt_format' not implemented yet. Switching to 'tsv'.\n";
$opt_format = 'tsv';
}
if ($opt_format eq 'csv') {
$opt_separator = ',';
}
}
if (not defined $ARGV[0]) {
print STDERR GREEN, "n50 - calculate N50 of FASTA/FASTQ files\n", RESET;
print STDERR "USAGE: $BASE [options] FILE1 FILE2 FILE3...\n";
print STDERR "No input files specified.\n";
exit;
}
foreach my $file (@ARGV) {
if (!-e "$file" and $file ne '-') {
die " FATAL ERROR:\n File not found ($file).\n";
} elsif ($file eq '-') {
$file = '<STDIN>';
} elsif ($file =~/.gz$/) {
open STDIN, '-|', "gzip -dc $file" || die " FATAL ERROR:\n Unable to open file for reading ($file).\n";
} else {
open STDIN, '<', "$file" || die " FATAL ERROR:\n Unable to open file for reading ($file).\n";
}
my @aux;
my %sizes;
my ($n, $slen) = (0, 0);
while (my ($name, $seq) = readfq(\*STDIN, \@aux)) {
next if ($name eq '');
$n++;
my $size = length($seq);
$slen += $size;
$sizes{$size}++;
}
my $n50 = n50fromHash(\%sizes, $slen);
say STDERR "[$file]\tTotalSize:$slen;N50:$n50;Sequences:$n" if ($opt_debug);
$file = basename($file) if ($opt_basename);
my %metrics = (
'seqs' => $n,
'N50' => $n50,
'size' => $slen,
);
$output_object{$file} = \%metrics;
}
my $file_num = scalar keys %output_object;
if (!$opt_format or $opt_format eq 'default') {
# DEFAULT
if ($file_num == 1) {
my @keys = keys %output_object;
say $output_object{$keys[0]}{'N50'};
} else {
foreach my $r (keys %output_object) {
say $r, $opt_separator ,$output_object{$r}{'N50'};
}
}
} elsif ($opt_format eq 'json') {
my $json = JSON::PP->new->allow_nonref;
my $pretty_printed = $json->pretty->encode( \%output_object );
say $pretty_printed;
} elsif ($opt_format eq 'tsv' or $opt_format eq 'csv') {
my @fields = ('path', 'seqs', 'size', 'N50');
say '#', join($opt_separator, @fields) if (!defined $opt_noheader);
foreach my $r (keys %output_object) {
print $r,$opt_separator;
for (my $i = 1; $i <= $#fields; $i++) {
print $output_object{$r}{$fields[$i]};
if ($i == $#fields and !$opt_nonewline) {
print "\n";
} else {
print $opt_separator;
}
}
}
} elsif ($opt_format eq 'custom') {
foreach my $r (keys %output_object) {
my $output_string = $opt_template;
$output_string =~s/{new}/$new/g;
$output_string =~s/{tab}/$tab/g;
$output_string =~s/{(\w+)}/$output_object{$r}{$1}/g;
$output_string =~s/{path}/$r/g;
print $output_string;
}
}
sub debug {
my ($message, $title) = @_;
$title = 'INFO' unless defined $title;
$title = uc($title);
printMessage($message, $title, 'green', 'reset');
}
sub printMessage {
my ($message, $title, $title_color, $message_color) = @_;
$title_color = 'reset' if (!defined $title_color or !colorvalid($title_color) or !$opt_color);
$message_color = 'reset' if (!defined $message_color or !colorvalid($message_color) or !$opt_color);
say STDERR colored("$title", $title_color), "\t", colored("$message", $message_color);
}
sub n50fromHash {
my ($hash_ref, $total) = @_;
my $tlen = 0;
foreach my $s (sort {$a <=> $b} keys %{$hash_ref}) {
$tlen += $s * ${$hash_ref}{$s};
return $s if ($tlen >= ($total/2));
}
}
sub version {
printMessage("$program{NAME}, ver. $program{VERSION}", '', 'RESET', 'bold green');
printMessage(qq(
$program{AUTHOR}
Program to calculate N50 from multiple FASTA/FASTQ files.
Type --help (or -h) to see the full documentation.), '', 'blue', 'green');
END;
exit;
}
sub readfq {
my ($fh, $aux) = @_;
@$aux = [undef, 0] if (!(@$aux));
return if ($aux->[1]);
if (!defined($aux->[0])) {
while (<$fh>) {
chomp;
if (substr($_, 0, 1) eq '>' || substr($_, 0, 1) eq '@') {
$aux->[0] = $_;
last;
}
}
if (!defined($aux->[0])) {
$aux->[1] = 1;
return;
}
}
my $name = '';
if (defined $_) {
$name = /^.(\S+)/? $1 : '';
}
my $seq = '';
my $c;
$aux->[0] = undef;
while (<$fh>) {
chomp;
$c = substr($_, 0, 1);
last if ($c eq '>' || $c eq '@' || $c eq '+');
$seq .= $_;
}
$aux->[0] = $_;
$aux->[1] = 1 if (!defined($aux->[0]));
return ($name, $seq) if ($c ne '+');
my $qual = '';
while (<$fh>) {
chomp;
$qual .= $_;
if (length($qual) >= length($seq)) {
$aux->[0] = undef;
return ($name, $seq, $qual);
}
}
$aux->[1] = 1;
return ($name, $seq);
}
__END__
=head1 NAME
B<n50.pl> - A program to calculate N50 from FASTA/FASTQ files
=head1 AUTHOR
Andrea Telatin <[email protected]>
=head1 DESCRIPTION
This program parses a list of FASTA/FASTQ files calculating for each one
the number of sequences, the sum of sequences lengths and the N50.
It will print the result in different formats, by default only the N50 is
printed for a single file and all metrics in TSV format for multiple files.
=head1 SYNOPSIS
n50.pl [options] [FILE1 FILE2 FILE3...]
=head1 PARAMETERS
=over 12
=item B<-f, --format>
Output format: default, tsv, json, custom.
See below for format specific switches.
=item B<-s, --separator>
Separator to be used in 'tsv' output. Default: tab.
The 'tsv' format will print a header line, followed
by a line for each file given as input with: file path,
as received, total number of sequences, total size in bp,
and finally N50.
=item B<-b, --basename>
Instead of printing the path of each file, will only print
the filename, stripping relative or absolute paths to it.
=item B<-j, --noheader>
When used with 'tsv' output format, will suppress header
line.
=item B<-n, --nonewline>
If used with 'default' or 'csv' output format, will NOT print the
newline character after the N50. Usually used in bash scripting.
=item B<-t, --template>
String to be used with 'custom' format. Will be used as template
string for each sample, replacing {new} with newlines, {tab} with
tab and {N50}, {seqs}, {size}, {path} with sample's N50, number of sequences,
total size in bp and file path respectively (the latter will
respect --basename if used).
=item B<-p, --pretty>
If used with 'json' output format, will format the JSON
in pretty print mode. Example:
{
"file1.fa" : {
"size" : 290,
"N50" : "290",
"seqs" : 2
},
"file2.fa" : {
"N50" : "456",
"size" : 456,
"seqs" : 2
}
}
=item B<-h, --help>
Will display this full help message and quit, even if other
arguments are supplied.
=back
=head1 INSTALLATION
A complete package with more feature is Proch::N50, installable with
cpan Proch::N50
Or from Bioconda with:
conda install -c bioconda n50
And a complete suite of tools including statistics, is available as
conda install -c bioconda "seqfu>=1.10"
=head1 CITATION
Telatin A, Fariselli P, Birolo G.
SeqFu: A Suite of Utilities for the Robust and Reproducible Manipulation of Sequence Files.
Bioengineering 2021, 8, 59. L<10.3390/bioengineering8050059|https://doi.org/10.3390/bioengineering8050059>
=head1 COPYRIGHT
Copyright (C) 2017 Andrea Telatin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
and this program. If not, see <http://www.gnu.org/licenses/>.
=cut