Phylo/Forest/Node.pm

filter: Statementstmt 87.6% 114 / 130 Branchbran 65.7% 25 / 38 Conditioncond 62.9% 17 / 27 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 96.7% 30 / 31 Podpod 100% 12 / 12 totaltotal 80.6% 204 / 253 CC 30 SCAR 17.4
CC
30
Cov
80%
set_child
13.4
_json_data
12.0
set_branch_length
10.1
CRAP 5.7
« Phylo/Parsers/Ubiosearch.pm ↑ Summary Phylo/Generator.pm »
1 package Bio::Phylo::Forest::Node;
2 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use Bio::Phylo::Forest::DrawNodeRole;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use base qw'Bio::Phylo::Forest::DrawNodeRole';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use Bio::Phylo::Util::CONSTANT qw':objecttypes /looks_like/';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use Bio::Phylo::Util::Exceptions 'throw';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # store type constant
11 33 my ( $TYPE_CONSTANT, $CONTAINER_CONSTANT ) = ( _NODE_, _TREE_ );
12  
13 {
14  
15 # @fields array necessary for object destruction
16 33 my @fields = \( my ( %branch_length, %parent, %tree, %rank ) );
17  
18 =head1 NAME
19  
20 Bio::Phylo::Forest::Node - Node in a phylogenetic tree
21  
22 =head1 SYNOPSIS
23  
24 # some way to get nodes:
25 use Bio::Phylo::IO;
26 my $string = '((A,B),C);';
27 my $forest = Bio::Phylo::IO->parse(
28 -format => 'newick',
29 -string => $string
30 );
31  
32 # prints 'Bio::Phylo::Forest'
33 print ref $forest;
34  
35 foreach my $tree ( @{ $forest->get_entities } ) {
36  
37 # prints 'Bio::Phylo::Forest::Tree'
38 print ref $tree;
39  
40 foreach my $node ( @{ $tree->get_entities } ) {
41  
42 # prints 'Bio::Phylo::Forest::Node'
43 print ref $node;
44  
45 # node has a parent, i.e. is not root
46 if ( $node->get_parent ) {
47 $node->set_branch_length(1);
48 }
49  
50 # node is root
51 else {
52 $node->set_branch_length(0);
53 }
54 }
55 }
56  
57 =head1 DESCRIPTION
58  
59 This module has the getters and setters that alter the state of a
60 node object. Useful behaviours (which are also available) are defined
61 in the L<Bio::Phylo::Forest::NodeRole> package.
62  
63 =head1 METHODS
64  
65 =cut
66  
67 9361 ▶ my $set_raw_parent = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
68 9361 my ( $self, $parent ) = @_;
69 9361 my $id = $self->get_id;
70 9361 $parent{$id} = $parent; # XXX here we modify parent
71 9361 ▶ weaken $parent{$id} if $parent;
Branch: 1 branch
BranchTrueFalseTotal
if $parent 9235 126 9361
72 33 };
73 504788 ▶ my $get_parent = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
74 504788 my $self = shift;
75 504788 return $parent{ $self->get_id };
76 33 };
77 122564 ▶ my $get_children = sub { shift->get_entities };
Subroutine __ANON__: called CC=1 SCAR=0.0
78 59233 ▶ my $get_branch_length = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
79 59233 my $self = shift;
80 59233 return $branch_length{ $self->get_id };
81 33 };
82 9235 ▶ my $set_raw_child = sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
83 9235 my ( $self, $child, $i ) = @_;
84 9235 ▶ $i = $self->last_index + 1 if not defined $i or $i == -1;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $i or $i == -1 0 9235 9235
Condition33%1/3
not defined $i or $i == -1
l!l&&r!l&&!r
009235
MC/DC0%0/2
not defined $i or $i == -1
not defined $i $i == -1
Truth table33%1/3
not(A) or B
A= not defined $iB= $i == -1
ABresult
1X1
011
000
85 9235 $self->insert_at_index( $child, $i ); # XXX here we modify children
86 33 };
87  
88 =over
89  
90 =item set_parent()
91  
92 Sets argument as invocant's parent.
93  
94 Type : Mutator
95 Title : set_parent
96 Usage : $node->set_parent($parent);
97 Function: Assigns a node's parent.
98 Returns : Modified object.
99 Args : If no argument is given, the current
100 parent is set to undefined. A valid
101 argument is Bio::Phylo::Forest::Node
102 object.
103  
104 =cut
105  
106 844 ▶ sub set_parent : Clonable {
Subroutine set_parent: called CC=4 SCAR=13.9
Pod documented
107 844 my ( $self, $parent ) = @_;
108 844 ▶ if ( $parent and looks_like_object $parent, $TYPE_CONSTANT ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($parent and &looks_like_object($parent, $TYPE_CONSTANT)) { } 718 125 843
elsif (not $parent) { } 125 0 125
Condition100%3/3
$parent and &looks_like_object($parent, $TYPE_CONSTANT)
!ll&&!rl&&r
1251718
MC/DC50%1/2
$parent and &looks_like_object($parent, $TYPE_CONSTANT)
$parent &looks_like_object($parent, $TYPE_CONSTANT)
Truth table66%2/3
A and B
A= $parentB= &looks_like_object($parent, $TYPE_CONSTANT)
ABresult
0X0
100
111
109 718 $parent->set_child($self);
110 }
111 elsif ( not $parent ) {
112 125 $self->set_raw_parent;
113 }
114 843 return $self;
115 33 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117 =item set_raw_parent()
118  
119 Sets argument as invocant's parent. This method does NO
120 sanity checks on the rest of the topology. Use with caution.
121  
122 Type : Mutator
123 Title : set_raw_parent
124 Usage : $node->set_raw_parent($parent);
125 Function: Assigns a node's parent.
126 Returns : Modified object.
127 Args : If no argument is given, the current
128 parent is set to undefined. A valid
129 argument is Bio::Phylo::Forest::Node
130 object.
131  
132 =cut
133  
134 9361 ▶ sub set_raw_parent {
Subroutine set_raw_parent: called CC=1 SCAR=0.0
Pod documented
135 9361 $set_raw_parent->(@_)
136 }
137  
138 =item set_child()
139  
140 Sets argument as invocant's child.
141  
142 Type : Mutator
143 Title : set_child
144 Usage : $node->set_child($child);
145 Function: Assigns a new child to $node
146 Returns : Modified object.
147 Args : A valid argument consists of a
148 Bio::Phylo::Forest::Node object.
149  
150 =cut
151  
152 9970 ▶ sub set_child {
Subroutine set_child: called CC=11 SCAR=26.0
Pod documented
153 9970 my ( $self, $child, $i ) = @_;
154  
155 # bad args?
156 9970 ▶ if ( not $child or not looks_like_object $child, $TYPE_CONSTANT ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($child and &looks_like_object($child, $TYPE_CONSTANT)) 460 9508 9968
Condition66%2/3
$child and &looks_like_object($child, $TYPE_CONSTANT)
!ll&&!rl&&r
46009510
MC/DC50%1/2
$child and &looks_like_object($child, $TYPE_CONSTANT)
$child &looks_like_object($child, $TYPE_CONSTANT)
Truth table66%2/3
A and B
A= $childB= &looks_like_object($child, $TYPE_CONSTANT)
ABresult
0X0
100
111
157 460 return;
158 }
159  
160 # maybe nothing to do?
161 9508 ▶ if ( not $child
Branch: 1 branch
BranchTrueFalseTotal
if (not $child or $child->get_id == $self->get_id or $child->is_child_of($self)) 312 9196 9508
Condition50%3/6
not $child or $child->get_id == $self->get_id or $child->is_child_of($self)
l!l&&r!l&&!r
03129196
not $child or $child->get_id == $self->get_id
l!l&&r!l&&!r
009508
MC/DC33%1/3
not $child or $child->get_id == $self->get_id or $child->is_child_of($self)
not $child $child->get_id == $self->get_id $child->is_child_of($self)
Truth table50%2/4
not(A) or B or C
A= not $childB= $child->get_id == $self->get_idC= $child->is_child_of($self)
ABCresult
1XX1
01X1
0011
0000
162 or $child->get_id == $self->get_id
163 or $child->is_child_of($self) )
164 {
165 312 return $self;
166 }
167  
168 # $child_parent is NEVER $self, see above
169 9196 my $child_parent = $child->get_parent;
170  
171 # child is ancestor: this is obviously problematic, because
172 # now we're trying to set a node nearer to the root on the
173 # same lineage as the CHILD of a descendant. Because they're
174 # on the same lineage it's hard to see how this can be done
175 # sensibly. The decision here is to do:
176 # 1. we prune what is to become the parent (now the descendant)
177 # from its current parent
178 # 2. we set this pruned node (and its descendants) as a sibling
179 # of what is to become the child
180 # 3. we prune what is to become the child from its parent
181 # 4. we set that pruned child as the child of $self
182 9196 ▶ if ( $child->is_ancestor_of($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->is_ancestor_of($self)) 1 9195 9196
183  
184 # step 1.
185 1 my $parent_parent = $self->get_parent;
186 1 $parent_parent->prune_child($self);
187  
188 # step 2.
189 1 $self->set_raw_parent( $child_parent ); # XXX could be undef
190 1 ▶ if ($child_parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child_parent) 0 1 1
191 0 $child_parent->set_raw_child( $self );
192 }
193 }
194  
195 # step 3.
196 9196 ▶ if ($child_parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child_parent) 276 8920 9196
197 276 $child_parent->prune_child($child);
198 }
199 9196 $child->set_raw_parent( $self );
200  
201 # now do the insert, first make room by shifting later siblings right
202 9196 my $children = $self->get_children;
203 9196 ▶ if ( defined $i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i) { } 0 9196 9196
204 0 ▶ for ( my $j = $#{$children} ; $j >= 0 ; $j-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= 0) 0 0 0
205 0 my $sibling = $children->[$j];
206 0 $self->set_raw_child( $sibling, $j + 1 );
207 }
208 }
209  
210 # no index was supplied, child becomes last daughter
211 else {
212 9196 $i = scalar @{$children};
213 }
214  
215 # step 4.
216 9196 $self->set_raw_child( $child, $i );
217 9196 return $self;
218 }
219  
220 =item set_raw_child()
221  
222 Sets argument as invocant's child. This method does NO
223 sanity checks on the rest of the topology. Use with caution.
224  
225 Type : Mutator
226 Title : set_raw_child
227 Usage : $node->set_raw_child($child);
228 Function: Assigns a new child to $node
229 Returns : Modified object.
230 Args : A valid argument consists of a
231 Bio::Phylo::Forest::Node object.
232  
233 =cut
234  
235 9235 ▶ sub set_raw_child {
Subroutine set_raw_child: called CC=1 SCAR=0.0
Pod documented
236 9235 $set_raw_child->(@_);
237 }
238
239 =item set_branch_length()
240  
241 Sets argument as invocant's branch length.
242  
243 Type : Mutator
244 Title : set_branch_length
245 Usage : $node->set_branch_length(0.423e+2);
246 Function: Assigns a node's branch length.
247 Returns : Modified object.
248 Args : If no argument is given, the
249 current branch length is set
250 to undefined. A valid argument
251 is a number in any of Perl's formats.
252  
253 =cut
254  
255 53768 ▶ sub set_branch_length : Clonable {
Subroutine set_branch_length: called CC=9 SCAR=23.2
Pod documented
256 53768 my ( $self, $bl ) = @_;
257 53768 my $id = $self->get_id;
258 53768 ▶ if ( defined $bl && looks_like_number $bl && !ref $bl ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $bl and &looks_like_number($bl) and not ref $bl) { } 53757 11 53768
elsif (defined $bl and !&looks_like_number($bl) || ref $bl) { } 1 10 11
elsif (not defined $bl) { } 10 0 10
Condition83%5/6
defined $bl and &looks_like_number($bl) and not ref $bl
!ll&&!rl&&r
11053757
defined $bl and &looks_like_number($bl)
!ll&&!rl&&r
10153757
MC/DC66%2/3
defined $bl and &looks_like_number($bl) and not ref $bl
defined $bl &looks_like_number($bl) not ref $bl
Truth table75%3/4
A and B and not(C)
A= defined $blB= &looks_like_number($bl)C= not ref $bl
ABCresult
0XX0
10X0
1100
1111
259 53757 $branch_length{$id} = $bl;
260 53757 ▶ if ( $bl < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bl < 0) 0 53757 53757
261 0 $self->get_logger->warn("Setting length < 0: $bl");
262 }
263 }
264 ▶ elsif ( defined $bl && ( !looks_like_number $bl || ref $bl ) ) {
Condition50%3/6
defined $bl and !&looks_like_number($bl) || ref $bl
!ll&&!rl&&r
1001
not &looks_like_number($bl) or ref $bl
l!l&&r!l&&!r
100
MC/DC33%1/3
defined $bl and !&looks_like_number($bl) || ref $bl
defined $bl not &looks_like_number($bl) ref $bl
Truth table50%2/4
A and not(B) or C
A= defined $blB= not &looks_like_number($bl)C= ref $bl
ABCresult
0XX0
1000
11X1
1011
265 1 throw 'BadNumber' => "Branch length \"$bl\" is a bad number";
266 }
267 elsif ( !defined $bl ) {
268 10 $branch_length{$id} = undef;
269 }
270 53767 return $self;
271 33 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
272  
273 =item set_tree()
274  
275 Sets what tree invocant belongs to
276  
277 Type : Mutator
278 Title : set_tree
279 Usage : $node->set_tree($tree);
280 Function: Sets what tree invocant belongs to
281 Returns : Invocant
282 Args : Bio::Phylo::Forest::Tree
283 Comments: This method is called automatically
284 when inserting or deleting nodes in
285 trees.
286  
287 =cut
288  
289 9440 ▶ sub set_tree : Clonable {
Subroutine set_tree: called CC=3 SCAR=11.1
Pod documented
290 9440 my ( $self, $tree ) = @_;
291 9440 my $id = $self->get_id;
292 9440 ▶ if ($tree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree) { } 9152 288 9440
293 9152 ▶ if ( looks_like_object $tree, $CONTAINER_CONSTANT ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_object($tree, $CONTAINER_CONSTANT)) { } 9152 0 9152
294 9152 $tree{$id} = $tree;
295 9152 weaken $tree{$id};
296 }
297 else {
298 0 throw 'ObjectMismatch' => "$tree is not a tree";
299 }
300 }
301 else {
302 288 $tree{$id} = undef;
303 }
304 9440 return $self;
305 33 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
306  
307 =item set_rank()
308  
309 Sets the taxonomic rank of the node
310  
311 Type : Mutator
312 Title : set_rank
313 Usage : $node->set_rank('genus');
314 Function: Sets the taxonomic rank of the node
315 Returns : Invocant
316 Args : String
317 Comments: Free-form, but highly recommended to use same rank names as in Bio::Taxon
318  
319 =cut
320  
321
322 2 ▶ sub set_rank : Clonable {
Subroutine set_rank: called CC=1 SCAR=0.0
Pod documented
323 2 my ( $self, $rank ) = @_;
324 2 $rank{$self->get_id} = $rank;
325 2 return $self;
326 33 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
327  
328 =item get_parent()
329  
330 Gets invocant's parent.
331  
332 Type : Accessor
333 Title : get_parent
334 Usage : my $parent = $node->get_parent;
335 Function: Retrieves a node's parent.
336 Returns : Bio::Phylo::Forest::Node
337 Args : NONE
338  
339 =cut
340  
341 504788 ▶ sub get_parent { return $get_parent->(shift) }
Subroutine get_parent: called CC=1 SCAR=0.0
Pod documented
342  
343 =item get_branch_length()
344  
345 Gets invocant's branch length.
346  
347 Type : Accessor
348 Title : get_branch_length
349 Usage : my $branch_length = $node->get_branch_length;
350 Function: Retrieves a node's branch length.
351 Returns : FLOAT
352 Args : NONE
353 Comments: Test for "defined($node->get_branch_length)"
354 for zero-length (but defined) branches. Testing
355 "if ( $node->get_branch_length ) { ... }"
356 yields false for zero-but-defined branches!
357  
358 =cut
359  
360 59233 ▶ sub get_branch_length { return $get_branch_length->(shift) }
Subroutine get_branch_length: called CC=1 SCAR=0.0
Pod documented
361  
362 =item get_children()
363  
364 Gets invocant's immediate children.
365  
366 Type : Query
367 Title : get_children
368 Usage : my @children = @{ $node->get_children };
369 Function: Returns an array reference of immediate
370 descendants, ordered from left to right.
371 Returns : Array reference of
372 Bio::Phylo::Forest::Node objects.
373 Args : NONE
374  
375 =cut
376  
377 122564 ▶ sub get_children { return $get_children->(shift) }
Subroutine get_children: called CC=1 SCAR=0.0
Pod documented
378
379 =item get_tree()
380  
381 Returns the tree invocant belongs to
382  
383 Type : Query
384 Title : get_tree
385 Usage : my $tree = $node->get_tree;
386 Function: Returns the tree $node belongs to
387 Returns : Bio::Phylo::Forest::Tree
388 Args : NONE
389  
390 =cut
391  
392 143 ▶ sub get_tree {
Subroutine get_tree: called CC=1 SCAR=0.0
Pod documented
393 143 my $self = shift;
394 143 my $id = $self->get_id;
395 143 return $tree{$id};
396 }
397  
398 =item get_rank()
399  
400 Gets the taxonomic rank of the node
401  
402 Type : Mutator
403 Title : get_rank
404 Usage : my $rank = $node->get_rank;
405 Function: Gets the taxonomic rank of the node
406 Returns : String
407 Args : NONE
408 Comments:
409  
410 =cut
411
412 2 ▶ sub get_rank { $rank{shift->get_id} }
Subroutine get_rank: called CC=1 SCAR=0.0
Pod documented
413  
414 =begin comment
415  
416 Type : Internal method
417 Title : _json_data
418 Usage : $node->_json_data;
419 Function: Populates a data structure to be serialized as JSON
420 Returns :
421 Args :
422  
423 =end comment
424  
425 =cut
426
427 0 ▶ sub _json_data {
Subroutine _json_data: not called CC=3 SCAR=24.8
428 0 my $self = shift;
429 0 my %result = %{ $self->SUPER::_json_data };
430 0 ▶ $result{'length'} = $self->get_branch_length if defined $self->get_branch_length;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->get_branch_length 0 0 0
431 0 ▶ $result{'rank'} = $self->get_rank if $self->get_rank;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_rank 0 0 0
432 0 $result{'children'} = [ map { $_->_json_data } @{ $self->get_children } ];
433 0 return \%result;
434 }
435  
436 =begin comment
437  
438 Type : Internal method
439 Title : _cleanup
440 Usage : $trees->_cleanup;
441 Function: Called during object destruction, for cleanup of instance data
442 Returns :
443 Args :
444  
445 =end comment
446  
447 =cut
448  
449 9119 ▶ sub _cleanup : Destructor {
Subroutine _cleanup: called CC=2 SCAR=6.9
450 9119 my $self = shift;
451 9119 my $id = $self->get_id;
452 9119 for my $field (@fields) {
453 36476 delete $field->{$id};
454 }
455 33 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
456
457 }
458  
459 =back
460  
461 # podinherit_insert_token
462  
463 =head1 SEE ALSO
464  
465 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
466 for any user or developer questions and discussions.
467  
468 =over
469  
470 =item L<Bio::Phylo::Forest::NodeRole>
471  
472 This object inherits from L<Bio::Phylo::Forest::NodeRole>, so methods
473 defined there are also applicable here.
474  
475 =item L<Bio::Phylo::Manual>
476  
477 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
478  
479 =back
480  
481 =head1 CITATION
482  
483 If you use Bio::Phylo in published research, please cite it:
484  
485 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
486 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
487 I<BMC Bioinformatics> B<12>:63.
488 L<http://dx.doi.org/10.1186/1471-2105-12-63>
489  
490 =cut
491  
492 33 1;
« Phylo/Parsers/Ubiosearch.pm ↑ Summary Phylo/Generator.pm »