phylotree 2.6.1 → 2.7.0
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/README.md +48 -4
- package/bin/phylotree-neighbor-join.js +4 -5
- package/bin/root-to-tip.js +1 -1
- package/bin/tip-date-extractor.js +26 -18
- package/dist/phylotree.d.ts +42 -6
- package/dist/phylotree.js +3236 -730
- package/dist/phylotree.min.js +1 -1
- package/package.json +14 -25
- package/src/branches.js +58 -34
- package/src/clustering/cluster-picker.js +18 -2
- package/src/clustering/phylopart.js +14 -6
- package/src/export.js +1 -1
- package/src/extract-dates.js +3 -3
- package/src/formats/beast.js +5 -1
- package/src/formats/newick.js +43 -7
- package/src/formats/nexml.js +87 -30
- package/src/formats/nexus.js +112 -32
- package/src/formats/phyloxml.js +74 -15
- package/src/formats/registry.js +5 -1
- package/src/index.js +1 -0
- package/src/main.js +76 -25
- package/src/metrics/center-of-tree.js +4 -2
- package/src/metrics/compute-midpoint.js +7 -1
- package/src/metrics/pairwise-distances.js +4 -2
- package/src/metrics/root-to-tip.js +43 -10
- package/src/metrics/sackins.js +7 -1
- package/src/msa-parsers/fasta.js +9 -12
- package/src/neighbor-join.js +107 -75
- package/src/nodes.js +43 -31
- package/src/render/clades.js +12 -3
- package/src/render/draw.js +93 -53
- package/src/render/edges.js +12 -3
- package/src/render/menus.js +83 -6
- package/src/render/nodes.js +15 -9
- package/src/render/selection-sets.js +18 -0
- package/src/render/unrooted.js +90 -12
- package/src/rooting.js +9 -3
- package/src/traversal.js +4 -3
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "phylotree",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.0",
|
|
4
4
|
"main": "dist/phylotree.js",
|
|
5
5
|
"types": "dist/phylotree.d.ts",
|
|
6
6
|
"unpkg": "dist/phylotree.js",
|
|
7
7
|
"jsdelivr": "dist/phylotree.js",
|
|
8
8
|
"module": "src/index.js",
|
|
9
9
|
"engines": {
|
|
10
|
-
"node": ">=
|
|
10
|
+
"node": ">=22.12"
|
|
11
11
|
},
|
|
12
12
|
"bin": {
|
|
13
13
|
"root-to-tip": "./bin/root-to-tip.js",
|
|
@@ -27,41 +27,30 @@
|
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@types/d3": "^7.4.3",
|
|
30
|
-
"commander": "^
|
|
31
|
-
"csv-stringify": "^6.6.0",
|
|
30
|
+
"commander": "^15.0.0",
|
|
32
31
|
"d3": "^7.9.0",
|
|
33
32
|
"fast-xml-parser": "^5.7.0",
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"moment": "^2.30.1",
|
|
37
|
-
"pretty-data": "^0.40.0",
|
|
38
|
-
"underscore": "^1.13.8",
|
|
39
|
-
"winston": "^3.17.0"
|
|
33
|
+
"moment": "^2.31.0",
|
|
34
|
+
"underscore": "^1.13.8"
|
|
40
35
|
},
|
|
41
36
|
"devDependencies": {
|
|
42
|
-
"@eslint/js": "^
|
|
43
|
-
"@rollup/plugin-commonjs": "^29.0.0",
|
|
37
|
+
"@eslint/js": "^10.0.1",
|
|
44
38
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
45
|
-
"@rollup/plugin-terser": "^0.
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"globals": "^16.2.0",
|
|
49
|
-
"http-server": "^14.1.1",
|
|
39
|
+
"@rollup/plugin-terser": "^1.0.2",
|
|
40
|
+
"eslint": "^10.13.0",
|
|
41
|
+
"globals": "^17.13.0",
|
|
50
42
|
"jsdoc-to-markdown": "^9.1.0",
|
|
51
|
-
"jsdom": "
|
|
52
|
-
"
|
|
53
|
-
"rollup": "^4.59.0",
|
|
54
|
-
"rollup-plugin-copy": "^3.5.0",
|
|
55
|
-
"source-map-support": "0.5.21",
|
|
43
|
+
"jsdom": "^30.1.3",
|
|
44
|
+
"rollup": "^4.65.0",
|
|
56
45
|
"tape": "^5.9.0",
|
|
57
|
-
"typescript": "
|
|
46
|
+
"typescript": "^7.0.2"
|
|
58
47
|
},
|
|
59
48
|
"scripts": {
|
|
60
49
|
"lint": "eslint .",
|
|
61
50
|
"dev": "rollup -w -c rollup.config.mjs",
|
|
62
|
-
"serve": "
|
|
51
|
+
"serve": "node scripts/serve.mjs",
|
|
63
52
|
"build": "rollup -c rollup.config.mjs",
|
|
64
|
-
"test": "
|
|
53
|
+
"test": "node --enable-source-maps node_modules/tape/bin/tape --strict \"test/*-test.js\"",
|
|
65
54
|
"typecheck": "tsc -p test/types",
|
|
66
55
|
"docs:generate": "node scripts/docs.mjs generate",
|
|
67
56
|
"docs:validate": "node scripts/docs.mjs validate",
|
package/src/branches.js
CHANGED
|
@@ -17,7 +17,7 @@ export function getPartitions(attributes) {
|
|
|
17
17
|
*/
|
|
18
18
|
export default function hasBranchLengths() {
|
|
19
19
|
|
|
20
|
-
let bl = this.
|
|
20
|
+
let bl = this.branch_length_accessor;
|
|
21
21
|
|
|
22
22
|
if (bl) {
|
|
23
23
|
return _.every(this.nodes.descendants(), function(node) {
|
|
@@ -36,7 +36,7 @@ export default function hasBranchLengths() {
|
|
|
36
36
|
*/
|
|
37
37
|
export function getBranchLengths() {
|
|
38
38
|
|
|
39
|
-
let bl = this.
|
|
39
|
+
let bl = this.branch_length_accessor;
|
|
40
40
|
return _.map(this.nodes.descendants(), node => { return bl(node)});
|
|
41
41
|
|
|
42
42
|
}
|
|
@@ -46,13 +46,15 @@ export function defBranchLengthAccessor(_node, new_length) {
|
|
|
46
46
|
|
|
47
47
|
let _node_data = _node.data;
|
|
48
48
|
|
|
49
|
+
let attribute = _node_data["attribute"];
|
|
50
|
+
|
|
49
51
|
if (
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
typeof attribute === "number" ||
|
|
53
|
+
((typeof attribute === "string" || Array.isArray(attribute)) &&
|
|
54
|
+
attribute.length)
|
|
53
55
|
) {
|
|
54
56
|
|
|
55
|
-
if(new_length
|
|
57
|
+
if(new_length !== undefined && new_length !== null && isFinite(new_length)) {
|
|
56
58
|
_node_data["attribute"] = String(new_length);
|
|
57
59
|
}
|
|
58
60
|
|
|
@@ -64,8 +66,8 @@ export function defBranchLengthAccessor(_node, new_length) {
|
|
|
64
66
|
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
// Allow for empty branch length at root
|
|
68
|
-
if(
|
|
69
|
+
// Allow for empty branch length at the root, which has no branch
|
|
70
|
+
if(!_node.parent) {
|
|
69
71
|
return 0;
|
|
70
72
|
}
|
|
71
73
|
|
|
@@ -101,6 +103,7 @@ export function defBranchLengthAccessor(_node, new_length) {
|
|
|
101
103
|
export function setBranchLength(attr) {
|
|
102
104
|
if (!arguments.length) return this.branch_length_accessor;
|
|
103
105
|
this.branch_length_accessor = attr ? attr : defBranchLengthAccessor;
|
|
106
|
+
this.branch_length = this.branch_length_accessor;
|
|
104
107
|
return this;
|
|
105
108
|
}
|
|
106
109
|
|
|
@@ -121,30 +124,60 @@ export function setBranchLength(attr) {
|
|
|
121
124
|
*/
|
|
122
125
|
export function normalize(attr) {
|
|
123
126
|
|
|
124
|
-
let
|
|
125
|
-
|
|
126
|
-
let branch_lengths = _.map(this.nodes.descendants(), function(node) {
|
|
127
|
-
if(bl(node)) {
|
|
128
|
-
return bl(node);
|
|
129
|
-
} else {
|
|
130
|
-
return null;
|
|
131
|
-
}
|
|
132
|
-
});
|
|
127
|
+
let lengths = finiteBranchLengths.call(this);
|
|
133
128
|
|
|
134
|
-
const
|
|
135
|
-
const
|
|
129
|
+
const values = _.map(lengths, (d) => d[1]);
|
|
130
|
+
const max_bl = _.max(values);
|
|
131
|
+
const min_bl = _.min(values);
|
|
136
132
|
|
|
137
133
|
let scaler = function (x) {
|
|
134
|
+
if (max_bl === min_bl) {
|
|
135
|
+
return max_bl === 0 ? 0 : 1;
|
|
136
|
+
}
|
|
138
137
|
return (x - min_bl)/(max_bl - min_bl);
|
|
139
138
|
}
|
|
140
139
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
140
|
+
return writeBranchLengths.call(this, lengths, scaler);
|
|
141
|
+
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// [node, length] for every node with a finite branch length. The root only
|
|
145
|
+
// counts when the input gave it an explicit length, since the accessor
|
|
146
|
+
// reports 0 for a root without one.
|
|
147
|
+
function finiteBranchLengths() {
|
|
148
|
+
|
|
149
|
+
let bl = this.branch_length_accessor;
|
|
150
|
+
|
|
151
|
+
return _.filter(
|
|
152
|
+
_.map(this.nodes.descendants(), (node) => [node, bl(node)]),
|
|
153
|
+
(d) => (d[0].parent || hasOwnLength(d[0])) && Number.isFinite(d[1])
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function hasOwnLength(node) {
|
|
159
|
+
let attribute = node.data.attribute;
|
|
160
|
+
return typeof attribute === "number" ||
|
|
161
|
+
(typeof attribute === "string" && attribute.length > 0);
|
|
162
|
+
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Store scaler(length) in each node's attribute. Lengths a custom accessor
|
|
166
|
+
// reads cannot be written back through it, so they are materialised and the
|
|
167
|
+
// default accessor is restored.
|
|
168
|
+
function writeBranchLengths(lengths, scaler) {
|
|
169
|
+
|
|
170
|
+
let custom = this.branch_length_accessor !== defBranchLengthAccessor;
|
|
171
|
+
|
|
172
|
+
_.each(lengths, (d) => {
|
|
173
|
+
let len = scaler(d[1]);
|
|
174
|
+
d[0].data.attribute = String(Number.isFinite(len) ? len : d[1]);
|
|
146
175
|
});
|
|
147
176
|
|
|
177
|
+
if (custom) {
|
|
178
|
+
this.setBranchLength(defBranchLengthAccessor);
|
|
179
|
+
}
|
|
180
|
+
|
|
148
181
|
return this;
|
|
149
182
|
|
|
150
183
|
}
|
|
@@ -177,16 +210,7 @@ export function normalize(attr) {
|
|
|
177
210
|
*/
|
|
178
211
|
export function scale(scale_by) {
|
|
179
212
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
_.each(this.nodes.descendants(), (node) => {
|
|
183
|
-
let len = bl(node);
|
|
184
|
-
if(len) {
|
|
185
|
-
bl(node, scale_by(len));
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
return this;
|
|
213
|
+
return writeBranchLengths.call(this, finiteBranchLengths.call(this), scale_by);
|
|
190
214
|
|
|
191
215
|
}
|
|
192
216
|
|
|
@@ -26,6 +26,18 @@ function clusterPicker(
|
|
|
26
26
|
root_node,
|
|
27
27
|
missing_bootstrap_value
|
|
28
28
|
) {
|
|
29
|
+
// numeric strings are accepted, as before
|
|
30
|
+
bootstrap_threshold = +bootstrap_threshold;
|
|
31
|
+
diameter_threshold = +diameter_threshold;
|
|
32
|
+
|
|
33
|
+
// Infinity is a valid "no limit" diameter
|
|
34
|
+
if (Number.isNaN(bootstrap_threshold)) {
|
|
35
|
+
throw "clusterPicker requires a numeric bootstrap threshold";
|
|
36
|
+
}
|
|
37
|
+
if (Number.isNaN(diameter_threshold) || diameter_threshold < 0) {
|
|
38
|
+
throw "clusterPicker requires a numeric diameter threshold >= 0";
|
|
39
|
+
}
|
|
40
|
+
|
|
29
41
|
root_node = root_node || tree.getRootNode();
|
|
30
42
|
missing_bootstrap_value = _.isNumber(missing_bootstrap_value)
|
|
31
43
|
? missing_bootstrap_value
|
|
@@ -35,7 +47,7 @@ function clusterPicker(
|
|
|
35
47
|
// where each internal node will receive a floating point value
|
|
36
48
|
// that stores the longest path from the internal node to any of its descendants,
|
|
37
49
|
// the diameter of the cluster, is then the sum of longest paths of all of its children
|
|
38
|
-
let bl = tree.
|
|
50
|
+
let bl = tree.branch_length_accessor;
|
|
39
51
|
|
|
40
52
|
// initialize member variables
|
|
41
53
|
tree.traverse_and_compute(function(n) {
|
|
@@ -63,7 +75,11 @@ function clusterPicker(
|
|
|
63
75
|
if (!tree.isLeafNode(n)) {
|
|
64
76
|
var bs = _.isString(n.data.bootstrap_values)
|
|
65
77
|
? +n.data.bootstrap_values
|
|
66
|
-
:
|
|
78
|
+
: NaN;
|
|
79
|
+
// unlabeled ('' or NaN) support is treated as missing
|
|
80
|
+
if (n.data.bootstrap_values === "" || _.isNaN(bs)) {
|
|
81
|
+
bs = missing_bootstrap_value;
|
|
82
|
+
}
|
|
67
83
|
|
|
68
84
|
if (bs >= bootstrap_threshold) {
|
|
69
85
|
var my_diameter = _.reduce(
|
|
@@ -110,10 +110,9 @@ function phylopart(
|
|
|
110
110
|
|
|
111
111
|
root_node.paths_to_leaves = new Array(leaf_count);
|
|
112
112
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
});
|
|
113
|
+
// the paths below the root already include the lengths of the root's child branches
|
|
114
|
+
_.each(root_node.cot_path_to_leaves_below, function(v, i) {
|
|
115
|
+
root_node.paths_to_leaves[i] = v;
|
|
117
116
|
});
|
|
118
117
|
|
|
119
118
|
tree.traverse_and_compute(function(n) {
|
|
@@ -146,7 +145,12 @@ function phylopart(
|
|
|
146
145
|
for (var n1 = 0; n1 < n.paths_to_leaves.length; n1++) {
|
|
147
146
|
for (var n2 = n1 + 1; n2 < n.paths_to_leaves.length; n2++) {
|
|
148
147
|
var sum = n.paths_to_leaves[n1] + n.paths_to_leaves[n2];
|
|
149
|
-
|
|
148
|
+
// clamp so that sums outside of [min_bl, max_path_length] stay in range
|
|
149
|
+
var bin = Math.min(
|
|
150
|
+
Math.max(((sum - min_bl) / resolution) >> 0, 0),
|
|
151
|
+
number_of_bins - 1
|
|
152
|
+
);
|
|
153
|
+
n.histogram[bin]++;
|
|
150
154
|
}
|
|
151
155
|
}
|
|
152
156
|
n.leaf_count = n.paths_to_leaves.length;
|
|
@@ -182,7 +186,11 @@ function phylopart(
|
|
|
182
186
|
if (!tree.isLeafNode(n)) {
|
|
183
187
|
var bs = _.isString(n.data.bootstrap_values)
|
|
184
188
|
? +n.data.bootstrap_values
|
|
185
|
-
:
|
|
189
|
+
: NaN;
|
|
190
|
+
// unlabeled ('' or NaN) support is treated as missing
|
|
191
|
+
if (n.data.bootstrap_values === "" || _.isNaN(bs)) {
|
|
192
|
+
bs = missing_bootstrap_value;
|
|
193
|
+
}
|
|
186
194
|
if (bs >= bootstrap_threshold) {
|
|
187
195
|
var total_comparisons = n.leaf_count * (n.leaf_count - 1) * 0.25;
|
|
188
196
|
|
package/src/export.js
CHANGED
|
@@ -17,7 +17,7 @@ export default function getTipLengths() {
|
|
|
17
17
|
let tips = self.getTips();
|
|
18
18
|
|
|
19
19
|
// Transform to name, attribute key-pair and sort by attribute length, descending
|
|
20
|
-
let toExport = _.map(tips, d => { return {'name' : d.data.name, 'length' : parseFloat(d
|
|
20
|
+
let toExport = _.map(tips, d => { return {'name' : d.data.name, 'length' : parseFloat(self.branch_length_accessor(d)) } });
|
|
21
21
|
toExport = _.sortBy(toExport, d=> -d.length)
|
|
22
22
|
return toExport;
|
|
23
23
|
|
package/src/extract-dates.js
CHANGED
|
@@ -3,12 +3,12 @@ import { isLeafNode } from "./nodes";
|
|
|
3
3
|
|
|
4
4
|
const default_date_converter = d3.timeParse("%Y%m%d");
|
|
5
5
|
|
|
6
|
-
const default_regexp = /([0-9]{4}).?([0-9]{2}).?([0-9]{2})
|
|
6
|
+
const default_regexp = /([0-9]{4}).?([0-9]{2}).?([0-9]{2})$/;
|
|
7
7
|
|
|
8
8
|
const default_date_getter = function(node) {
|
|
9
9
|
if (isLeafNode(node)) {
|
|
10
|
-
if (
|
|
11
|
-
let location = default_regexp.exec(node.name);
|
|
10
|
+
if (node.data && node.data.name) {
|
|
11
|
+
let location = default_regexp.exec(node.data.name);
|
|
12
12
|
if (location) {
|
|
13
13
|
return location[1] + location[2] + location[3];
|
|
14
14
|
}
|
package/src/formats/beast.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import newickParser from "./newick";
|
|
2
2
|
|
|
3
|
-
export default function(newick, options) {
|
|
3
|
+
export default function(newick, options = {}) {
|
|
4
|
+
options = Object.assign({}, options);
|
|
4
5
|
options.left_delimiter = '[';
|
|
5
6
|
options.right_delimiter = ']';
|
|
6
7
|
const parsed_newick = newickParser(newick, options);
|
|
8
|
+
if (!parsed_newick.json) {
|
|
9
|
+
return parsed_newick;
|
|
10
|
+
}
|
|
7
11
|
function parseBeastNode(node) {
|
|
8
12
|
if(node.annotation) {
|
|
9
13
|
node.beast = {};
|
package/src/formats/newick.js
CHANGED
|
@@ -38,8 +38,20 @@ import { isLeafNode } from "../nodes";
|
|
|
38
38
|
|
|
39
39
|
function newickParser(nwk_str, options={}) {
|
|
40
40
|
|
|
41
|
+
if (typeof nwk_str !== "string" || !nwk_str.trim().length) {
|
|
42
|
+
return { json: null, error: "empty or non-string newick" };
|
|
43
|
+
}
|
|
44
|
+
|
|
41
45
|
const bootstrap_values = true,
|
|
42
|
-
int_or_float = /^-?\d+(\.\d+)
|
|
46
|
+
int_or_float = /^-?\d+(\.\d+)?$/,
|
|
47
|
+
reserved_keys = [
|
|
48
|
+
"name",
|
|
49
|
+
"attribute",
|
|
50
|
+
"children",
|
|
51
|
+
"annotation",
|
|
52
|
+
"bootstrap_values",
|
|
53
|
+
"original_child_order"
|
|
54
|
+
];
|
|
43
55
|
let left_delimiter = options.left_delimiter || '{',
|
|
44
56
|
right_delimiter = options.right_delimiter || '}';
|
|
45
57
|
let clade_stack = [];
|
|
@@ -70,7 +82,9 @@ function newickParser(nwk_str, options={}) {
|
|
|
70
82
|
this_node["name"] = current_node_name;
|
|
71
83
|
|
|
72
84
|
if (bootstrap_values && "children" in this_node) {
|
|
73
|
-
|
|
85
|
+
if (current_node_name.length) {
|
|
86
|
+
this_node["bootstrap_values"] = current_node_name;
|
|
87
|
+
}
|
|
74
88
|
} else {
|
|
75
89
|
this_node["name"] = current_node_name;
|
|
76
90
|
}
|
|
@@ -82,8 +96,10 @@ function newickParser(nwk_str, options={}) {
|
|
|
82
96
|
.slice(1)
|
|
83
97
|
.forEach(annotation => {
|
|
84
98
|
const [key, value] = annotation.split('=');
|
|
85
|
-
|
|
99
|
+
const node_key = reserved_keys.includes(key) ? "nhx_" + key : key;
|
|
100
|
+
this_node[node_key] = int_or_float.test(value) ? +value : value;
|
|
86
101
|
});
|
|
102
|
+
this_node["annotation"] = "";
|
|
87
103
|
} else {
|
|
88
104
|
this_node["annotation"] = current_node_annotation;
|
|
89
105
|
}
|
|
@@ -181,6 +197,11 @@ function newickParser(nwk_str, options={}) {
|
|
|
181
197
|
}
|
|
182
198
|
} else {
|
|
183
199
|
if (automaton_state == 3) {
|
|
200
|
+
if (current_char == ";") {
|
|
201
|
+
// semicolon terminates tree definition
|
|
202
|
+
char_index = nwk_str.length;
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
184
205
|
current_node_attribute += current_char;
|
|
185
206
|
} else {
|
|
186
207
|
if (space.test(current_char)) {
|
|
@@ -238,6 +259,14 @@ function newickParser(nwk_str, options={}) {
|
|
|
238
259
|
return generateError(nwk_str.length - 1);
|
|
239
260
|
}
|
|
240
261
|
|
|
262
|
+
// label and length following the final ')' belong to the root
|
|
263
|
+
if (current_node_name.length) {
|
|
264
|
+
tree_json["name"] = current_node_name;
|
|
265
|
+
}
|
|
266
|
+
if (current_node_attribute.length) {
|
|
267
|
+
tree_json["attribute"] = current_node_attribute;
|
|
268
|
+
}
|
|
269
|
+
|
|
241
270
|
return {
|
|
242
271
|
json: tree_json,
|
|
243
272
|
error: null
|
|
@@ -278,13 +307,20 @@ export function getNewick(annotator, root) {
|
|
|
278
307
|
|
|
279
308
|
if (!annotator) annotator = d => '';
|
|
280
309
|
|
|
310
|
+
// A node is written if it is shown and is either a leaf or has at least
|
|
311
|
+
// one written descendant.
|
|
312
|
+
function isWritten(n) {
|
|
313
|
+
if (n.notshown) return false;
|
|
314
|
+
return isLeafNode(n) || n.children.some(isWritten);
|
|
315
|
+
}
|
|
316
|
+
|
|
281
317
|
function nodeDisplay(n) {
|
|
282
318
|
// Skip the node if it is hidden
|
|
283
|
-
if (n
|
|
319
|
+
if (!isWritten(n)) return;
|
|
284
320
|
|
|
285
321
|
if (!isLeafNode(n)) {
|
|
286
322
|
element_array.push("(");
|
|
287
|
-
n.children.forEach(function(d, i) {
|
|
323
|
+
n.children.filter(isWritten).forEach(function(d, i) {
|
|
288
324
|
if (i) {
|
|
289
325
|
element_array.push(",");
|
|
290
326
|
}
|
|
@@ -293,8 +329,8 @@ export function getNewick(annotator, root) {
|
|
|
293
329
|
element_array.push(")");
|
|
294
330
|
}
|
|
295
331
|
|
|
296
|
-
if(n.data.name !== 'root') {
|
|
297
|
-
const node_label = n.data.name.replaceAll("'", "''");
|
|
332
|
+
if(n.data.name !== 'root' || n.parent) {
|
|
333
|
+
const node_label = String(n.data.name ?? '').replaceAll("'", "''");
|
|
298
334
|
|
|
299
335
|
// Surround the entire string with single quotes if it contains any
|
|
300
336
|
// non-alphanumeric characters.
|
package/src/formats/nexml.js
CHANGED
|
@@ -1,46 +1,103 @@
|
|
|
1
|
-
import * as _ from "underscore";
|
|
2
1
|
import { XMLParser } from "fast-xml-parser";
|
|
3
2
|
|
|
3
|
+
// Returns the values of all keys whose local name (the part after any
|
|
4
|
+
// namespace prefix) is ``name``, as a flat array of objects.
|
|
5
|
+
function childrenByLocalName(obj, name) {
|
|
6
|
+
var result = [];
|
|
7
|
+
if (obj === null || typeof obj !== "object") {
|
|
8
|
+
return result;
|
|
9
|
+
}
|
|
10
|
+
Object.keys(obj).forEach(function(key) {
|
|
11
|
+
if (key.split(":").pop() === name) {
|
|
12
|
+
result = result.concat(obj[key]);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
return result.filter(d => d !== null && typeof d === "object");
|
|
16
|
+
}
|
|
17
|
+
|
|
4
18
|
var nexml_parser = function(xml_string, options) {
|
|
5
19
|
const parser = new XMLParser({
|
|
6
20
|
ignoreAttributes: false,
|
|
7
21
|
attributeNamePrefix: "",
|
|
8
22
|
parseAttributeValue: true
|
|
9
23
|
});
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
|
|
25
|
+
var trees = [];
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const xml = parser.parse(xml_string);
|
|
29
|
+
|
|
30
|
+
var nexml = childrenByLocalName(xml, "nexml")[0];
|
|
31
|
+
if (!nexml) {
|
|
32
|
+
return { json: null, error: "no nexml element" };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// OTU id -> label
|
|
36
|
+
var otu_labels = {};
|
|
37
|
+
childrenByLocalName(nexml, "otus").forEach(function(otus) {
|
|
38
|
+
childrenByLocalName(otus, "otu").forEach(function(otu) {
|
|
39
|
+
if (otu.label !== undefined) {
|
|
40
|
+
otu_labels[otu.id] = String(otu.label);
|
|
41
|
+
}
|
|
26
42
|
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
childrenByLocalName(nexml, "trees").forEach(function(trees_block) {
|
|
46
|
+
childrenByLocalName(trees_block, "tree").forEach(function(nexml_tree) {
|
|
47
|
+
var node_list = childrenByLocalName(nexml_tree, "node"),
|
|
48
|
+
edge_list = childrenByLocalName(nexml_tree, "edge");
|
|
49
|
+
if (node_list.length === 0) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
var node_hash = node_list.reduce(function(a, b) {
|
|
53
|
+
b.edges = [];
|
|
54
|
+
b.name =
|
|
55
|
+
b.otu !== undefined && b.otu in otu_labels
|
|
56
|
+
? otu_labels[b.otu]
|
|
57
|
+
: String(b.id);
|
|
58
|
+
a[b.id] = b;
|
|
59
|
+
return a;
|
|
60
|
+
}, {}),
|
|
61
|
+
roots = node_list.filter(d => d.root),
|
|
62
|
+
targets = new Set(edge_list.map(edge => edge.target)),
|
|
63
|
+
// without a root flag, the one node no edge points at is the root
|
|
64
|
+
unreferenced = node_list.filter(d => !targets.has(d.id)),
|
|
65
|
+
root_id = roots.length > 0
|
|
66
|
+
? roots[0].id
|
|
67
|
+
: unreferenced.length === 1 ? unreferenced[0].id : node_list[0].id;
|
|
68
|
+
node_hash[root_id].name = "root";
|
|
27
69
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
70
|
+
edge_list.forEach(function(edge) {
|
|
71
|
+
node_hash[edge.source].edges.push(edge);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
function parseNexml(node, index) {
|
|
75
|
+
if (node.edges) {
|
|
76
|
+
node.children = node.edges
|
|
77
|
+
.filter(edge => edge.target in node_hash)
|
|
78
|
+
.map(function(edge) {
|
|
79
|
+
var child = node_hash[edge.target];
|
|
80
|
+
child.attribute =
|
|
81
|
+
edge.length === undefined ? "" : String(edge.length);
|
|
82
|
+
return child;
|
|
83
|
+
});
|
|
84
|
+
node.children.forEach(parseNexml);
|
|
85
|
+
node.annotation = "";
|
|
86
|
+
}
|
|
37
87
|
}
|
|
38
|
-
}
|
|
39
88
|
|
|
40
|
-
|
|
41
|
-
|
|
89
|
+
parseNexml(node_hash[root_id]);
|
|
90
|
+
trees.push(node_hash[root_id]);
|
|
91
|
+
});
|
|
42
92
|
});
|
|
43
|
-
|
|
93
|
+
} catch {
|
|
94
|
+
return { json: null, error: "could not parse nexml" };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
json: trees[0] || null,
|
|
99
|
+
error: trees.length > 0 ? null : "no trees"
|
|
100
|
+
};
|
|
44
101
|
};
|
|
45
102
|
|
|
46
103
|
export default nexml_parser;
|