skbio.tree.TreeNode.root_at_midpoint#
- TreeNode.root_at_midpoint(reset=False, branch_attrs=['name'], root_name='root')[source]#
Reroot the tree at the midpoint of the two tips farthest apart.
- Parameters:
- resetbool, optional
Whether remove the original root of a rooted tree before performing the rerooting operation. Default is
False
.Added in version 0.6.2.
Note
The default value will be set as
True
in 0.7.0.- branch_attrsiterable of str, optional
Attributes of each node that should be considered as attributes of the branch connecting the node to its parent. This is important for the correct rerooting operation. “length” and “support” will be automatically included as they are always branch attributes.
Added in version 0.6.2.
Note
name
will be removed from the default in 0.7.0, as it is usually considered as an attribute of the node instead of the branch.- root_namestr or None, optional
Name for the new root node, if it doesn’t have one.
Added in version 0.6.2.
Note
The default value will be set as
None
in 0.7.0.
- Returns:
- TreeNode
A tree rooted at its midpoint.
- Raises:
- TreeError
If a tip ends up being the mid point.
- LengthError
Midpoint rooting requires length and will raise (indirectly) if evaluated nodes don’t have length.
Warning
The default behavior of
root_at_midpoint
is subject to change in 0.7.0. The new default behavior can be achieved by specifyingreset=True, branch_attrs=[], root_name=None
.See also
Notes
The midpoint rooting (MPR) method was originally described in [1].
References
[1]Farris, J. S. (1972). Estimating phylogenetic trees from distance matrices. The American Naturalist, 106(951), 645-668.
Examples
>>> from skbio import TreeNode >>> tree = TreeNode.read(["((a:1,b:1)c:2,(d:3,e:4)f:5,g:1)h;"]) >>> print(tree.ascii_art()) /-a /c-------| | \-b | -h-------| /-d |-f-------| | \-e | \-g
>>> t = tree.root_at_midpoint(branch_attrs=[]) >>> print(t) ((d:3.0,e:4.0)f:2.0,((a:1.0,b:1.0)c:2.0,g:1.0)h:3.0)root; >>> print(t.ascii_art()) /-d /f-------| | \-e -root----| | /-a | /c-------| \h-------| \-b | \-g