2 # BioPerl module for Bio::Tree::NodeI
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Jason Stajich <jason@bioperl.org>
8 # Copyright Jason Stajich
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
16 Bio::Tree::NodeI - Interface describing a Tree Node
20 # get a Tree::NodeI somehow
23 # read in a clustalw NJ in phylip/newick format
24 my $treeio = Bio::TreeIO->new(-format => 'newick', -file => 'file.dnd');
26 my $tree = $treeio->next_tree; # we'll assume it worked for demo purposes
27 # you might want to test that it was defined
29 my $rootnode = $tree->get_root_node;
31 # process just the next generation
32 foreach my $node ( $rootnode->each_Descendent() ) {
33 print "branch len is ", $node->branch_length, "\n";
36 # process all the children
37 my $example_leaf_node;
38 foreach my $node ( $rootnode->get_all_Descendents() ) {
39 if( $node->is_Leaf ) {
40 print "node is a leaf ... ";
41 # for example use below
42 $example_leaf_node = $node unless defined $example_leaf_node;
44 print "branch len is ", $node->branch_length, "\n";
47 # The ancestor() method points to the parent of a node
48 # A node can only have one parent
50 my $parent = $example_leaf_node->ancestor;
52 # parent won't likely have an description because it is an internal node
53 # but child will because it is a leaf
55 print "Parent id: ", $parent->id," child id: ",
56 $example_leaf_node->id, "\n";
61 A NodeI is capable of the basic structure of building a tree and
62 storing the branch length between nodes. The branch length is the
63 length of the branch between the node and its ancestor, thus a root
64 node in a Tree will not typically have a valid branch length.
66 Various implementations of NodeI may extend the basic functions and
67 allow storing of other information (like attaching a species object
68 or full sequences used to build a tree or alternative sequences). If
69 you don't know how to extend a Bioperl object please ask, happy to
70 help, we would also greatly appreciate contributions with improvements
71 or extensions of the objects back to the Bioperl code base so that
72 others don't have to reinvent your ideas.
79 User feedback is an integral part of the evolution of this and other
80 Bioperl modules. Send your comments and suggestions preferably to
81 the Bioperl mailing list. Your participation is much appreciated.
83 bioperl-l@bioperl.org - General discussion
84 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
88 Please direct usage questions or support issues to the mailing list:
90 I<bioperl-l@bioperl.org>
92 rather than to the module maintainer directly. Many experienced and
93 reponsive experts will be able look at the problem and quickly
94 address it. Please include a thorough description of the problem
95 with code and data examples if at all possible.
99 Report bugs to the Bioperl bug tracking system to help us keep track
100 of the bugs and their resolution. Bug reports can be submitted via
103 https://github.com/bioperl/bioperl-live/issues
105 =head1 AUTHOR - Jason Stajich
107 Email jason@bioperl.org
111 Aaron Mackey amackey@virginia.edu
115 The rest of the documentation details each of the object methods.
116 Internal methods are usually preceded with a _
120 # Let the code begin...
122 package Bio
::Tree
::NodeI
;
125 no warnings
'recursion';
127 use base
qw(Bio::Root::RootI);
129 =head2 add_Descendent
131 Title : add_Descendent
132 Usage : $node->add_Descendent($node);
133 Function: Adds a descendent to a node
134 Returns : number of current descendents for this node
135 Args : Bio::Node::NodeI
141 my ($self,@args) = @_;
143 $self->throw_not_implemented();
147 =head2 each_Descendent
149 Title : each_Descendent
150 Usage : my @nodes = $node->each_Descendent;
151 Function: all the descendents for this Node (but not their descendents
152 i.e. not a recursive fetchall)
153 Returns : Array of Bio::Tree::NodeI objects
160 $self->throw_not_implemented();
163 =head2 Decorated Interface methods
167 =head2 get_all_Descendents
169 Title : get_all_Descendents($sortby)
170 Usage : my @nodes = $node->get_all_Descendents;
171 Function: Recursively fetch all the nodes and their descendents
172 *NOTE* This is different from each_Descendent
173 Returns : Array or Bio::Tree::NodeI objects
174 Args : $sortby [optional] "height", "creation", "alpha", "revalpha",
175 or a coderef to be used to sort the order of children nodes.
179 sub get_all_Descendents
{
180 my ($self, $sortby) = @_;
183 foreach my $node ( $self->each_Descendent($sortby) ) {
184 push @nodes, ($node,$node->get_all_Descendents($sortby));
189 *get_Descendents
= \
&get_all_Descendents
;
194 Usage : if( $node->is_Leaf )
195 Function: Get Leaf status
203 $self->throw_not_implemented();
206 =head2 descendent_count
208 Title : descendent_count
209 Usage : my $count = $node->descendent_count;
210 Function: Counts the number of descendents a node has
211 (and all of their subnodes)
217 sub descendent_count
{
221 foreach my $node ( $self->each_Descendent ) {
223 $node->can('descendent_count') ?
$count += $node->descendent_count : next;
231 Usage : my $str = $node->to_string()
232 Function: For debugging, provide a node as a string
241 return join('',defined $self->id_output ?
$self->id_output : '',
242 defined $self->branch_length ?
':' . $self->branch_length
249 Usage : my $len = $node->height
250 Function: Returns the height of the tree starting at this
251 node. Height is the maximum branchlength to get to the tip.
252 Returns : The longest length (weighting branches with branch_length) to a leaf
260 return 0 if( $self->is_Leaf );
263 foreach my $subnode ( $self->each_Descendent ) {
264 my $s = $subnode->height + $subnode->branch_length;;
265 if( $s > $max ) { $max = $s; }
273 Usage : my $len = $node->depth
274 Function: Returns the depth of the tree starting at this
275 node. Depth is the distance from this node to the root.
276 Returns : The branch length to the root.
286 while( defined $node->ancestor ) {
287 $depth += $node->branch_length;
288 $node = $node->ancestor;
293 =head2 Get/Set methods
299 Title : branch_length
300 Usage : $obj->branch_length()
301 Function: Get/Set the branch length
302 Returns : value of branch_length
303 Args : newvalue (optional)
310 $self->throw_not_implemented();
316 Usage : $obj->id($newval)
317 Function: The human readable identifier for the node
318 Returns : value of human readable id
319 Args : newvalue (optional)
326 $self->throw_not_implemented();
332 Usage : my $internalid = $node->internal_id
333 Function: Returns the internal unique id for this Node
341 $self->throw_not_implemented();
347 Usage : $obj->description($newval)
348 Function: Get/Set the description string
349 Returns : value of description
350 Args : newvalue (optional)
357 $self->throw_not_implemented();
363 Usage : $obj->bootstrap($newval)
364 Function: Get/Set the bootstrap value
365 Returns : value of bootstrap
366 Args : newvalue (optional)
373 $self->throw_not_implemented();
379 Usage : my $node = $node->ancestor;
380 Function: Get/Set the ancestor node pointer for a Node
381 Returns : Null if this is top level node
388 my ($self,@args) = @_;
389 $self->throw_not_implemented();
392 =head2 invalidate_height
394 Title : invalidate_height
395 Usage : private helper method
396 Function: Invalidate our cached value of the node height in the tree
402 sub invalidate_height
{
403 shift->throw_not_implemented();
406 =head2 Methods for associating Tag/Values with a Node
408 These methods associate tag/value pairs with a Node
412 Title : set_tag_value
413 Usage : $node->set_tag_value($tag,$value)
414 $node->set_tag_value($tag,@values)
415 Function: Sets a tag value(s) to a node. Replaces old values.
416 Returns : number of values stored for this tag
417 Args : $tag - tag name
418 $value - value to store for the tag
423 shift->throw_not_implemented();
428 Title : add_tag_value
429 Usage : $node->add_tag_value($tag,$value)
430 Function: Adds a tag value to a node
431 Returns : number of values stored for this tag
432 Args : $tag - tag name
433 $value - value to store for the tag
439 shift->throw_not_implemented();
445 Usage : $node->remove_tag($tag)
446 Function: Remove the tag and all values for this tag
447 Returns : boolean representing success (0 if tag does not exist)
448 Args : $tag - tagname to remove
454 shift->throw_not_implemented();
457 =head2 remove_all_tags
459 Title : remove_all_tags
460 Usage : $node->remove_all_tags()
461 Function: Removes all tags
469 shift->throw_not_implemented();
475 Usage : my @tags = $node->get_all_tags()
476 Function: Gets all the tag names for this Node
477 Returns : Array of tagnames
484 shift->throw_not_implemented();
487 =head2 get_tag_values
489 Title : get_tag_values
490 Usage : my @values = $node->get_tag_values($tag)
491 Function: Gets the values for given tag ($tag)
492 Returns : Array of values or empty list if tag does not exist
493 Args : $tag - tag name
499 shift->throw_not_implemented();
505 Usage : $node->has_tag($tag)
506 Function: Boolean test if tag exists in the Node
508 Args : $tag - tagname
514 shift->throw_not_implemented();
518 =head2 Helper Functions
525 Usage : my $id = $node->id_output;
526 Function: Return an id suitable for output in format like newick
527 so that if it contains spaces or ():; characters it is properly
529 Returns : $id string if $node->id has a value
538 return unless( defined $id && length($id ) );
539 # single quotes must become double quotes
541 if( $id =~ /[\(\);:,\s]/ ) {