Bio::Taxonomy
Tree
Toolbar
Summary
Bio::Taxonomy::Tree - An Organism Level Implementation of TreeI interface.
Package variables
No package variables defined.
Included modules
Inherit
Synopsis
# like from a TreeIO
my $treeio = new Bio::TreeIO(-format => 'newick', -file => 'treefile.dnd');
my $tree = $treeio->next_tree;
my @nodes = $tree->get_nodes;
my $root = $tree->get_root_node;
my @leaves = $tree->get_leaves;
Description
This object holds handles to Taxonomic Nodes which make up a tree.
Methods
Methods description
Title : add_branch Usage : $tree->add_branch($node,boolean) Function: Return a of Bio::Taxonomy::Tree object with a new branch added Returns : array of Bio::Taxonomy::Taxon objects of the resulting tree Args : Bio::Taxonomy::Taxon object boolean flag to force overwrite of descendent (see Bio::Node->add_Descendent) |
Title : add_species Usage : @nodes = $tree->add_species($species,$taxonomy) Function: Return a of Bio::Taxonomy::Tree object with a new species added Returns : array of Bio::Taxonomy::Taxon added to tree Args : Bio::Species object |
Title : get_leaves Usage : my @nodes = $tree->get_leaves() Function: Return list of Bio::Taxonomy::Taxon objects Returns : array of Bio::Taxonomy::Taxon objects Args : |
Title : get_nodes Usage : my @nodes = $tree->get_nodes() Function: Return list of Bio::Taxonomy::Taxon objects Returns : array of Bio::Taxonomy::Taxon objects Args : (named values) hash with one value order => 'b|breadth' first order or 'd|depth' first order |
Title : get_root_node Usage : my $node = $tree->get_root_node(); Function: Get the Top Node in the tree, in this implementation Trees only have one top node. Returns : Bio::Taxonomy::Taxon object Args : none |
Title : make_branch Usage : $tree->make_branch($node) Function: Make a linear Bio::Taxonomy::Tree object from a leafish node Returns : Args : Bio::Taxonomy::Taxon object |
Title : make_species_branch Usage : @nodes = $tree->make_species_branch($species,$taxonomy) Function: Return list of Bio::Taxonomy::Taxon objects based on a Bio::Species object Returns : array of Bio::Taxonomy::Taxon objects Args : Bio::Species and Bio::Taxonomy objects |
Title : new Usage : my $obj = new Bio::Taxonomy::Tree(); Function: Builds a new Bio::Taxonomy::Tree object Returns : Bio::Taxonomy::Tree Args : |
Title : remove_branch Usage : $tree->remove_branch($node) Function: remove a branch up to the next multifurcation Returns : Args : Bio::Taxonomy::Taxon object |
Title : set_root_node Usage : $tree->set_root_node($node) Function: Set the Root Node for the Tree Returns : Bio::Taxonomy::Taxon Args : Bio::Taxonomy::Taxon |
Title : splice Usage : @nodes = $tree->splice($tree) Function: Return a of Bio::Taxonomy::Tree object that is a fusion of two Returns : array of Bio::Taxonomy::Taxon added to tree Args : Bio::Taxonomy::Tree object |
Methods code
sub add_branch
{ my ($self,$node,$force) = @_;
my $best_node_level=0;
my ($best_node,@nodes,$common);
my @leaves=$self->get_leaves;
foreach my $leaf (@leaves) {
$common=$node->recent_common_ancestor($leaf); if (defined $common && ($common->distance_to_root > $best_node_level)) {
$best_node_level = $common->distance_to_root;
$best_node = $common;
}
}
return unless defined $best_node;
push @nodes,($self->get_root_node,$self->get_root_node->get_Descendents);
foreach my $node (@nodes) {
if ((defined $best_node->id && $best_node->id == $node->id) ||
($best_node->rank eq $node->rank && $best_node->taxon eq $node->taxon) &&
($best_node->rank ne 'no rank')) {
foreach my $descendent ($common->each_Descendent) {
$node->add_Descendent($descendent,$force);
}
}
$self->set_root_node($node) if $node->distance_to_root==0;
}
return ($common->get_Descendents); } |
sub add_species
{ my ($self,$species,$taxonomy) = @_;
my $branch=Bio::Taxonomy::Tree->new;
my @nodes=$branch->make_species_branch($species,$taxonomy);
my ($newleaf)=$branch->get_leaves;
return $self->add_branch($newleaf); } |
sub get_leaves
{ my ($self) = @_;
my $node = $self->get_root_node;
my @leaves;
my @children = ($node);
for (@children) {
push @children, $_->each_Descendent();
}
for (@children) {
push @leaves, $_ if $_->is_Leaf;
}
return @leaves; } |
sub get_nodes
{ my ($self, @args) = @_;
my ($order, $sortby) = $self->_rearrange([qw(ORDER SORTBY)],@args);
$order ||= 'depth';
$sortby ||= 'height';
if ($order =~ m/^b|(breadth)$/oi) { my $node = $self->get_root_node; my @children = ($node);
for (@children) {
push @children, $_->each_Descendent($sortby);
}
return @children;
}
if ($order =~ m/^d|(depth)$/oi) { # this is depth-first search I believe my $node = $self->get_root_node; my @children = ($node,$node->get_Descendents($sortby));
return @children;
} } |
sub get_root_node
{ my ($self) = @_;
return $self->{'_rootnode'}; } |
sub make_branch
{ my ($self,$node) = @_;
$self->get_root_node->remove_all_Descendents;
while (defined $node->ancestor) {
$self->set_root_node($node);
$node=$node->ancestor;
} } |
sub make_species_branch
{ my ($self,$species,$taxonomy) = @_;
if (! $species->isa('Bio::Species') ) {
$self->throw("Trying to classify $species which is not a Bio::Species object");
}
if (! $taxonomy->isa('Bio::Taxonomy') ) {
$self->throw("Trying to classify with $taxonomy which is not a Bio::Taxonomy object");
}
if (defined $self->get_root_node) {
$self->get_root_node->remove_all_Descendents;
}
my @nodes;
my @taxa=$taxonomy->classify($species);
for (my $i = 0; $i < @taxa; $i++) {
$nodes[$i]=Bio::Taxonomy::Taxon->new(-taxon => $taxa[$i][0],
-rank => $taxa[$i][1]);
}
for (my $i = 0; $i < @taxa-1; $i++) {
$nodes[$i]->add_Descendent($nodes[$i+1]);
}
$self->set_root_node($nodes[0]);
return @nodes; } |
sub new
{ my($class,@args) = @_;
my $self = $class->SUPER::new(@args);
$self->{'_rootnode'} = undef;
$self->{'_maxbranchlen'} = 0;
my ($root)= $self->_rearrange([qw(ROOT)], @args);
if( $root ) { $self->set_root_node($root); }
return $self; } |
sub remove_branch
{ my ($self,$node) = @_;
while (defined $node->ancestor) {
last if $node->ancestor->each_Descendent > 1;
$node=$node->ancestor;
}
$node->remove_all_Descendents; $node->ancestor->remove_Descendent($node);
}
1; } |
sub set_root_node
{ my ($self,$value) = @_;
if( defined $value ) {
if( ! $value->isa('Bio::Taxonomy::Taxon') ) {
$self->warn("Trying to set the root node to $value which is not a Bio::Taxonomy::Taxon");
return $self->get_root_node;
}
$self->{'_rootnode'} = $value;
}
return $self->get_root_node; } |
sub splice
{ my ($self,$tree) = @_;
my @nodes;
my @newleaves = $tree->get_leaves;
foreach my $leaf (@newleaves) {
push @nodes,$self->add_branch($leaf);
}
return @nodes; } |
General documentation
use Bio::Species;
use Bio::Taxonomy::Tree;
my $human=new Bio::Species;
my $chimp=new Bio::Species;
my $bonobo=new Bio::Species;
$human->classification(qw( sapiens Homo Hominidae
Catarrhini Primates Eutheria
Mammalia Euteleostomi Vertebrata
Craniata Chordata
Metazoa Eukaryota ));
$chimp->classification(qw( troglodytes Pan Hominidae
Catarrhini Primates Eutheria
Mammalia Euteleostomi Vertebrata
Craniata Chordata
Metazoa Eukaryota ));
$bonobo->classification(qw( paniscus Pan Hominidae
Catarrhini Primates Eutheria
Mammalia Euteleostomi Vertebrata
Craniata Chordata
Metazoa Eukaryota ));
# ranks passed to $taxonomy match ranks of species
my @ranks = ('superkingdom','kingdom','phylum','subphylum',
'no rank 1','no rank 2','class','no rank 3','order',
'suborder','family','genus','species');
my $taxonomy=new Bio::Taxonomy(-ranks => \@ranks,
-method => 'trust',
-order => -1);
my @nodes;
my $tree1=new Bio::Taxonomy::Tree;
my $tree2=new Bio::Taxonomy::Tree;
push @nodes,$tree1->make_species_branch($human,$taxonomy);
push @nodes,$tree2->make_species_branch($chimp,$taxonomy);
my ($homo_sapiens)=$tree1->get_leaves;
$tree1->splice($tree2);
push @nodes,$tree1->add_species($bonobo,$taxonomy);
my @taxa;
foreach my $leaf ($tree1->get_leaves) {
push @taxa,$leaf->taxon;
}
print join(", ",@taxa)."\n";
@taxa=();
$tree1->remove_branch($homo_sapiens);
foreach my $leaf ($tree1->get_leaves) {
push @taxa,$leaf->taxon;
}
print join(", ",@taxa)."\n";
See AUTHOR
AUTHOR - Dan Kortschak | Top |
Mainly Jason Stajich
The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _