skbio.tree.TreeNode.prune#

TreeNode.prune(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:
uncachebool, optional

Whether to clear caches of the tree if present (default: True). See details.

Added in version 0.6.3.

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.prune()
>>> print(tree.ascii_art())
                              /-a
                    /c-------|
          /g-------|          \-b
         |         |
-root----|          \-d
         |
         |          /-h
          \j-------|
                    \-i