Bio.Nexus.Trees module

Tree class to handle phylogenetic trees.

Provides a set of methods to read and write newick-format tree descriptions, get information about trees (monphyly of taxon sets, congruence between trees, common ancestors,…) and to manipulate trees (re-root trees, split terminal nodes).

exception Bio.Nexus.Trees.TreeError

Bases: Exception

Provision for the management of Tree exceptions.

class Bio.Nexus.Trees.NodeData(taxon=None, branchlength=0.0, support=None, comment=None)

Bases: object

Store tree-relevant data associated with nodes (e.g. branches or otus).

__init__(taxon=None, branchlength=0.0, support=None, comment=None)

Initialize the class.

class Bio.Nexus.Trees.Tree(tree=None, weight=1.0, rooted=False, name='', data=NodeData, values_are_support=False, max_support=1.0)

Bases: Chain

Represent a tree using a chain of nodes with on predecessor (=ancestor) and multiple successors (=subclades).

__init__(tree=None, weight=1.0, rooted=False, name='', data=NodeData, values_are_support=False, max_support=1.0)

Ntree(self,tree).

node(node_id)

Return the instance of node_id.

node = node(self,node_id)

split(parent_id=None, n=2, branchlength=1.0)

Speciation: generates n (default two) descendants of a node.

[new ids] = split(self,parent_id=None,n=2,branchlength=1.0):

search_taxon(taxon)

Return the first matching taxon in self.data.taxon. Not restricted to terminal nodes.

node_id = search_taxon(self,taxon)

prune(taxon)

Prune a terminal taxon from the tree.

id_of_previous_node = prune(self,taxon) If taxon is from a bifurcation, the connectiong node will be collapsed and its branchlength added to remaining terminal node. This might be no longer a meaningful value’

get_taxa(node_id=None)

Return a list of all otus downwards from a node.

nodes = get_taxa(self,node_id=None)

get_terminals()

Return a list of all terminal nodes.

is_terminal(node)

Return True if node is a terminal node.

is_internal(node)

Return True if node is an internal node.

is_preterminal(node)

Return True if all successors of a node are terminal ones.

count_terminals(node=None)

Count the number of terminal nodes that are attached to a node.

collapse_genera(space_equals_underscore=True)

Collapse all subtrees which belong to the same genus.

(i.e share the same first word in their taxon name.)

sum_branchlength(root=None, node=None)

Add up the branchlengths from root (default self.root) to node.

sum = sum_branchlength(self,root=None,node=None)

set_subtree(node)

Return subtree as a set of nested sets.

sets = set_subtree(self,node)

is_identical(tree2)

Compare tree and tree2 for identity.

result = is_identical(self,tree2)

is_compatible(tree2, threshold, strict=True)

Compare branches with support>threshold for compatibility.

result = is_compatible(self,tree2,threshold)

common_ancestor(node1, node2)

Return the common ancestor that connects two nodes.

node_id = common_ancestor(self,node1,node2)

distance(node1, node2)

Add and return the sum of the branchlengths between two nodes.

dist = distance(self,node1,node2)

is_monophyletic(taxon_list)

Return node_id of common ancestor if taxon_list is monophyletic, -1 otherwise.

result = is_monophyletic(self,taxon_list)

is_bifurcating(node=None)

Return True if tree downstream of node is strictly bifurcating.

branchlength2support()

Move values stored in data.branchlength to data.support, and set branchlength to 0.0.

This is necessary when support has been stored as branchlength (e.g. paup), and has thus been read in as branchlength.

convert_absolute_support(nrep)

Convert absolute support (clade-count) to rel. frequencies.

Some software (e.g. PHYLIP consense) just calculate how often clades appear, instead of calculating relative frequencies.

has_support(node=None)

Return True if any of the nodes has data.support != None.

randomize(ntax=None, taxon_list=None, branchlength=1.0, branchlength_sd=None, bifurcate=True)

Generate a random tree with ntax taxa and/or taxa from taxlabels.

new_tree = randomize(self,ntax=None,taxon_list=None,branchlength=1.0,branchlength_sd=None,bifurcate=True) Trees are bifurcating by default. (Polytomies not yet supported).

display()

Quick and dirty lists of all nodes.

to_string(support_as_branchlengths=False, branchlengths_only=False, plain=True, plain_newick=False, ladderize=None, ignore_comments=True)

Return a paup compatible tree line.

__str__()

Short version of to_string(), gives plain tree.

unroot()

Define a unrooted Tree structure, using data of a rooted Tree.

root_with_outgroup(outgroup=None)

Define a tree’s root with a reference group outgroup.

merge_with_support(bstrees=None, constree=None, threshold=0.5, outgroup=None)

Merge clade support (from consensus or list of bootstrap-trees) with phylogeny.

tree=merge_bootstrap(phylo,bs_tree=<list_of_trees>) or tree=merge_bootstrap(phylo,consree=consensus_tree with clade support)

Bio.Nexus.Trees.consensus(trees, threshold=0.5, outgroup=None)

Compute a majority rule consensus tree of all clades with relative frequency>=threshold from a list of trees.