EnsEMBL/Utils/Tree/Interval/Mutable/PP.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 21 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 201 CC 26 SCAR 50.0
CC
26
Cov
0%
remove
306.0
insert
56.0
root
6.0
CRAP 148.6
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::Mutable::PP
34  
35 =head1 DESCRIPTION
36  
37 Pure Perl fall back implementation of a mutable interval tree, uses
38 augmented AVL binary balanced trees.
39
40 =head1 METHODS
41  
42 =cut
43  
44 package Bio::EnsEMBL::Utils::Tree::Interval::Mutable::PP;
45 $Bio::EnsEMBL::Utils::Tree::Interval::Mutable::PP::VERSION = '114.0.0_50'; # TRIAL
46 $Bio::EnsEMBL::Utils::Tree::Interval::Mutable::PP::VERSION = '114.0.050';
47 use strict;
48 use Carp;
49  
50 use Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node;
51 use Bio::EnsEMBL::Utils::Interval;
52 use Bio::EnsEMBL::Utils::Exception qw(throw);
53  
54 =head2 new
55  
56 Arg [] : none
57 Example : my $tree = Bio::EnsEMBL::Utils::Tree::Mutable();
58 Description : Constructor. Creates a new mutable tree instance
59 Returntype : Bio::EnsEMBL::Utils::Tree::Interval::Mutable::PP
60 Exceptions : none
61 Caller : general
62  
63 =cut
64  
65 sub new {
66 my $caller = shift;
67 my $class = ref($caller) || $caller;
68  
69 # mmhh, should probably return just the hash ref
70 return bless({ _root => undef, _size => 0 }, $class);
71 }
72  
73 =head2 root
74  
75 Arg [] : none
76 Example : my $root = $tree->root;
77 Description : Returns the tree top node
78 Returntype : Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node
79 Exceptions : none
80 Caller : general
81  
82 =cut
83  
84 sub root {
85 my $self = shift;
86 $self->{_root} = shift if( @_ );
87
88 return $self->{_root};
89 }
90  
91 =head2 size
92  
93 Arg [] : none
94 Example : print "Tree size is ", $tree->size, "\n";
95 Description : Return the size of the tree, i.e. the number of nodes
96 Returntype : scalar
97 Exceptions : none
98 Caller : general
99  
100 =cut
101  
102 sub size {
103 return shift->{_size};
104 }
105  
106 =head2 insert
107  
108 Arg [1] : Bio::EnsEMBL::Utils::Interval
109 Interval to insert
110 Example : $tree->insert(Bio::EnsEMBL::Utils::Interval->new(10, 20, 'data'));
111 Description : Insert an interval in the tree
112 Returntype : scalar (1), upon success
113 Exceptions : thrown if Interval spans origin (has start > end)
114 Caller : general
115  
116 =cut
117  
118 sub insert {
119 my ($self, $i) = @_;
120
121 if ($i->spans_origin) {
122 throw "Cannot insert an interval that spans the origin into a mutable tree";
123 }
124 # base case: empty tree, assign new node to root
125 unless (defined $self->root) {
126 $self->root(Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node->new($self, $i));
127 $self->{_size}++;
128
129 return 1;
130 }
131  
132 # check if node exists with the same key
133 my $node = $self->root->search_by_key($i->start);
134 if ($node) {
135 # check the node's intervals if there's already one
136 # which is the same as the interval we're trying to insert
137 map { return 0 if $_->start == $i->start and $_->end == $i->end } @{$node->intervals};
138  
139 # add the interval to the node
140 $node->add_interval($i);
141  
142 # update max of node and its ancestors if necessary
143 if ($node->max < $i->end) {
144 $node->max($i->end);
145 $node->_update_parents_max if $node->parent;
146 }
147  
148 $self->{_size}++;
149
150 return 1;
151 } else {
152 # node with the interval's key doesn't exist
153 # insert from root node
154 $self->root->insert($i);
155 $self->{_size}++;
156
157 return 1;
158 }
159  
160 croak "Shouldn't be here";
161 }
162  
163 =head2 search
164  
165 Arg [1] : scalar, $start
166 the starting point of the interval to search
167 Arg [2] : scalar, $end
168 the end point of the interval to search
169 Example : my $result = $tree->search(85, 100);
170 Description : Search the intervals in the tree overlapping the query
171 Returntype : Arrayref of Bio::EnsEMBL::Utils::Interval instances
172 Exceptions : none
173 Caller : general
174  
175 =cut
176  
177 sub search {
178 my ($self, $start, $end) = @_;
179
180 return unless $self->root; # empty tree
181 return $self->root->search(Bio::EnsEMBL::Utils::Interval->new($start, $end));
182
183 }
184  
185 =head2 remove
186  
187 Arg [1] : Bio::EnsEMBL::Utils::Interval
188 The interval to remove from the tree
189 Example : $tree->remove($i);
190 Description : Remove an interval from the tree
191 Returntype : scalar, 1 if removal is successful, 0 otherwise
192 Exceptions : none
193 Caller : general
194  
195 =cut
196  
197 sub remove {
198 my ($self, $i) = @_;
199  
200 return 0 unless $self->root; # empty tree
201  
202 my $node = $self->root->search_by_key($i->start);
203 return 0 unless $node;
204
205 my $node_intervals = $node->intervals;
206 if (scalar @{$node_intervals} > 1) {
207 # node with this key has more than this interval. Find it and remove
208 my $removed = 0;
209 foreach my $j (0 .. $#{$node_intervals}) {
210 if ($node_intervals->[$j]->start == $i->start and $node_intervals->[$j]->end == $i->end) {
211 splice @{$node_intervals}, $j, 1;
212 $removed = 1;
213 last;
214 }
215 }
216  
217 if ($removed) {
218 $removed = 0;
219  
220 # update max of node and its ancestors, if necessary
221 if ($i->end == $node->max) {
222 my $node_highest_end = $node->_highest_end;
223 if ($node->left and $node->right) {
224 $node->{max} = max $node->left->max, $node->rigth->max, $node_highest_end;
225 } elsif ($node->left and not $node->right) {
226 $node->{max} = max $node->left->max, $node_highest_end;
227 } elsif ($node->right and not $node->left) {
228 $node->{max} = max $node->right->max, $node_highest_end;
229 } else {
230 $node->{max} = $node_highest_end;
231 }
232  
233 $node->parent->_update_parents_max if $node->parent;
234 }
235  
236 $self->{_size}--;
237
238 return 1;
239 } else {
240 return 0;
241 }
242 } elsif (scalar @{$node_intervals}) {
243 # node with this key has only this interval
244 # check if the remaining node's interval is the one we want to remove
245 if ($node_intervals->[0]->start == $i->start and $node_intervals->[0]->end == $i->end) {
246 # remove the whole node from the tree
247 if ($node->key == $self->root->key) {
248 # we're removing the root node
249 # create dummy node temporarily taking root's parent node
250 my $root_parent =
251 Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node->new($self,
252 Bio::EnsEMBL::Utils::Interval->new($i->start, $i->end));
253 $root_parent->left($self->root);
254 $self->root->parent($root_parent);
255
256 my $removed_node = $self->root->remove($node);
257 $self->root($root_parent->left);
258  
259 $self->root->parent(undef) if $self->root;
260 if ($removed_node) {
261 $removed_node = undef;
262 $self->{_size}--;
263
264 return 1;
265 } else {
266 return 0;
267 }
268 } else {
269 my $removed_node = $self->root->remove($node);
270 if ($removed_node) {
271 $removed_node = undef;
272 $self->{_size}--;
273
274 return 1;
275 } else {
276 return 0;
277 }
278 }
279 } else {
280 # the remaining record is not the one we want to remove
281 return 0;
282 }
283
284 } else {
285 # no records at all in this node, shouldn't happen
286 croak "This should not happen";
287 }
288 }
289  
290 sub _in_order_traversal_delete {
291 my ($self, $node) = @_;
292  
293 return unless $node;
294  
295 $self->_in_order_traversal_delete($node->left);
296 $node->left(undef);
297 $self->_in_order_traversal_delete($node->right);
298 $node->right(undef);
299  
300 $node->parent(undef);
301  
302 }
303  
304 sub DESTROY {
305 my $self = shift;
306  
307 $self->_in_order_traversal_delete($self->root);
308 }
309  
310 1;
311