Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
make it so locations where start > end do not flip but throw exception
  • Loading branch information
Chris Fields committed Feb 29, 2012
1 parent 760ae39 commit f3e4e7f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 44 deletions.
17 changes: 1 addition & 16 deletions lib/Biome/Location/Simple.pm
Expand Up @@ -6,25 +6,10 @@ use 5.010;
use Biome;
use namespace::autoclean;

# TODO - It should be possible to stack roles (have an implementation role
# consume an interface role). The problem is tracable to an issue with Moose,
# so we simply punt for now and supply a simple Role and default methods the
# class can override

with 'Biome::Role::Location::Simple';
with 'Biome::Role::Location::Collection' => {base_name => 'Location', top => 0};
with 'Biome::Role::Location::Collection';
with 'Biome::Role::Location::Locatable';

sub BUILD {
my ($self, $params) = @_;
if ($params->{start} && $params->{end} && ($params->{end} < $params->{start})) {
$self->warn('End is greater than start; flipping strands');
$self->end($params->{start});
$self->start($params->{end});
$self->strand($self->strand * -1);
}
}

__PACKAGE__->meta->make_immutable;

1;
Expand Down
12 changes: 6 additions & 6 deletions lib/Biome/Role/Location/Simple.pm
Expand Up @@ -17,34 +17,34 @@ use Biome::Type::Sequence qw(Sequence_Strand);
has 'start' => (
isa => 'Num',
is => 'rw',
# TODO: may remove these and move to a validate() root method (see below)
trigger => sub {
my ($self, $start) = @_;
my $end = $self->end;
return unless $start && $end;
# could put start<->end reversal here...
$self->throw("Start must be less than end") if $start > $end;
if ($self->location_type eq 'IN-BETWEEN' &&
(abs($end - $start) != 1 )) {
$self->throw("length of location with IN-BETWEEN position type ".
"cannot be larger than 1; got ".abs($end - $start));
}
}
});
);

has 'end' => (
isa => 'Num',
is => 'rw',
# TODO: may remove these and add a validate() root method (see below)
trigger => sub {
my ($self, $end) = @_;
my $start = $self->start;
return unless $start && $end;
# could put start<->end reversal here...
$self->throw("Start must be less than end") if $start > $end;
if ($self->location_type eq 'IN-BETWEEN' &&
(abs($end - $start) != 1) ) {
$self->throw("length of location with IN-BETWEEN position type ".
"cannot be larger than 1; got ".abs($end - $start));
}
}
});
);

has strand => (
isa => Sequence_Strand,
Expand Down
34 changes: 12 additions & 22 deletions t/Location/Simple.t
Expand Up @@ -30,17 +30,7 @@ ok(!$simple->is_fuzzy);

is ($simple->to_string, 'my1:10..20', 'full FT string');

# test that even when end < start that length is always positive
my $f = Biome::Location::Simple->new(
-strict => -1,
-start => 100,
-end => 20,
-strand => 1);

is($f->length, 81, 'Positive length');
is($f->strand,-1, 'Negative strand' );

is ($f->to_string, 'complement(20..100)','full FT string');
my $f;

my $exact = Biome::Location::Simple->new(
-start => 10,
Expand Down Expand Up @@ -117,15 +107,24 @@ is($exact->end_pos_type, 'EXACT');
is($exact->to_string, '<10..20', 'full FT string');

# check exception handling
throws_ok { $exact = $exact = Biome::Location::Simple->new(

# Locations must have start < end or will throw an exception
throws_ok { $exact = Biome::Location::Simple->new(
-start => 100,
-end => 10,
-strand => '+') }
qr/Start must be less than end/,
'Start must be less than end';

throws_ok { $exact = Biome::Location::Simple->new(
-start => 10,
-end => 12,
-start_pos_type => '>',
-strand => '+') }
qr/Start position can't have type AFTER/,
'Check start_pos_type constraint';

throws_ok { $exact = $exact = Biome::Location::Simple->new(
throws_ok { $exact = Biome::Location::Simple->new(
-start => 10,
-end => 12,
-end_pos_type => '<',
Expand Down Expand Up @@ -161,15 +160,6 @@ is($fuzzy->end_pos_type, 'EXACT');
is($fuzzy->seq_id, 'my2');
is($fuzzy->seq_id('my3'), 'my3');

$f = Biome::Location::Simple->new(
-strict => -1,
-start => 100,
-end => 20,
-strand => 1);

is($f->length, 81, 'Positive length');
is($f->strand,-1);

# Test Biome::Location::Simple

ok($exact = Biome::Location::Simple->new(-start => 10,
Expand Down

0 comments on commit f3e4e7f

Please sign in to comment.