3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
6 Licensed under the Apache License, Version 2.0 (the
"License");
7 you may not use
this file except in compliance with the License.
8 You may obtain a copy of the License at
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an
"AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License
for the specific language governing permissions and
16 limitations under the License.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
37 # let's get an interval spanning 9e5 bp and associated it with some data
40 # and another one which overlaps with the previous,
41 # but with scalar associated data
44 warn
"Empty interval(s)\n" if $i1->
is_empty or $i2->is_empty;
45 warn
"Point interval(s)\n" if $i1->is_point or $i2->is_point;
47 if ($i1->intersects($i2)) {
48 print
"I1 and I2 overlap\n";
50 print
"I1 and I2 do not overlap\n";
53 # If an interval is defined with a start > end, then it is assumed
54 # to be spanning the origin on a circular chromosome
55 my $i3 = Bio::EnsEMBL::Utilities::Interval->new(1e5, 1e2);
56 warn
"Interval spans the origin" if $i3->spans_origin;
62 A
class representing an interval defined on a genomic region. Instances of this
63 class can store arbitrarily defined data. If created with start > end, then it
64 is assumed that this interval is on a circular chromosome spanning the origin.
70 package Bio::EnsEMBL::Utils::Interval;
74 use Scalar::Util qw(looks_like_number);
80 Arg [1] : scalar $start
81 The start coordinate of the region
83 The end coordinate of the region
84 Arg [3] : (optional) $data
85 The data associated with the interval, can be anything
87 my $i2 = Bio::EnsEMBL::Utilities::Interval(1e5, 1e2);
88 $i->spans_origin # returns 0
89 $i2->spans_origin # returns 1
90 Description : Constructor. Creates a
new instance
92 Exceptions : Throws an exception
if start and end are not defined.
99 my $class = ref($caller) || $caller;
101 my ($start, $end, $data) = @_;
102 throw 'Must specify interval boundaries [start, end]'
103 unless defined $start and defined $end;
105 my $spans_origin = 0;
110 my $self = bless({ start => $start,
113 spans_origin => $spans_origin},
121 Description : Returns the start coordinate of the region
131 return $self->{start};
137 Description : Returns the end coordinate of the region
153 Description : Returns the data associated with the region
163 return $self->{data};
169 Description : Returns whether
this interval was created spanning zero
170 (more particularly:
if the interval was instantiated with start > end)
180 return $self->{spans_origin};
186 Description : Returns whether or not the interval is empty
196 if ($self->spans_origin) {
197 return ($self->end >= $self->start);
199 return ($self->start >= $self->end);
206 Description : Determines
if the current interval is a single point
216 return $self->
start == $self->end;
221 Arg [1] : scalar, the point coordinate
222 Description : Determines
if the current instance contains the query point
230 my ($self, $point) = @_;
232 return 0
if $self->is_empty or not defined $point;
233 throw 'point must be a number' unless looks_like_number($point);
235 if ($self->spans_origin) {
236 return ($point >= $self->start or $point <= $self->end);
238 return ($point >= $self->start and $point <= $self->end);
245 Description : Determines
if the the instance intersects the given interval
253 my ($self, $interval) = @_;
254 assert_ref($interval,
'Bio::EnsEMBL::Utils::Interval');
256 if ($self->spans_origin and $interval->spans_origin) {
258 } elsif ($self->spans_origin or $interval->spans_origin) {
259 return ($interval->end >= $self->start or $interval->start <= $self->end);
261 return ($self->start <= $interval->end and $interval->start <= $self->end);
268 Description : Checks
if this current interval is entirely to the right of a point
270 More formally, the method will
return true,
if for every point x from
271 the current interval the inequality x > point holds, where point
272 is either a single scalar, or point is the end of another Interval.
273 If spans_origin is
true for either
this Interval or an Interval
274 passed in, then
this method returns
false.
282 my ($self, $other) = @_;
284 return 0 unless defined $other;
286 if ( looks_like_number($other) ) {
288 throw "is_right_of not defined for an interval that spans the origin" :
289 $self->start > $other;
290 } elsif ($self->spans_origin or $other->spans_origin) {
291 throw "is_right_of not defined for an interval that spans the origin";
293 return $self->start > $other->end;
300 Description : Checks
if this current interval is entirely to the left of a point
302 More formally, the method will
return true,
if for every point x from
303 the current interval the inequality x < point holds, where point
304 is either a single scalar, or point is the start of another Interval.
305 If spans_origin is
true for either
this Interval or an Interval
306 passed in, then
this method returns
false
314 my ($self, $other) = @_;
316 return 0 unless defined $other;
318 if ( looks_like_number($other) ) {
320 throw "is_left_of not defined for an interval that spans the origin" :
322 } elsif ($self->spans_origin or $other->spans_origin) {
323 throw "is_left_of not defined for an interval that spans the origin";
325 return $self->end < $other->start;