phylotree 1.2.0 → 1.2.2
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/bin/phylotree.js +1 -1
- package/package.json +4 -3
- package/src/branches.js +155 -0
- package/src/clustering/cluster-picker.js +115 -0
- package/src/clustering/phylopart.js +238 -0
- package/src/export.js +25 -0
- package/src/extract-dates.js +69 -0
- package/src/formats/beast.js +29 -0
- package/src/formats/newick.js +272 -0
- package/src/formats/nexml.js +42 -0
- package/src/formats/nexus.js +96 -0
- package/src/formats/phyloxml.js +80 -0
- package/src/formats/registry.js +20 -0
- package/src/index.js +26 -0
- package/src/main.js +311 -0
- package/src/max-parsimony.js +81 -0
- package/src/metrics/center-of-tree.js +138 -0
- package/src/metrics/compute-midpoint.js +81 -0
- package/src/metrics/pairwise-distances.js +119 -0
- package/src/metrics/root-to-tip.js +220 -0
- package/src/metrics/sackins.js +21 -0
- package/src/msa-parsers/fasta.js +18 -0
- package/src/neighbor-join.js +196 -0
- package/src/nodes.js +241 -0
- package/src/render/cartesian.js +23 -0
- package/src/render/clades.js +116 -0
- package/src/render/coordinates.js +7 -0
- package/src/render/draw.js +1365 -0
- package/src/render/edges.js +134 -0
- package/src/render/events.js +166 -0
- package/src/render/helpers.js +7 -0
- package/src/render/menus.js +452 -0
- package/src/render/nodes.js +355 -0
- package/src/render/options.js +141 -0
- package/src/render/radial.js +79 -0
- package/src/rooting.js +183 -0
- package/src/transformations.js +20 -0
- package/src/traversal.js +114 -0
package/src/main.js
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import * as d3 from "d3";
|
|
2
|
+
import * as _ from "underscore";
|
|
3
|
+
|
|
4
|
+
import { default as parser_registry } from "./formats/registry";
|
|
5
|
+
import { default as newickParser, getNewick } from "./formats/newick";
|
|
6
|
+
import { default as getTipLengths } from "./export";
|
|
7
|
+
import * as nexus from "./formats/nexus";
|
|
8
|
+
import { default as phyloxml_parser } from "./formats/phyloxml";
|
|
9
|
+
import { default as maxParsimony } from "./max-parsimony";
|
|
10
|
+
import { leftChildRightSibling, postOrder, preOrder, default as inOrder } from "./traversal";
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
default as hasBranchLengths,
|
|
14
|
+
getBranchLengths,
|
|
15
|
+
defBranchLengthAccessor,
|
|
16
|
+
setBranchLength,
|
|
17
|
+
branchName,
|
|
18
|
+
normalize,
|
|
19
|
+
scale
|
|
20
|
+
} from "./branches";
|
|
21
|
+
|
|
22
|
+
import * as node_operations from "./nodes";
|
|
23
|
+
import * as rooting from "./rooting";
|
|
24
|
+
import { default as TreeRender } from "./render/draw";
|
|
25
|
+
|
|
26
|
+
function resortChildren(comparator, start_node, filter) {
|
|
27
|
+
// ascending
|
|
28
|
+
this.nodes
|
|
29
|
+
.sum(function(d) {
|
|
30
|
+
return d.value;
|
|
31
|
+
})
|
|
32
|
+
.sort(comparator);
|
|
33
|
+
|
|
34
|
+
// if a tree is rendered in the DOM
|
|
35
|
+
if (this.display) {
|
|
36
|
+
this.display.update_layout(this.nodes);
|
|
37
|
+
this.display.update();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Return most recent common ancestor of a pair of nodes.
|
|
45
|
+
* @returns An array of strings, comprising each tag that was read.
|
|
46
|
+
*/
|
|
47
|
+
function mrca() {
|
|
48
|
+
|
|
49
|
+
var mrca_nodes, mrca;
|
|
50
|
+
|
|
51
|
+
if (arguments.length == 1) {
|
|
52
|
+
mrca_nodes = arguments[0];
|
|
53
|
+
} else {
|
|
54
|
+
mrca_nodes = Array.from(arguments);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
mrca_nodes = mrca_nodes.map(function(mrca_node) {
|
|
58
|
+
return typeof mrca_node == "string" ? mrca_node : mrca_node.name;
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
this.traverse_and_compute(function(node) {
|
|
62
|
+
if (!node.children) {
|
|
63
|
+
node.mrca = _.intersection([node.name], mrca_nodes);
|
|
64
|
+
} else if (!node.parent) {
|
|
65
|
+
if (!mrca) {
|
|
66
|
+
mrca = node;
|
|
67
|
+
}
|
|
68
|
+
} else {
|
|
69
|
+
node.mrca = _.union(...node.descendants().map(child => child.mrca));
|
|
70
|
+
if (!mrca && node.mrca.length == mrca_nodes.length) {
|
|
71
|
+
mrca = node;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return mrca;
|
|
77
|
+
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* An instance of a phylotree. Sets event listeners, parses tags, and creates links
|
|
82
|
+
* that represent branches.
|
|
83
|
+
*
|
|
84
|
+
* @param {Object} nwk - A Newick string, PhyloXML string, or hierarchical JSON representation of a phylogenetic tree.
|
|
85
|
+
* @param {Object} options
|
|
86
|
+
* - boostrap_values
|
|
87
|
+
* - type - format type
|
|
88
|
+
* @returns {Phylotree} phylotree - itself, following the builder pattern.
|
|
89
|
+
*/
|
|
90
|
+
let Phylotree = class {
|
|
91
|
+
|
|
92
|
+
constructor(nwk, options = {}) {
|
|
93
|
+
|
|
94
|
+
this.newick_string = "";
|
|
95
|
+
|
|
96
|
+
this.nodes = [];
|
|
97
|
+
this.links = [];
|
|
98
|
+
this.parsed_tags = [];
|
|
99
|
+
this.partitions = [];
|
|
100
|
+
this.branch_length_accessor = defBranchLengthAccessor;
|
|
101
|
+
this.branch_length = defBranchLengthAccessor;
|
|
102
|
+
this.logger = options.logger || console;
|
|
103
|
+
this.selection_attribute_name = "selected";
|
|
104
|
+
|
|
105
|
+
// initialization
|
|
106
|
+
var type = options.type || undefined,
|
|
107
|
+
_node_data = [],
|
|
108
|
+
self = this;
|
|
109
|
+
|
|
110
|
+
// If the type is a string, check the parser_registry
|
|
111
|
+
if (_.isString(type)) {
|
|
112
|
+
if (type in parser_registry) {
|
|
113
|
+
_node_data = parser_registry[type](nwk, options);
|
|
114
|
+
} else {
|
|
115
|
+
// Hard failure
|
|
116
|
+
self.logger.error(
|
|
117
|
+
"type " +
|
|
118
|
+
type +
|
|
119
|
+
" not in registry! Available types are " +
|
|
120
|
+
_.keys(parser_registry)
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
} else if (_.isFunction(type)) {
|
|
124
|
+
// If the type is a function, try executing the function
|
|
125
|
+
try {
|
|
126
|
+
_node_data = type(nwk, options);
|
|
127
|
+
} catch (e) {
|
|
128
|
+
// Hard failure
|
|
129
|
+
self.logger.error("Could not parse custom format!");
|
|
130
|
+
}
|
|
131
|
+
} else {
|
|
132
|
+
// this builds children and links;
|
|
133
|
+
if (nwk.name == "root") {
|
|
134
|
+
// already parsed by phylotree.js
|
|
135
|
+
_node_data = { json: nwk, error: null };
|
|
136
|
+
} else if (typeof nwk != "string") {
|
|
137
|
+
// old default
|
|
138
|
+
_node_data = nwk;
|
|
139
|
+
} else if (nwk.contentType == "application/xml") {
|
|
140
|
+
// xml
|
|
141
|
+
_node_data = phyloxml_parser(nwk);
|
|
142
|
+
} else {
|
|
143
|
+
// newick string
|
|
144
|
+
this.newick_string = nwk;
|
|
145
|
+
_node_data = newickParser(nwk, options);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (!_node_data["json"]) {
|
|
151
|
+
|
|
152
|
+
self.nodes = [];
|
|
153
|
+
|
|
154
|
+
} else {
|
|
155
|
+
|
|
156
|
+
self.nodes = d3.hierarchy(_node_data.json);
|
|
157
|
+
|
|
158
|
+
// Parse tags
|
|
159
|
+
let _parsed_tags = {};
|
|
160
|
+
|
|
161
|
+
self.nodes.each(node => {
|
|
162
|
+
if (node.data.annotation) {
|
|
163
|
+
_parsed_tags[node.data.annotation] = true;
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
self.parsed_tags = Object.keys(_parsed_tags);
|
|
168
|
+
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
self.links = self.nodes.links();
|
|
172
|
+
|
|
173
|
+
// If no branch lengths are supplied, set all to 1
|
|
174
|
+
if(!this.hasBranchLengths()) {
|
|
175
|
+
console.warn("Phylotree User Warning : NO BRANCH LENGTHS DETECTED, SETTING ALL LENGTHS TO 1");
|
|
176
|
+
this.setBranchLength(x => 1)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return self;
|
|
180
|
+
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/*
|
|
184
|
+
Export the nodes of the tree with all local keys to JSON
|
|
185
|
+
The return will be an array of nodes in the specified traversal_type
|
|
186
|
+
('post-order' : default, 'pre-order', or 'in-order')
|
|
187
|
+
with parents and children referring to indices in that array
|
|
188
|
+
|
|
189
|
+
*/
|
|
190
|
+
json(traversal_type) {
|
|
191
|
+
|
|
192
|
+
var index = 0;
|
|
193
|
+
|
|
194
|
+
this.traverse_and_compute(function(n) {
|
|
195
|
+
n.json_export_index = index++;
|
|
196
|
+
}, traversal_type);
|
|
197
|
+
|
|
198
|
+
var node_array = new Array(index);
|
|
199
|
+
|
|
200
|
+
index = 0;
|
|
201
|
+
|
|
202
|
+
this.traverse_and_compute(function(n) {
|
|
203
|
+
let node_copy = _.clone(n);
|
|
204
|
+
delete node_copy.json_export_index;
|
|
205
|
+
|
|
206
|
+
if (n.parent) {
|
|
207
|
+
node_copy.parent = n.parent.json_export_index;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (n.children) {
|
|
211
|
+
node_copy.children = _.map(n.children, function(c) {
|
|
212
|
+
return c.json_export_index;
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
node_array[index++] = node_copy;
|
|
216
|
+
}, traversal_type);
|
|
217
|
+
|
|
218
|
+
this.traverse_and_compute(function(n) {
|
|
219
|
+
delete n.json_export_index;
|
|
220
|
+
}, traversal_type);
|
|
221
|
+
|
|
222
|
+
return JSON.stringify(node_array);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/*
|
|
226
|
+
* Traverse the tree in a prescribed order, and compute a value at each node.
|
|
227
|
+
*
|
|
228
|
+
* @param {Function} callback A function to be called on each node.
|
|
229
|
+
* @param {String} traversal_type Either ``"pre-order"`` or ``"post-order"`` or ``"in-order"``.
|
|
230
|
+
* @param {Node} root_node start traversal here, if provided, otherwise start at root
|
|
231
|
+
* @param {Function} backtrack ; if provided, then at each node n, backtrack (n) will be called,
|
|
232
|
+
and if it returns TRUE, traversal will NOT continue past into this
|
|
233
|
+
node and its children
|
|
234
|
+
*/
|
|
235
|
+
traverse_and_compute(callback, traversal_type, root_node, backtrack) {
|
|
236
|
+
traversal_type = traversal_type || "post-order";
|
|
237
|
+
|
|
238
|
+
function post_order(node) {
|
|
239
|
+
if (_.isUndefined(node)) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
postOrder(node, callback, backtrack);
|
|
244
|
+
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function pre_order(node) {
|
|
248
|
+
preOrder(node, callback, backtrack);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function in_order(node) {
|
|
252
|
+
inOrder(node, callback, backtrack);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (traversal_type == "pre-order") {
|
|
256
|
+
traversal_type = pre_order;
|
|
257
|
+
} else {
|
|
258
|
+
if (traversal_type == "in-order") {
|
|
259
|
+
traversal_type = in_order;
|
|
260
|
+
} else {
|
|
261
|
+
traversal_type = post_order;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
traversal_type(root_node ? root_node : this.nodes);
|
|
266
|
+
|
|
267
|
+
return this;
|
|
268
|
+
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
get_parsed_tags() {
|
|
272
|
+
return this.parsed_tags;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
update(json) {
|
|
276
|
+
// update with new hiearchy layout
|
|
277
|
+
this.nodes = json;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Warning : Requires DOM!
|
|
281
|
+
render(options) {
|
|
282
|
+
this.display = new TreeRender(this, options);
|
|
283
|
+
return this.display;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
Phylotree.prototype.isLeafNode = node_operations.isLeafNode;
|
|
289
|
+
Phylotree.prototype.mrca = mrca;
|
|
290
|
+
Phylotree.prototype.hasBranchLengths = hasBranchLengths;
|
|
291
|
+
Phylotree.prototype.getBranchLengths = getBranchLengths;
|
|
292
|
+
Phylotree.prototype.branchName = branchName;
|
|
293
|
+
Phylotree.prototype.normalizeBranchLengths = normalize;
|
|
294
|
+
Phylotree.prototype.scaleBranchLengths = scale;
|
|
295
|
+
Phylotree.prototype.getNewick = getNewick;
|
|
296
|
+
Phylotree.prototype.resortChildren = resortChildren;
|
|
297
|
+
Phylotree.prototype.setBranchLength = setBranchLength;
|
|
298
|
+
Phylotree.prototype.maxParsimony = maxParsimony;
|
|
299
|
+
|
|
300
|
+
Phylotree.prototype.getTipLengths = getTipLengths;
|
|
301
|
+
Phylotree.prototype.leftChildRightSibling = leftChildRightSibling;
|
|
302
|
+
|
|
303
|
+
_.extend(Phylotree.prototype, node_operations);
|
|
304
|
+
_.extend(Phylotree.prototype, rooting);
|
|
305
|
+
_.extend(Phylotree.prototype, nexus);
|
|
306
|
+
|
|
307
|
+
export function itemTagged(item) {
|
|
308
|
+
return item.tag || false;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export default Phylotree;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import * as _ from "underscore";
|
|
2
|
+
import { isLeafNode } from "./nodes";
|
|
3
|
+
|
|
4
|
+
export default function maxParsimony(respect_existing, attr_name) {
|
|
5
|
+
|
|
6
|
+
function populateMpMatrix(attr_name, d) {
|
|
7
|
+
|
|
8
|
+
d.mp = [
|
|
9
|
+
[0, 0], // score for parent selected / not selected
|
|
10
|
+
[false, false]
|
|
11
|
+
]; // selected or not
|
|
12
|
+
|
|
13
|
+
if (isLeafNode(d)) {
|
|
14
|
+
|
|
15
|
+
d.mp[1][0] = d.mp[1][1] = d[attr_name] || false;
|
|
16
|
+
d.mp[0][0] = d.mp[1][0] ? 1 : 0;
|
|
17
|
+
d.mp[0][1] = 1 - d.mp[0][0];
|
|
18
|
+
|
|
19
|
+
} else {
|
|
20
|
+
|
|
21
|
+
d.children.forEach(pop_mp_mat);
|
|
22
|
+
|
|
23
|
+
var s0 = d.children.reduce(function(p, n) {
|
|
24
|
+
return n.mp[0][0] + p;
|
|
25
|
+
}, 0);
|
|
26
|
+
|
|
27
|
+
// cumulative children score if this node is 0
|
|
28
|
+
var s1 = d.children.reduce(function(p, n) {
|
|
29
|
+
return n.mp[0][1] + p;
|
|
30
|
+
}, 0);
|
|
31
|
+
|
|
32
|
+
// cumulative children score if this node is 1
|
|
33
|
+
// parent = 0
|
|
34
|
+
|
|
35
|
+
if (d[attr_name]) {
|
|
36
|
+
// respect selected
|
|
37
|
+
d.mp[0][0] = s1 + 1;
|
|
38
|
+
d.mp[1][0] = true;
|
|
39
|
+
d.mp[0][1] = s1;
|
|
40
|
+
d.mp[1][1] = true;
|
|
41
|
+
} else {
|
|
42
|
+
if (s0 < s1 + 1) {
|
|
43
|
+
d.mp[0][0] = s0;
|
|
44
|
+
d.mp[1][0] = false;
|
|
45
|
+
} else {
|
|
46
|
+
d.mp[0][0] = s1 + 1;
|
|
47
|
+
d.mp[1][0] = true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// parent = 1
|
|
51
|
+
|
|
52
|
+
if (s1 < s0 + 1) {
|
|
53
|
+
d.mp[0][1] = s1;
|
|
54
|
+
d.mp[1][1] = true;
|
|
55
|
+
} else {
|
|
56
|
+
d.mp[0][1] = s0 + 1;
|
|
57
|
+
d.mp[1][1] = false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const pop_mp_mat = _.partial(populateMpMatrix, attr_name);
|
|
64
|
+
pop_mp_mat(this.nodes);
|
|
65
|
+
|
|
66
|
+
this.nodes.each(d => {
|
|
67
|
+
if (d.parent) {
|
|
68
|
+
d.mp = d.mp[1][d.parent.mp ? 1 : 0];
|
|
69
|
+
} else {
|
|
70
|
+
d.mp = d.mp[1][d.mp[0][0] < d.mp[0][1] ? 0 : 1];
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
this.display.modifySelection((d, callback) => {
|
|
75
|
+
if (isLeafNode(d.target)) {
|
|
76
|
+
return d.target[attr_name];
|
|
77
|
+
}
|
|
78
|
+
return d.target.mp;
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import * as _ from "underscore";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
*special case for L^2
|
|
5
|
+
*
|
|
6
|
+
*For a fixed branch B, we have one parameter : where to break the branch `t` in [0, T],
|
|
7
|
+
*where T is the branch length
|
|
8
|
+
*
|
|
9
|
+
*D = sum (a is a leaf above B) [d(B,a) + (T-t)] ^ 2 +
|
|
10
|
+
* sum (b is a leaf below B) [d(B,b) + t] ^ 2;
|
|
11
|
+
*
|
|
12
|
+
*expanding, we have
|
|
13
|
+
*
|
|
14
|
+
*D = sum (a is a leaf above B) [d^2 (B,a) + 2(T-t) d(B,a) + (T-t)^2]
|
|
15
|
+
* sum (b is a leaf below B) [d^2 (B,b) + 2(t) d(B,b) + t^2]
|
|
16
|
+
*
|
|
17
|
+
* = (sum distances above)^2 + sum (distances above)*2(T-t) + (T-t)^2 * # leaves_above
|
|
18
|
+
* (sum distances below)^2 + sum (distances below)*2*t + t^2 * # leaves_below
|
|
19
|
+
*
|
|
20
|
+
*
|
|
21
|
+
*Taking a derivative with respect to t we have
|
|
22
|
+
*
|
|
23
|
+
*dD / dt = - 2 sum (a is a leaf above B) [d(B,a) + (T-t)]
|
|
24
|
+
* + 2 sum (b is a leaf below B) [d(B,b) + t]
|
|
25
|
+
*
|
|
26
|
+
*
|
|
27
|
+
*second derivative is positive the function is convex, so can set to the nearest boundary if solution is outside range
|
|
28
|
+
*
|
|
29
|
+
*setting dD / dt to zero, we have
|
|
30
|
+
*
|
|
31
|
+
*- sum (distances leaves above) - T * (#leaves above) + t * (#leaves above) + sum (distances leaves below) + t * (#leaves below) = 0
|
|
32
|
+
*
|
|
33
|
+
*t = [sum (distances leaves above) - sum (distances leaves below) + T * (#leaves above)] / (#leaves)
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
import { default as pairwise_distances } from "./pairwise-distances";
|
|
37
|
+
|
|
38
|
+
export function centerOfTree(tree, power) {
|
|
39
|
+
power = power || 2;
|
|
40
|
+
|
|
41
|
+
var leaf_count = pairwise_distances(tree);
|
|
42
|
+
|
|
43
|
+
var current_min = Number.MAX_VALUE,
|
|
44
|
+
current_split = 0,
|
|
45
|
+
current_location = null;
|
|
46
|
+
|
|
47
|
+
if (power == 2) {
|
|
48
|
+
tree.traverse_and_compute(function(n) {
|
|
49
|
+
if (n.parent) {
|
|
50
|
+
// can't consider the root
|
|
51
|
+
var sum_below = 0,
|
|
52
|
+
sum_below_squared = 0,
|
|
53
|
+
sum_above = 0,
|
|
54
|
+
sum_above_squared = 0;
|
|
55
|
+
|
|
56
|
+
var count_below = 0;
|
|
57
|
+
|
|
58
|
+
_.each(n.cot_path_to_leaves_below, function(l) {
|
|
59
|
+
sum_below += l;
|
|
60
|
+
sum_below_squared += l * l;
|
|
61
|
+
count_below++;
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
_.each(n.cot_path_to_leaves_above, function(l) {
|
|
65
|
+
sum_above += l;
|
|
66
|
+
sum_above_squared += l * l;
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
var count_above = leaf_count - count_below;
|
|
70
|
+
|
|
71
|
+
var tt =
|
|
72
|
+
(sum_above - sum_below + n.cot_computed_length * count_above) /
|
|
73
|
+
leaf_count;
|
|
74
|
+
if (tt < 0) {
|
|
75
|
+
tt = 0;
|
|
76
|
+
} else if (tt > n.cot_computed_length) {
|
|
77
|
+
tt = n.cot_computed_length;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
var score =
|
|
81
|
+
sum_above_squared +
|
|
82
|
+
sum_below_squared +
|
|
83
|
+
2 * (sum_above * (n.cot_computed_length - tt) + sum_below * tt) +
|
|
84
|
+
count_below * tt * tt +
|
|
85
|
+
(n.cot_computed_length - tt) *
|
|
86
|
+
(n.cot_computed_length - tt) *
|
|
87
|
+
count_above;
|
|
88
|
+
|
|
89
|
+
if (score < current_min) {
|
|
90
|
+
current_location = n;
|
|
91
|
+
current_split = tt / n.cot_computed_length; //n.cot_computed_length-tt;//1 - tt / n.cot_computed_length;
|
|
92
|
+
current_min = score;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
delete n.cot_computed_length;
|
|
96
|
+
delete n.cot_path_to_leaves_below;
|
|
97
|
+
delete n.cot_path_to_leaves_above;
|
|
98
|
+
delete n.cot_leaf_index;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
} else {
|
|
102
|
+
// in the general case try a simple grid optimization
|
|
103
|
+
tree.traverse_and_compute(function(n) {
|
|
104
|
+
if (n.parent) {
|
|
105
|
+
// can't consider the root
|
|
106
|
+
|
|
107
|
+
var optimization_step =
|
|
108
|
+
n.cot_computed_length > 0.0 ? n.cot_computed_length * 0.05 : 0.1,
|
|
109
|
+
current_t = 0;
|
|
110
|
+
|
|
111
|
+
while (current_t < n.cot_computed_length) {
|
|
112
|
+
var score = 0.0;
|
|
113
|
+
|
|
114
|
+
_.each(n.cot_path_to_leaves_below, function(l) {
|
|
115
|
+
score += Math.pow(l + current_t, power);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
_.each(n.cot_path_to_leaves_above, function(l) {
|
|
119
|
+
score += Math.pow(l + (n.cot_computed_length - current_t), power);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
if (score < current_min) {
|
|
123
|
+
current_location = n;
|
|
124
|
+
current_split = current_t / n.cot_computed_length; //n.cot_computed_length-tt;//1 - tt / n.cot_computed_length;
|
|
125
|
+
current_min = score;
|
|
126
|
+
}
|
|
127
|
+
current_t += optimization_step;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
location: current_location,
|
|
135
|
+
breakpoint: current_split,
|
|
136
|
+
distance: current_min
|
|
137
|
+
};
|
|
138
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compute midpoint of a phylogenetic tree
|
|
3
|
+
*
|
|
4
|
+
* @param {Object} tree -- the phylotree.js tree object
|
|
5
|
+
* @return {Object} the calculated midpoint to root on
|
|
6
|
+
* { location: root_node, breakpoint: 0 }
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
export function computeMidpoint(tree) {
|
|
10
|
+
if (!tree.hasBranchLengths()) {
|
|
11
|
+
throw "Center of tree calculation cannot be performed on trees that do not have fully specified branch lengths";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
var bl = tree.branch_length;
|
|
15
|
+
|
|
16
|
+
tree.traverse_and_compute(function(node) {
|
|
17
|
+
if (node.parent) {
|
|
18
|
+
var my_longest_path_length = bl(node);
|
|
19
|
+
var my_longest_path_terminus;
|
|
20
|
+
|
|
21
|
+
if (tree.isLeafNode(node)) {
|
|
22
|
+
my_longest_path_terminus = node;
|
|
23
|
+
node.max_path = 0;
|
|
24
|
+
node.max_path_terminus = node;
|
|
25
|
+
} else {
|
|
26
|
+
my_longest_path_length += node.max_path;
|
|
27
|
+
my_longest_path_terminus = node.max_path_terminus;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (
|
|
31
|
+
!node.parent.max_path ||
|
|
32
|
+
node.parent.max_path < my_longest_path_length
|
|
33
|
+
) {
|
|
34
|
+
node.parent.max_path = my_longest_path_length;
|
|
35
|
+
node.parent.max_path_terminus = my_longest_path_terminus;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
var root_node = tree.getRootNode();
|
|
41
|
+
var longest_path_length = 0;
|
|
42
|
+
|
|
43
|
+
root_node.children.forEach(function(root_child) {
|
|
44
|
+
if (root_child.max_path_terminus !== root_node.max_path_terminus) {
|
|
45
|
+
var pl = root_child.max_path + bl(root_child);
|
|
46
|
+
if (pl >= longest_path_length) {
|
|
47
|
+
longest_path_length = pl;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
if (root_node.max_path > longest_path_length) {
|
|
53
|
+
// not already midpoint rooted
|
|
54
|
+
longest_path_length = (longest_path_length + root_node.max_path) * 0.5;
|
|
55
|
+
|
|
56
|
+
// start traversing up from the deepest leaf to the root, until we find the
|
|
57
|
+
// half-way point
|
|
58
|
+
|
|
59
|
+
var root_on = root_node.max_path_terminus;
|
|
60
|
+
|
|
61
|
+
while (true) {
|
|
62
|
+
var current_bl = bl(root_on);
|
|
63
|
+
//console.log (current_bl, longest_path_length);
|
|
64
|
+
if (current_bl <= longest_path_length) {
|
|
65
|
+
longest_path_length -= current_bl;
|
|
66
|
+
root_on = root_on.parent;
|
|
67
|
+
} else {
|
|
68
|
+
//console.log ("Rooting on ", root_on, longest_path_length[0], current_bl);
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
location: root_on,
|
|
72
|
+
breakpoint: longest_path_length / current_bl
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
//console.log ("Longest " + root_path (tree.getNodeByName(root_node.max_path_terminus.name)));
|
|
76
|
+
//console.log ("Second longest " + root_path (tree.getNodeByName(longest_path_length[1].name)));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return { location: root_node, breakpoint: 0 };
|
|
81
|
+
}
|