/
Range.pm6
189 lines (153 loc) · 6.13 KB
/
Range.pm6
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
subset RangeTest of Str where .lc eq any <ignore weak strong>;
# NOTE: much of this is a direct translation of the Bioperl (v5) class. It could
# be feasibly replaced by something infinitely smarter or even reimplemented
# using Perl 6 Range class (though the benefits may not be immediately obvious
# yet for that).
role Bio::Role::Range {
has Int $.start is rw;
has Int $.end is rw;
has Int $.strand is rw;
method length {
die "Must define both start and end" if !$.start.defined | !$.end.defined;
die "End must be larger than start" if $!start > $!end;
return $.end - $.start + 1;
}
method overlaps (Bio::Role::Range:D $range, RangeTest :$test = 'ignore') {
(self!teststranded($range, test => $test) &&
!(($!start > $range.end || $!end < $range.start)))
}
method contains (Bio::Role::Range:D $range, RangeTest :$test = 'ignore') {
(self!teststranded($range, :$test) &&
$range.start >= $!start && $range.end <= $!end)
}
method equals (Bio::Role::Range:D $range, RangeTest :$test = 'ignore') {
(self!teststranded($range, :test($test)) &&
($!start == $range.start && $!end == $range.end))
}
method !teststranded (Bio::Role::Range $self: Bio::Role::Range:D $r, RangeTest :$test) {
my ($s1, $s2) = ($!strand, $r.strand);
given $test {
when 'ignore' {
# strand doesn't matter
return True
}
when 'weak' {
# strand matters only when set
if $s1 == 0 || $s2 == 0 || $s1 == $s2 {
return True
}
}
when 'strong' {
# strand matters and must be set
if $s1 != 0 && $s1 == $s2 {
return True
}
}
}
return False;
}
method intersection ( *@ranges, RangeTest :$test = 'ignore') {
my $intersect;
while @ranges > 0 {
$intersect //= self;
my $compare = @ranges.shift;
last if !$compare.defined;
if !$compare!teststranded($intersect, test => $test) {
return; # this returns a Failure (via the signature)
}
my $start = ($intersect.start, $compare.start).max; # larger of the 2 starts
my $end = ($intersect.end, $compare.end).min; # smaller of the 2 ends
my $intersect_strand = ($intersect.strand == $compare.strand) ??
$compare.strand !! 0;
if $start > $end {
return; # this returns a Failure (via the signature)
} else {
$intersect = self.new(start => $start,
end => $end,
strand => $intersect_strand);
}
}
return $intersect;
}
method union (*@ranges, RangeTest :$test = 'ignore') {
my $union_strand = self.strand; # Strand for the union range object.
#$union_strand = 0 if any(@ranges».strand) != $union_strand;
for @ranges -> $range {
if $range.strand != $union_strand {
$union_strand = 0;
last;
}
}
my $max = (@ranges, self).flat.max: { $^a.end <=> $^b.end };
my $min = (@ranges, self).flat.min: { $^a.start <=> $^b.start };
# what if the end is undef...
return self.new(start => $min.start,
end => $max.end,
strand => $union_strand);
}
# this should have a return type of Array of Bio::Role::Range, but NYI
method subtract (Bio::Role::Range $range, RangeTest :$test = 'ignore') {
if !(self!teststranded($range, :$test)) || !self.overlaps($range, :$test) {
return self
}
# Subtracts everything (empty Range of length = 0 and strand = 0
if (self.equals($range, :$test) || $range.contains(self, :$test)) {
return self.new(start => 0, end => 0, strand => 0);
}
# TODO: oddity with named parameters, see note above
my $int = self.intersection($range, :$test);
my ($start, $end, $strand) = ($int.start, $int.end, $int.strand);
#Subtract intersection from $self
my @outranges = ();
if ($!start < $start) {
@outranges.push(
self.new(
start => $!start,
end => $start - 1,
strand => $!strand,
));
}
if ($!end > $end) {
@outranges.push(
self.new(
start => $end + 1,
end => $!end,
strand => $!strand,
));
}
return @outranges;
}
=begin start
Title : start
Usage : $start = $range.start();
Function: get/set the start of this range
Returns : the start of this range
Args : optionally allows the start to be set
using $range.start = $start
=end start
=begin end
Title : end
Usage : $end = $range.end();
Function: get/set the end of this range
Returns : the end of this range
Args : optionally allows the end to be set
using $range.end = $end
=end end
=begin length
Title : length
Usage : $length = $range.length();
Function: get/set the length of this range
Returns : the length of this range
Args : optionally allows the length to be set
using $range.length = $length
=end length
=begin strand
Title : strand
Usage : $strand = $range.strand;
$range.strand = 1;
Function: get/set the strand of this range
Returns : the strandedness (-1, 0, +1)
Args : optionally allows the strand to be set
using $range->strand($strand)
=end strand
}