biotite.sequence.phylo.TreeNode

class biotite.sequence.phylo.TreeNode(child1=None, child2=None, child1_distance=None, child2_distance=None, index=None)

Bases: object

TreeNode objects are part of a rooted binary tree (e.g. phylogentic tree). There are two TreeNode subtypes:

  • Leaf node - Cannot have child nodes but has an index referring to an array-like reference object.
  • Intermediate node - Has two child nodes but no reference index

This type is determined by the time of the object’s creation.

Every TreeNode has a reference to its parent node. A root node is node without a parent node, that is finalized using as_root(). The call of this function prevents that a the node can be used as child.

TreeNode objects are semi-immutable: The child nodes or the reference index are fixed at the time of creation. Only the parent can be set once, when the parent node is created. TreeNode objects that are finalized using as_root() are completely immutable.

All object properties are read-only.

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

Parameters:
child1, child2: TreeNode, optional

The childs of this node. As this causes the creation of an intermediate node, this parameter cannot be used in combination with index.

child1_distance, child2_distance: float, optional

The distances of the child nodes to this node. Must be set if child1 and child2 are set.

index: int, optional

Index to a reference array-like object (e.g. list of sequences or labels). Must be a positive integer. As this causes the creation of a leaf node, this parameter cannot be used in combination with the other parameters.

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)
>>> print(root)
((0:5.0,1:7.0):3.0,2:10.0):0.0
Attributes:
parent : TreeNode

The parent node. None if node has no parent.

childs : tuple(TreeNode, TreeNode)

The child nodes. None if node has no child nodes.

index : int

The index to a reference array-like object. None if node is not a leaf node.

distance : float

Distance to parent node. None if parent is Ǹone.

as_root()

Convert the node into a root node.

When a root node is used as child parameter in the construction of a potential parent node, a TreeError is raised.

copy()

Create a deep copy of this TreeNode.

The copy includes this node, its reference index and deep copies of its child nodes. The parent node and the distance to it is not included.

distance_to(node, topological=False)

Get the distance of this node to another node.

The distance is the sum of all distances from this and the other node to the lowest common ancestor.

Parameters:
node : TreeNode

The second node for distance calculation.

Returns:
distance : float

The distance of this node to node.

Raises:
TreeError

If the nodes have no common ancestor.

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)
>>> print(leaf1.distance_to(leaf2))
12.0
>>> print(leaf1.distance_to(leaf3))
18.0
static from_newick(newick, labels=None)

Create a node and all its child nodes from a Newick notation.

Parameters:
newick : str

The Newick notation to create the node 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

The provided Newick notation must not have a terminal semicolon. If you have a Newick notation that covers an entire tree, you may use the same method in the Tree class instead. Keep in mind that the TreeNode class does support any labels on intermediate nodes. If the string contains such labels, they are discarded. Furthermore, only binary trees can be parsed.

get_indices()

Get an array of reference indices that leaf nodes of this node contain.

This method identifies all leaf nodes, which have this node as ancestor and puts the contained indices into an array. If this node is a leaf node itself, the array contains the reference index of this node as single element.

Returns:
indices : ndarray, dtype=int32

The reference indices of direct and indirect child leaf nodes of this node.

Examples

>>> leaf0 = TreeNode(index=0)
>>> leaf1 = TreeNode(index=1)
>>> leaf2 = TreeNode(index=2)
>>> leaf3 = TreeNode(index=3)
>>> intr0 = TreeNode(leaf0, leaf2, 0, 0)
>>> intr1 = TreeNode(leaf1, leaf3, 0, 0)
>>> root  = TreeNode(intr0, intr1, 0, 0)
>>> print(leaf0.get_indices())
[0]
>>> print(intr0.get_indices())
[0 2]
>>> print(intr1.get_indices())
[1 3]
>>> print(root.get_indices())
[0 2 1 3]
get_leaf_count()

” get_leaf_count()

Get the number of direct or indirect leaves of this ńode.

This method identifies all leaf nodes, which have this node as ancestor. If this node is a leaf node itself, 1 is returned.

get_leaves()

Get a list of leaf nodes that are direct or indirect child nodes of this node.

This method identifies all leaf nodes, which have this node as ancestor. If this node is a leaf node itself, the list contains this node as single element.

Returns:
leaf_nodes : list

The leaf nodes, that are direct or indirect child nodes of this node.

is_leaf()

Check if the node is a leaf node.

Returns:
is_leaf : bool

True if the node is a leaf node, false otherwise.

is_root()

Check if the node is a root node.

Returns:
is_root : bool

True if the node is a root node, false otherwise.

lowest_common_ancestor(node)

Get the lowest common ancestor of this node and another node.

Parameters:
node : TreeNode

The node to get the lowest common ancestor with.

Returns:
ancestor : TreeNode

The lowest common ancestor. None if the nodes have no common ancestor, i.e. they are not in the same tree

to_newick(labels=None, include_distance=True)

Obtain the node represented in Newick notation.

The terminal semicolon is not included.

Parameters:
labels : iterable object of str

The labels the indices in the leaf nodes refer 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 node.

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)
>>> print(root.to_newick())
((0:5.0,1:7.0):3.0,2:10.0):0.0
>>> print(root.to_newick(include_distance=False))
((0,1),2)
>>> labels = ["foo", "bar", "foobar"]
>>> print(root.to_newick(labels=labels, include_distance=False))
((foo,bar),foobar)