-
Notifications
You must be signed in to change notification settings - Fork 6
/
bnx2quantiles.pl
executable file
·363 lines (303 loc) · 10 KB
/
bnx2quantiles.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
#!/usr/bin/perl -w
# bnx2quantiles.pl
# first version: 2016-05-31
# report data-distributions for several key BNX value types
# designed to work with BNX 1.2 format
#
# Stephane Plaisance (VIB-NC+BITS) 2016/06/01; v1.1
# added average inter-label distance
# Stephane Plaisance (VIB-NC+BITS) 2016/06/01; v1.2
# handle no label case leading to div/0 error (empty rows X11 and X12)
#
# visit our Git: https://github.com/Nucleomics-VIB
use strict;
use warnings;
use File::Basename;
use Getopt::Std;
use Statistics::Descriptive;
use POSIX qw(strftime);
my $version = "1.2";
my $date = strftime "%m/%d/%Y", localtime;
# autoflush
$|=1;
############################
# handle command parameters
############################
getopts('i:p:P:h');
our($opt_i, $opt_p, $opt_P, $opt_h);
my $usage="You must provide a BNX file with -i
## Usage: bnx2quantiles.pl <-i bnx-file>
# script version:".$version."
# Additional optional parameters are:
# <-p additional low percentile (1)>
# <-P additional high percentile (99)>
# <-h to display this help>";
####################
# declare variables
####################
my $inputfile = $opt_i || die $usage."\n";
our $lowperc = $opt_p || 1;
our $highperc = $opt_P || 99;
defined($opt_h) && die $usage."\n";
# open stream from BNX file
my $FILE = OpenArchiveFile ($inputfile) or die $!;
my $outpath = dirname($inputfile);
# remove possible suffixes from filename
my @sufx=( ".bnx", ".bnx.gzip", ".bnx.gz", ".bnx.zip" );
my $outbase = basename($inputfile, @sufx);
my $outfile = $outpath."/".$outbase."_p-".$lowperc."_P-".$highperc."_quantiles.txt";
open OUT, "> $outfile" || die $!;
# working variables
my $counttot = 0;
my $totnucl = 0;
my $first = 1;
my @BigArray = ();
our @result = ();
our $spacer = join( "", "#", ("-" x 50 ), "\n");
our $cntln = 0;
my $quantheader = sprintf("%20s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s",
".", "min","25%","median","75%","max","mean", "stdev", "N50", $lowperc."%", $highperc."%", "skewness", "kurtosis"
);
################################
# parse data and store in array
################################
while (my $line = <$FILE>) {
# count header lines and abort if '# BNX' is not found in top 10 rows
$cntln++;
# check top line for "# BNX File Version: 1.2"
if ($first == 1) {
if ($line !~ /#\ BNX\ File\ Version:/) {
if ($cntln > 10) {
die "$line\n This does not seem to be a bnx file";
} else {
next;
}
}
$first = 0;
}
# header block
if ($line =~ /^#/) {
next;
}
# entering data part
# load four lines in @data
my @data = (); # store current four lines of data
# test data consistency
$line =~ /^0/ or die "aborted, does not seem to be a valid bnx format";
$counttot++;
push (@data, $line);
## 0 1 94694.1 ...
# 1 504.4 3008.2 ...
# QX11 1.0645 1.3571 ...
# QX12 0.0771 0.0778 ...
# read three more lines
for (my $d=1; $d<4; $d++) {
$line = <$FILE> || die "premature end of file";
push @data, $line;
}
######### analyze one record
# 0-line data
my $zerol=$data[0];
chomp($zerol);
my (undef, undef, $Length, $AvgIntensity, $SNR, $NumberofLabels) = split(/\t/, $zerol);
$totnucl += $Length;
my $labfreq = 100000*$NumberofLabels/$Length;
# 1-line data
my $onel=$data[1];
chomp($onel);
my (undef, @labpos) = split(/\t/, $onel);
my @labdist = interlabel(\@labpos);
my $avg_labdist = average(\@labdist);
# X11 row data
my $x11l = $data[2];
chomp($x11l);
my ( undef, @labsnr ) = split( /\t/, $x11l );
# handle empty row (no labels on molecule)
@labsnr || next;
my $avg_labsnr = average(\@labsnr);
# X12 row data
my $x12l = $data[3];
chomp($x12l);
my ( undef, @labai ) = split( /\t/, $x12l );
# handle empty row (no labels on molecule)
@labai || next;
my $avg_labai = average(\@labai);
# store results in @BigArray
push (@BigArray,
( [$Length,
sprintf("%.2f", $labfreq),
$SNR,
$AvgIntensity,
sprintf("%.4f",$avg_labsnr),
sprintf("%.4f",$avg_labai),
sprintf("%.3f",$avg_labdist)] ) );
}
undef $FILE;
#################
# REPORT RESULTS
#################
# print title and header
my $topline = "# BioNanoGenomics data quantiles (v".$version."), ".$date;
push (@result, $topline);
push (@result, "# input file: ".basename($inputfile));
# print molecule counts in subsets
push (@result, "# tot-molecules: ".(format_num($counttot)));
push (@result, "# input file size (Gb): ".(sprintf("%.3f", $totnucl/1_000_000_000)) );
push (@result, "# additional percentiles:");
push (@result, "# low_percentile: ".$lowperc);
push (@result, "# high_percentile: ".$highperc);
push (@result, $spacer);
# reports stats for all molecules
report_stats("All labeled molecules", \@BigArray);
# output results to screen and file
print STDOUT join("\n", @result)."\n";
print OUT join("\n", @result)."\n";
close OUT;
exit 0;
##############
#### Subs ####
##############
sub OpenArchiveFile
{
# $Filename passed in, handle to file passed out
my $File = shift; # filename
my $FH; # file handle
if ($File =~ /.bnx$/) {
open ($FH, "cat $File | ") or die ("$!: can't open file $File");
} elsif ($File =~ /.bnx.zip$/) {
open ($FH, "unzip -p $File | ") or die ("$!: can't open file $File");
} elsif ($File =~ /(.bnx.gzip|.bnx.gz)$/) {
open ($FH, "gzip -dc $File | ") or die ("$!: can't open file $File");
} else {
die ("$!: the file $File does not seem to be a 'bnx' file");
}
return $FH;
}
##############
sub format_num {
my $num = shift;
$num = reverse $num; # reverse the number's digits
$num =~ s/(\d{3})/$1\'/g; # insert quote every 3 digits, from beginning
$num = reverse $num; # Reverse the result
$num =~ s/^\'//; # remove leading sep, if any
return $num;
}
###########
sub average {
@_ == 1 or die ('Sub usage: $average = average(\@array);');
my ($array_ref) = @_;
my $sum;
my $count = scalar @$array_ref;
foreach (@$array_ref) { $sum += $_; }
return $sum / $count;
}
###############
sub interlabel {
@_ == 1 or die ('Sub usage: @interlabel = interlabel(\@array);');
return undef unless ( scalar(@_) );
my ($array_ref) = @_;
my @result;
# case only one label in current molecule
(scalar @$array_ref) >2 || return 0;
# shift first label out of array
my $previous = shift @$array_ref;
# remove last coordinate (= molecule end)
splice @$array_ref, (scalar @$array_ref -1);
# get each inter-label distance
foreach (@$array_ref) {
push @result, ($_-$previous);
$previous=$_;
}
return(@result);
}
##############
sub get_N50 {
return undef unless ( scalar(@_) );
my @sort = sort {$b <=> $a} @_;
my $totsum;
map { $totsum += $_ } @_;
my $cumsum; # cumulative sum
my $ranknum; # sorted rank
foreach my $curlength(@sort){
$cumsum += $curlength;
$ranknum++;
if($cumsum >= $totsum/2){
return ($curlength);
last;
}
}
}
##############
sub get_stats {
# uses https://metacpan.org/module/Statistics::Descriptive
# test input
return undef unless ( scalar(@_) );
my $n50 = get_N50(@_);
my $stat = Statistics::Descriptive::Full->new();
$stat->add_data(@_);
my @result = (
$stat->quantile(0),
$stat->quantile(1),
$stat->quantile(2),
$stat->quantile(3),
$stat->quantile(4),
$stat->mean(),
$stat->standard_deviation(),
(defined( $n50 ) ? $n50 : "0"),
(defined( scalar($stat->percentile($lowperc)) ) ? scalar($stat->percentile($lowperc)) : "0"),
(defined( scalar($stat->percentile($highperc)) ) ? scalar($stat->percentile($highperc)) : "0"),
(defined( $stat->skewness() ) ? $stat->skewness() : "0"),
(defined( $stat->kurtosis() ) ? $stat->kurtosis() : "0")
);
return @result;
}
##############
sub report_stats {
my($title, $array_ref) = @_;
# global counts
my $tot = scalar(@$array_ref);
my $pc = sprintf("%.1f", 100*$tot/$counttot);
# compute stats on columns 0 (Length) 3 (AvgIntensity), 2 (AvgSNR),
push (@result, "# distributions for ".$title." (N=".( format_num($tot) ).", ".$pc."%)");
push (@result, $quantheader);
# molecule lengths [0]
my @sizea = map $_->[ 0 ], @$array_ref;
my $totnucl;
map { $totnucl += $_ } @sizea;
my @sizedist = map { sprintf("%d", $_/1000)} ( get_stats(@sizea) );
push ( @result, sprintf("%20s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s",
"length (kb)", @sizedist) );
# molecule averageIntensities [3]
my @aia = map $_->[ 3 ], @$array_ref;
my @aidist = map { sprintf("%.2f", $_)} ( get_stats(@aia) );
push ( @result, sprintf("%20s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s",
"molecAvgIntensity", @aidist) );
# molecule SNR [2]
my @snra = map $_->[ 2 ], @$array_ref;
my @snrdist = map { sprintf("%.2f", $_)} ( get_stats(@snra) );
push ( @result, sprintf("%20s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s",
"molecAvgSNR", @snrdist) );
# molecule label frequency [1]
my @labfr = map $_->[ 1 ], @$array_ref;
my @labfrdist = map { sprintf("%.2f", $_)} ( get_stats(@labfr) );
push ( @result, sprintf("%20s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s",
"labelDensity", @labfrdist) );
# label average distance [6]
my @labdist = map $_->[ 6 ], @$array_ref;
my @labdistdist = map { sprintf("%.2f", $_)} ( get_stats(@labdist) );
push ( @result, sprintf("%20s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s",
"labelAvgDistance", @labdistdist) );
# label averageIntensities [5]
my @laia = map $_->[ 5 ], @$array_ref;
my @laidist = map { sprintf("%.2f", $_)} ( get_stats(@laia) );
push ( @result, sprintf("%20s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s",
"labelAvgIntensity", @laidist) );
# label snr [4]
my @lsnra = map $_->[ 4 ], @$array_ref;
my @lsnrdist = map { sprintf("%.2f", $_)} ( get_stats(@lsnra) );
push ( @result, sprintf("%20s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s",
"labelAvgSNR", @lsnrdist) );
# add spacer line
push ( @result, $spacer );
return @result;
}