from_newick#
- biotite.sequence.phylo.from_newick(newick: str, labels: Sequence[str] | None = None) DiGraph[source]#
Create a tree from a Newick notation.
- Parameters:
- newickstr
The Newick notation to create the tree from.
- labelssequence of str, optional
If the Newick notation contains leaf labels that are not parseable into integers, this parameter can be provided to convert these labels into leaf nodes. The corresponding leaf node is the position of the label in the given sequence.
- Returns:
- treeDiGraph
The tree created from the Newick notation. The leaf nodes are the integers
0ton-1, the intermediate nodes continue the numbering in the order they are closed in the Newick notation. Hence, the root is the node with the highest number. Each edge points from the parent to the child node and has a"distance"attribute, that is 0 if the Newick notation does not provide a distance.
- Raises:
- InvalidFileError
If the Newick notation is malformed or the leaf nodes are not the integers
0ton-1.
Notes
This function does accept but does not require the Newick string to have the terminal semicolon.
Labels of intermediate nodes are discarded.
Examples
>>> tree = from_newick("((0:5.0,1:7.0):3.0,2:10.0);") >>> print(tree.nodes) [0, 1, 2, 3, 4] >>> for parent, child, distance in tree.edges(data="distance"): ... print(parent, child, distance) 3 0 5.0 3 1 7.0 4 3 3.0 4 2 10.0 >>> tree = from_newick("((A,B),C);", labels=["A", "B", "C"]) >>> print(tree.nodes) [0, 1, 2, 3, 4]