skbio.tree.TreeNode.lca#

TreeNode.lca(tipnames)[source]#

Find the lowest common ancestor of a list of nodes.

Parameters:
tipnamesiterable of TreeNode or str

The nodes of interest.

Returns:
TreeNode

The lowest common ancestor of the nodes.

Raises:
ValueError

If no tips could be found in the tree, or if not all tips were found.

Notes

Despite the parameter is named as tipnames, it can accept both tips and internal nodes.

Examples

>>> from skbio import TreeNode
>>> tree = TreeNode.read(["((a,b)c,(d,e)f)root;"])
>>> nodes = [tree.find('a'), tree.find('b')]
>>> lca = tree.lowest_common_ancestor(nodes)
>>> print(lca.name)
c
>>> nodes = [tree.find('a'), tree.find('e')]
>>> lca = tree.lca(nodes)  # lca is an alias for convience
>>> print(lca.name)
root