@euphrasiologist/lwphylo 1.1.7 → 1.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +2 -0
- package/package.json +9 -5
- package/src/phisheye/phisheye.js +111 -65
- package/src/radial/describeArc.js +22 -12
- package/src/radial/getArcs.js +47 -56
- package/src/radial/getRadii.js +32 -38
- package/src/radial/polarToCartesian.js +5 -7
- package/src/radial/radialData.js +71 -89
- package/src/radial/radialLayout.js +8 -9
- package/src/radial/reflectAngle.js +7 -17
- package/src/rectangle/getChildVerticals.js +47 -0
- package/src/rectangle/getHorizontal.js +13 -7
- package/src/rectangle/getVertical.js +2 -1
- package/src/rectangle/rectangleLayout.js +23 -10
- package/src/unrooted/equalAngleLayout.js +36 -25
- package/src/unrooted/unrooted.js +1 -1
- package/src/utils/edges.js +13 -42
- package/src/utils/indexing.js +64 -0
- package/src/utils/meanAngle.js +10 -35
- package/src/utils/numTips.js +15 -20
- package/src/utils/parentFisheye.js +9 -11
- package/src/utils/preorder.js +33 -6
- package/src/utils/readTree.js +53 -63
- package/dist/@euphrasiologist/lwphylo.js +0 -822
- package/dist/@euphrasiologist/lwphylo.min.js +0 -2
package/src/utils/readTree.js
CHANGED
|
@@ -1,74 +1,64 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Parse a Newick tree string into a doubly-linked
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* Parse a Newick tree string into a doubly-linked list of JS Objects.
|
|
3
|
+
* Assigns labels, branch lengths, and node IDs (tips before internals if input emits them that way).
|
|
4
|
+
*
|
|
5
|
+
* Notes / limitations:
|
|
6
|
+
* - Quoted labels and NHX annotations are not fully supported.
|
|
7
|
+
* - Branch lengths in scientific notation are supported (parseFloat).
|
|
6
8
|
*/
|
|
7
9
|
|
|
8
|
-
export default function (text) {
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
export default function readTree(text) {
|
|
11
|
+
// Remove all whitespace (space, tabs, newlines)
|
|
12
|
+
text = String(text).replace(/\s+/g, '');
|
|
11
13
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
const tokens = text.split(/(;|\(|\)|,)/);
|
|
15
|
+
const root = { parent: null, children: [] };
|
|
16
|
+
let curnode = root;
|
|
17
|
+
let nodeId = 0;
|
|
16
18
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
continue
|
|
20
|
-
}
|
|
21
|
-
if (token == '(') {
|
|
22
|
-
// add a child to current node
|
|
23
|
-
let child = {
|
|
24
|
-
'parent': curnode,
|
|
25
|
-
'children': []
|
|
26
|
-
};
|
|
27
|
-
curnode.children.push(child);
|
|
28
|
-
curnode = child; // climb up
|
|
29
|
-
}
|
|
30
|
-
else if (token == ',') {
|
|
31
|
-
// climb down, add another child to parent
|
|
32
|
-
curnode = curnode.parent;
|
|
33
|
-
let child = {
|
|
34
|
-
'parent': curnode,
|
|
35
|
-
'children': []
|
|
36
|
-
}
|
|
37
|
-
curnode.children.push(child);
|
|
38
|
-
curnode = child; // climb up
|
|
39
|
-
}
|
|
40
|
-
else if (token == ')') {
|
|
41
|
-
// climb down twice
|
|
42
|
-
curnode = curnode.parent;
|
|
43
|
-
if (curnode === null) {
|
|
44
|
-
break;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
var nodeinfo = token.split(':');
|
|
19
|
+
for (const token of tokens) {
|
|
20
|
+
if (!token || token === ';') continue;
|
|
49
21
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
22
|
+
if (token === '(') {
|
|
23
|
+
const child = { parent: curnode, children: [] };
|
|
24
|
+
curnode.children.push(child);
|
|
25
|
+
curnode = child; // descend
|
|
26
|
+
} else if (token === ',') {
|
|
27
|
+
// back to parent, then create sibling
|
|
28
|
+
curnode = curnode.parent;
|
|
29
|
+
const child = { parent: curnode, children: [] };
|
|
30
|
+
curnode.children.push(child);
|
|
31
|
+
curnode = child;
|
|
32
|
+
} else if (token === ')') {
|
|
33
|
+
// ascend one level
|
|
34
|
+
curnode = curnode.parent;
|
|
35
|
+
if (curnode === null) break;
|
|
36
|
+
} else {
|
|
37
|
+
// label/branch-length chunk (e.g., "A:0.01" or "A")
|
|
38
|
+
const nodeinfo = token.split(':');
|
|
39
|
+
if (nodeinfo.length === 1) {
|
|
40
|
+
if (token.startsWith(':')) {
|
|
41
|
+
curnode.label = '';
|
|
42
|
+
curnode.branchLength = parseFloat(nodeinfo[0]);
|
|
43
|
+
} else {
|
|
44
|
+
curnode.label = nodeinfo[0];
|
|
45
|
+
curnode.branchLength = null;
|
|
68
46
|
}
|
|
47
|
+
} else if (nodeinfo.length === 2) {
|
|
48
|
+
curnode.label = nodeinfo[0];
|
|
49
|
+
curnode.branchLength = parseFloat(nodeinfo[1]);
|
|
50
|
+
} else {
|
|
51
|
+
console.warn(token, "Unhandled token with multiple ':' characters");
|
|
52
|
+
curnode.label = nodeinfo[0] || '';
|
|
53
|
+
curnode.branchLength = parseFloat(nodeinfo[nodeinfo.length - 1]);
|
|
54
|
+
}
|
|
55
|
+
curnode.id = nodeId++; // assign then increment
|
|
69
56
|
}
|
|
57
|
+
}
|
|
70
58
|
|
|
71
|
-
|
|
59
|
+
// Ensure root has an id if not assigned during parsing
|
|
60
|
+
if (root.id == null) root.id = nodeId;
|
|
72
61
|
|
|
73
|
-
|
|
62
|
+
return root;
|
|
74
63
|
}
|
|
64
|
+
|