Permalink
Browse files

Add is_rooted method to TreeI and a rooted() getter / setter to Tree.…

… Auto-set the rooted flag according to convention in newick.pm.
  • Loading branch information...
Greg Jordan
Greg Jordan committed Nov 11, 2010
1 parent 5245f8d commit 1bec491c39b8bf1ba242e62b1e62742060942c4b
Showing with 62 additions and 0 deletions.
  1. +37 −0 Bio/Tree/Tree.pm
  2. +15 −0 Bio/Tree/TreeI.pm
  3. +10 −0 Bio/TreeIO/newick.pm
View
@@ -163,6 +163,43 @@ sub new {
return $self;
}
=head2 rooted
Title : rooted
Usage : $tree->rooted(0);
Function: Get/Set Boolean whether or not this tree should be considered
to be rooted or not. Note that the TreeI method is_rooted
is an interface to this getter.
Returns : boolean
Args : on set, new boolean value
=cut
sub rooted {
my $self = shift;
return $self->{'_rooted'} = shift if @_;
return $self->{'_rooted'};
}
=head2 is_rooted
Title : is_rooted
Usage : die unless ($tree->is_rooted);
Function: Indicates whether this should be handled as a rooted tree.
Returns : 1 if this tree is rooted; 0 if unrooted.
Args : none
=cut
sub is_rooted {
my $self = shift;
if (defined $self->{'_rooted'}) {
return $self->{'_rooted'};
} else {
# Default to a rooted tree.
return 1;
}
}
=head2 nodelete
View
@@ -87,6 +87,21 @@ use strict;
use base qw(Bio::Root::RootI);
=head2 is_rooted
Title : is_rooted
Usage : die unless ($tree->is_rooted);
Function: Indicates whether this should be handled as a rooted tree.
Returns : 1 if this tree is rooted; 0 if unrooted.
Args : none
=cut
sub is_rooted {
my ($self) = @_;
$self->throw_not_implemented();
}
=head2 get_nodes
Title : get_nodes
View
@@ -149,6 +149,16 @@ s/([^"]*)(".+?")([^"]*)/$despace->($1) . $dequote->($2) . $despace->($3)/egsx;
# Add the tree score afterwards if it exists.
if (defined $tree) {
$tree->score($score);
# Auto-set the rooted parameter according to the convention:
# (1) unrooted if the root node has exactly three children
# (2) rooted otherwise
$tree->rooted(1);
if (defined $tree->get_root_node) {
my $root_node_count = scalar($tree->get_root_node->each_Descendent);
$tree->rooted(0) if ($root_node_count == 3);
}
return $tree;
}
}

0 comments on commit 1bec491

Please sign in to comment.