JavaScript library for parsing the Newick Tree format.
import { Newick } from 'newick-tree-parser'
const newick = new Newick('some data')
Returns nodes data
import { NewickTools } from 'newick-tree-parser'
NewickTools.dfs(tree)
tree - string or parsed object
var tree = NewickTools.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var data = NewickTools.dfs(tree);
Returns object
{
A: 0.1,
B: 0.2,
C: 0.3,
D: 0.4,
E: 0.5
}
NewickTools.dfs(tree, nodeCallback)
- tree - string or parsed object
- nodeCallback - function (node)
var tree = NewickTools.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var data = NewickTools.dfs(tree, function (e) {
e.length *= 10;
return e;
});
Returns object
{
A: 1,
B: 2,
C: 3,
D: 4,
E: 5
}
Check if two trees are equal
import { NewickTools } from 'newick-tree-parser'
NewickTools.equals(tree1, tree2)
tree1, tree2 - Newick instanse Returns boolean true or false
var tree1 = NewickTools.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var tree2 = NewickTools.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
NewickTools.equals(tree1, tree2); // true;
tree2 = NewickTools.parse('(A:0.11,B:0.22,(C:0.33,D:0.44)E:0.55)F');
NewickTools.equals(tree1, tree2); // false
Returns a root of the tree
import { NewickTools } from 'newick-tree-parser'
NewickTools.getRoot(s)
s - string on parsed object
var tree = NewickTools.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var root = NewickTools.getRoot(tree);\
Returns string (root name)
'F'
Iterates tree's nodes and applies callback for each node
Alias for NewickTools.dfs(tree, callback)
import { NewickTools } from 'newick-tree-parser'
NewickTools.map(tree, callback)
tree - string or parsed object callback - function (node)
var tree = NewickTools.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var mappedTree = NewickTools.map(tree, function (e) {
e *= 10;
e.name += ' mapped';
return e;
});
Returns object
{
name: "F mapped",
branchset: [
{
name: "A mapped",
length: 1
},
{
name: "B mapped",
length: 2
},
{
name: "E mapped",
length: 5,
branchset: [
{
name: "C mapped",
length: 3
},
{
name: "D mapped",
length: 4
}]
}]
}
Returns normalized tree in [0; 1]
import { NewickTools } from 'newick-tree-parser'
NewickTools.normalize(tree)
tree - string or parsed object
var tree = NewickTools.parse('(A:5,B:20)F;');
var normalizedTree = NewickTools.normalize(tree)
Returns object
{
name:"F",
branchset: [
{name:"A", length: 0.2},
{name:"B", length: 0.8}
]
}
Parse Newick string into tree-object
import { NewickTools } from 'newick-tree-parser'
NewickTools.parse(s)
s - string
var tree = NewickTools.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
Returns object
{
name: "F",
branchset: [
{
name: "A",
length: 0.1
},
{
name: "B",
length: 0.2
},
{
name: "E",
length: 0.5,
branchset: [
{
name: "C",
length: 0.3
},
{
name: "D",
length: 0.4
}]
}]
}
Serializes tree
import { NewickTools } from 'newick-tree-parser'
NewickTools.serialize(tree)
tree - string or parsed object
var tree = NewickTools.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var serializedTree = NewickTools.serialize(tree)
Returns string
'(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F;'
Casts tree or string to tree-object
private
cast(s)
s - string or parsed object
var tree = '(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F';
tree = cast(tree);
Returns object
{
name: "F",
branchset: [
{
name: "A",
length: 0.1
},
{
name: "B",
length: 0.2
},
{
name: "E",
length: 0.5,
branchset: [
{
name: "C",
length: 0.3
},
{
name: "D",
length: 0.4
}]
}]
}
Returns a root of the tree
private
getRoot(s)
s - string or parsed object
var tree = Newick.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
tree = getRoot(tree);
Returns string (root name)
'F'
Serializes tree
private
serialize(node)
node - tree node
var tree = Newick.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var serializedTree = Newick.serialize(tree)
Returns string
'(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F'
Returns nodes data
public method
var data = newick.dfs();
var tree = new Newick('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var data = newick.dfs();
Returns object
{
A: 0.1,
B: 0.2,
C: 0.3,
D: 0.4,
E: 0.5
}
var data = newick.dfs(nodeCallback);
nodeCallback - function (node)
var tree = new Newick('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var data = newick.dfs(function (e) {
e.length *= 10;
return e;
});
Returns object
{
A: 1,
B: 2,
C: 3,
D: 4,
E: 5
}