From 5ef84030370e00881dd4cf6080d82dd108385c58 Mon Sep 17 00:00:00 2001 From: Kieron Taylor <ktaylor@ebi.ac.uk> Date: Wed, 6 Mar 2013 16:29:07 +0000 Subject: [PATCH] Added constrain_to_seq_region for web rendering reasons, where requested slice->expand were flying off the end of chromosomes. --- modules/Bio/EnsEMBL/Slice.pm | 31 +++++++++++++++++++++++++++++-- modules/t/slice.t | 11 +++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/modules/Bio/EnsEMBL/Slice.pm b/modules/Bio/EnsEMBL/Slice.pm index 9b4f11c694..e20864779d 100644 --- a/modules/Bio/EnsEMBL/Slice.pm +++ b/modules/Bio/EnsEMBL/Slice.pm @@ -1194,6 +1194,33 @@ sub expand { return bless \%new_slice, ref($self); } ## end sub expand +=head2 constrain_to_seq_region + Example : $new_slice = $slice->expand(1000,10000); + $new_slice = $new_slice->constrain_to_seq_region(); + Description: Used to prevent overly zealous expand calls going off the end of + the sequence region. It contracts the start and end where needed + and produces a slice copy with the tweaked coordinates. + Returntype : Bio::EnsEMBL::Slice +=cut + +sub constrain_to_seq_region { + my ($self) = @_; + # circular calculations should already be taken care of + if ($self->is_circular) {return $self} + my $new_start = $self->start; + my $new_end = $self->end; + + my $seq_region = $self->seq_region_Slice; + + if ($new_start < $seq_region->start) {$new_start = $seq_region->start} + if ($new_end > $seq_region->end) {$new_end = $seq_region->end} + + my %new_slice = %$self; + $new_slice{'start'} = $new_start; + $new_slice{'end'} = $new_end; + + return bless \%new_slice, ref($self); +} =head2 sub_Slice @@ -1202,8 +1229,8 @@ sub expand { Arg 2 : int $end Arge [3] : int $strand Example : none - Description: Makes another Slice that covers only part of this slice - If a slice is requested which lies outside of the boundaries + Description: Makes another Slice that covers only part of this Slice + If a Slice is requested which lies outside of the boundaries of this function will return undef. This means that behaviour will be consistant whether or not the slice is attached to the database (i.e. if there is attached sequence diff --git a/modules/t/slice.t b/modules/t/slice.t index 22baf6ad0c..ccb8b577d6 100644 --- a/modules/t/slice.t +++ b/modules/t/slice.t @@ -188,6 +188,17 @@ ok(($clone->start == 1) && ($clone->end() == $len + 1000)); $clone = $clone->expand(-1000, 0); ok(($clone->start == 1001) && ($clone->end() == $len + 1000)); +# +# Test constrain_to_seq_region +# +my $tidy_clone = $clone->expand(1000000,10000000); +$tidy_clone = $tidy_clone->constrain_to_seq_region; +ok($tidy_clone->start == 1 && $tidy_clone->end == 84710, 'Huge expand call truncates nicely'); + +$tidy_clone = $clone->expand(0,-1000); +$tidy_clone = $tidy_clone->constrain_to_seq_region; +note($tidy_clone->start."-".$tidy_clone->end()); +ok(($tidy_clone->start == 1001) && ($tidy_clone->end() == 84710), 'constrain_to_seq_region does no harm'); # # Test Slice::invert -- GitLab