Skip to content

Commit

Permalink
Move t.unroot() to operations.pyx.
Browse files Browse the repository at this point in the history
  • Loading branch information
jordibc committed Nov 28, 2023
1 parent c808d8e commit b02364f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
15 changes: 15 additions & 0 deletions ete4/core/operations.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,21 @@ def join_branch(node):
child.up = up


def unroot(tree):
"""Unroot the tree (make the root not have 2 children).
The convention in phylogenetic trees is that if the root has 2
children, it is a "rooted" tree (the root is a real ancestor).
Otherwise (typically a root with 3 children), the root is just
an arbitrary place to hang the tree.
"""
assert tree.is_root, 'unroot only makes sense from the root node'
if len(tree.children) == 2:
n1, n2 = tree.children
assert not (n1.is_leaf and n2.is_leaf), 'tree has just two leaves'
root_at(n1 if not n1.is_leaf else n2)


def move(node, shift=1):
"""Change the position of the current node with respect to its parent."""
# ╴up╶┬╴node -> ╴up╶┬╴sibling
Expand Down
7 changes: 1 addition & 6 deletions ete4/core/tree.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1086,12 +1086,7 @@ cdef class Tree(object):
Otherwise (typically a root with 3 children), the root is just
an arbitrary place to hang the tree.
"""
assert self.is_root, 'unroot only makes sense from the root node'
if len(self.children) == 2:
n1, n2 = self.children
if n1.is_leaf and n2.is_leaf:
raise TreeError('cannot unroot a tree with only two leaves')
ops.root_at(n1 if not n1.is_leaf else n2)
ops.unroot(self)

def show(self, layout=None, tree_style=None, name="ETE"):
"""Start an interactive session to visualize the current node."""
Expand Down

0 comments on commit b02364f

Please sign in to comment.