phylotree 1.0.17 → 1.0.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/phylotree.js +2 -2
- package/dist/phylotree.min.js +1 -1
- package/package.json +3 -2
- package/src/branches.js +156 -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 +68 -0
- package/src/formats/beast.js +29 -0
- package/src/formats/newick.js +271 -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 +22 -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/nodes.js +215 -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,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
|
+
}
|
|
@@ -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
|
+
}
|