biotite.sequence.phylo.Tree

class biotite.sequence.phylo.Tree(root)

Bases: biotite.copyable.Copyable

A Tree represents a rooted binary tree (e.g. phylogentic tree). The tree itself is represented by TreeNode objects. The root node is accessible via the root property. The property leaves contains a list of the leaf nodes, where the index of the leaf node in this list is equal to the reference index of the leaf node (leaf.index).

Two Tree objects are equal if they are the same object, so the == operator is equal to the is operator.

Objects of this class are immutable.

Parameters:
root: TreeNode

The root of the tree. The constructor calls the node’s as_root() method, in order to make it immutable.

Examples

>>> leaf1 = TreeNode(index=0)
>>> leaf2 = TreeNode(index=1)
>>> leaf3 = TreeNode(index=2)
>>> inter = TreeNode(leaf1, leaf2, 5.0, 7.0)
>>> root  = TreeNode(inter, leaf3, 3.0, 10.0)
>>> tree  = Tree(root)
>>> print(tree)
((0:5.0,1:7.0):3.0,2:10.0):0.0;
Attributes:
root : TreeNode

The root node of the tree.

leaves : list of TreeNode

The leaf nodes of the tree. The index of the leaf node in this list is equal to the reference index of the leaf node. This attribute is a shallow copy of the repsective internal object.

copy()

Create a deep copy of this object.

Returns:
copy

A copy of this object.

from_newick(newick, labels=None)

Create a tree from a Newick notation.

Parameters:
newick : str

The Newick notation to create the tree from.

labels : list of str, optional

If the Newick notation contains labels, that are not parseable into reference indices, i.e. they are not integers, this parameter can be provided to convert these labels into reference indices. The corresponding index is the position of the label in the provided list.

Returns:
tree : Tree

A tree created from the Newick notation

Notes

This function does accept but does not require the Newick string to have the terminal semicolon. Keep in mind that the Tree class does support any labels on intermeiate nodes. If the string contains such labels, they are discarded. Furthermore, only binary trees can be parsed.

get_distance(index1, index2, topological=False)

Get the distance between two leaf nodes.

The distance is the sum of all distances from the each of the two nodes to their lowest common ancestor.

Parameters:
index1, index2

The reference indices of the two leaf nodes, to calculate the distance for.

Returns:
distance : float

The distance between the nodes.

Examples

>>> leaf1 = TreeNode(index=0)
>>> leaf2 = TreeNode(index=1)
>>> leaf3 = TreeNode(index=2)
>>> inter = TreeNode(leaf1, leaf2, 5.0, 7.0)
>>> root  = TreeNode(inter, leaf3, 3.0, 10.0)
>>> tree = Tree(root)
>>> print(tree.get_distance(0,1))
12.0
>>> print(tree.get_distance(0,2))
18.0
>>> print(tree.get_distance(1,2))
20.0
to_newick(labels=None, include_distance=True)

Obtain the Newick notation of the tree.

Parameters:
labels : iterable object of str

The labels the indices in the leaf nodes srefer to

include_distance : bool

If true, the distances are displayed in the newick notation, otherwise they are omitted.

Returns:
newick : str

The Newick notation of the tree.

Examples

>>> leaf1 = TreeNode(index=0)
>>> leaf2 = TreeNode(index=1)
>>> leaf3 = TreeNode(index=2)
>>> inter = TreeNode(leaf1, leaf2, 5.0, 7.0)
>>> root  = TreeNode(inter, leaf3, 3.0, 10.0)
>>> tree = Tree(root)
>>> print(tree.to_newick())
((0:5.0,1:7.0):3.0,2:10.0):0.0;
>>> print(tree.to_newick(include_distance=False))
((0,1),2);
>>> labels = ["foo", "bar", "foobar"]
>>> print(tree.to_newick(labels=labels, include_distance=False))
((foo,bar),foobar);