forked from vikas0633/perl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinner.pl
87 lines (59 loc) · 1.65 KB
/
binner.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
#!/usr/bin/env perl
use strict;
my $binparam;
unless ($binparam = $ARGV[0]) {
die "usage: $0 bin_size [include zero counts]\n";
}
# for binning, want the following output:
# range (0-10) -> 10
# range (11-20) -> 20
# (21-30) -> 30
# etc.
# formula is:
# x = number
# binned(x) at seg-y = int ( (x-1+y)/y)
#analyse data
my $include_zero_counts = $ARGV[1];
my $value_counter = 0;
my %value_dist_counter;
my $entry_counter = 0;
my %tracker;
while (<STDIN>) {
$_ =~ s/\s+//g;
my $orig = $_;
unless (/^[+-]?[\d\.]+$/) {
print STDERR "can't process $_\n";
next;
}
my $val_examine = $orig;
if ($val_examine > 0) {
$val_examine--;
} elsif ($val_examine < 0) {
$val_examine++;
}
my $index = int ( $val_examine/ $binparam);
if ($orig > 0) {
$index++;
} elsif ($orig < 0) {
$index--;
}
$tracker{$index}++;
$value_counter += abs($orig);
$value_dist_counter{$index} += abs($orig);
$entry_counter++;
}
my $total_counts = $entry_counter;
#print results
my @values = sort {$a<=>$b} keys %tracker;
my $maxvalue = $values[$#values];
my $minvalue = $values[0];
print STDERR "#bin\tcounts\t%counts\t%value of total\n";
for (my $i = $minvalue; $i <= $maxvalue; $i++) {
my $index = ($i) * ($binparam);
my $value = (exists($tracker{$i})) ? $tracker{$i} : 0;
if ($value != 0 || $include_zero_counts) {
my $percentage_counts = sprintf ("%.2f", $value / $total_counts * 100);
my $percentage_values = sprintf ("%.2f", $value_dist_counter{$i}/$value_counter * 100);
print "$index\t$value\t$percentage_counts\t$percentage_values\n";
}
}