rejigger sort order so it doesn't require query to get the height (by default)
[bioperl-live.git] / Bio / Tree / TreeI.pm
blob2e328fc6b0ddb5338dcebb8d355f2b4625e20af6
1 # $Id$
3 # BioPerl module for Bio::Tree::TreeI
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
13 =head1 NAME
15 Bio::Tree::TreeI - A Tree object suitable for lots of things, designed
16 originally for Phylogenetic Trees.
18 =head1 SYNOPSIS
20 # get a Bio::Tree::TreeI somehow
21 # like from a TreeIO
22 my $treeio = new Bio::TreeIO(-format => 'newick', -file => 'treefile.dnd');
23 my $tree = $treeio->next_tree;
24 my @nodes = $tree->get_nodes;
25 my @leaves = $tree->get_leaf_nodes;
26 my $root = $tree->get_root_node;
28 =head1 DESCRIPTION
30 This object holds a pointer to the Root of a Tree which is a
31 Bio::Tree::NodeI.
33 =head1 FEEDBACK
35 =head2 Mailing Lists
37 User feedback is an integral part of the evolution of this and other
38 Bioperl modules. Send your comments and suggestions preferably to
39 the Bioperl mailing list. Your participation is much appreciated.
41 bioperl-l@bioperl.org - General discussion
42 http://bioperl.org/MailList.shtml - About the mailing lists
44 =head2 Reporting Bugs
46 Report bugs to the Bioperl bug tracking system to help us keep track
47 of the bugs and their resolution. Bug reports can be submitted via
48 the web:
50 http://bugzilla.bioperl.org/
52 =head1 AUTHOR - Jason Stajich
54 Email jason@bioperl.org
56 =head1 CONTRIBUTORS
58 Aaron Mackey amackey@virginia.edu
59 Elia Stupka, elia@fugu-sg.org
61 =head1 APPENDIX
63 The rest of the documentation details each of the object methods.
64 Internal methods are usually preceded with a _
66 =cut
69 # Let the code begin...
72 package Bio::Tree::TreeI;
73 use Bio::Tree::NodeI;
74 use vars qw(@ISA);
75 use strict;
77 @ISA = qw(Bio::Tree::NodeI);
79 =head2 get_nodes
81 Title : get_nodes
82 Usage : my @nodes = $tree->get_nodes()
83 Function: Return list of Tree::NodeI objects
84 Returns : array of Tree::NodeI objects
85 Args : (named values) hash with one value
86 order => 'b|breadth' first order or 'd|depth' first order
88 =cut
90 sub get_nodes{
91 my ($self) = @_;
92 $self->throw_not_implemented();
95 =head2 get_root_node
97 Title : get_root_node
98 Usage : my $node = $tree->get_root_node();
99 Function: Get the Top Node in the tree, in this implementation
100 Trees only have one top node.
101 Returns : Bio::Tree::NodeI object
102 Args : none
104 =cut
106 sub get_root_node{
107 my ($self) = @_;
108 $self->throw_not_implemented();
111 =head2 number_nodes
113 Title : number_nodes
114 Usage : my $size = $tree->number_nodes
115 Function: Returns the number of nodes
116 Example :
117 Returns :
118 Args :
121 =cut
123 sub number_nodes{
124 my ($self) = @_;
125 my $root = $self->get_root_node;
126 if( defined $root && $root->isa('Bio::Tree::NodeI')) {
127 return $root->descendent_count;
129 return 0;
132 =head2 total_branch_length
134 Title : total_branch_length
135 Usage : my $size = $tree->total_branch_length
136 Function: Returns the sum of the length of all branches
137 Returns : integer
138 Args : none
140 =cut
142 sub total_branch_length {
143 my ($self) = @_;
144 $self->throw_not_implemented();
147 =head2 height
149 Title : height
150 Usage : my $height = $tree->height
151 Function: Gets the height of tree - this LOG_2($number_nodes)
152 WARNING: this is only true for strict binary trees. The TreeIO
153 system is capable of building non-binary trees, for which this
154 method will currently return an incorrect value!!
155 Returns : integer
156 Args : none
158 =cut
160 sub height{
161 my ($self) = @_;
162 my $nodect = $self->number_nodes;
163 return 0 if( ! $nodect );
164 return log($nodect) / log(2);
167 =head2 id
169 Title : id
170 Usage : my $id = $tree->id();
171 Function: An id value for the tree
172 Returns : scalar
173 Args :
176 =cut
178 sub id{
179 my ($self,@args) = @_;
180 $self->throw_not_implemented();
183 =head2 score
185 Title : score
186 Usage : $obj->score($newval)
187 Function: Sets the associated score with this tree
188 This is a generic slot which is probably best used
189 for log likelihood or other overall tree score
190 Returns : value of score
191 Args : newvalue (optional)
194 =cut
196 sub score{
197 my ($self,$value) = @_;
198 $self->throw_not_implemented();
201 =head2 get_leaf_nodes
203 Title : get_leaf_nodes
204 Usage : my @leaves = $tree->get_leaf_nodes()
205 Function: Returns the leaves (tips) of the tree
206 Returns : Array of Bio::Tree::NodeI objects
207 Args : none
210 =cut
212 sub get_leaf_nodes{
213 my ($self) = @_;
214 return grep { $_->is_Leaf() } $self->get_nodes(-sortby => 'none');