lib/Bio/Phylo/Forest/DBTree/Result/Node.pm

filter: Statementstmt 62.0% 54 / 87 Branchbran 25.0% 2 / 8 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 59.2% 16 / 27 Podpod 100% 17 / 17 totaltotal 60.5% 89 / 147 SCAR 26.0
CC
8
Cov
56%
_index
42.0
is_descendant_of
6.0
_schema
2.0
CRAP 13.5
« lib/Bio/Phylo/Forest/DBTree.pm ↑ Summary
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 sub _schema {
86 7 ▶ if ( not $schema ) {
Subroutine _schema: called CC=2 SCAR=6.9
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 sub get_parent {
104 0 ▶ my $self = shift;
Subroutine get_parent: not called CC=1 SCAR=6.9
Pod documented
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 sub get_children_rs {
115 2 ▶ my $self = shift;
Subroutine get_children_rs: called CC=1 SCAR=0.0
Pod documented
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 sub get_descendants_rs {
140 0 ▶ my $self = shift;
Subroutine get_descendants_rs: not called CC=1 SCAR=6.9
Pod documented
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 sub get_terminals_rs {
166 0 ▶ my $self = shift;
Subroutine get_terminals_rs: not called CC=1 SCAR=6.9
Pod documented
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 sub get_internals_rs {
194 0 ▶ my $self = shift;
Subroutine get_internals_rs: not called CC=1 SCAR=6.9
Pod documented
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 sub get_ancestors_rs {
222 2 ▶ my $self = shift;
Subroutine get_ancestors_rs: called CC=1 SCAR=0.0
Pod documented
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_mrca()
242  
243 Given another node in the same tree, returns the most recent common ancestor of the two.
244  
245 =cut
246  
247 sub get_mrca {
248 3 ▶ my ( $self, $other ) = @_;
Subroutine get_mrca: called CC=1 SCAR=0.0
Pod documented
249 3 my @lefts = sort { $a <=> $b } $self->left, $other->left;
250 3 my @rights = sort { $a <=> $b } $self->right, $other->right;
251 3 return $self->_schema->search(
252 {
253 '-and' => [
254 'left' => { '<' => $lefts[0] },
255 'right' => { '>' => $rights[1] },
256 ]
257 },
258 {
259 'order_by' => 'right',
260 'rows' => 1,
261 }
262 )->single;
263 }
264  
265 {
266 2 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
267 sub _index {
268 0 ▶ my ( $self, $counter, $height ) = @_;
Subroutine _index: not called CC=6 SCAR=37.4
269 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
270
271 # initialize or update counter
272 0 ▶ if ( ref($counter) eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $counter eq 'SCALAR') { } 0 0 0
273 0 $$counter = $$counter + 1;
274 }
275 else {
276 0 my $i = 1;
277 0 $counter = \$i;
278 }
279
280 # report progress
281 0 ▶ if ( not $$counter % 1000 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$counter % 1000) 0 0 0
282 0 $log->info("updated index ".$$counter);
283 }
284
285 # update and recurse
286 0 $self->update({ 'left' => $$counter, 'height' => $height });
287 0 my @c = @{ $self->get_children };
288 0 for my $child ( @c ) {
289 0 $child->_index($counter, $height);
290 }
291 0 ▶ if ( @c ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@c) 0 0 0
292 0 $$counter = $$counter + 1;
293 }
294 0 $self->update({ 'right' => $$counter });
295 }
296 }
297  
298 =head2 get_id()
299  
300 Same as C<id()>, see above.
301  
302 =cut
303  
304 17 ▶sub get_id { shift->id }
Subroutine get_id: called CC=1 SCAR=0.0
Pod documented
305  
306 =head2 get_name()
307  
308 Same as C<name()>, see above.
309  
310 =cut
311  
312 0 ▶sub get_name { shift->name }
Subroutine get_name: not called CC=1 SCAR=6.9
Pod documented
313  
314 =head2 get_branch_length()
315  
316 Same as C<length()>, see above.
317  
318 =cut
319  
320 0 ▶sub get_branch_length { shift->length }
Subroutine get_branch_length: not called CC=1 SCAR=6.9
Pod documented
321  
322 =head2 is_descendant_of()
323  
324 Given another node, determines of the invocant is the descendant of the argument.
325  
326 =cut
327  
328 sub is_descendant_of {
329 0 ▶ my ( $self, $other ) = @_;
Subroutine is_descendant_of: not called CC=2 SCAR=17.9
Pod documented
330 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
331 }
332  
333 =head2 calc_patristic_distance()
334  
335 Given another node, calculates the patristic distance between the two.
336  
337 =cut
338  
339 sub calc_patristic_distance {
340 1 ▶ my ( $self, $other ) = @_;
Subroutine calc_patristic_distance: called CC=1 SCAR=0.0
Pod documented
341 1 my $mrca = $self->get_mrca($other);
342 1 my $mh = $mrca->height;
343 1 my $sh = $self->height;
344 1 my $oh = $other->height;
345 1 return ( $sh - $mh ) + ( $oh - $mh );
346 }
347  
348 2 1;
« lib/Bio/Phylo/Forest/DBTree.pm ↑ Summary