phylotree 1.2.0 → 1.2.1
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/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
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import * as _ from "underscore";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* given a tree, this function will compute quantities required to work with
|
|
5
|
+
* all v all pairwise distances (like in COT)
|
|
6
|
+
*
|
|
7
|
+
* @param tree the tree object
|
|
8
|
+
* @returns leaf count
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
function computePairwiseDistances(tree) {
|
|
12
|
+
/*
|
|
13
|
+
* traverse the tree and populate the following values in each node
|
|
14
|
+
*
|
|
15
|
+
* .cot_computed_length -> for each node (except the root), the current branch length
|
|
16
|
+
* (so as to not compute them each time via a callback)
|
|
17
|
+
* .cot_leaf_index -> post_order traversal order of a leaf (0 to number of leaves - 1)
|
|
18
|
+
*
|
|
19
|
+
* for each node
|
|
20
|
+
*
|
|
21
|
+
* .cot_path_to_leaves_below
|
|
22
|
+
* -> a dictionary that maps a leaf index to the total path length from this node
|
|
23
|
+
* to each of the descendant leaves, EXCLUDING the length of this branch
|
|
24
|
+
*
|
|
25
|
+
* .cot_path_to_leaves_above
|
|
26
|
+
* -> a dictionary that maps a leaf index to the total path length from this node
|
|
27
|
+
* to each of the leaves outside the split defined by this node, EXCLUDING
|
|
28
|
+
* the length of this branch
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
var bl = tree.branch_length_accessor;
|
|
32
|
+
|
|
33
|
+
if (!bl) {
|
|
34
|
+
throw "A non-null branch lengths accessor function is required for this operation";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
var leaf_count = 0;
|
|
38
|
+
|
|
39
|
+
tree.traverse_and_compute(function(n) {
|
|
40
|
+
n.cot_computed_length = bl(n);
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
if (n.parent && _.isUndefined(n.cot_computed_length)) {
|
|
44
|
+
throw "Non-null branch lengths are required for this operation: missing branch length at node " + n.data.name;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (tree.isLeafNode(n)) {
|
|
48
|
+
n.cot_leaf_index = leaf_count++;
|
|
49
|
+
n.cot_path_to_leaves_below = {};
|
|
50
|
+
n.cot_path_to_leaves_below[n.cot_leaf_index] = 0;
|
|
51
|
+
n.cot_path_to_leaves_above = {};
|
|
52
|
+
} else {
|
|
53
|
+
n.cot_path_to_leaves_below = {};
|
|
54
|
+
n.cot_path_to_leaves_above = {};
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// populate all cot_path_to_leaves_below
|
|
59
|
+
tree.traverse_and_compute(function(n) {
|
|
60
|
+
if (n.parent) {
|
|
61
|
+
_.each(n.cot_path_to_leaves_below, function(length_so_far, leaf_index) {
|
|
62
|
+
n.parent.cot_path_to_leaves_below[leaf_index] =
|
|
63
|
+
length_so_far + n.cot_computed_length;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// populate all cot_path_to_leaves_above; this is done via a 'pre-order' traversal
|
|
69
|
+
// handle root node first
|
|
70
|
+
var root_node = tree.getRootNode();
|
|
71
|
+
|
|
72
|
+
function CopyFromSiblings(a_node) {
|
|
73
|
+
for (var this_node = 0; this_node < a_node.children.length; this_node++) {
|
|
74
|
+
for (
|
|
75
|
+
var other_node = 0;
|
|
76
|
+
other_node < a_node.children.length;
|
|
77
|
+
other_node++
|
|
78
|
+
) {
|
|
79
|
+
if (this_node != other_node) {
|
|
80
|
+
_.each(a_node.children[other_node].cot_path_to_leaves_below, function(
|
|
81
|
+
length,
|
|
82
|
+
index
|
|
83
|
+
) {
|
|
84
|
+
if (a_node.children[this_node].cot_path_to_leaves_above) {
|
|
85
|
+
a_node.children[this_node].cot_path_to_leaves_above[index] =
|
|
86
|
+
length + a_node.children[other_node].cot_computed_length;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
CopyFromSiblings(root_node);
|
|
95
|
+
|
|
96
|
+
// takes two passes
|
|
97
|
+
|
|
98
|
+
tree.traverse_and_compute(function(n) {
|
|
99
|
+
if (n.parent) {
|
|
100
|
+
// copy parent's 'above' nodes
|
|
101
|
+
_.each(n.parent.cot_path_to_leaves_above, function(
|
|
102
|
+
length_so_far,
|
|
103
|
+
leaf_index
|
|
104
|
+
) {
|
|
105
|
+
n.cot_path_to_leaves_above[leaf_index] =
|
|
106
|
+
length_so_far + n.parent.cot_computed_length;
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
if (!tree.isLeafNode(n)) {
|
|
110
|
+
CopyFromSiblings(n);
|
|
111
|
+
}
|
|
112
|
+
// copy sibling's 'below' nodes
|
|
113
|
+
}
|
|
114
|
+
}, "pre-order");
|
|
115
|
+
|
|
116
|
+
return leaf_count;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export default computePairwiseDistances;
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import * as _ from "underscore";
|
|
2
|
+
|
|
3
|
+
function annotateCopyNumber(tree) {
|
|
4
|
+
tree.traverse_and_compute(function(node) {
|
|
5
|
+
if (tree.isLeafNode(node)) {
|
|
6
|
+
node.data.copy_number = 1;
|
|
7
|
+
}
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// internal function used by best root fitting
|
|
12
|
+
function computeRootToTipOtherRoot(
|
|
13
|
+
tree,
|
|
14
|
+
node,
|
|
15
|
+
coming_from,
|
|
16
|
+
shared_distance,
|
|
17
|
+
distance_to_new_root
|
|
18
|
+
) {
|
|
19
|
+
|
|
20
|
+
var my_bl = tree.branch_length(node);
|
|
21
|
+
|
|
22
|
+
var go_up = false;
|
|
23
|
+
|
|
24
|
+
if (!coming_from) {
|
|
25
|
+
shared_distance = node.data.rootToTip;
|
|
26
|
+
distance_to_new_root = 0;
|
|
27
|
+
go_up = true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (node.children) {
|
|
31
|
+
for (var c = 0; c < node.children.length; c++) {
|
|
32
|
+
if (node.children[c] != coming_from) {
|
|
33
|
+
computeRootToTipOtherRoot(
|
|
34
|
+
tree,
|
|
35
|
+
node.children[c],
|
|
36
|
+
node,
|
|
37
|
+
shared_distance,
|
|
38
|
+
distance_to_new_root
|
|
39
|
+
);
|
|
40
|
+
} else {
|
|
41
|
+
go_up = true;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
node.data.rtta = node.data.rootToTip - shared_distance + distance_to_new_root;
|
|
47
|
+
|
|
48
|
+
if (go_up) {
|
|
49
|
+
shared_distance -= my_bl;
|
|
50
|
+
distance_to_new_root += my_bl;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (node.parent && go_up) {
|
|
54
|
+
computeRootToTipOtherRoot(
|
|
55
|
+
tree,
|
|
56
|
+
node.parent,
|
|
57
|
+
node,
|
|
58
|
+
shared_distance,
|
|
59
|
+
distance_to_new_root
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function fitRootToTip(tree) {
|
|
65
|
+
|
|
66
|
+
var linear_data = [],
|
|
67
|
+
max_r2 = 0,
|
|
68
|
+
best_node = 0;
|
|
69
|
+
|
|
70
|
+
annotateCopyNumber(tree);
|
|
71
|
+
rootToTip(tree);
|
|
72
|
+
|
|
73
|
+
// To return if best node is the root already
|
|
74
|
+
tree.traverse_and_compute(function(node) {
|
|
75
|
+
if (tree.isLeafNode(node) && !_.isNull(node.data.decimal_date_value)) {
|
|
76
|
+
linear_data.push([node.data.decimal_date_value, node.data.rtta, node.data.copy_number]);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
let best_fit = linearFit(linear_data);
|
|
81
|
+
|
|
82
|
+
tree.traverse_and_compute(function(node) {
|
|
83
|
+
|
|
84
|
+
if (tree.isLeafNode(node) && !_.isNull(node.data.decimal_date_value)) {
|
|
85
|
+
|
|
86
|
+
computeRootToTipOtherRoot(tree, node, null, 0, 0);
|
|
87
|
+
|
|
88
|
+
linear_data = [];
|
|
89
|
+
|
|
90
|
+
tree.traverse_and_compute(function(node) {
|
|
91
|
+
if (tree.isLeafNode(node) && !_.isNull(node.data.decimal_date_value)) {
|
|
92
|
+
linear_data.push([
|
|
93
|
+
node.data.decimal_date_value,
|
|
94
|
+
node.data.rtta,
|
|
95
|
+
node.data.copy_number
|
|
96
|
+
]);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
var fit = linearFit(linear_data),
|
|
101
|
+
r2 = fit["r2"];
|
|
102
|
+
|
|
103
|
+
if (r2 > max_r2) {
|
|
104
|
+
max_r2 = r2;
|
|
105
|
+
best_node = node;
|
|
106
|
+
best_fit = fit;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
return { root: best_node, fit: best_fit };
|
|
113
|
+
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// linear fit of root to tip distances
|
|
117
|
+
function linearFit(data) {
|
|
118
|
+
|
|
119
|
+
var ss = data.reduce(function(p, c) {
|
|
120
|
+
return c[2] + p;
|
|
121
|
+
}, 0), // sample count
|
|
122
|
+
sx = data.reduce(function(p, c) {
|
|
123
|
+
return c[2] * c[0] + p;
|
|
124
|
+
}, 0), // sum X
|
|
125
|
+
sy = data.reduce(function(p, c) {
|
|
126
|
+
return c[2] * c[1] + p;
|
|
127
|
+
}, 0), // sum Y
|
|
128
|
+
sxoss = sx / ss,
|
|
129
|
+
syoss = sy / ss;
|
|
130
|
+
|
|
131
|
+
var fitB = 0,
|
|
132
|
+
st2 = 0,
|
|
133
|
+
vary = 0;
|
|
134
|
+
|
|
135
|
+
data.forEach(function(point) {
|
|
136
|
+
var t = point[0] - sxoss;
|
|
137
|
+
st2 += point[2] * t * t;
|
|
138
|
+
fitB += point[2] * t * point[1];
|
|
139
|
+
vary += point[2] * (point[1] - syoss) * (point[1] - syoss);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
fitB /= st2;
|
|
143
|
+
|
|
144
|
+
var a = (sy - sx * fitB) / ss;
|
|
145
|
+
|
|
146
|
+
var varres = 0;
|
|
147
|
+
|
|
148
|
+
data.forEach(function(point) {
|
|
149
|
+
var t = point[1] - a - fitB * point[0];
|
|
150
|
+
varres += point[2] * t * t;
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
intercept: a,
|
|
155
|
+
slope: fitB,
|
|
156
|
+
r2: 1 - varres / vary,
|
|
157
|
+
"var (intercept)": Math.sqrt((1 + sx * sx / (ss * st2)) / ss),
|
|
158
|
+
"var (slope)": Math.sqrt(1 / st2)
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* fast and memory efficient root to tip distance calculator
|
|
164
|
+
* for each leaf node stores the current root to tip distance in
|
|
165
|
+
* the node.rootToTip field
|
|
166
|
+
*
|
|
167
|
+
* @param tree
|
|
168
|
+
* @return tree with rootToTip computed
|
|
169
|
+
*
|
|
170
|
+
*/
|
|
171
|
+
export default function rootToTip(tree) {
|
|
172
|
+
|
|
173
|
+
var bl = tree.branch_length_accessor,
|
|
174
|
+
index = 0;
|
|
175
|
+
|
|
176
|
+
tree.traverse_and_compute(n => {
|
|
177
|
+
if (n.parent) {
|
|
178
|
+
n.data._computed_length = bl(n);
|
|
179
|
+
if (!_.isNumber(n.data._computed_length)) {
|
|
180
|
+
throw "rootToTip cannot be run on trees with missing branch lengths";
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (tree.isLeafNode(n)) {
|
|
184
|
+
n.data.leaf_index = index++;
|
|
185
|
+
}
|
|
186
|
+
if ("r2t" in n.data) {
|
|
187
|
+
delete n.data.r2t;
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
tree.traverse_and_compute(n => {
|
|
192
|
+
if (n.parent) {
|
|
193
|
+
if (!("r2t" in n.parent.data)) {
|
|
194
|
+
n.parent.data.r2t = {};
|
|
195
|
+
}
|
|
196
|
+
if (tree.isLeafNode(n)) {
|
|
197
|
+
n.parent.data.r2t[n.data.leaf_index] = n.data._computed_length;
|
|
198
|
+
} else {
|
|
199
|
+
_.each(n.data.r2t, function(v, idx) {
|
|
200
|
+
n.parent.data.r2t[idx] = v + n.data._computed_length;
|
|
201
|
+
});
|
|
202
|
+
delete n.data.r2t;
|
|
203
|
+
}
|
|
204
|
+
delete n.data._computed_length;
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
var r2t = tree.getRootNode().data.r2t;
|
|
209
|
+
|
|
210
|
+
tree.traverse_and_compute(n => {
|
|
211
|
+
if (tree.isLeafNode(n)) {
|
|
212
|
+
n.data.rootToTip = r2t[n.data.leaf_index] || 0;
|
|
213
|
+
delete n.data.leaf_index;
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
delete tree.getRootNode().data.r2t;
|
|
218
|
+
|
|
219
|
+
return tree;
|
|
220
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as _ from "underscore";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* The Sackin's index is computed as the sum of the number of ancestors for each
|
|
5
|
+
* tips of the tree.
|
|
6
|
+
*
|
|
7
|
+
* The less balanced a tree is, the larger its Sackin's index.
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export default function sackin(tree) {
|
|
12
|
+
|
|
13
|
+
// Get tips of tree
|
|
14
|
+
let tips = tree.getTips();
|
|
15
|
+
|
|
16
|
+
// Count number of ancestors to root for each tree
|
|
17
|
+
let depths = _.map(tips, d => { return d.depth });
|
|
18
|
+
|
|
19
|
+
return _.reduce(depths, function(memo, num){ return memo + num; }, 0);
|
|
20
|
+
|
|
21
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as _ from "lodash";
|
|
2
|
+
|
|
3
|
+
export default function parseFasta(fastaData) {
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
let sfasta = _.split(fastaData, "\n");
|
|
7
|
+
|
|
8
|
+
let seqs = _.chain(sfasta)
|
|
9
|
+
.map((d, i) => (d.startsWith(">") ? i : -1))
|
|
10
|
+
.filter((d) => d != -1)
|
|
11
|
+
.map((d, i, c) => _.slice(sfasta, c[i], c[i + 1]))
|
|
12
|
+
.keyBy((d) => _.trim(d[0], ">"))
|
|
13
|
+
.mapValues((d) => _.tail(d).join(""))
|
|
14
|
+
.value();
|
|
15
|
+
|
|
16
|
+
return seqs;
|
|
17
|
+
|
|
18
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import * as _ from "lodash";
|
|
2
|
+
import { default as phylotree } from "./main";
|
|
3
|
+
|
|
4
|
+
function remove(i, D) {
|
|
5
|
+
let dNew = [];
|
|
6
|
+
|
|
7
|
+
for (let j of _.range(D.length)) {
|
|
8
|
+
if (j != i) {
|
|
9
|
+
let dNewRow = [];
|
|
10
|
+
for (let k of _.range(D[j].length)) {
|
|
11
|
+
if (k != i) {
|
|
12
|
+
dNewRow.push(D[j][k]);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
dNew.push(dNewRow);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return dNew;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getDPrime(distanceMatrix, totalDistances, N) {
|
|
23
|
+
let DPrime = _.chunk(_.fill(Array(N * N), 0), N);
|
|
24
|
+
for (let i of _.range(N)) {
|
|
25
|
+
for (let j of _.range(_.parseInt(i) + 1, N)) {
|
|
26
|
+
DPrime[i][j] =
|
|
27
|
+
(N - 2) * distanceMatrix[i][j] - totalDistances[i] - totalDistances[j];
|
|
28
|
+
DPrime[j][i] = DPrime[i][j];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return DPrime;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function ijMinDPrime(dPrime, N) {
|
|
35
|
+
let i = -1;
|
|
36
|
+
let j = -1;
|
|
37
|
+
let minD = Infinity;
|
|
38
|
+
for (let ii of _.range(N)) {
|
|
39
|
+
for (let jj of _.range(i, N)) {
|
|
40
|
+
if (dPrime[ii][jj] < minD) {
|
|
41
|
+
i = ii;
|
|
42
|
+
j = jj;
|
|
43
|
+
minD = dPrime[i][j];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return [i, j, minD];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function createDelta(totalDistances, N) {
|
|
51
|
+
let deltaMatrix = _.chunk(Array(N * N), N);
|
|
52
|
+
|
|
53
|
+
for (let i of _.range(N)) {
|
|
54
|
+
for (let j of _.range(parseInt(i) + 1, N)) {
|
|
55
|
+
deltaMatrix[i][j] = (totalDistances[i] - totalDistances[j]) / (N - 2);
|
|
56
|
+
deltaMatrix[j][i] = deltaMatrix[i][j];
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return deltaMatrix;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function getDistanceMatrix(seqs) {
|
|
64
|
+
// Gaps are masked
|
|
65
|
+
|
|
66
|
+
let initKey = _.keys(seqs)[0];
|
|
67
|
+
let seqLength = seqs[initKey].length;
|
|
68
|
+
|
|
69
|
+
return _.mapValues(seqs, (seq) =>
|
|
70
|
+
_.map(seqs, (seq2) =>
|
|
71
|
+
_.sum(
|
|
72
|
+
_.map(
|
|
73
|
+
_.range(seqLength),
|
|
74
|
+
(i) => seq[i] != seq2[i] && seq[i] != "-" && seq2[i] != "-"
|
|
75
|
+
)
|
|
76
|
+
)
|
|
77
|
+
)
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function getTotalDistances(distanceMatrix) {
|
|
82
|
+
return _.map(distanceMatrix, _.sum);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Create a neighbor joining tree from a distance matrix
|
|
87
|
+
* See test/neighbor-join-test.js for a working example
|
|
88
|
+
*
|
|
89
|
+
* @param {Array} distanceMatrixArr The NxN distance matrix.
|
|
90
|
+
* const D = [
|
|
91
|
+
* [0, 5, 9, 9, 8],
|
|
92
|
+
* [5, 0, 10, 10, 9],
|
|
93
|
+
* [9, 10, 0, 8, 7],
|
|
94
|
+
* [9, 10, 8, 0, 3],
|
|
95
|
+
* [8, 9, 7, 3, 0]
|
|
96
|
+
* ];
|
|
97
|
+
*
|
|
98
|
+
* @param {Number} n The dimension of the distanceMatrixArr.
|
|
99
|
+
* @param {Array} nodeList The names of each row in the distanceMatrix
|
|
100
|
+
* @returns The neighbor joining new tree.
|
|
101
|
+
*/
|
|
102
|
+
export default function neighborJoining(distanceMatrixArr, n, nodeList) {
|
|
103
|
+
if (n <= 2) {
|
|
104
|
+
let tree = new phylotree("");
|
|
105
|
+
|
|
106
|
+
//T.link(nodeList[0],nodeList[1],D[0][1])
|
|
107
|
+
|
|
108
|
+
let newNode = tree.getNodes();
|
|
109
|
+
// Get root
|
|
110
|
+
let distance = distanceMatrixArr[0][1] / 2;
|
|
111
|
+
|
|
112
|
+
let nodeA = tree.createNode(nodeList[0], [null, [distance]]);
|
|
113
|
+
let nodeB = tree.createNode(nodeList[1], [null, [distance]]);
|
|
114
|
+
|
|
115
|
+
//// Add the children to the newly created node
|
|
116
|
+
tree.addChild(newNode, nodeA);
|
|
117
|
+
tree.addChild(newNode, nodeB);
|
|
118
|
+
|
|
119
|
+
return tree;
|
|
120
|
+
} else {
|
|
121
|
+
let N = n;
|
|
122
|
+
let totalDistances = getTotalDistances(distanceMatrixArr);
|
|
123
|
+
let dPrime = getDPrime(distanceMatrixArr, totalDistances, N);
|
|
124
|
+
let [i, j, minD] = ijMinDPrime(dPrime, N);
|
|
125
|
+
let deltaMatrix = createDelta(totalDistances, N);
|
|
126
|
+
let limbLengthI = (distanceMatrixArr[i][j] + deltaMatrix[i][j]) / 2;
|
|
127
|
+
let limbLengthJ = (distanceMatrixArr[i][j] - deltaMatrix[i][j]) / 2;
|
|
128
|
+
let newRow = _.concat(
|
|
129
|
+
0,
|
|
130
|
+
_.filter(
|
|
131
|
+
_.map(_.range(n), (k) => {
|
|
132
|
+
if (k != i && k != j) {
|
|
133
|
+
return (
|
|
134
|
+
0.5 *
|
|
135
|
+
(distanceMatrixArr[k][i] +
|
|
136
|
+
distanceMatrixArr[k][j] -
|
|
137
|
+
distanceMatrixArr[i][j])
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
}),
|
|
141
|
+
_.isNumber
|
|
142
|
+
)
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
let nodeI = nodeList[i];
|
|
146
|
+
let nodeJ = nodeList[j];
|
|
147
|
+
|
|
148
|
+
// Get all nodes of type InternalNode{x} and increment number.
|
|
149
|
+
// If there are none, start with InternalNode0
|
|
150
|
+
let m = "InternalNode0";
|
|
151
|
+
let internalNodes = _.filter(nodeList, (x) =>
|
|
152
|
+
_.includes(x, "InternalNode")
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
if (internalNodes.length) {
|
|
156
|
+
let highestNum = _.max(
|
|
157
|
+
_.map(internalNodes, (label) => _.split(label, "InternalNode")[1])
|
|
158
|
+
);
|
|
159
|
+
m = "InternalNode" + ++highestNum;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
nodeList.unshift(m);
|
|
163
|
+
|
|
164
|
+
distanceMatrixArr = remove(_.max([i, j]), distanceMatrixArr);
|
|
165
|
+
distanceMatrixArr = remove(_.min([i, j]), distanceMatrixArr);
|
|
166
|
+
|
|
167
|
+
distanceMatrixArr.unshift(newRow);
|
|
168
|
+
|
|
169
|
+
_.each(_.range(1, n - 1), (l) => distanceMatrixArr[l].unshift(newRow[l]));
|
|
170
|
+
|
|
171
|
+
// Remove from nodeList
|
|
172
|
+
_.remove(nodeList, (n) => n == nodeI || n == nodeJ);
|
|
173
|
+
let tree = neighborJoining(distanceMatrixArr, N - 1, nodeList);
|
|
174
|
+
|
|
175
|
+
let treeNodeI = tree.createNode(nodeI, [null, [limbLengthI]]);
|
|
176
|
+
let treeNodeJ = tree.createNode(nodeJ, [null, [limbLengthJ]]);
|
|
177
|
+
|
|
178
|
+
// If the node doesn't exist, create. Otherwise, reassign the length
|
|
179
|
+
if (tree.getNodeByName(m)) {
|
|
180
|
+
let internalNode = tree.getNodeByName(m);
|
|
181
|
+
tree.addChild(internalNode, treeNodeI);
|
|
182
|
+
tree.addChild(internalNode, treeNodeJ);
|
|
183
|
+
} else {
|
|
184
|
+
let newNode = tree.createNode(m, [null, [0]]);
|
|
185
|
+
tree.addChild(tree.getNodes(), newNode);
|
|
186
|
+
// Add the children to the newly created node
|
|
187
|
+
tree.addChild(newNode, treeNodeI);
|
|
188
|
+
tree.addChild(newNode, treeNodeJ);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Set negative to 0 and add distance to other limblength
|
|
192
|
+
return tree;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return null;
|
|
196
|
+
}
|