DBTree/Result/Node.pm

filter: Statementstmt 60.0% 54 / 90 Branchbran 25.0% 2 / 8 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 55.1% 16 / 29 Podpod 100% 19 / 19 totaltotal 59.0% 91 / 154 CC 8 SCAR 21.4
CC
8
Cov
54%
_index
42.0
is_descendant_of
6.0
_schema
2.0
CRAP 8.5
↑ Summary DBTree.pm »
1 package Bio::Phylo::Forest::DBTree::Result::Node;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Bio::Phylo::Forest::DBTree;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Bio::Phylo::Forest::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Bio::Phylo::Util::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use base 'DBIx::Class::Core';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use base 'Bio::Phylo::Forest::Node';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 my $log = Bio::Phylo::Util::Logger->new;
11  
12 2 __PACKAGE__->table("node");
13  
14 =head1 NAME
15  
16 Bio::Phylo::Forest::DBTree::Result::Node - Phylogenetic database record as a node object
17  
18 =head1 SYNOPSIS
19  
20 # same API is Bio::Phylo::Forest::Node
21
22 =head1 DESCRIPTION
23  
24 This package implements an object-relational interface to records in a phylogenetic
25 database. This way, the record can be used as if it's a tree node with the same
26 programming interface as is used by L<Bio::Phylo>, but without making the demands that
27 loading an entire tree in memory would make.
28  
29 =head1 DATABASE ACCESSORS
30  
31 The following methods directly access fields of the database-backed node record. In
32 principle, these methods can also be used as setters, although for the indexes and keys
33 you really need to know what you're doing or the topology of the tree could become
34 irreparably corrupted.
35  
36 =head2 id()
37  
38 Returns primary key of the node object, an integer.
39  
40 =head2 parent()
41  
42 Returns the foreign key of the node object's parent, an integer.
43  
44 =head2 left()
45  
46 Returns the pre-order index of the node object, an integer.
47  
48 =head2 right()
49  
50 Returns the post-order index of the node object, an integer.
51  
52 =head2 name()
53  
54 Returns the node name, a string.
55  
56 =head2 length()
57  
58 Returns the node's branch length, a floating point number.
59  
60 =head2 height()
61  
62 Returns the node's height from the root, a floating point number.
63  
64 =cut
65  
66 2 __PACKAGE__->add_columns(
67 "id",
68 { data_type => "int", is_nullable => 0 },
69 "parent",
70 { data_type => "int", is_nullable => 0 },
71 "left",
72 { data_type => "int", is_nullable => 1 },
73 "right",
74 { data_type => "int", is_nullable => 1 },
75 "name",
76 { data_type => "string", is_nullable => 0 },
77 "length",
78 { data_type => "float", is_nullable => 0 },
79 "height",
80 { data_type => "float", is_nullable => 1 },
81 );
82 2 __PACKAGE__->set_primary_key("id");
83  
84 2 my $schema;
85 7 ▶sub _schema {
Subroutine _schema: called CC=2 SCAR=6.9
86 7 ▶ if ( not $schema ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema) 1 6 7
87 1 $schema = Bio::Phylo::Forest::DBTree->connect->resultset('Node');
88 }
89 7 return $schema;
90 }
91  
92 =head1 NODE METHODS
93  
94 These methods override methods of the same name in L<Bio::Phylo>, to make more efficient
95 use of the database.
96  
97 =head2 get_parent()
98  
99 Returns the node's parent, if any.
100  
101 =cut
102  
103 0 ▶sub get_parent {
Subroutine get_parent: not called CC=1 SCAR=6.9
Pod documented
104 0 my $self = shift;
105 0 return $self->_schema->find($self->parent);
106 }
107  
108 =head2 get_children_rs()
109  
110 Returns the node's children, if any, as a L<DBIx::Class> result set.
111  
112 =cut
113  
114 2 ▶sub get_children_rs {
Subroutine get_children_rs: called CC=1 SCAR=0.0
Pod documented
115 2 my $self = shift;
116 2 my $id = $self->id;
117 2 return $self->_schema->search({
118 '-and' => [
119 'parent' => { '==' => $id },
120 'id' => { '!=' => $id },
121 ]
122 });
123 }
124  
125 =head2 get_children()
126  
127 Returns the node's children, if any, as an array reference.
128  
129 =cut
130  
131 2 ▶sub get_children { [ shift->get_children_rs->all ] }
Subroutine get_children: called CC=1 SCAR=0.0
Pod documented
132  
133 =head2 get_descendants_rs()
134  
135 Returns the node's descendants, if any, as a L<DBIx::Class> result set.
136  
137 =cut
138  
139 0 ▶sub get_descendants_rs {
Subroutine get_descendants_rs: not called CC=1 SCAR=6.9
Pod documented
140 0 my $self = shift;
141 0 return $self->_schema->search(
142 {
143 '-and' => [
144 'left' => { '>' => $self->left },
145 'right' => { '<' => $self->right },
146 ]
147 }
148 )
149 }
150  
151 =head2 get_descendants()
152  
153 Returns the node's descendants, if any, as an array reference.
154  
155 =cut
156  
157 0 ▶sub get_descendants { [ shift->get_descendants_rs->all ] }
Subroutine get_descendants: not called CC=1 SCAR=6.9
Pod documented
158  
159 =head2 get_terminals_rs()
160  
161 Returns the node's descendant tips, if any, as a L<DBIx::Class> result set.
162  
163 =cut
164  
165 0 ▶sub get_terminals_rs {
Subroutine get_terminals_rs: not called CC=1 SCAR=6.9
Pod documented
166 0 my $self = shift;
167 0 my $scalar = 'right';
168 0 return $self->_schema->search(
169 {
170 '-and' => [
171 'left' => { '>' => $self->left },
172 'right' => { '<' => $self->right },
173 'left' => { '==' => \$scalar },
174 ]
175 }
176 )
177 }
178  
179 =head2 get_terminals()
180  
181 Returns the node's descendant tips, if any, as an array reference.
182  
183 =cut
184  
185 0 ▶sub get_terminals { [ shift->get_terminals_rs->all ] }
Subroutine get_terminals: not called CC=1 SCAR=6.9
Pod documented
186  
187 =head2 get_internals_rs()
188  
189 Returns the node's descendant internal nodes, if any, as a L<DBIx::Class> result set.
190  
191 =cut
192  
193 0 ▶sub get_internals_rs {
Subroutine get_internals_rs: not called CC=1 SCAR=6.9
Pod documented
194 0 my $self = shift;
195 0 my $scalar = 'right';
196 0 return $self->_schema->search(
197 {
198 '-and' => [
199 'left' => { '>' => $self->left },
200 'right' => { '<' => $self->right },
201 'left' => { '!=' => \$scalar },
202 ]
203 }
204 )
205 }
206  
207 =head2 get_internals()
208  
209 Returns the node's descendant internal nodes, if any, as an array reference.
210  
211 =cut
212  
213 0 ▶sub get_internals { [ shift->get_internals_rs->all ] }
Subroutine get_internals: not called CC=1 SCAR=6.9
Pod documented
214  
215 =head2 get_ancestors_rs()
216  
217 Returns the node's ancestors, if any, as a L<DBIx::Class> result set.
218  
219 =cut
220  
221 2 ▶sub get_ancestors_rs {
Subroutine get_ancestors_rs: called CC=1 SCAR=0.0
Pod documented
222 2 my $self = shift;
223 2 return $self->_schema->search(
224 {
225 '-and' => [
226 'left' => { '<' => $self->left },
227 'right' => { '>' => $self->right },
228 ]
229 }
230 )
231 }
232  
233 =head2 get_ancestors()
234  
235 Returns the node's ancestors, if any, as an array ref.
236  
237 =cut
238  
239 2 ▶sub get_ancestors { [ shift->get_ancestors_rs->all ] }
Subroutine get_ancestors: called CC=1 SCAR=0.0
Pod documented
240  
241 =head2 get_siblings_rs()
242  
243 Returns the node's siblings, if any, as a L<DBIx::Class> result set.
244  
245 =cut
246  
247 0 ▶sub get_siblings_rs {
Subroutine get_siblings_rs: not called CC=1 SCAR=6.9
Pod documented
248 0 my $self = shift;
249 0 return $self->_schema->search(
250 {
251 '-and' => [
252 'parent' => { '==' => $self->parent },
253 'id' => { '!=' => $self->id },
254 ]
255 }
256 )
257 }
258  
259 =head2 get_siblings()
260  
261 Returns the node's siblings, if any, as an array ref.
262  
263 =cut
264  
265 0 ▶sub get_siblings { [ shift->get_siblings_rs->all ] }
Subroutine get_siblings: not called CC=1 SCAR=6.9
Pod documented
266  
267 =head2 get_mrca()
268  
269 Given another node in the same tree, returns the most recent common ancestor of the two.
270  
271 =cut
272  
273 3 ▶sub get_mrca {
Subroutine get_mrca: called CC=1 SCAR=0.0
Pod documented
274 3 my ( $self, $other ) = @_;
275 3 my @lefts = sort { $a <=> $b } $self->left, $other->left;
276 3 my @rights = sort { $a <=> $b } $self->right, $other->right;
277 3 return $self->_schema->search(
278 {
279 '-and' => [
280 'left' => { '<' => $lefts[0] },
281 'right' => { '>' => $rights[1] },
282 ]
283 },
284 {
285 'order_by' => 'right',
286 'rows' => 1,
287 }
288 )->single;
289 }
290  
291 {
292 2 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
293 0 ▶ sub _index {
Subroutine _index: not called CC=6 SCAR=37.4
294 0 my ( $self, $counter, $height ) = @_;
295 0 ▶ $height += ( $self->get_branch_length || 0 );
Condition0%0/2
$self->get_branch_length || 0
l!l
00
MC/DC0%0/1
$self->get_branch_length || 0
$self->get_branch_length
Truth table0%0/2
A
A= $self->get_branch_length
Aresult
11
00
296
297 # initialize or update counter
298 0 ▶ if ( ref($counter) eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $counter eq 'SCALAR') { } 0 0 0
299 0 $$counter = $$counter + 1;
300 }
301 else {
302 0 my $i = 1;
303 0 $counter = \$i;
304 }
305
306 # report progress
307 0 ▶ if ( not $$counter % 1000 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$counter % 1000) 0 0 0
308 0 $log->info("updated index ".$$counter);
309 }
310
311 # update and recurse
312 0 $self->update({ 'left' => $$counter, 'height' => $height });
313 0 my @c = @{ $self->get_children };
314 0 for my $child ( @c ) {
315 0 $child->_index($counter, $height);
316 }
317 0 ▶ if ( @c ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@c) 0 0 0
318 0 $$counter = $$counter + 1;
319 }
320 0 $self->update({ 'right' => $$counter });
321 }
322 }
323  
324 =head2 get_id()
325  
326 Same as C<id()>, see above.
327  
328 =cut
329  
330 17 ▶sub get_id { shift->id }
Subroutine get_id: called CC=1 SCAR=0.0
Pod documented
331  
332 =head2 get_name()
333  
334 Same as C<name()>, see above.
335  
336 =cut
337  
338 0 ▶sub get_name { shift->name }
Subroutine get_name: not called CC=1 SCAR=6.9
Pod documented
339  
340 =head2 get_branch_length()
341  
342 Same as C<length()>, see above.
343  
344 =cut
345  
346 0 ▶sub get_branch_length { shift->length }
Subroutine get_branch_length: not called CC=1 SCAR=6.9
Pod documented
347  
348 =head2 is_descendant_of()
349  
350 Given another node, determines of the invocant is the descendant of the argument.
351  
352 =cut
353  
354 0 ▶sub is_descendant_of {
Subroutine is_descendant_of: not called CC=2 SCAR=17.9
Pod documented
355 0 my ( $self, $other ) = @_;
356 0 ▶ return ( $self->left > $other->left ) && ( $self->right < $other->right );
Condition0%0/3
$self->left > $other->left && $self->right < $other->right
!ll&&!rl&&r
000
MC/DC0%0/2
$self->left > $other->left && $self->right < $other->right
$self->left > $other->left $self->right < $other->right
Truth table0%0/3
A && B
A= $self->left > $other->leftB= $self->right < $other->right
ABresult
0X0
100
111
357 }
358  
359 =head2 calc_patristic_distance()
360  
361 Given another node, calculates the patristic distance between the two.
362  
363 =cut
364  
365 1 ▶sub calc_patristic_distance {
Subroutine calc_patristic_distance: called CC=1 SCAR=0.0
Pod documented
366 1 my ( $self, $other ) = @_;
367 1 my $mrca = $self->get_mrca($other);
368 1 my $mh = $mrca->height;
369 1 my $sh = $self->height;
370 1 my $oh = $other->height;
371 1 return ( $sh - $mh ) + ( $oh - $mh );
372 }
373  
374 2 1;
↑ Summary DBTree.pm »