skbio.tree.TreeNode.prune#

TreeNode.prune(inplace=True, uncache=True)[source]#

Collapse single-child nodes in the tree.

Internal nodes with only one child will be removed, and direct connections will be made from the parent to the child. The branch length of the node will be added to the child. The name and properties of the child will override those of the parent following the operation.

Parameters:
inplacebool, optional

Whether to modify the tree in place (True, default) or to create a modified copy of the tree (False).

Added in version 0.7.4.

uncachebool, optional

Whether to clear caches of the tree if present (default: True). See details. Only applicable when inplace is True.

Added in version 0.6.3.

Returns:
TreeNode

The resulting tree.

Changed in version 0.7.4: Now returns the tree. Previously the method returned nothing.

Notes

This method is useful for cleaning up single-child nodes after some nodes were removed from a tree.

If called from an internal node of the tree, only the clade below the node will be pruned.

Examples

>>> from skbio import TreeNode
>>> tree = TreeNode.read(["(((a,b)c,(d)e)g,((h,i)j)k)root;"])
>>> print(tree.ascii_art())
                              /-a
                    /c-------|
          /g-------|          \-b
         |         |
-root----|          \e------- /-d
         |
         |                    /-h
          \k------- /j-------|
                              \-i
>>> tree = tree.prune()
>>> print(tree.ascii_art())
                              /-a
                    /c-------|
          /g-------|          \-b
         |         |
-root----|          \-d
         |
         |          /-h
          \j-------|
                    \-i