biotite.sequence.phylo.Tree

class biotite.sequence.phylo.Tree(root)

Bases: biotite.copyable.Copyable

A Tree represents a rooted tree (e.g. alignment guide 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).

The amount of leaves in a tree can be determined via the len() function.

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
rootTreeNode

The root node of the tree.

leaveslist 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(self)

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
newickstr

The Newick notation to create the tree from.

labelslist 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
treeTree

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 intermediate nodes. If the string contains such labels, they are discarded.

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, index2int

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

topologicalbool, optional

If True the topological distance is measured, i.e. all child-parent distance is 1. Otherwise, the distances from the distance attribute are used.

Returns
distancefloat

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
labelsiterable object of str

The labels the indices in the leaf nodes srefer to

include_distancebool

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

Returns
newickstr

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);