| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# Reads a list of numbers on stdin, computes the n50. |
| 4 |
# |
| 5 |
# If there are two numbers per line, they are assumed to be |
| 6 |
# a begin-end pair. |
| 7 |
|
| 8 |
# grep "M u " ATAC/atac.shift.atac | cut -d' ' -f 7 | perl n50.pl 3076782067 |
| 9 |
# grep "M u " ATAC/box2.shift.atac | cut -d' ' -f 7 | perl n50.pl 3076782067 |
| 10 |
|
| 11 |
my @values; |
| 12 |
|
| 13 |
while (<STDIN>) { |
| 14 |
s/^\s+//; |
| 15 |
s/\s$//; |
| 16 |
s/\s+/ /; |
| 17 |
|
| 18 |
my @vals = split '\s+', $_; |
| 19 |
my $val; |
| 20 |
|
| 21 |
if (scalar(@vals) == 1) { |
| 22 |
$val = $vals[0]; |
| 23 |
} elsif (scalar(@vals) == 1) { |
| 24 |
$val = $vals[1] - $vals[0]; |
| 25 |
$val = $vals[0] - $vals[1] if ($val < 0); |
| 26 |
} else { |
| 27 |
} |
| 28 |
|
| 29 |
push @values, $val; |
| 30 |
} |
| 31 |
|
| 32 |
if (scalar(@ARGV) > 0) { |
| 33 |
$totalLength = int($ARGV[0]); |
| 34 |
} else { |
| 35 |
foreach my $v (@values) { |
| 36 |
$totalLength += $v; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
@values = sort { $a <=> $b } @values; |
| 41 |
|
| 42 |
for (my $nvalue = 1; $nvalue <= 100; $nvalue += 1) { |
| 43 |
my $limit = $nvalue * $totalLength / 100; |
| 44 |
my $iter = 0; |
| 45 |
my $sum = 0; |
| 46 |
|
| 47 |
while (($sum < $limit) && ($iter < scalar(@values))) { |
| 48 |
$sum += $values[$iter++]; |
| 49 |
} |
| 50 |
|
| 51 |
print STDOUT "$nvalue $limit : $values[$iter-1]\n"; |
| 52 |
} |