-
Notifications
You must be signed in to change notification settings - Fork 4
/
calc.median.read.length.pl
49 lines (46 loc) · 1.15 KB
/
calc.median.read.length.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
# !/usr/local/bin/perl -w
# Set use
use strict;
use warnings;
# Set files to scalar variables
my $usage = "Usage: perl $0 <INFILE>";
my $infile = shift or die $usage;
open(IN, "<$infile") || die "Unable to open $infile: $!";
# Set variable for list of length values
my @lengths = ();
my $line_length = 0;
my $result = 0;
my $flag = 3;
# Assign lengths of each sequence to the array
while (my $line = <IN>) {
# Skip the sequence identifier lines
if ($flag%4) {
# Add line lengths to array
my $flag = $flag++;
next;
} else {
# Add line lengths to array
chomp $line;
my $line_length = length $line;
push @lengths, $line_length;
my $flag = $flag++;
}
}
# Sort the length array
my @sorted_lengths = sort {$a <=> $b} @lengths;
# Assign array length to a variable
my $array_length = @sorted_lengths;
# Get the median of the array, depending on whether the link is even or odd
if ($array_length%2) {
# Odd
my $result = $sorted_lengths[($array_length/2)];
print $result;
} else {
# Even
my $one = $sorted_lengths[($array_length/2)-1];
my $two = $sorted_lengths[($array_length/2)];
my $result = ($one + $two) / 2;
print $result;
}
#Close out files
close(IN);