biotite.sequence.phylo.Tree¶
- class biotite.sequence.phylo.Tree(root)[source]¶
Bases:
Copyable
A
Tree
represents a rooted tree (e.g. alignment guide tree or phylogenetic tree).The tree itself wraps a root
TreeNode
object, accessible via theroot
property.A
Tree
is not a container itself: Objects, e.g species names or sequences, that are represented by the nodes, cannot be stored directly in aTree
or its nodes. Instead, each leafTreeNode
has a reference index: These indices refer to a separate list or array, containing the actual reference objects.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
>>> objects = ["An object", "Another object", "Yet another one"] >>> 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; >>> print([objects[node.index] for node in tree.leaves]) ['An object', 'Another object', 'Yet another one']
- 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.
- as_graph()¶
Obtain a graph representation of the
Tree
.- Returns:
- bond_setDiGraph
A NetworkX directed graph. For a leaf node the graph node is its reference index. For an intermediate and root node the graph node is a tuple containing it children nodes. Each edge has a
"distance"
attribute depicting the distance between the nodes. Each edge starts from the parent ends at its child.
Examples
>>> leaves = [TreeNode(index=i) for i in range(3)] >>> intermediate = TreeNode([leaves[0], leaves[1]], [2.0, 3.0]) >>> root = TreeNode([intermediate, leaves[2]], [1.0, 5.0]) >>> tree = Tree(root) >>> graph = tree.as_graph() >>> for node_i, node_j in graph.edges: ... print(f"{str(node_i):12} -> {str(node_j):12}") (0, 1) -> 0 (0, 1) -> 1 ((0, 1), 2) -> (0, 1) ((0, 1), 2) -> 2
- copy()¶
Create a deep copy of this object.
- Returns:
- copy
A copy of this object.
- static 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 not 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.
- round_distanceint, optional
If set, the distances are rounded to the given number of digits.
- 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);