From f2682ee3bd71047f663a8022a42637418cad1e3b Mon Sep 17 00:00:00 2001 From: Jordi Date: Fri, 19 Jan 2024 22:49:20 +0100 Subject: [PATCH] tree.pyx: Simplify t.ancestors(). This new version is clearer and will also produce an exception if root is no ancestor of node. --- ete4/core/tree.pyx | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/ete4/core/tree.pyx b/ete4/core/tree.pyx index 87665713..163c2578 100644 --- a/ete4/core/tree.pyx +++ b/ete4/core/tree.pyx @@ -769,27 +769,22 @@ cdef class Tree(object): def ancestors(self, root=None, include_root=True): """Yield all ancestor nodes of this node (up to the root if given).""" + # root === n1 === n2 === .. === nN === node -> [nN, .., n2, n1, root] node = self - if node is root or (not include_root and node.up is root): + if node is root: return # node is not an ancestor of itself - while node.up is not None: + while node.up is not root: node = node.up - yield node - if ((include_root and node == root) or - (not include_root and node.up == root)): - break # we already yielded all the nodes we want - - if root is not None and ((include_root and node != root) or - (not include_root and node.up != root)): - raise TreeError('node is no descendant from given root: %r' % root) + if root is not None and include_root: + yield root def lineage(self, root=None, include_root=True): """Yield all nodes in the lineage of this node (up to root if given).""" - if not include_root and self == root: + if not include_root and self is root: return # the node itself would not be in its lineage # Same as ancestors() but also yielding itself first.