trust your DB implementation, particularly if ancestor data are already available
[bioperl-live.git] / Bio / Tree / TreeI.pm
blobb26ee95c328c57115c33d1c2620c5844ff06ed5c
2 # BioPerl module for Bio::Tree::TreeI
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
14 =head1 NAME
16 Bio::Tree::TreeI - A Tree object suitable for lots of things, designed
17 originally for Phylogenetic Trees.
19 =head1 SYNOPSIS
21 # get a Bio::Tree::TreeI somehow
22 # like from a TreeIO
23 my $treeio = Bio::TreeIO->new(-format => 'newick', -file => 'treefile.dnd');
24 my $tree = $treeio->next_tree;
25 my @nodes = $tree->get_nodes;
26 my @leaves = $tree->get_leaf_nodes;
27 my $root = $tree->get_root_node;
29 =head1 DESCRIPTION
31 This object holds a pointer to the Root of a Tree which is a
32 Bio::Tree::NodeI.
34 =head1 FEEDBACK
36 =head2 Mailing Lists
38 User feedback is an integral part of the evolution of this and other
39 Bioperl modules. Send your comments and suggestions preferably to
40 the Bioperl mailing list. Your participation is much appreciated.
42 bioperl-l@bioperl.org - General discussion
43 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
45 =head2 Support
47 Please direct usage questions or support issues to the mailing list:
49 I<bioperl-l@bioperl.org>
51 rather than to the module maintainer directly. Many experienced and
52 reponsive experts will be able look at the problem and quickly
53 address it. Please include a thorough description of the problem
54 with code and data examples if at all possible.
56 =head2 Reporting Bugs
58 Report bugs to the Bioperl bug tracking system to help us keep track
59 of the bugs and their resolution. Bug reports can be submitted via
60 the web:
62 https://github.com/bioperl/bioperl-live/issues
64 =head1 AUTHOR - Jason Stajich
66 Email jason@bioperl.org
68 =head1 CONTRIBUTORS
70 Aaron Mackey, amackey@virginia.edu
71 Elia Stupka, elia@fugu-sg.org
72 Sendu Bala, bix@sendu.me.uk
74 =head1 APPENDIX
76 The rest of the documentation details each of the object methods.
77 Internal methods are usually preceded with a _
79 =cut
82 # Let the code begin...
85 package Bio::Tree::TreeI;
86 use strict;
88 use base qw(Bio::Root::RootI);
90 =head2 get_nodes
92 Title : get_nodes
93 Usage : my @nodes = $tree->get_nodes()
94 Function: Return list of Tree::NodeI objects
95 Returns : array of Tree::NodeI objects
96 Args : (named values) hash with one value
97 order => 'b|breadth' first order or 'd|depth' first order
99 =cut
101 sub get_nodes{
102 my ($self) = @_;
103 $self->throw_not_implemented();
106 =head2 get_root_node
108 Title : get_root_node
109 Usage : my $node = $tree->get_root_node();
110 Function: Get the Top Node in the tree, in this implementation
111 Trees only have one top node.
112 Returns : Bio::Tree::NodeI object
113 Args : none
115 =cut
117 sub get_root_node{
118 my ($self) = @_;
119 $self->throw_not_implemented();
122 =head2 number_nodes
124 Title : number_nodes
125 Usage : my $size = $tree->number_nodes
126 Function: Find the number of nodes in the tree.
127 Returns : int
128 Args : none
130 =cut
132 sub number_nodes{
133 my ($self) = @_;
134 my $root = $self->get_root_node;
135 if( defined $root && $root->isa('Bio::Tree::NodeI')) {
136 return ($root->descendent_count + 1);
138 return 0;
141 =head2 total_branch_length
143 Title : total_branch_length
144 Usage : my $size = $tree->total_branch_length
145 Function: Returns the sum of the length of all branches
146 Returns : integer
147 Args : none
149 =cut
151 sub total_branch_length {
152 my ($self) = @_;
153 $self->throw_not_implemented();
156 =head2 height
158 Title : height
159 Usage : my $height = $tree->height
160 Function: Gets the height of tree - this LOG_2($number_nodes)
161 WARNING: this is only true for strict binary trees. The TreeIO
162 system is capable of building non-binary trees, for which this
163 method will currently return an incorrect value!!
164 Returns : integer
165 Args : none
167 =cut
169 sub height{
170 my ($self) = @_;
171 my $nodect = $self->number_nodes;
172 return 0 if( ! $nodect );
173 return log($nodect) / log(2);
176 =head2 id
178 Title : id
179 Usage : my $id = $tree->id();
180 Function: An id value for the tree
181 Returns : scalar
182 Args :
185 =cut
187 sub id{
188 my ($self,@args) = @_;
189 $self->throw_not_implemented();
192 =head2 score
194 Title : score
195 Usage : $obj->score($newval)
196 Function: Sets the associated score with this tree
197 This is a generic slot which is probably best used
198 for log likelihood or other overall tree score
199 Returns : value of score
200 Args : newvalue (optional)
203 =cut
205 sub score{
206 my ($self,$value) = @_;
207 $self->throw_not_implemented();
210 =head2 get_leaf_nodes
212 Title : get_leaf_nodes
213 Usage : my @leaves = $tree->get_leaf_nodes()
214 Function: Returns the leaves (tips) of the tree
215 Returns : Array of Bio::Tree::NodeI objects
216 Args : none
219 =cut
221 sub get_leaf_nodes{
222 my ($self) = @_;
223 return grep { $_->is_Leaf() } $self->get_nodes(-sortby => 'none');
227 =head2 Methods for associating Tag/Values with a Tree
229 These methods associate tag/value pairs with a Tree
231 =head2 set_tag_value
233 Title : set_tag_value
234 Usage : $tree->set_tag_value($tag,$value)
235 $tree->set_tag_value($tag,@values)
236 Function: Sets a tag value(s) to a tree. Replaces old values.
237 Returns : number of values stored for this tag
238 Args : $tag - tag name
239 $value - value to store for the tag
241 =cut
243 sub set_tag_value{
244 shift->throw_not_implemented();
247 =head2 add_tag_value
249 Title : add_tag_value
250 Usage : $tree->add_tag_value($tag,$value)
251 Function: Adds a tag value to a tree
252 Returns : number of values stored for this tag
253 Args : $tag - tag name
254 $value - value to store for the tag
257 =cut
259 sub add_tag_value{
260 shift->throw_not_implemented();
263 =head2 remove_tag
265 Title : remove_tag
266 Usage : $tree->remove_tag($tag)
267 Function: Remove the tag and all values for this tag
268 Returns : boolean representing success (0 if tag does not exist)
269 Args : $tag - tagname to remove
272 =cut
274 sub remove_tag {
275 shift->throw_not_implemented();
278 =head2 remove_all_tags
280 Title : remove_all_tags
281 Usage : $tree->remove_all_tags()
282 Function: Removes all tags
283 Returns : None
284 Args : None
287 =cut
289 sub remove_all_tags{
290 shift->throw_not_implemented();
293 =head2 get_all_tags
295 Title : get_all_tags
296 Usage : my @tags = $tree->get_all_tags()
297 Function: Gets all the tag names for this Tree
298 Returns : Array of tagnames
299 Args : None
302 =cut
304 sub get_all_tags {
305 shift->throw_not_implemented();
308 =head2 get_tag_values
310 Title : get_tag_values
311 Usage : my @values = $tree->get_tag_values($tag)
312 Function: Gets the values for given tag ($tag)
313 Returns : Array of values or empty list if tag does not exist
314 Args : $tag - tag name
317 =cut
319 sub get_tag_values{
320 shift->throw_not_implemented();
323 =head2 has_tag
325 Title : has_tag
326 Usage : $tree->has_tag($tag)
327 Function: Boolean test if tag exists in the Tree
328 Returns : Boolean
329 Args : $tag - tagname
332 =cut
334 sub has_tag{
335 shift->throw_not_implemented();