3 # BioPerl module for Bio::Tree::Tree
5 # Cared for by Jason Stajich <jason@bioperl.org>
7 # Copyright Jason Stajich
9 # You may distribute this module under the same terms as perl itself
11 # POD documentation - main docs before the code
15 Bio::Tree::Tree - An Implementation of TreeI interface.
20 my $treeio = new Bio::TreeIO(-format => 'newick', -file => 'treefile.dnd');
21 my $tree = $treeio->next_tree;
22 my @nodes = $tree->get_nodes;
23 my $root = $tree->get_root_node;
28 This object holds handles to Nodes which make up a tree.
34 User feedback is an integral part of the evolution of this and other
35 Bioperl modules. Send your comments and suggestions preferably to
36 the Bioperl mailing list. Your participation is much appreciated.
38 bioperl-l@bioperl.org - General discussion
39 http://bioperl.org/MailList.shtml - About the mailing lists
43 Report bugs to the Bioperl bug tracking system to help us keep track
44 of the bugs and their resolution. Bug reports can be submitted via
47 http://bugzilla.bioperl.org/
49 =head1 AUTHOR - Jason Stajich
51 Email jason@bioperl.org
55 Aaron Mackey amackey@virginia.edu
59 The rest of the documentation details each of the object methods.
60 Internal methods are usually preceded with a _
65 # Let the code begin...
68 package Bio
::Tree
::Tree
;
72 # Object preamble - inherits from Bio::Root::Root
75 use Bio::Tree::TreeFunctionsI;
78 @ISA = qw(Bio::Root::Root Bio::Tree::TreeI Bio::Tree::TreeFunctionsI );
83 Usage : my $obj = new Bio::Tree::Tree();
84 Function: Builds a new Bio::Tree::Tree object
85 Returns : Bio::Tree::Tree
86 Args : -root => L<Bio::Tree::NodeI> object which is the root
87 -nodelete => boolean, whether or not to try and cleanup all
88 the nodes when this this tree goes out
90 -id => optional tree ID
91 -score => optional tree score value
96 my($class,@args) = @_;
98 my $self = $class->SUPER::new
(@args);
99 $self->{'_rootnode'} = undef;
100 $self->{'_maxbranchlen'} = 0;
101 $self->_register_for_cleanup(\
&cleanup_tree
);
102 my ($root,$nodel,$id,$score)= $self->_rearrange([qw(ROOT NODELETE
104 if( $root ) { $self->set_root_node($root); }
105 $self->nodelete($nodel || 0);
106 $self->id($id) if defined $id;
107 $self->score($score) if defined $score;
115 Usage : $obj->nodelete($newval)
116 Function: Get/Set Boolean whether or not to delete the underlying
117 nodes when it goes out of scope. By default this is false
118 meaning trees are cleaned up.
120 Args : on set, new boolean value
127 return $self->{'nodelete'} = shift if @_;
128 return $self->{'nodelete'};
134 Usage : my @nodes = $tree->get_nodes()
135 Function: Return list of Tree::NodeI objects
136 Returns : array of Tree::NodeI objects
137 Args : (named values) hash with one value
138 order => 'b|breadth' first order or 'd|depth' first order
143 my ($self, @args) = @_;
145 my ($order, $sortby) = $self->_rearrange([qw(ORDER SORTBY)],@args);
148 return () unless defined $self->get_root_node;
149 if ($order =~ m/^b|(breadth)$/oi) {
150 my $node = $self->get_root_node;
151 my @children = ($node);
153 push @children, $_->each_Descendent($sortby);
158 if ($order =~ m/^d|(depth)$/oi) {
159 # this is depth-first search I believe
160 my $node = $self->get_root_node;
161 my @children = ($node,$node->get_all_Descendents($sortby));
168 Title : get_root_node
169 Usage : my $node = $tree->get_root_node();
170 Function: Get the Top Node in the tree, in this implementation
171 Trees only have one top node.
172 Returns : Bio::Tree::NodeI object
180 return $self->{'_rootnode'};
185 Title : set_root_node
186 Usage : $tree->set_root_node($node)
187 Function: Set the Root Node for the Tree
188 Returns : Bio::Tree::NodeI
189 Args : Bio::Tree::NodeI
197 if( defined $value &&
198 ! $value->isa('Bio::Tree::NodeI') ) {
199 $self->warn("Trying to set the root node to $value which is not a Bio::Tree::NodeI");
200 return $self->get_root_node;
202 $self->{'_rootnode'} = $value;
204 return $self->get_root_node;
207 =head2 total_branch_length
209 Title : total_branch_length
210 Usage : my $size = $tree->total_branch_length
211 Function: Returns the sum of the length of all branches
217 sub total_branch_length
{
220 if( defined $self->get_root_node ) {
221 for ( $self->get_root_node->get_all_Descendents('none') ) {
222 $sum += $_->branch_length || 0;
231 Usage : my $id = $tree->id();
232 Function: An id value for the tree
234 Args : [optional] new value to set
240 my ($self,$val) = @_;
242 $self->{'_treeid'} = $val;
244 return $self->{'_treeid'};
250 Usage : $obj->score($newval)
251 Function: Sets the associated score with this tree
252 This is a generic slot which is probably best used
253 for log likelihood or other overall tree score
254 Returns : value of score
255 Args : newvalue (optional)
261 my ($self,$val) = @_;
263 $self->{'_score'} = $val;
265 return $self->{'_score'};
269 # decorated interface TreeI Implements this
274 Usage : my $height = $tree->height
275 Function: Gets the height of tree - this LOG_2($number_nodes)
276 WARNING: this is only true for strict binary trees. The TreeIO
277 system is capable of building non-binary trees, for which this
278 method will currently return an incorrect value!!
285 Usage : my $size = $tree->number_nodes
286 Function: Returns the number of nodes in the tree
294 # -- private internal methods --
298 unless( $self->nodelete ) {
299 for my $node ( $self->get_nodes(-order
=> 'b',
300 -sortby
=> 'none') ) {
301 $node->ancestor(undef);
305 $self->{'_rootnode'} = undef;