skbio.tree.TreeNode.insert#

TreeNode.insert(node, distance=None, branch_attrs=[])[source]#

Insert a node into the branch connecting self and its parent.

Added in version 0.6.2.

Parameters:
nodeTreeNode

Node to insert.

distancefloat, int or None, optional

Distance between self and the insertion point. Must not exceed self.length. If None whereas self.length is not None, will insert at the midpoint of the branch.

branch_attrsiterable of str, optional

Attributes of self that should be transferred to the inserted node as they are considered as attributes of the branch. support will be automatically included as it is always a branch attribute.

Raises:
NoParentError

If self has no parent.

ValueError

If distance is specified but branch has no length.

ValueError

If distance exceeds branch length.

See also

append

Examples

>>> from skbio import TreeNode
>>> tree = TreeNode.read(["((a:1,b:2)c:4,d:5)e;"])
>>> print(tree.ascii_art())
                    /-a
          /c-------|
-e-------|          \-b
         |
          \-d
>>> tree.find("c").insert(TreeNode("x"))
>>> print(tree.ascii_art())
                              /-a
          /x------- /c-------|
-e-------|                    \-b
         |
          \-d
>>> tree.find("c").length
2.0
>>> tree.find("x").length
2.0