Parse a string in the New Hampshire (Newick) format and return a tree object.
This function reads a Newick string from left to right. It is based on
the kn_parse function by Heng Li for jstreeview (https://github.com/lh3/jstreeview/blob/main/knhx.js),
modified for compatibility with our Tree object and to prevent ';' assignment as root label.
Supports node annotations in both BEAST [&...] and NHX [&&NHX:...] formats through the annotationParser parameter.
Users can provide their own annotation parser function to handle custom formats.
Also handles hybrid nodes marked with # notation following Cardona et al. 2008.
If the input string contains multiple trees (separated by newlines), only the first tree is parsed
and a warning is issued. Use readTreesFromNewick() to parse multiple trees.
Node IDs are unique and allocated in order of parsing. Specifically, leaf node IDs are numbered
according to the order in which they are encountered. Where a close parenthesis ')' is
encountered, an internal node is added with an incremented ID. This means that leaf node IDs
are not guaranteed to be contiguous or numbered from 0 to n-1. For example, for (A,B);, leaf A has index 1,
leaf B has index 2, and the root node has index 3. In general the root will have the highest index.
To renumber nodes, one could use .preorderTraversal() or .postorderTraversal() methods.
Parse a string in the New Hampshire (Newick) format and return a tree object.
This function reads a Newick string from left to right. It is based on the
kn_parse
function by Heng Li for jstreeview (https://github.com/lh3/jstreeview/blob/main/knhx.js), modified for compatibility with our Tree object and to prevent ';' assignment as root label.Supports node annotations in both BEAST [&...] and NHX [&&NHX:...] formats through the annotationParser parameter. Users can provide their own annotation parser function to handle custom formats. Also handles hybrid nodes marked with # notation following Cardona et al. 2008.
If the input string contains multiple trees (separated by newlines), only the first tree is parsed and a warning is issued. Use readTreesFromNewick() to parse multiple trees.
Node IDs are unique and allocated in order of parsing. Specifically, leaf node IDs are numbered according to the order in which they are encountered. Where a close parenthesis ')' is encountered, an internal node is added with an incremented ID. This means that leaf node IDs are not guaranteed to be contiguous or numbered from 0 to n-1. For example, for
(A,B);
, leaf A has index 1, leaf B has index 2, and the root node has index 3. In general the root will have the highest index.To renumber nodes, one could use
.preorderTraversal()
or.postorderTraversal()
methods.