EnsEMBL/Utils/Tree/Interval/Immutable.pm untested

filter: Statementstmt 0.0% 0 / 165 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 8 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 257 CC 27 SCAR 36.4
CC
27
Cov
0%
query
110.0
_divide_intervals
42.0
_query_point
30.0
CRAP 38.0
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::Tree::Interval::Immutable
34  
35 =head1 SYNOPSIS
36  
37 # define a set of intervals to be added to the tree
38 my $intervals = [ Bio::EnsEMBL::Utils::Interval->new(121626874, 122092717),
39 Bio::EnsEMBL::Utils::Interval->new(121637917, 121658918),
40 Bio::EnsEMBL::Utils::Interval->new(122096077, 124088369) ];
41  
42 # initialise the tree with the above intervals
43 my $tree = Bio::EnsEMBL::Utils::Tree::Interval::Immutable->new($intervals);
44  
45 # point query
46 my $results = $tree->query(121779004);
47 if (scalar @$results) {
48 print "Intervals contain 121779004\n";
49 }
50  
51 # same query, but use interval query
52 my $results = $tree->query(121779004, 121779004);
53 if (scalar @$results) {
54 print "Found containing interval: [", $result->[0]->start, ', ', $result->[0]->end, "\n";
55 }
56  
57 =head1 DESCRIPTION
58  
59 An implementation of an immutable interval tree. Immutable means the tree is
60 initialised with a fixed set of intervals at creation time. Intervals cannot
61 be added to or removed from the tree during its life cycle.
62  
63 Implementation heavily inspired by https://github.com/tylerkahn/intervaltree-python
64  
65 This implementation does not support Intervals having a start > end - i.e.
66 intervals spanning the origin of a circular chromosome.
67  
68 =head1 METHODS
69  
70 =cut
71  
72 package Bio::EnsEMBL::Utils::Tree::Interval::Immutable;
73 $Bio::EnsEMBL::Utils::Tree::Interval::Immutable::VERSION = '114.0.0_50'; # TRIAL
74 $Bio::EnsEMBL::Utils::Tree::Interval::Immutable::VERSION = '114.0.050';
75 use strict;
76  
77 use Tie::RefHash;
78  
79 use Bio::EnsEMBL::Utils::Scalar qw(assert_ref);
80 use Bio::EnsEMBL::Utils::Exception qw(throw);
81 use Bio::EnsEMBL::Utils::Tree::Interval::Immutable::Node;
82 use Bio::EnsEMBL::Utils::Interval;
83  
84  
85 =head2 new
86  
87 Arg [1] : Arrayref of Bio::EnsEMBL::Utils::Interval instances
88 Example : my $tree = Bio::EnsEMBL::Utils::Tree::Immutable([ $i1, $i2, $i3 ]);
89 Description : Constructor. Creates a new immutable tree instance
90 Returntype : Bio::EnsEMBL::Utils::Tree::Interval::Immutable
91 Exceptions : none
92 Caller : general
93  
94 =cut
95  
96 sub new {
97 my $caller = shift;
98 my $class = ref($caller) || $caller;
99  
100 my $intervals = shift;
101 if (defined $intervals ) {
102 assert_ref($intervals, 'ARRAY');
103 }
104
105 my $self = bless({}, $class);
106  
107 $self->{top_node} = $self->_divide_intervals($intervals);
108  
109 return $self;
110 }
111  
112 =head2 root
113  
114 Arg [] : none
115 Example : my $root = $tree->root();
116 Description : Return the tree top node
117 Returntype : Bio::EnsEMBL::Utils::Tree::Interval::Immutable::Node
118 Exceptions : none
119 Caller : general
120  
121 =cut
122  
123 sub root {
124 return shift->{top_node};
125 }
126  
127 =head2 query
128  
129 Arg [1] : scalar, $start
130 Where the query interval begins
131 Arg [2] : (optional) scalar, $end
132 Where the query interval ends
133 Example : my $results = $tree->query(121626874, 122092717);
134 Description : Query the tree if its intervals overlap the interval whose start
135 and end points are specified by the argument list.
136 If end is not specified, it is assumed to be the same as start
137 so effectively making a point query.
138 Returntype : An arrayref of Bio::EnsEMBL::Utils::Interval instances
139 Exceptions : none
140 Caller : general
141  
142 =cut
143  
144 sub query {
145 my ($self, $start, $end) = @_;
146
147 my $interval;
148 if (defined $start) {
149 $end = $start unless defined $end;
150 $interval = Bio::EnsEMBL::Utils::Interval->new($start, $end);
151 }
152
153 return [] unless $interval;
154 return $self->_query_point($self->root, $interval->start, []) if $interval->is_point;
155  
156 my $result = [];
157 return $result unless $self->root or $interval->is_empty;
158  
159 my $node = $self->root;
160 while ($node) {
161 if ($interval->contains($node->x_center)) {
162 push @{$result}, @{$node->s_center_beg};
163 $self->_range_query_left($node->left, $interval, $result);
164 $self->_range_query_right($node->right, $interval, $result);
165 last;
166 }
167 if ($interval->is_left_of($node->x_center)) {
168 foreach my $s_beg (@{$node->s_center_beg}) {
169 last unless $interval->intersects($s_beg);
170 push @{$result}, $s_beg;
171 }
172 $node = $node->left;
173 } else {
174 foreach my $s_end (@{$node->s_center_end}) {
175 last unless $interval->intersects($s_end);
176 push @{$result}, $s_end;
177 }
178 $node = $node->right;
179 }
180 }
181  
182 return sort_by_begin(uniq($result));
183 }
184  
185 sub _query_point {
186 my ($self, $node, $point, $result) = @_;
187  
188 return $result unless $node;
189  
190 # if x is less than x_center, the leftmost set of intervals, S_left, is considered
191 if ($point <= $node->x_center) {
192 # if x is less than x_center, we know that all intervals in S_center end after x,
193 # or they could not also overlap x_center. Therefore, we need only find those intervals
194 # in S_center that begin before x. We can consult the lists of S_center that have already
195 # been constructed. Since we only care about the interval beginnings in this scenario,
196 # we can consult the list sorted by beginnings.
197 # Suppose we find the closest number no greater than x in this list. All ranges from the
198 # beginning of the list to that found point overlap x because they begin before x and end
199 # after x (as we know because they overlap x_center which is larger than x).
200 # Thus, we can simply start enumerating intervals in the list until the startpoint value exceeds x.
201 foreach my $s_beg (@{$node->s_center_beg}) {
202 last if $s_beg->is_right_of($point);
203 push @{$result}, $s_beg;
204 }
205  
206 # since x < x_center, we also consider the leftmost set of intervals
207 return $self->_query_point($node->left, $point, $result);
208 } else {
209 # if x is greater than x_center, we know that all intervals in S_center must begin before x,
210 # so we find those intervals that end after x using the list sorted by interval endings.
211 foreach my $s_end (@{$node->s_center_end}) {
212 last if $s_end->is_left_of($point);
213 push @{$result}, $s_end;
214 }
215
216 # since x > x_center, we also consider the rightmost set of intervals
217 return $self->_query_point($node->right, $point, $result);
218 }
219
220 return sort_by_begin(uniq($result));
221 }
222  
223 # This corresponds to the left branch of the range search, once we find a node, whose
224 # midpoint is contained in the query interval. All intervals in the left subtree of that node
225 # are guaranteed to intersect with the query, if they have an endpoint greater or equal than
226 # the start of the query interval. Basically, this means that every time we branch to the left
227 # in the binary search, we need to add the whole right subtree to the result set.
228  
229 sub _range_query_left {
230 my ($self, $node, $interval, $result) = @_;
231
232 while ($node) {
233 if ($interval->contains($node->x_center)) {
234 push @{$result}, @{$node->s_center_beg};
235 if ($node->right) {
236 # in-order traversal of the right subtree to add all its intervals
237 $self->_in_order_traversal($node->right, $result);
238 }
239 $node = $node->left;
240 } else {
241 foreach my $seg_end (@{$node->s_center_end}) {
242 last if $seg_end->is_left_of($interval);
243 push @{$result}, $seg_end;
244 }
245 $node = $node->right;
246 }
247 }
248 }
249  
250 # This corresponds to the right branch of the range search, once we find a node, whose
251 # midpoint is contained in the query interval. All intervals in the right subtree of that node
252 # are guaranteed to intersect with the query, if they have an endpoint smaller or equal than
253 # the end of the query interval. Basically, this means that every time we branch to the right
254 # in the binary search, we need to add the whole left subtree to the result set.
255  
256 sub _range_query_right {
257 my ($self, $node, $interval, $result) = @_;
258  
259 while ($node) {
260 if ($interval->contains($node->x_center)) {
261 push @{$result}, @{$node->s_center_beg};
262 if ($node->left) {
263 # in-order traversal of the left subtree to add all its intervals
264 $self->_in_order_traversal($node->left, $result);
265 }
266 $node = $node->right;
267 } else {
268 foreach my $seg_beg (@{$node->s_center_beg}) {
269 last if $seg_beg->is_right_of($interval);
270 push @{$result}, $seg_beg;
271 }
272 $node = $node->left;
273 }
274 }
275 }
276  
277 sub in_order_traversal {
278 my ($self) = @_;
279  
280 my $result = [];
281 $self->_in_order_traversal($self->root, $result);
282  
283 return $result;
284 }
285  
286 sub _in_order_traversal {
287 my ($self, $node, $result) = @_;
288  
289 return unless $node;
290 $result ||= [];
291  
292 $self->_in_order_traversal($node->left, $result);
293 push @{$result}, @{$node->s_center_beg};
294 $self->_in_order_traversal($node->right, $result);
295 }
296  
297 sub _divide_intervals {
298 my ($self, $intervals, $sorted) = @_;
299  
300 return undef unless scalar @{$intervals};
301  
302 my $sorted_intervals;
303 if ($sorted) {
304 $sorted_intervals = $intervals;
305 } else {
306 $sorted_intervals = sort_by_begin($intervals);
307 }
308  
309 my $x_center = $self->_center_sorted($sorted_intervals);
310 my ($s_center, $s_left, $s_right) = ([], [], []);
311
312 foreach my $sorted_interval (@{$sorted_intervals}) {
313 if ($sorted_interval->spans_origin) {
314 throw "Cannot build a tree containing an interval that spans the origin";
315 }
316 if ($sorted_interval->end < $x_center) {
317 push @{$s_left}, $sorted_interval;
318 } elsif ($sorted_interval->start > $x_center) {
319 push @{$s_right}, $sorted_interval;
320 } else {
321 push @{$s_center}, $sorted_interval;
322 }
323 }
324  
325 my $node = Bio::EnsEMBL::Utils::Tree::Interval::Immutable::Node->new($x_center,
326 $s_center,
327 $self->_divide_intervals($s_left, 1),
328 $self->_divide_intervals($s_right, 1));
329 }
330  
331 sub _center {
332 my ($self, $intervals) = @_;
333  
334 my $sorted_intervals = sort_by_begin($intervals);
335 my $len = scalar @{$sorted_intervals};
336
337 return $sorted_intervals->[int($len/2)]->start;
338 }
339  
340 sub _center_sorted {
341 my ($self, $sorted_intervals) = @_;
342 my $len = scalar @{$sorted_intervals};
343  
344 return $sorted_intervals->[int($len/2)]->start;
345 }
346  
347 sub sort_by_begin {
348 my $intervals = shift;
349  
350 return [ map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [ $_->start, $_ ] } @{$intervals} ];
351 }
352  
353 sub uniq {
354 my $intervals = shift;
355  
356 tie my %seen, 'Tie::RefHash';
357
358 return [ grep { ! $seen{ $_ }++ } @{$intervals} ];
359 }
360  
361 1;
362