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/src/formats/nexus.js
CHANGED
|
@@ -4,45 +4,52 @@ import {default as newickParser} from "./newick";
|
|
|
4
4
|
export function parseAnnotations (buf) {
|
|
5
5
|
|
|
6
6
|
let str = buf;
|
|
7
|
-
let index = str.
|
|
8
|
-
let data = str.slice(index);
|
|
7
|
+
let index = str.search(/BEGIN\s+DATA\s*;/i);
|
|
9
8
|
|
|
10
|
-
if(
|
|
11
|
-
return '';
|
|
9
|
+
if(index < 0) {
|
|
10
|
+
return { 'dimensions' : {}, 'format' : { symbols : [] }, 'matrix' : {} };
|
|
12
11
|
}
|
|
13
12
|
|
|
13
|
+
let data = str.slice(index);
|
|
14
|
+
|
|
14
15
|
index = data.toUpperCase().indexOf('END;');
|
|
15
|
-
let data_str = data.slice(0, index);
|
|
16
|
+
let data_str = index < 0 ? data : data.slice(0, index);
|
|
16
17
|
|
|
17
18
|
// split on semicolon
|
|
18
19
|
data = _.map(data_str.split(';'), d => { return d.trim() } );
|
|
19
20
|
|
|
21
|
+
// turn "KEY=value KEY=value" into an object with lower case keys
|
|
22
|
+
const toObject = line => {
|
|
23
|
+
return _.object(_.map(_.compact(_.rest(line.split(/\s+/))), d => {
|
|
24
|
+
let pair = d.split('=');
|
|
25
|
+
return [pair[0].toLowerCase(), pair[1]];
|
|
26
|
+
}));
|
|
27
|
+
};
|
|
28
|
+
|
|
20
29
|
// get dimensions
|
|
21
|
-
let dimensions = _.filter(data, d => {return d.toUpperCase().startsWith('DIMENSION')});
|
|
22
|
-
dimensions = dimensions[0]
|
|
23
|
-
dimensions = _.object(_.map(_.rest(dimensions), d => { return d.split('=') }));
|
|
30
|
+
let dimensions = _.filter(data, d => {return d.toUpperCase().startsWith('DIMENSION')});
|
|
31
|
+
dimensions = dimensions.length ? toObject(dimensions[0]) : {};
|
|
24
32
|
|
|
25
33
|
// get formats
|
|
26
|
-
let format = _.filter(data, d => {return d.toUpperCase().startsWith('FORMAT')});
|
|
27
|
-
format = format[0]
|
|
28
|
-
format = _.
|
|
29
|
-
format.symbols = _.reject(format.symbols.split(""), d => d=='"');
|
|
34
|
+
let format = _.filter(data, d => {return d.toUpperCase().startsWith('FORMAT')});
|
|
35
|
+
format = format.length ? toObject(format[0]) : {};
|
|
36
|
+
format.symbols = _.reject((format.symbols || '').split(""), d => d=='"');
|
|
30
37
|
|
|
31
38
|
// get character matrix
|
|
32
|
-
let matrix = _.filter(data, d => {return d.toUpperCase().startsWith('MATRIX')});
|
|
33
|
-
matrix = matrix[0].split('\n')
|
|
34
|
-
matrix = _.object(_.map(_.rest(matrix), d=> { return _.compact(d.split(
|
|
39
|
+
let matrix = _.filter(data, d => {return d.toUpperCase().startsWith('MATRIX')});
|
|
40
|
+
matrix = matrix.length ? matrix[0].split('\n') : [];
|
|
41
|
+
matrix = _.object(_.filter(_.map(_.rest(matrix), d=> { return _.compact(d.split(/\s+/)) }), d => d.length > 1));
|
|
35
42
|
|
|
36
43
|
// create all possible states for matrix
|
|
37
|
-
matrix = _.mapObject(matrix, (v,k) => {
|
|
44
|
+
matrix = _.mapObject(matrix, (v,k) => {
|
|
38
45
|
|
|
39
46
|
if(v == '?') {
|
|
40
47
|
return format.symbols
|
|
41
48
|
}
|
|
42
|
-
else {
|
|
49
|
+
else {
|
|
43
50
|
return Array(v)
|
|
44
51
|
}
|
|
45
|
-
|
|
52
|
+
|
|
46
53
|
});
|
|
47
54
|
|
|
48
55
|
return { 'dimensions' : dimensions, 'format' : format, 'matrix' : matrix }
|
|
@@ -60,37 +67,110 @@ export function parseAnnotations (buf) {
|
|
|
60
67
|
export function loadAnnotations(tree, label, annotations) {
|
|
61
68
|
|
|
62
69
|
// if filename, then load from filesystem
|
|
63
|
-
_.each(tree.getTips(), d => { d.data[
|
|
70
|
+
_.each(tree.getTips(), d => { d.data[label] = annotations.matrix[d.data.name] });
|
|
64
71
|
|
|
65
72
|
// decorate nodes with annotations
|
|
66
73
|
|
|
67
74
|
}
|
|
68
75
|
|
|
69
|
-
|
|
76
|
+
// Removes surrounding single or double quotes from a NEXUS token
|
|
77
|
+
function unquote(token) {
|
|
78
|
+
// a single-quoted label escapes a quote by doubling it; strip one layer only
|
|
79
|
+
if (/^'.*'$/.test(token)) {
|
|
80
|
+
return token.slice(1, -1).replace(/''/g, "'");
|
|
81
|
+
}
|
|
82
|
+
return token.replace(/^"(.*)"$/, '$1');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Split on `sep` wherever it is not inside a 'quoted label' ('' escapes a
|
|
86
|
+
// quote) or a [comment].
|
|
87
|
+
function splitOutside(str, sep) {
|
|
88
|
+
let parts = [], current = '', quoted = false, comment = 0;
|
|
89
|
+
for (let i = 0; i < str.length; i++) {
|
|
90
|
+
let c = str[i];
|
|
91
|
+
if (quoted) {
|
|
92
|
+
if (c === "'") {
|
|
93
|
+
if (str[i + 1] === "'") { current += c; i++; }
|
|
94
|
+
else { quoted = false; }
|
|
95
|
+
}
|
|
96
|
+
} else if (comment) {
|
|
97
|
+
if (c === ']') comment--;
|
|
98
|
+
else if (c === '[') comment++;
|
|
99
|
+
} else if (c === "'") {
|
|
100
|
+
quoted = true;
|
|
101
|
+
} else if (c === '[') {
|
|
102
|
+
comment = 1;
|
|
103
|
+
} else if (c === sep) {
|
|
104
|
+
parts.push(current);
|
|
105
|
+
current = '';
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
current += c;
|
|
109
|
+
}
|
|
110
|
+
parts.push(current);
|
|
111
|
+
return parts;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export default function loadTree(buf, options) {
|
|
70
115
|
|
|
71
116
|
// if filename, then load from filesystem
|
|
72
117
|
// Parse first tree from NEXUS file and send to newickParser
|
|
73
118
|
|
|
74
|
-
// Make all upper case
|
|
75
119
|
let str = buf;
|
|
76
120
|
|
|
77
121
|
// Get TREE block
|
|
78
|
-
let index = str.
|
|
79
|
-
let split = str.slice(index);
|
|
122
|
+
let index = str.search(/BEGIN\s+TREES\s*;/i);
|
|
80
123
|
|
|
81
|
-
if(
|
|
82
|
-
return '';
|
|
124
|
+
if(index < 0) {
|
|
125
|
+
return { json: null, error: 'no TREES block' };
|
|
83
126
|
}
|
|
84
127
|
|
|
85
|
-
|
|
86
|
-
|
|
128
|
+
// Statements are separated by semicolons outside quotes and [comments];
|
|
129
|
+
// the block ends at the END statement
|
|
130
|
+
let statements = [];
|
|
131
|
+
for (let statement of splitOutside(str.slice(index), ';')) {
|
|
132
|
+
// a statement may be preceded by [comments]
|
|
133
|
+
statement = statement.replace(/^(\s*\[[^\]]*\])+/, '').trim();
|
|
134
|
+
if (/^END(BLOCK)?$/i.test(statement)) {
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
statements.push(statement);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
let tree = _.find(statements, d => { return /^TREE\s/i.test(d) });
|
|
141
|
+
|
|
142
|
+
if(!tree) {
|
|
143
|
+
return { json: null, error: 'no tree in TREES block' };
|
|
144
|
+
}
|
|
87
145
|
|
|
88
|
-
//
|
|
89
|
-
let
|
|
90
|
-
|
|
146
|
+
// Map translate tokens to names
|
|
147
|
+
let translate = {};
|
|
148
|
+
let translate_statement = _.find(statements, d => { return /^TRANSLATE\s/i.test(d) });
|
|
149
|
+
|
|
150
|
+
if(translate_statement) {
|
|
151
|
+
_.each(splitOutside(translate_statement.replace(/^TRANSLATE\s+/i, ''), ','), d => {
|
|
152
|
+
let tokens = d.trim().split(/\s+/);
|
|
153
|
+
if(tokens.length > 1) {
|
|
154
|
+
translate[tokens[0]] = unquote(tokens.slice(1).join(' '));
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
91
158
|
|
|
92
159
|
// Identify start of newick string
|
|
93
|
-
|
|
160
|
+
let result = newickParser(tree + ';', options);
|
|
161
|
+
|
|
162
|
+
if(result.json && !_.isEmpty(translate)) {
|
|
163
|
+
let translateTips = node => {
|
|
164
|
+
if(node.children && node.children.length) {
|
|
165
|
+
_.each(node.children, translateTips);
|
|
166
|
+
}
|
|
167
|
+
else if(_.has(translate, node.name)) {
|
|
168
|
+
node.name = translate[node.name];
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
translateTips(result.json);
|
|
172
|
+
}
|
|
94
173
|
|
|
95
|
-
|
|
174
|
+
return result;
|
|
96
175
|
|
|
176
|
+
}
|
package/src/formats/phyloxml.js
CHANGED
|
@@ -44,52 +44,111 @@ function xmlToJson(xml) {
|
|
|
44
44
|
return obj;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
// Converts the output of fast-xml-parser (attributes prefixed with "@_") to
|
|
48
|
+
// the same shape as xmlToJson, so both XML paths can share the code below.
|
|
49
|
+
function normalizeParsed(value) {
|
|
50
|
+
if (Array.isArray(value)) {
|
|
51
|
+
return value.map(normalizeParsed);
|
|
52
|
+
}
|
|
53
|
+
if (value !== null && typeof value === "object") {
|
|
54
|
+
var obj = {};
|
|
55
|
+
Object.keys(value).forEach(function(key) {
|
|
56
|
+
if (key.indexOf("@_") === 0) {
|
|
57
|
+
obj["@attributes"] = obj["@attributes"] || {};
|
|
58
|
+
obj["@attributes"][key.slice(2)] = String(value[key]);
|
|
59
|
+
} else {
|
|
60
|
+
obj[key] = normalizeParsed(value[key]);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
if (Object.keys(obj).length === 1 && "#text" in obj) {
|
|
64
|
+
return obj["#text"];
|
|
65
|
+
}
|
|
66
|
+
return obj;
|
|
67
|
+
}
|
|
68
|
+
return value === null || value === undefined ? "" : String(value);
|
|
69
|
+
}
|
|
70
|
+
|
|
47
71
|
var phyloxml_parser = function(xml, options) {
|
|
48
72
|
|
|
49
73
|
function parsePhyloxml(node, index) {
|
|
50
|
-
|
|
74
|
+
// a lone self-closing <clade/> parses as "", which is still a child
|
|
75
|
+
if (node.clade !== undefined && node.clade !== null) {
|
|
51
76
|
if (!Array.isArray(node.clade)) {
|
|
52
77
|
node.clade = [node.clade];
|
|
53
78
|
}
|
|
79
|
+
node.clade = node.clade.map(function(child) {
|
|
80
|
+
return child !== null && typeof child === "object" ? child : {};
|
|
81
|
+
});
|
|
54
82
|
node.clade.forEach(parsePhyloxml);
|
|
55
83
|
node.children = node.clade;
|
|
56
84
|
delete node.clade;
|
|
57
85
|
}
|
|
58
86
|
|
|
59
87
|
node.annotation = 1;
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
88
|
+
|
|
89
|
+
var attributes = node["@attributes"] || {},
|
|
90
|
+
branch_length = attributes.branch_length;
|
|
91
|
+
if (branch_length === undefined) {
|
|
92
|
+
branch_length = node.branch_length;
|
|
63
93
|
}
|
|
64
|
-
if (
|
|
65
|
-
node.
|
|
94
|
+
if (typeof branch_length === "string" && branch_length.trim().length) {
|
|
95
|
+
node.attribute = branch_length.trim();
|
|
66
96
|
}
|
|
67
97
|
|
|
98
|
+
var name = typeof node.name === "string" ? node.name : "";
|
|
99
|
+
if (
|
|
100
|
+
node.taxonomy &&
|
|
101
|
+
typeof node.taxonomy === "object" &&
|
|
102
|
+
typeof node.taxonomy.scientific_name === "string"
|
|
103
|
+
) {
|
|
104
|
+
name = node.taxonomy.scientific_name;
|
|
105
|
+
}
|
|
106
|
+
node.name = name;
|
|
107
|
+
|
|
68
108
|
node.annotation = "";
|
|
69
109
|
|
|
70
110
|
}
|
|
71
111
|
|
|
72
112
|
var tree_json;
|
|
73
113
|
|
|
74
|
-
|
|
75
|
-
if (
|
|
76
|
-
|
|
77
|
-
|
|
114
|
+
try {
|
|
115
|
+
if (typeof xml === "string") {
|
|
116
|
+
if (typeof DOMParser !== "undefined") {
|
|
117
|
+
const parser = new DOMParser();
|
|
118
|
+
xml = xmlToJson(parser.parseFromString(xml, "text/xml"));
|
|
119
|
+
} else {
|
|
120
|
+
const parser = new XMLParser({
|
|
121
|
+
ignoreAttributes: false,
|
|
122
|
+
attributeNamePrefix: "@_",
|
|
123
|
+
parseTagValue: false,
|
|
124
|
+
parseAttributeValue: false
|
|
125
|
+
});
|
|
126
|
+
xml = normalizeParsed(parser.parse(xml));
|
|
127
|
+
}
|
|
78
128
|
} else {
|
|
79
|
-
|
|
80
|
-
xml = parser.parse(xml);
|
|
129
|
+
xml = xmlToJson(xml);
|
|
81
130
|
}
|
|
131
|
+
} catch {
|
|
132
|
+
return { json: null, error: "could not parse phyloxml" };
|
|
82
133
|
}
|
|
83
134
|
|
|
84
|
-
|
|
85
|
-
var phylogeny = xml.phyloxml.phylogeny;
|
|
135
|
+
var phylogeny = xml && xml.phyloxml && xml.phyloxml.phylogeny;
|
|
86
136
|
if (Array.isArray(phylogeny)) {
|
|
87
137
|
phylogeny = phylogeny[0];
|
|
88
138
|
console.warn("PhyloXML files with multiple phylogenies are not currently supported. Only the first phylogeny will be loaded.")
|
|
89
139
|
}
|
|
140
|
+
if (!phylogeny || typeof phylogeny !== "object") {
|
|
141
|
+
return { json: null, error: "no phylogeny" };
|
|
142
|
+
}
|
|
90
143
|
tree_json = phylogeny.clade;
|
|
91
|
-
tree_json
|
|
144
|
+
if (Array.isArray(tree_json)) {
|
|
145
|
+
tree_json = tree_json[0];
|
|
146
|
+
}
|
|
147
|
+
if (!tree_json || typeof tree_json !== "object") {
|
|
148
|
+
return { json: null, error: "no clade" };
|
|
149
|
+
}
|
|
92
150
|
parsePhyloxml(tree_json, 0);
|
|
151
|
+
tree_json.name = "root";
|
|
93
152
|
|
|
94
153
|
return {
|
|
95
154
|
json: tree_json,
|
package/src/formats/registry.js
CHANGED
|
@@ -13,7 +13,11 @@ var format_registry = {
|
|
|
13
13
|
phyloxml: phyloxml_parser,
|
|
14
14
|
nexus : nexus_parser,
|
|
15
15
|
nwk: newickParser,
|
|
16
|
-
nhx:
|
|
16
|
+
nhx: (nwk, options) =>
|
|
17
|
+
newickParser(
|
|
18
|
+
nwk,
|
|
19
|
+
Object.assign({ left_delimiter: "[", right_delimiter: "]" }, options)
|
|
20
|
+
),
|
|
17
21
|
beast: beast_parser
|
|
18
22
|
};
|
|
19
23
|
|
package/src/index.js
CHANGED
|
@@ -17,6 +17,7 @@ export { default as neighborJoining, getDistanceMatrix } from "./neighbor-join";
|
|
|
17
17
|
export { loadAnnotations, parseAnnotations } from "./formats/nexus";
|
|
18
18
|
|
|
19
19
|
export { default as newickParser, getNewick } from "./formats/newick";
|
|
20
|
+
export { addCustomMenu } from "./render/menus";
|
|
20
21
|
|
|
21
22
|
export {
|
|
22
23
|
default as inOrder,
|
package/src/main.js
CHANGED
|
@@ -46,34 +46,44 @@ function resortChildren(comparator, start_node, filter) {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
/**
|
|
49
|
-
* Return most recent common ancestor of a
|
|
50
|
-
* @
|
|
49
|
+
* Return most recent common ancestor of a set of nodes.
|
|
50
|
+
* @param {Array} mrca_nodes Node names, or nodes.
|
|
51
|
+
* @returns The deepest node whose descendants include all of the given nodes,
|
|
52
|
+
* or undefined if there is none.
|
|
51
53
|
*/
|
|
52
54
|
function mrca(mrca_nodes) {
|
|
53
|
-
|
|
55
|
+
let wanted = new Set(
|
|
56
|
+
mrca_nodes.map(function (mrca_node) {
|
|
57
|
+
return typeof mrca_node == "string" ? mrca_node : mrca_node.data.name;
|
|
58
|
+
})
|
|
59
|
+
);
|
|
54
60
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
});
|
|
61
|
+
let found = new Map(),
|
|
62
|
+
result;
|
|
58
63
|
|
|
59
64
|
this.traverse_and_compute(function (node) {
|
|
65
|
+
let names = new Set();
|
|
66
|
+
|
|
60
67
|
if (!node.children) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (!mrca) {
|
|
64
|
-
mrca = node;
|
|
68
|
+
if (wanted.has(node.data.name)) {
|
|
69
|
+
names.add(node.data.name);
|
|
65
70
|
}
|
|
66
71
|
} else {
|
|
67
|
-
node.
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
72
|
+
node.children.forEach(function (child) {
|
|
73
|
+
found.get(child).forEach(function (name) {
|
|
74
|
+
names.add(name);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
found.set(node, names);
|
|
80
|
+
|
|
81
|
+
if (!result && wanted.size && names.size == wanted.size) {
|
|
82
|
+
result = node;
|
|
73
83
|
}
|
|
74
84
|
});
|
|
75
85
|
|
|
76
|
-
return
|
|
86
|
+
return result;
|
|
77
87
|
}
|
|
78
88
|
|
|
79
89
|
/**
|
|
@@ -116,8 +126,7 @@ let Phylotree = class {
|
|
|
116
126
|
this.links = [];
|
|
117
127
|
this.parsed_tags = [];
|
|
118
128
|
this.partitions = [];
|
|
119
|
-
this.
|
|
120
|
-
this.branch_length = defBranchLengthAccessor;
|
|
129
|
+
this.setBranchLength(defBranchLengthAccessor);
|
|
121
130
|
this.logger = options.logger || console;
|
|
122
131
|
this.selection_attribute_name = "selected";
|
|
123
132
|
|
|
@@ -149,13 +158,24 @@ let Phylotree = class {
|
|
|
149
158
|
}
|
|
150
159
|
} else {
|
|
151
160
|
// this builds children and links;
|
|
152
|
-
if (nwk
|
|
161
|
+
if (nwk === null || nwk === undefined) {
|
|
162
|
+
throw new Error("Unable to parse tree: no tree was supplied");
|
|
163
|
+
} else if (nwk.name == "root") {
|
|
153
164
|
// already parsed by phylotree.js
|
|
154
165
|
_node_data = { json: nwk, error: null };
|
|
155
166
|
} else if (typeof nwk != "string") {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
167
|
+
if (typeof nwk != "object") {
|
|
168
|
+
throw new Error("Unable to parse tree: expected a string or an object");
|
|
169
|
+
}
|
|
170
|
+
// a parser result ({json, error}) unless the object has children of
|
|
171
|
+
// its own, in which case it is hierarchical JSON
|
|
172
|
+
_node_data =
|
|
173
|
+
"json" in nwk && !Array.isArray(nwk.children)
|
|
174
|
+
? { json: nwk.json, error: nwk.error || null }
|
|
175
|
+
: { json: nwk, error: null };
|
|
176
|
+
} else if (
|
|
177
|
+
/^\s*<(\?xml|phyloxml|phylogeny)/i.test(nwk)
|
|
178
|
+
) {
|
|
159
179
|
// xml
|
|
160
180
|
_node_data = phyloxml_parser(nwk);
|
|
161
181
|
} else {
|
|
@@ -165,8 +185,10 @@ let Phylotree = class {
|
|
|
165
185
|
}
|
|
166
186
|
}
|
|
167
187
|
|
|
168
|
-
if (!_node_data["json"]) {
|
|
169
|
-
|
|
188
|
+
if (!_node_data || !_node_data["json"]) {
|
|
189
|
+
throw new Error(
|
|
190
|
+
(_node_data && _node_data.error) || "Unable to parse tree"
|
|
191
|
+
);
|
|
170
192
|
} else {
|
|
171
193
|
self.nodes = d3.hierarchy(_node_data.json);
|
|
172
194
|
|
|
@@ -174,6 +196,14 @@ let Phylotree = class {
|
|
|
174
196
|
let _parsed_tags = {};
|
|
175
197
|
|
|
176
198
|
self.nodes.each((node) => {
|
|
199
|
+
if (typeof node.data.name === "number") {
|
|
200
|
+
node.data.name = String(node.data.name);
|
|
201
|
+
} else if (node.data.name === undefined || node.data.name === null) {
|
|
202
|
+
node.data.name = "";
|
|
203
|
+
}
|
|
204
|
+
if (node.data.bootstrap_values === "") {
|
|
205
|
+
node.data.bootstrap_values = undefined;
|
|
206
|
+
}
|
|
177
207
|
if (node.data.annotation) {
|
|
178
208
|
_parsed_tags[node.data.annotation] = true;
|
|
179
209
|
}
|
|
@@ -312,6 +342,27 @@ let Phylotree = class {
|
|
|
312
342
|
update(json) {
|
|
313
343
|
// update with new hiearchy layout
|
|
314
344
|
this.nodes = json;
|
|
345
|
+
|
|
346
|
+
this.nodes.eachBefore((node) => {
|
|
347
|
+
_.each(node.children, (child) => {
|
|
348
|
+
child.parent = node;
|
|
349
|
+
});
|
|
350
|
+
node.depth = node.parent ? node.parent.depth + 1 : 0;
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
this.nodes.eachAfter((node) => {
|
|
354
|
+
node.height = node.children
|
|
355
|
+
? 1 + Math.max(...node.children.map((child) => child.height))
|
|
356
|
+
: 0;
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
this.links = this.nodes.links();
|
|
360
|
+
|
|
361
|
+
if (this.display) {
|
|
362
|
+
this.display.links = this.links;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
return this;
|
|
315
366
|
}
|
|
316
367
|
|
|
317
368
|
/**
|
|
@@ -88,7 +88,8 @@ export function centerOfTree(tree, power) {
|
|
|
88
88
|
|
|
89
89
|
if (score < current_min) {
|
|
90
90
|
current_location = n;
|
|
91
|
-
current_split =
|
|
91
|
+
current_split =
|
|
92
|
+
n.cot_computed_length > 0 ? tt / n.cot_computed_length : 0; //n.cot_computed_length-tt;//1 - tt / n.cot_computed_length;
|
|
92
93
|
current_min = score;
|
|
93
94
|
}
|
|
94
95
|
|
|
@@ -121,7 +122,8 @@ export function centerOfTree(tree, power) {
|
|
|
121
122
|
|
|
122
123
|
if (score < current_min) {
|
|
123
124
|
current_location = n;
|
|
124
|
-
current_split =
|
|
125
|
+
current_split =
|
|
126
|
+
n.cot_computed_length > 0 ? current_t / n.cot_computed_length : 0; //n.cot_computed_length-tt;//1 - tt / n.cot_computed_length;
|
|
125
127
|
current_min = score;
|
|
126
128
|
}
|
|
127
129
|
current_t += optimization_step;
|
|
@@ -11,7 +11,13 @@ export function computeMidpoint(tree) {
|
|
|
11
11
|
throw "Center of tree calculation cannot be performed on trees that do not have fully specified branch lengths";
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
var bl = tree.
|
|
14
|
+
var bl = tree.branch_length_accessor;
|
|
15
|
+
|
|
16
|
+
// clear values left over from any previous call
|
|
17
|
+
tree.traverse_and_compute(function(node) {
|
|
18
|
+
delete node.max_path;
|
|
19
|
+
delete node.max_path_terminus;
|
|
20
|
+
});
|
|
15
21
|
|
|
16
22
|
tree.traverse_and_compute(function(node) {
|
|
17
23
|
if (node.parent) {
|
|
@@ -39,8 +39,7 @@ function computePairwiseDistances(tree) {
|
|
|
39
39
|
tree.traverse_and_compute(function(n) {
|
|
40
40
|
n.cot_computed_length = bl(n);
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
if (n.parent && _.isUndefined(n.cot_computed_length)) {
|
|
42
|
+
if (n.parent && !_.isFinite(n.cot_computed_length)) {
|
|
44
43
|
throw "Non-null branch lengths are required for this operation: missing branch length at node " + n.data.name;
|
|
45
44
|
}
|
|
46
45
|
|
|
@@ -70,6 +69,9 @@ function computePairwiseDistances(tree) {
|
|
|
70
69
|
var root_node = tree.getRootNode();
|
|
71
70
|
|
|
72
71
|
function CopyFromSiblings(a_node) {
|
|
72
|
+
if (!a_node.children) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
73
75
|
for (var this_node = 0; this_node < a_node.children.length; this_node++) {
|
|
74
76
|
for (
|
|
75
77
|
var other_node = 0;
|
|
@@ -17,7 +17,7 @@ function computeRootToTipOtherRoot(
|
|
|
17
17
|
distance_to_new_root
|
|
18
18
|
) {
|
|
19
19
|
|
|
20
|
-
var my_bl = tree.
|
|
20
|
+
var my_bl = tree.branch_length_accessor(node);
|
|
21
21
|
|
|
22
22
|
var go_up = false;
|
|
23
23
|
|
|
@@ -65,42 +65,55 @@ export function fitRootToTip(tree) {
|
|
|
65
65
|
|
|
66
66
|
var linear_data = [],
|
|
67
67
|
max_r2 = 0,
|
|
68
|
-
best_node =
|
|
68
|
+
best_node = tree.getRootNode();
|
|
69
69
|
|
|
70
70
|
annotateCopyNumber(tree);
|
|
71
71
|
rootToTip(tree);
|
|
72
72
|
|
|
73
73
|
// To return if best node is the root already
|
|
74
74
|
tree.traverse_and_compute(function(node) {
|
|
75
|
-
if (tree.isLeafNode(node) &&
|
|
76
|
-
|
|
75
|
+
if (tree.isLeafNode(node) && tipDate(node) !== null) {
|
|
76
|
+
// distances from the current root; rtta is only set when trying other roots
|
|
77
|
+
linear_data.push([tipDate(node), node.data.rootToTip, node.data.copy_number]);
|
|
77
78
|
}
|
|
78
79
|
});
|
|
79
80
|
|
|
81
|
+
if (linear_data.length < 2) {
|
|
82
|
+
return { root: best_node, fit: null };
|
|
83
|
+
}
|
|
84
|
+
|
|
80
85
|
let best_fit = linearFit(linear_data);
|
|
81
86
|
|
|
87
|
+
// a candidate root has to beat the current one; when the current root has
|
|
88
|
+
// no usable fit (e.g. identical distances) any finite fit will do
|
|
89
|
+
max_r2 = best_fit ? best_fit["r2"] : -Infinity;
|
|
90
|
+
|
|
82
91
|
tree.traverse_and_compute(function(node) {
|
|
83
92
|
|
|
84
|
-
if (tree.isLeafNode(node) &&
|
|
93
|
+
if (tree.isLeafNode(node) && tipDate(node) !== null) {
|
|
85
94
|
|
|
86
95
|
computeRootToTipOtherRoot(tree, node, null, 0, 0);
|
|
87
96
|
|
|
88
97
|
linear_data = [];
|
|
89
98
|
|
|
90
99
|
tree.traverse_and_compute(function(node) {
|
|
91
|
-
if (tree.isLeafNode(node) &&
|
|
100
|
+
if (tree.isLeafNode(node) && tipDate(node) !== null) {
|
|
92
101
|
linear_data.push([
|
|
93
|
-
node
|
|
102
|
+
tipDate(node),
|
|
94
103
|
node.data.rtta,
|
|
95
104
|
node.data.copy_number
|
|
96
105
|
]);
|
|
97
106
|
}
|
|
98
107
|
});
|
|
99
108
|
|
|
109
|
+
if (linear_data.length < 2) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
100
113
|
var fit = linearFit(linear_data),
|
|
101
|
-
r2 = fit["r2"];
|
|
114
|
+
r2 = fit ? fit["r2"] : NaN;
|
|
102
115
|
|
|
103
|
-
if (r2 > max_r2) {
|
|
116
|
+
if (isFinite(r2) && r2 > max_r2) {
|
|
104
117
|
max_r2 = r2;
|
|
105
118
|
best_node = node;
|
|
106
119
|
best_fit = fit;
|
|
@@ -113,6 +126,16 @@ export function fitRootToTip(tree) {
|
|
|
113
126
|
|
|
114
127
|
}
|
|
115
128
|
|
|
129
|
+
// A tip's decimal date as a number, or null when it has none. Dates set by
|
|
130
|
+
// hand may be numeric strings.
|
|
131
|
+
function tipDate(node) {
|
|
132
|
+
let value = node.data.decimal_date_value;
|
|
133
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
134
|
+
value = Number(value);
|
|
135
|
+
}
|
|
136
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
137
|
+
}
|
|
138
|
+
|
|
116
139
|
// linear fit of root to tip distances
|
|
117
140
|
function linearFit(data) {
|
|
118
141
|
|
|
@@ -139,6 +162,11 @@ function linearFit(data) {
|
|
|
139
162
|
vary += point[2] * (point[1] - syoss) * (point[1] - syoss);
|
|
140
163
|
});
|
|
141
164
|
|
|
165
|
+
// no spread in the dates: the slope is undefined
|
|
166
|
+
if (!(st2 > 0)) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
|
|
142
170
|
fitB /= st2;
|
|
143
171
|
|
|
144
172
|
var a = (sy - sx * fitB) / ss;
|
|
@@ -150,6 +178,11 @@ function linearFit(data) {
|
|
|
150
178
|
varres += point[2] * t * t;
|
|
151
179
|
});
|
|
152
180
|
|
|
181
|
+
// zero or infinite distances, or identical distances, leave no finite line
|
|
182
|
+
if (!Number.isFinite(a) || !Number.isFinite(fitB) || !(vary > 0)) {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
|
|
153
186
|
return {
|
|
154
187
|
intercept: a,
|
|
155
188
|
slope: fitB,
|
|
@@ -205,7 +238,7 @@ export default function rootToTip(tree) {
|
|
|
205
238
|
}
|
|
206
239
|
});
|
|
207
240
|
|
|
208
|
-
var r2t = tree.getRootNode().data.r2t;
|
|
241
|
+
var r2t = tree.getRootNode().data.r2t || {};
|
|
209
242
|
|
|
210
243
|
tree.traverse_and_compute(n => {
|
|
211
244
|
if (tree.isLeafNode(n)) {
|