| 1 | =head1 LICENSE | ||
| 2 | |||
| 3 | See the NOTICE file distributed with this work for additional information | ||
| 4 | regarding copyright ownership. | ||
| 5 | |||
| 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 | ||
| 9 | |||
| 10 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 11 | |||
| 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. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | |||
| 21 | =head1 CONTACT | ||
| 22 | |||
| 23 | Please email comments or questions to the public Ensembl | ||
| 24 | developers list at <http://lists.ensembl.org/mailman/listinfo/dev>. | ||
| 25 | |||
| 26 | Questions may also be sent to the Ensembl help desk at | ||
| 27 | <http://www.ensembl.org/Help/Contact>. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | =head1 NAME | ||
| 32 | |||
| 33 | Bio::EnsEMBL::Utils::Interval | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | # let's get an interval spanning 9e5 bp and associated it with some data | ||
| 38 | my $i1 = Bio::EnsEMBL::Utils::Interval->new(1e5, 1e6, { 'key1' => 'value1', 'key2' => 'value2' }); | ||
| 39 | |||
| 40 | # and another one which overlaps with the previous, | ||
| 41 | # but with scalar associated data | ||
| 42 | my $i2 = Bio::EnsEMBL::Utils::Interval->new(2e5, 3e5, 'a string' ); | ||
| 43 | |||
| 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; | ||
| 46 | |||
| 47 | if ($i1->intersects($i2)) { | ||
| 48 | print "I1 and I2 overlap\n"; | ||
| 49 | } else { | ||
| 50 | print "I1 and I2 do not overlap\n"; | ||
| 51 | } | ||
| 52 | |||
| 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; | ||
| 57 | |||
| 58 | etc. | ||
| 59 | |||
| 60 | =head1 DESCRIPTION | ||
| 61 | |||
| 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. | ||
| 65 | |||
| 66 | =head1 METHODS | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | package Bio::EnsEMBL::Utils::Interval; | ||
| 71 | $Bio::EnsEMBL::Utils::Interval::VERSION = '114.0.0_50'; # TRIAL | ||
| 72 | $Bio::EnsEMBL::Utils::Interval::VERSION = '114.0.050'; | ||
| 73 | use strict; | ||
| 74 | |||
| 75 | use Scalar::Util qw(looks_like_number); | ||
| 76 | use Bio::EnsEMBL::Utils::Scalar qw(assert_ref); | ||
| 77 | use Bio::EnsEMBL::Utils::Exception qw(throw); | ||
| 78 | |||
| 79 | =head2 new | ||
| 80 | |||
| 81 | Arg [1] : scalar $start | ||
| 82 | The start coordinate of the region | ||
| 83 | Arg [2] : scalar $end | ||
| 84 | The end coordinate of the region | ||
| 85 | Arg [3] : (optional) $data | ||
| 86 | The data associated with the interval, can be anything | ||
| 87 | Example : my $i = Bio::EnsEMBL::Utils::Interval(1e2, 2e2, { 'key' => 'value' }); | ||
| 88 | my $i2 = Bio::EnsEMBL::Utilities::Interval(1e5, 1e2); | ||
| 89 | $i->spans_origin # returns 0 | ||
| 90 | $i2->spans_origin # returns 1 | ||
| 91 | Description : Constructor. Creates a new instance | ||
| 92 | Returntype : Bio::EnsEMBL::Utils::Interval | ||
| 93 | Exceptions : Throws an exception if start and end are not defined. | ||
| 94 | Caller : general | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub new { | ||
| 99 | my $caller = shift; | ||
| 100 | my $class = ref($caller) || $caller; | ||
| 101 | |||
| 102 | my ($start, $end, $data) = @_; | ||
| 103 | throw 'Must specify interval boundaries [start, end]' | ||
| 104 | unless defined $start and defined $end; | ||
| 105 | |||
| 106 | my $spans_origin = 0; | ||
| 107 | if ($start > $end) { | ||
| 108 | $spans_origin = 1; | ||
| 109 | } | ||
| 110 | |||
| 111 | my $self = bless({ start => $start, | ||
| 112 | end => $end, | ||
| 113 | data => $data , | ||
| 114 | spans_origin => $spans_origin}, | ||
| 115 | $class); | ||
| 116 | return $self; | ||
| 117 | } | ||
| 118 | |||
| 119 | =head2 start | ||
| 120 | |||
| 121 | Arg [] : none | ||
| 122 | Description : Returns the start coordinate of the region | ||
| 123 | Returntype : scalar | ||
| 124 | Exceptions : none | ||
| 125 | Caller : general | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub start { | ||
| 130 | my $self = shift; | ||
| 131 | |||
| 132 | return $self->{start}; | ||
| 133 | } | ||
| 134 | |||
| 135 | =head2 end | ||
| 136 | |||
| 137 | Arg [] : none | ||
| 138 | Description : Returns the end coordinate of the region | ||
| 139 | Returntype : scalar | ||
| 140 | Exceptions : none | ||
| 141 | Caller : general | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub end { | ||
| 146 | my $self = shift; | ||
| 147 | |||
| 148 | return $self->{end}; | ||
| 149 | } | ||
| 150 | |||
| 151 | =head2 data | ||
| 152 | |||
| 153 | Arg [] : none | ||
| 154 | Description : Returns the data associated with the region | ||
| 155 | Returntype : Any | ||
| 156 | Exceptions : none | ||
| 157 | Caller : general | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub data { | ||
| 162 | my $self = shift; | ||
| 163 | |||
| 164 | return $self->{data}; | ||
| 165 | } | ||
| 166 | |||
| 167 | =head2 spans_origin | ||
| 168 | |||
| 169 | Arg [] : none | ||
| 170 | Description : Returns whether this interval was created spanning zero | ||
| 171 | (more particularly: if the interval was instantiated with start > end) | ||
| 172 | Returntype : boolean | ||
| 173 | Exceptions : none | ||
| 174 | Caller : general | ||
| 175 | |||
| 176 | =cut | ||
| 177 | |||
| 178 | sub spans_origin { | ||
| 179 | my $self = shift; | ||
| 180 | |||
| 181 | return $self->{spans_origin}; | ||
| 182 | } | ||
| 183 | |||
| 184 | =head2 is_empty | ||
| 185 | |||
| 186 | Arg [] : none | ||
| 187 | Description : Returns whether or not the interval is empty | ||
| 188 | Returntype : boolean | ||
| 189 | Exceptions : none | ||
| 190 | Caller : general | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | sub is_empty { | ||
| 195 | my $self = shift; | ||
| 196 | |||
| 197 | if ($self->spans_origin) { | ||
| 198 | return ($self->end >= $self->start); | ||
| 199 | } else { | ||
| 200 | return ($self->start >= $self->end); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | =head2 is_point | ||
| 205 | |||
| 206 | Arg [] : none | ||
| 207 | Description : Determines if the current interval is a single point | ||
| 208 | Returntype : boolean | ||
| 209 | Exceptions : none | ||
| 210 | Caller : general | ||
| 211 | |||
| 212 | =cut | ||
| 213 | |||
| 214 | sub is_point { | ||
| 215 | my $self = shift; | ||
| 216 | |||
| 217 | return $self->start == $self->end; | ||
| 218 | } | ||
| 219 | |||
| 220 | =head2 contains | ||
| 221 | |||
| 222 | Arg [1] : scalar, the point coordinate | ||
| 223 | Description : Determines if the current instance contains the query point | ||
| 224 | Returntype : boolean | ||
| 225 | Exceptions : none | ||
| 226 | Caller : general | ||
| 227 | |||
| 228 | =cut | ||
| 229 | |||
| 230 | sub contains { | ||
| 231 | my ($self, $point) = @_; | ||
| 232 | |||
| 233 | return 0 if $self->is_empty or not defined $point; | ||
| 234 | throw 'point must be a number' unless looks_like_number($point); | ||
| 235 | |||
| 236 | if ($self->spans_origin) { | ||
| 237 | return ($point >= $self->start or $point <= $self->end); | ||
| 238 | } else { | ||
| 239 | return ($point >= $self->start and $point <= $self->end); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | =head2 intersects | ||
| 244 | |||
| 245 | Arg [1] : An instance of Bio::EnsEMBL::Utils::Interval | ||
| 246 | Description : Determines if the the instance intersects the given interval | ||
| 247 | Returntype : boolean | ||
| 248 | Exceptions : none | ||
| 249 | Caller : general | ||
| 250 | |||
| 251 | =cut | ||
| 252 | |||
| 253 | sub intersects { | ||
| 254 | my ($self, $interval) = @_; | ||
| 255 | assert_ref($interval, 'Bio::EnsEMBL::Utils::Interval'); | ||
| 256 | |||
| 257 | if ($self->spans_origin and $interval->spans_origin) { | ||
| 258 | return 1; | ||
| 259 | } elsif ($self->spans_origin or $interval->spans_origin) { | ||
| 260 | return ($interval->end >= $self->start or $interval->start <= $self->end); | ||
| 261 | } else { | ||
| 262 | return ($self->start <= $interval->end and $interval->start <= $self->end); | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | =head2 is_right_of | ||
| 267 | |||
| 268 | Arg [1] : An instance of Bio::EnsEMBL::Utils::Interval or a scalar | ||
| 269 | Description : Checks if this current interval is entirely to the right of a point | ||
| 270 | or Interval. | ||
| 271 | More formally, the method will return true, if for every point x from | ||
| 272 | the current interval the inequality x > point holds, where point | ||
| 273 | is either a single scalar, or point is the end of another Interval. | ||
| 274 | If spans_origin is true for either this Interval or an Interval | ||
| 275 | passed in, then this method returns false. | ||
| 276 | Returntype : boolean | ||
| 277 | Exceptions : none | ||
| 278 | Caller : general | ||
| 279 | |||
| 280 | =cut | ||
| 281 | |||
| 282 | sub is_right_of { | ||
| 283 | my ($self, $other) = @_; | ||
| 284 | |||
| 285 | return 0 unless defined $other; | ||
| 286 | |||
| 287 | if ( looks_like_number($other) ) { | ||
| 288 | return $self->spans_origin ? | ||
| 289 | throw "is_right_of not defined for an interval that spans the origin" : | ||
| 290 | $self->start > $other; | ||
| 291 | } elsif ($self->spans_origin or $other->spans_origin) { | ||
| 292 | throw "is_right_of not defined for an interval that spans the origin"; | ||
| 293 | } else { | ||
| 294 | return $self->start > $other->end; | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | =head2 is_left_of | ||
| 299 | |||
| 300 | Arg [1] : An instance of Bio::EnsEMBL::Utils::Interval or a scalar | ||
| 301 | Description : Checks if this current interval is entirely to the left of a point | ||
| 302 | or Interval. | ||
| 303 | More formally, the method will return true, if for every point x from | ||
| 304 | the current interval the inequality x < point holds, where point | ||
| 305 | is either a single scalar, or point is the start of another Interval. | ||
| 306 | If spans_origin is true for either this Interval or an Interval | ||
| 307 | passed in, then this method returns false | ||
| 308 | Returntype : boolean | ||
| 309 | Exceptions : none | ||
| 310 | Caller : general | ||
| 311 | |||
| 312 | =cut | ||
| 313 | |||
| 314 | sub is_left_of { | ||
| 315 | my ($self, $other) = @_; | ||
| 316 | |||
| 317 | return 0 unless defined $other; | ||
| 318 | |||
| 319 | if ( looks_like_number($other) ) { | ||
| 320 | return $self->spans_origin ? | ||
| 321 | throw "is_left_of not defined for an interval that spans the origin" : | ||
| 322 | $self->end < $other; | ||
| 323 | } elsif ($self->spans_origin or $other->spans_origin) { | ||
| 324 | throw "is_left_of not defined for an interval that spans the origin"; | ||
| 325 | } else { | ||
| 326 | return $self->end < $other->start; | ||
| 327 | } | ||
| 328 | } | ||
| 329 | |||
| 330 | 1; | ||
| 331 |