Skip to content

Commit

Permalink
get basic union function working by splitting up the build between roles
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Fields committed Feb 20, 2012
1 parent b3de5dc commit 2caef8f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
11 changes: 2 additions & 9 deletions lib/Biome/Role/Location/Locatable.pm
Expand Up @@ -5,6 +5,7 @@ use namespace::clean -except => 'meta';
use List::Util qw(max min reduce);

requires qw(start end strand from_string to_string);
requires qw(_build_union);

# This should not be set here, and should be Biome::Role::Identifiable to make
# the primary ID less Seq-specific
Expand Down Expand Up @@ -133,9 +134,6 @@ sub union {

unshift @given, $self if blessed($self) && $self->start && $self->end;

# cannot give union if a contained location is remote
return if grep { $_->is_remote } (@given);

my $union_strand = $given[0]->strand;

for my $r (@given[1..$#given]) {
Expand All @@ -154,12 +152,7 @@ sub union {

return unless $five_prime && $three_prime;

# TODO: do we assign offsets?
return (blessed $self)->new(-start => $five_prime->start,
-start_pos_type => $five_prime->start_pos_type,
-end => $three_prime->end,
-end_pos_type => $three_prime->end_pos_type,
-strand => $union_strand);
return $self->_build_union($five_prime, $three_prime, $union_strand);
}

### Other methods
Expand Down
15 changes: 11 additions & 4 deletions lib/Biome/Role/Location/Range.pm
Expand Up @@ -44,6 +44,13 @@ sub from_string {

sub flip_strand {$_[0]->strand($_[0]->strand * -1);}

sub _build_union {
my ($self, $five_prime, $three_prime, $strand) = @_;
return (blessed $self)->new(-start => $five_prime->start,
-end => $three_prime->end,
-strand => $strand);
}

1;

__END__
Expand Down Expand Up @@ -280,12 +287,12 @@ BioPerl mailing lists. Your participation is much appreciated.
Patches are always welcome.
=head2 Support
=head2 Support
Please direct usage questions or support issues to the mailing list:
L<bioperl-l@bioperl.org>
rather than to the module maintainer directly. Many experienced and reponsive
experts will be able look at the problem and quickly address it. Please include
a thorough description of the problem with code and data examples if at all
Expand Down
9 changes: 9 additions & 0 deletions lib/Biome/Role/Location/Simple.pm
Expand Up @@ -326,6 +326,15 @@ sub flip_strand {
$self->strand($self->strand * -1);
}

sub _build_union {
my ($self, $five_prime, $three_prime, $strand) = @_;
return (blessed $self)->new(-start => $five_prime->start,
-start_pos_type => $five_prime->start_pos_type,
-end => $three_prime->end,
-end_pos_type => $three_prime->end_pos_type,
-strand => $strand);
}

1;

__END__
Expand Down
30 changes: 18 additions & 12 deletions lib/Biome/Role/Location/Split.pm
Expand Up @@ -34,12 +34,19 @@ has 'auto_expand' => (
default => 1
);

sub add_sub_Location {
$_[0]->add_sub_Locations([$_[1]])
}

sub add_sub_Locations {
my ($self, $newlocs) = @_;
return unless ref $newlocs eq 'ARRAY';

my $locs = $self->locations;

if ($self->auto_expand) {
my $remote = grep {$_->is_remote} @$newlocs;

if ($self->auto_expand && !$remote) {
my $union_loc = $self->union($newlocs);
if (defined($union_loc)) {
for my $att (qw(start end strand start_pos_type end_pos_type)) {
Expand Down Expand Up @@ -81,27 +88,26 @@ Biome::Role::Location::Split - Role describing split locations.
other necessary roles...
}
my $split = Foo->new(-start => 7, -end => 100, -strand => 1);
my $split = Foo->new();
my $loc1 = Bar->new(-start => 1, -end => 50, -strand => -1);
my $loc2 = Bar->new(-start => 75, -end => 150); # no strandedness defined
$split->add_subLocation($loc1);
$split->add_subLocation($loc2);
$split->add_sub_LocationS([$loc1, $loc2]);
Split locations autoexpand to whatever subLocations they contain by
default and the strand is defined by the subLocations. This is b/c this
implementation is just a simple top-level location that contains other
simple Locations, so the borders should match accordingly and the strand
be dictated by them. However, as this is a simple location, the strand
won't be affected.
# Split locations autoexpand to whatever subLocations they contain by
# default, the strand being defined by the subLocations. This is b/c this
# implementation is just a simple top-level location that contains other
# simple Locations, so the borders should match accordingly and the strand
# be dictated by them. However, as this is a simple location, the strand
# won't be affected.
say $split->start; # 1
say $split->end; # 150
say $split->strand; # 0, strand for sublocations is different
If you want to explicitly change the top-level coordinate in some way,
then do so after one has finished adding subLocations.
# If you want to explicitly change the top-level coordinate in some way,
# then do so after one has finished adding subLocations.
$split->start(100);
$split->strand(1);
Expand Down

0 comments on commit 2caef8f

Please sign in to comment.