Skip to content

Commit

Permalink
various fixes to Ranges including some test brainos; now passing Rang…
Browse files Browse the repository at this point in the history
…e tests
  • Loading branch information
Chris Fields committed Sep 4, 2012
1 parent 8b70f83 commit 4df69ff
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 143 deletions.
17 changes: 9 additions & 8 deletions lib/Bio/Role/Range.pm
Expand Up @@ -13,32 +13,33 @@ our Int method length {
}

our Bool method overlaps (Bio::Role::Range $range, :$test = 'ignore') {
(self!teststranded($range, test => $test) && !((self.start() > $range.end() || self.end() < $range.start())))
?? True !! False;
(self!teststranded($range, test => $test) &&
!((self.start() > $range.end() || self.end() < $range.start())))
}

our Bool method contains (Bio::Role::Range $range, :$test = 'ignore') {
(self!teststranded($range, :$test) && $range.start() >= self.start() && $range.end() <= self.end())
?? True !! False;
(self!teststranded($range, :$test) &&
$range.start() >= self.start() && $range.end() <= self.end())
}

our Bool method equals (Bio::Role::Range $range, :$test = 'ignore') {
(self!teststranded($range, :$test) && self.start() == $range.start() && self.end() == $range.end())
?? True !! False;
(self!teststranded($range, :test($test)) &&
(self.start == $range.start && self.end == $range.end))
}

our Bool method !teststranded (Bio::Role::Range $self: Bio::Role::Range $r, :$test = 'ignore') {
my ($s1, $s2) = (self.strand, $r.strand);
given $test {
when 'ignore' {
return True
}
when 'weak' {
if self.strand == 0 || $r.strand == 0 || self.strand == $r.strand {
if $s1 == 0 || $s2 == 0 || $s1 == $s2 {
return True
}
}
when 'strong' {
if self.strand != 0 && self.strand == $r.strand {
if $s1 != 0 && $s1 == $s2 {
return True
}
}
Expand Down
268 changes: 133 additions & 135 deletions t/Range.t
Expand Up @@ -33,21 +33,21 @@ use Bio::Role::Range;
##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
## 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
## iwsiwsiws|iwsiwsiws|iwsiwsiws|iwsiwsiws|iwsiwsiws|iwsiwsiws|iwsiwsiws|iwsiwsiws|iwsiwsiws|iwsiwsiws|iwsiwsiws|iwsiwsiws
##r0 111111111|110110110|100100100|111111000|110110000|100100000|111000000|110000000|100000000|000000000|000000000|000000000
##r1 xxxxxxxxx|110110110|110110110|110110000|110110000|110110000|110000000|110000000|110000000|000000000|000000000|000000000
##r2 xxxxxxxxx|xxxxxxxxx|111111111|100100000|110110000|111111000|100000000|110000000|111000000|000000000|000000000|000000000
##r3 xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|111111111|110110110|100100100|111000000|110000000|100000000|000000000|000000000|000000000
##r4 xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|110110110|110110110|110000000|110000000|110000000|000000000|000000000|000000000
##r5 xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|111111111|100000000|110000000|111000000|000000000|000000000|000000000
##r6 xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|111111111|110110110|100100100|111000000|110000000|100000000
##r7 xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|110110110|110110110|110000000|110000000|110000000
##r8 xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|111111111|100000000|110000000|111000000
##r9 xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|111111111|110110110|100100100
##r10 xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|110110110|110110110
##r11 xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|111111111
##
##=end Range tests

Expand Down Expand Up @@ -88,18 +88,18 @@ is(@ranges[11].length, 50);
# test overlaps, contains, equals, cover all variations

my %map = (
r0 => '111111111110110110100100100111111000110110000100100000111000000110000000100000000000000000000000000000000000',
#r1 => '111111000111111000110110000110110000110110000100100000100100000100100000000000000000000000000000000',
#r2 => '111111111110000000110110000110110110100000000100100000100100100000000000000000000000000000',
#r3 => '111111111111111000111000000100100100100100000100000000000000000000000000000000000',
#r4 => '111111000111111000100100000100100000100100000000000000000000000000000000',
#r5 => '111111111100000000100100000100100100000000000000000000000000000',
#r6 => '111111111111111000111000000100100100100100000100000000',
#r7 => '111111000111111000100100000100100000100100000',
#r8 => '111111111100000000100100000100100100',
#r9 => '111111111111111000111000000',
#r10 => '111111000111111000',
#r11 => '111111111',
r0 => '111111111|110110110|100100100|111111000|110110000|100100000|111000000|110000000|100000000|000000000|000000000|000000000',
r1 => '110110110|110110110|110110000|110110000|110110000|110000000|110000000|110000000|000000000|000000000|000000000',
r2 => '111111111|100100000|110110000|111111000|100000000|110000000|111000000|000000000|000000000|000000000',
r3 => '111111111|110110110|100100100|111000000|110000000|100000000|000000000|000000000|000000000',
r4 => '110110110|110110110|110000000|110000000|110000000|000000000|000000000|000000000',
r5 => '111111111|100000000|110000000|111000000|000000000|000000000|000000000',
r6 => '111111111|110110110|100100100|111000000|110000000|100000000',
r7 => '110110110|110110110|110000000|110000000|110000000',
r8 => '111111111|100000000|110000000|111000000',
r9 => '111111111|110110110|100100100',
r10 => '110110110|110110110',
r11 => '111111111',
);

for 0..@ranges.end -> $i {
Expand All @@ -108,13 +108,11 @@ for 0..@ranges.end -> $i {
for $i..@ranges.end -> $j {
my $r1 = @ranges[$i];
my $r2 = @ranges[$j];
for <ignore weak strong> -> $test {
my $current = ?@tests.shift;
is($r1.overlaps($r2, test => $test ), ?$current, ~$r1 ~ ' overlaps ' ~ $r2 ~ ", $test: "~ $current);
$current = ?@tests.shift;
is($r1.contains($r2, :test<$test>), ?$current, ~$r1 ~ ' contains ' ~ $r2 ~ ", $test: " ~ $current);
$current = ?@tests.shift;
is($r1.equals($r2, :test<$test>), ?$current, ~$r1 ~ ' equals ' ~ $r2 ~ ", $test: " ~ $current);
for <overlaps contains equals> -> $method {
for <ignore weak strong> -> $test {
my $current = ?@tests.shift;
is($r1."$method"($r2, :test($test) ), $current, "$r1 $method $r2, $test: $current");
}
}
}
}
Expand Down Expand Up @@ -153,110 +151,110 @@ for 0..@ranges.end -> $i {
#
#=end Geometric tests

#my %geo_tests =
#('0,3,6' => { # intersection union
# 'strong' => ['(75, 75) strand=1', '(1, 125) strand=1'],
# 'weak' => ['(75, 75) strand=1', '(1, 125) strand=1'],
# 'ignore' => ['(75, 75) strand=1', '(1, 125) strand=1'],
# },
# '6,9' => {
# 'strong' => ['(101, 125) strand=1', '(75, 150) strand=1'],
# 'weak' => ['(101, 125) strand=1', '(75, 150) strand=1'],
# 'ignore' => ['(101, 125) strand=1', '(75, 150) strand=1'],
# },
# '6,10' => {
# 'strong' => ['', '(75, 150) strand=0'],
# 'weak' => ['(101, 125) strand=0', '(75, 150) strand=0'],
# 'ignore' => ['(101, 125) strand=0', '(75, 150) strand=0'],
# },
# '6,11' => {
# 'strong' => ['', '(75, 150) strand=0'],
# 'weak' => ['', '(75, 150) strand=0'],
# 'ignore' => ['(101, 125) strand=0', '(75, 150) strand=0'],
# },
# '0,6,9' => {
# 'strong' => ['', '(1, 150) strand=1'],
# 'weak' => ['', '(1, 150) strand=1'],
# 'ignore' => ['', '(1, 150) strand=1'],
# },
#);
my %geo_tests =
('0,3,6' => { # intersection union
'strong' => ['(75, 75) strand=1', '(1, 125) strand=1'],
'weak' => ['(75, 75) strand=1', '(1, 125) strand=1'],
'ignore' => ['(75, 75) strand=1', '(1, 125) strand=1'],
},
'6,9' => {
'strong' => ['(101, 125) strand=1', '(75, 150) strand=1'],
'weak' => ['(101, 125) strand=1', '(75, 150) strand=1'],
'ignore' => ['(101, 125) strand=1', '(75, 150) strand=1'],
},
'6,10' => {
'strong' => ['', '(75, 150) strand=0'],
'weak' => ['(101, 125) strand=0', '(75, 150) strand=0'],
'ignore' => ['(101, 125) strand=0', '(75, 150) strand=0'],
},
'6,11' => {
'strong' => ['', '(75, 150) strand=0'],
'weak' => ['', '(75, 150) strand=0'],
'ignore' => ['(101, 125) strand=0', '(75, 150) strand=0'],
},
'0,6,9' => {
'strong' => ['', '(1, 150) strand=1'],
'weak' => ['', '(1, 150) strand=1'],
'ignore' => ['', '(1, 150) strand=1'],
},
);

for %geo_tests.keys.sort -> $set {
my @rest = @ranges[$set.split(',')];
my $primary = @rest.shift;
for <ignore weak strong> -> $test {
my $int = $primary.intersection(@rest, :$test);
my $union = $primary.union(@rest, :$test);
is(($int.defined ?? $int.Str !! ''), %geo_tests{$set}{$test}[0], "intersection of $set, $test");
is(($union.defined ?? $union.Str !! ''), %geo_tests{$set}{$test}[1], "union of $set, $test");
}
}

#=begin Subtraction
#
#for %geo_tests.keys.sort -> $set {
# my @rest = @ranges[$set.split(',')];
# my $primary = @rest.shift;
# for <ignore weak strong> -> $test {
# my $int = $primary.intersection(@rest, :$test);
# my $union = $primary.union(@rest, :$test);
# is(($int.defined ?? $int.Str !! ''), %geo_tests{$set}{$test}[0], "intersection of $set, $test");
# is(($union.defined ?? $union.Str !! ''), %geo_tests{$set}{$test}[1], "union of $set, $test");
# }
#}
# r0 |--------->
# r1 |---------|
# r2 <---------|
#
##=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
# r3 |-->
# r4 |--|
# r5 <--|
#
#my %subtract_tests = ( # rx->subtract(ry) ry->subtract(rx)
# '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' => {
# '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' => {
# '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' => {
# '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' => {
# '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'],
# },
#);
# r6 |-------->
# r7 |--------|
# r8 <--------|
#
#for %subtract_tests.keys.sort -> $set {
# my ($r1, $r2) = @ranges[split(',',$set)];
# for <ignore weak strong> -> $st {
# my @sub1 = $r1.subtract($r2, test => $st);
# my @sub2 = $r2.subtract($r1, test => $st);
# is(join(',', @sub1».Str), %subtract_tests{$set}{$st}[0], "subtract" );
# is(join(',', @sub2».Str), %subtract_tests{$set}{$st}[1], "subtract");
# }
#}
# 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' => {
'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' => {
'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' => {
'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' => {
'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' => {
'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'],
},
);

for %subtract_tests.keys.sort -> $set {
my ($r1, $r2) = @ranges[split(',',$set)];
for <ignore weak strong> -> $st {
my @sub1 = $r1.subtract($r2, test => $st);
my @sub2 = $r2.subtract($r1, test => $st);
is(join(',', @sub1».Str), %subtract_tests{$set}{$st}[0], "subtract" );
is(join(',', @sub2».Str), %subtract_tests{$set}{$st}[1], "subtract");
}
}

done();

0 comments on commit 4df69ff

Please sign in to comment.