Tree.py - A phylogenetic tree

The Tree is derived from the class from Bio.Nexus.Trees.Tree adding some additional functionality.

Reference

Tree.updateNexus(nexus)

change trees in a nexus object (see Biopython_) to Tree.

Tree.Nop(x)

empty function for tree traversal

class Tree.Tree(*args, **kwargs)

Bases: Bio.Nexus.Trees.Tree

A phylogenetic tree.

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

Ntree(self,tree).

root_at_node(node, distance=0)

root tree at node.

Parameters
  • node – New root

  • distance (float) – Distance of node to new root.

  • is a subset of the code taken from root_with_outgroup. (This) –

to_string(support_as_branchlengths=False, branchlengths_only=False, plain=True, write_all_taxa=False, branchlength_format='%1.5f', support_format='%1.2f', format='nexus')

Return a paup compatible tree line.

Parameters
  • support_as_branchlengths (bool) – If true, output bootstrap support value as branch lengths.

  • branchlengths_only (bool) – Only output branchlengths, no support values

  • plain (bool) – Output plain tree (no branch lengths/support values).

  • write_all_taxa (bool) – If true, internal node names are output

  • branchlength_format (string) – Format to use for branch lengths

  • support_format (string) – Format to use for bootstrap support values

  • format (string) – Either nexus on NHX.

Returns

tree – A PAUP compatible tree line.

Return type

string

get_nodes(node_id)

Return a list of nodes downwards from a node (self, node_id).

The list includes the given node_id.

get_leaves(node_id)

Return a list of leaf nodes downward from a node (self, node_id).

root_midpoint()

perform midpoint rooting of tree.

The root is placed at equal distance to the two leaves furthest apart in the tree (centroid of the tree).

getNumLeaves()

return list with number of leaves beyond each node

root_balanced()

perform balanced rooting of tree.

The root is placed such that the number of leaves on either side of the tree is equal (or at most different by 1).

dfs(node_id, pre_function=<function Nop>, descend_condition=<function Nop>, post_function=<function Nop>)

dfs tree tree traversal starting at node_id.

Apply functions pre_function at first and post_function at last visit of a node.

writeToFile(outfile, with_branchlengths=True, format='nh')

write a tree to a file.

truncate(node_id, taxon=None, keep_node=None)

truncate tree at node_id.

This function will not change any branch lengths. If keep is given, single child nodes will be collapsed until keep_node is reached.

relabel(map_old2new, warn=False)

relabel taxa in tree using the provided mapping.

rescaleBranchLengths(value)

rescale branch length so that they sum up to value.