skbio.tree.TreeNode.lca#

TreeNode.lca(nodes=None, **kwargs)[source]#

Find the lowest common ancestor of a list of nodes.

Parameters:
nodesiterable of TreeNode or str

Instances or names of the nodes of interest.

Changed in version 0.6.3: Renamed from tipnames. The old name is preserved as an alias.

Returns:
TreeNode

The lowest common ancestor of the nodes.

Raises:
MissingNodeError

If some nodes cannot be found in the tree.

Notes

Both tips and internal nodes may be provided in nodes. If internal node names are provided, it is the user’s responsibility to ensure that they are unique in the tree.

This method considers the entire tree rather than the subtree below self. Therefore, if some nodes are not descendants of self, the LCA of nodes will be ancestral to self.

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