JavaScript library for parsing and serializing the Newick tree format.
Reference: https://en.wikipedia.org/wiki/Newick_format
Parses a Newick string and returns a tree object.
- Input:
input(string): Newick text (for example"(A:0.1,B:0.2)Root;").
- Returns:
- A tree node object with optional fields:
name(string)length(number)branchset(Array<TreeNode>)
- A tree node object with optional fields:
- Throws:
SyntaxErrorfor invalid syntax (for example unterminated quotes, invalid branch length tokens, malformed structure).
- Supports quoted labels (
'A B') and escaped quotes ('E''F'->E'F). - Ignores comments in square brackets (
[comment]), including nested comments. - Converts underscores in unquoted labels to spaces (
A_B->A B). - Validates branch lengths as signed/unsigned decimal numbers, with optional exponent.
- Requires a trailing semicolon (
;).
Example:
const Newick = require("./src/newick");
const tree = Newick.parse("(A:0.1,'B C':0.2)Root:1.0;");
// tree:
// {
// name: "Root",
// length: 1,
// branchset: [
// { name: "A", length: 0.1 },
// { name: "B C", length: 0.2 }
// ]
// }Serializes a tree object into Newick text.
- Input:
tree(TreeNode): Object using the same shape returned byparse.
- Returns:
- Newick
stringending in;.
- Newick
- Writes branch sets as
(child1,child2,...). - Writes
nameand:lengthwhen present. - Quotes labels when required and escapes
'as''.
Example:
const Newick = require("./src/newick");
const text = Newick.serialize({
name: "Root",
branchset: [
{ name: "A", length: 0.1 },
{ name: "B C", length: 0.2 }
]
});
// "(A:0.1,'B C':0.2)Root;"When loaded directly in a browser, the library exports a global Newick object:
<script src="src/newick.js"></script>
<script>
const tree = Newick.parse("(A:1,B:2)Root;");
const text = Newick.serialize(tree);
</script>