skbio.tree.TreeNode.distance#

TreeNode.distance(other, use_length=True, missing_as_zero=False)[source]#

Calculate the distance between self and another node.

Parameters:
otherTreeNode

The node to compute a distance to.

use_lengthbool, optional

Whether to return the sum of branch lengths (True, default) or the number of branches (False) connecting self and other.

Added in version 0.6.3.

missing_as_zerobool, optional

When a node without an associated branch length is encountered, raise an error (False, default) or use 0 (True). Applicable when use_length is True.

Added in version 0.6.3.

Returns:
float

The distance between two nodes.

Raises:
NoLengthError

If nodes without branch length are encountered, but missing_as_zero is False.

Notes

The distance between two nodes is the length of the path (branches) connecting them. It is also known as the patristic distance [1].

When use_length=False, it is the number of branches in the path.

This method can be used to compute the distance between two given nodes. However, it is not optimized for computing all pairwise tip distances. Use cophenet() instead for that purpose.

References

[1]

Fourment, M., & Gibbs, M. J. (2006). PATRISTIC: a program for calculating patristic distances and graphically comparing the components of genetic change. BMC evolutionary biology, 6, 1-5.

Examples

>>> from skbio import TreeNode
>>> tree = TreeNode.read(["((a:1,b:2)c:3,(d:4,e:5)f:6)root;"])
>>> tip_a = tree.find('a')
>>> tip_d = tree.find('d')
>>> tip_a.distance(tip_d)
14.0
>>> tip_a.distance(tip_d, use_length=False)
4.0