-
Notifications
You must be signed in to change notification settings - Fork 2
/
Coord_spanner.pm
executable file
·75 lines (48 loc) · 1.59 KB
/
Coord_spanner.pm
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
package Coord_spanner;
use strict;
use warnings;
use Carp;
## provide a list of coordinates like so: ([end5, end3], [end5, end3], ...)
## returns exclusive regions of overlap: ([lend, rend], [lend, rend], ...)
sub get_coord_spans {
my @coord_sets = @_;
unless (@coord_sets) { confess "need coordsets as param"; }
my @coords;
## convert end5, end3 to lend,rend w/o strand information.
foreach my $coordset (@coord_sets) {
my ($lend, $rend) = sort {$a<=>$b} @$coordset;
push (@coords, [$lend, $rend]);
}
# put coordsets in ascending order
@coords = sort {$a->[0]<=>$b->[0]} @coords;
## determine non-overlapping maximal spans:
my @spans = shift @coords; # prime it w/ the first pair
while (@coords) {
my $prev_coordset = $spans[$#spans];
my $next_coordset = shift @coords;
my ($prev_lend, $prev_rend) = @$prev_coordset;
my ($next_lend, $next_rend) = @$next_coordset;
if ($next_lend <= $prev_rend) {
if ($next_rend > $prev_lend) {
$prev_coordset->[1] = $next_rend; # reset right boundary
}
}
else {
# don't overlap, so start new span
push (@spans, $next_coordset);
}
}
return (@spans);
}
####
sub sum_coord_spans {
my @coordsets = @_;
my $sum_length = 0;
foreach my $coordset (@coordsets) {
my ($lend, $rend) = @$coordset;
my $length = abs ($rend - $lend) + 1;
$sum_length += $length;
}
return ($sum_length);
}
1; #EOM