Skip to content

Commit

Permalink
get Range compiling again with rakudo
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Fields committed Apr 13, 2012
1 parent 8277344 commit bc60cb5
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 124 deletions.
29 changes: 14 additions & 15 deletions lib/Bio/Role/Range.pm
Expand Up @@ -54,15 +54,15 @@ our Bool method !teststranded (Bio::Role::Range $r, RangeTest :$test = 'ignore')
#
# May be rakudobug, may be the signature (and me), needs checking

our Bio::Role::Range method intersection ( RangeTest :$test = 'ignore', *@ranges of Bio::Role::Range) {
our Bio::Role::Range method intersection ( RangeTest :$test = 'ignore', *@ranges) {
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)
}
Expand All @@ -83,17 +83,17 @@ our Bio::Role::Range method intersection ( RangeTest :$test = 'ignore', *@ranges
return $intersect;
}

our Bio::Role::Range method union ( RangeTest :$test = 'ignore', *@ranges of Bio::Role::Range ) {
our Bio::Role::Range method union ( RangeTest :$test = 'ignore', *@ranges) {
my $union_strand = self.strand; # Strand for the union range object.

# beware the hyperoperator!
$union_strand = 0 if any(@ranges».strand) != $union_strand;

@ranges.unshift(self);

my $max = @ranges.max: { $^a.end <=> $^b.end };
my $min = @ranges.min: { $^a.start <=> $^b.start };

# what if the end is undef...
return self.new(start => $min.start,
end => $max.end,
Expand All @@ -103,22 +103,22 @@ our Bio::Role::Range method union ( RangeTest :$test = 'ignore', *@ranges of Bio
# this should have a return type of Array of Bio::Role::Range, but NYI
our method subtract (Bio::Role::Range $range, RangeTest :$test = 'ignore') {
if !(self!teststranded($range, :$test)) || !self.overlaps($range, :$test) {
return self
return self
}
# Subtracts everything (empty Range of length = 0 and strand = 0
# 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 (self.start < $start) {
@outranges.push(
@outranges.push(
self.new(
start => self.start,
end => $start - 1,
Expand All @@ -131,7 +131,7 @@ our method subtract (Bio::Role::Range $range, RangeTest :$test = 'ignore') {
start => $end + 1,
end => self.end,
strand => self.strand,
));
));
}
return @outranges;
}
Expand Down Expand Up @@ -182,4 +182,3 @@ our method subtract (Bio::Role::Range $range, RangeTest :$test = 'ignore') {
using $range->strand($strand)
=end strand
218 changes: 109 additions & 109 deletions t/Range.t
Expand Up @@ -8,48 +8,48 @@ use Test;

use Bio::Role::Range;

=begin Range tests
Test out simple ranges. Locations will expand on these...
r0 |--------->
r1 |---------|
r2 <---------|
r3 |-->
r4 |--|
r5 <--|
r6 |-------->
r7 |--------|
r8 <--------|
r9 |-------->
r10 |--------|
r11 <--------|
Logic table for overlaps, contains, equals
m = method, o = overlaps() c = contains() e = equals
st = strand tests, i = ignore, w = weak, s = strong
r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11
o c e o c e o c e o c e o c e o c e o c e o c e o c e o c e o c e o c e
iwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiws
r0 111111111110110110100100100111111000110110000100100000111000000110000000100000000000000000000000000000000000
r1 xxxxxxxxx110110110110110110110110000110110000110110000110000000110000000110000000000000000000000000000000000
r2 xxxxxxxxxxxxxxxxxx111111111100100000110110000111111000100000000110000000111000000000000000000000000000000000
r3 xxxxxxxxxxxxxxxxxxxxxxxxxxx111111111110110110100100100111000000110000000100000000000000000000000000000000000
r4 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx110110110110110110110000000110000000110000000000000000000000000000000000
r5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx111111111100000000110000000111000000000000000000000000000000000
r6 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx111111111110110110100100100111000000110000000100000000
r7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx110110110110110110110000000110000000110000000
r8 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx111111111100000000110000000111000000
r9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx111111111110110110100100100
r10 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx110110110110110110
r11 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx111111111
=end Range tests
##=begin Range tests
##
##Test out simple ranges. Locations will expand on these...
##
## r0 |--------->
## r1 |---------|
## r2 <---------|
##
## r3 |-->
## r4 |--|
## r5 <--|
##
## r6 |-------->
## r7 |--------|
## r8 <--------|
##
## r9 |-------->
## r10 |--------|
## r11 <--------|
##
##Logic table for overlaps, contains, equals
##
##m = method, o = overlaps() c = contains() e = equals
##st = strand tests, i = ignore, w = weak, s = strong
##
## r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11
## o c e o c e o c e o c e o c e o c e o c e o c e o c e o c e o c e o c e
## iwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiwsiws
##r0 111111111110110110100100100111111000110110000100100000111000000110000000100000000000000000000000000000000000
##r1 xxxxxxxxx110110110110110110110110000110110000110110000110000000110000000110000000000000000000000000000000000
##r2 xxxxxxxxxxxxxxxxxx111111111100100000110110000111111000100000000110000000111000000000000000000000000000000000
##r3 xxxxxxxxxxxxxxxxxxxxxxxxxxx111111111110110110100100100111000000110000000100000000000000000000000000000000000
##r4 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx110110110110110110110000000110000000110000000000000000000000000000000000
##r5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx111111111100000000110000000111000000000000000000000000000000000
##r6 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx111111111110110110100100100111000000110000000100000000
##r7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx110110110110110110110000000110000000110000000
##r8 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx111111111100000000110000000111000000
##r9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx111111111110110110100100100
##r10 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx110110110110110110
##r11 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx111111111
##
##=end Range tests

class MyRange does Bio::Role::Range {
our method Str {
Expand Down Expand Up @@ -99,7 +99,7 @@ r7 => '111111000111111000100100000100100000100100000',
r8 => '111111111100000000100100000100100100',
r9 => '111111111111111000111000000',
r10 => '111111000111111000',
r11 => '111111111',
r11 => '111111111',
);

for 0..@ranges.end -> $i {
Expand All @@ -116,39 +116,39 @@ for 0..@ranges.end -> $i {
}
}

=begin Geometric tests
With these ranges:
r0 |--------->
r1 |---------|
r2 <---------|
r3 |-->
r4 |--|
r5 <--|
r6 |-------->
r7 |--------|
r8 <--------|
r9 |-------->
r10 |--------|
r11 <--------|
intersection of r0, r3, r6 => [75,75,1] for all st
intersection of r6, r9 => [101, 125, 1] for all st
intersection of r6, r10 => [101, 125, 0] for ignore, weak, undef for strong
intersection of r6, r11 => [101, 125, 0] for ignore, undef for weak & strong
intersection of r0, r6, r9 => undef for all
union of r0, r3, r6 => [1,125,1] for all st
union of r6, r9 => [75, 150, 1] for all st
union of r6, r10 => [75, 150, 0] for all st
union of r6, r11 => [75, 150, 0] for all st
union of r0, r6, r9 => [1,150,1] for all st
=end Geometric tests
##=begin Geometric tests
##
##With these ranges:
##
## r0 |--------->
## r1 |---------|
## r2 <---------|
##
## r3 |-->
## r4 |--|
## r5 <--|
##
## r6 |-------->
## r7 |--------|
## r8 <--------|
##
## r9 |-------->
## r10 |--------|
## r11 <--------|
##
## intersection of r0, r3, r6 => [75,75,1] for all st
## intersection of r6, r9 => [101, 125, 1] for all st
## intersection of r6, r10 => [101, 125, 0] for ignore, weak, undef for strong
## intersection of r6, r11 => [101, 125, 0] for ignore, undef for weak & strong
## intersection of r0, r6, r9 => undef for all
##
## union of r0, r3, r6 => [1,125,1] for all st
## union of r6, r9 => [75, 150, 1] for all st
## union of r6, r10 => [75, 150, 0] for all st
## union of r6, r11 => [75, 150, 0] for all st
## union of r0, r6, r9 => [1,150,1] for all st
##
##=end Geometric tests

my %geo_tests =
('0,3,6' => { # intersection union
Expand Down Expand Up @@ -189,57 +189,57 @@ for %geo_tests.keys.sort -> $set {
}
}

=begin Subtraction
r0 |--------->
r1 |---------|
r2 <---------|
r3 |-->
r4 |--|
r5 <--|
r6 |-------->
r7 |--------|
r8 <--------|
r9 |-------->
r10 |--------|
r11 <--------|
subtraction of r3 from r0 => two Ranges [1, 24, 1] and [76, 100, 1]
subtraction of r0 from r3 => one Range [0,0,1] - empty
subtraction of r6 from r0 => one Range [1, 74, 1]
subtraction of r0 from r6 => one Range [101,125,1]
subtraction of r9 from r6 => one Range [75,100,1]
subtraction of r6 from r9 => one Range [126,150,1]
subtraction of r9 from r0 => original (or clone?) r0 Range [1, 100, 1]
subtraction of r0 from r9 => original (or clone?) r9 Range [101,150,1]
=end Subtraction
##=begin Subtraction
##
## r0 |--------->
## r1 |---------|
## r2 <---------|
##
## r3 |-->
## r4 |--|
## r5 <--|
##
## r6 |-------->
## r7 |--------|
## r8 <--------|
##
## r9 |-------->
## r10 |--------|
## r11 <--------|
##
## subtraction of r3 from r0 => two Ranges [1, 24, 1] and [76, 100, 1]
## subtraction of r0 from r3 => one Range [0,0,1] - empty
## subtraction of r6 from r0 => one Range [1, 74, 1]
## subtraction of r0 from r6 => one Range [101,125,1]
## subtraction of r9 from r6 => one Range [75,100,1]
## subtraction of r6 from r9 => one Range [126,150,1]
## subtraction of r9 from r0 => original (or clone?) r0 Range [1, 100, 1]
## subtraction of r0 from r9 => original (or clone?) r9 Range [101,150,1]
##
##=end Subtraction

my %subtract_tests = ( # rx->subtract(ry) ry->subtract(rx)
'0,3' => {
'0,3' => {
'strong' => ['(1, 24) strand=1,(76, 100) strand=1','(0, 0) strand=0'],
'weak' => ['(1, 24) strand=1,(76, 100) strand=1','(0, 0) strand=0'],
'ignore' => ['(1, 24) strand=1,(76, 100) strand=1','(0, 0) strand=0'],
},
'0,4' => {
'0,4' => {
'strong' => ['(1, 100) strand=1', '(25, 75) strand=0'],
'weak' => ['(1, 24) strand=1,(76, 100) strand=1','(0, 0) strand=0'],
'ignore' => ['(1, 24) strand=1,(76, 100) strand=1','(0, 0) strand=0'],
},
'0,6' => {
'0,6' => {
'strong' => ['(1, 74) strand=1', '(101, 125) strand=1'],
'weak' => ['(1, 74) strand=1', '(101, 125) strand=1'],
'ignore' => ['(1, 74) strand=1', '(101, 125) strand=1'],
},
'6,9' => {
'6,9' => {
'strong' => ['(75, 100) strand=1', '(126, 150) strand=1'],
'weak' => ['(75, 100) strand=1', '(126, 150) strand=1'],
'ignore' => ['(75, 100) strand=1', '(126, 150) strand=1'],
},
'0,9' => {
'0,9' => {
'strong' => ['(1, 100) strand=1', '(101, 150) strand=1'],
'weak' => ['(1, 100) strand=1', '(101, 150) strand=1'],
'ignore' => ['(1, 100) strand=1', '(101, 150) strand=1'],
Expand Down

0 comments on commit bc60cb5

Please sign in to comment.