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/rooting.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import * as d3 from "d3";
|
|
2
|
+
import * as _ from "underscore";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Reroot the tree on the given node.
|
|
6
|
+
*
|
|
7
|
+
* @param {Node} node Node to reroot on.
|
|
8
|
+
* @param {fraction} if specified, partition the branch not into 0.5 : 0.5, but according to
|
|
9
|
+
the specified fraction
|
|
10
|
+
|
|
11
|
+
* @returns {Phylotree} The current ``phylotree``.
|
|
12
|
+
*/
|
|
13
|
+
export function reroot(node, fraction) {
|
|
14
|
+
|
|
15
|
+
/** TODO add the option to root in the middle of a branch */
|
|
16
|
+
if(!(node instanceof d3.hierarchy)) {
|
|
17
|
+
throw new Error('node needs to be an instance of a d3.hierarchy node!');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let nodes = this.nodes.copy();
|
|
21
|
+
|
|
22
|
+
fraction = fraction !== undefined ? fraction : 0.5;
|
|
23
|
+
|
|
24
|
+
if (node.parent) {
|
|
25
|
+
|
|
26
|
+
var new_json = d3.hierarchy({
|
|
27
|
+
name: "new_root"
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
new_json.children = [node.copy()];
|
|
31
|
+
new_json.data.__mapped_bl = undefined
|
|
32
|
+
|
|
33
|
+
nodes.each(n => {
|
|
34
|
+
n.data.__mapped_bl = this.branch_length_accessor(n);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
this.setBranchLength(n => {
|
|
38
|
+
return n.data.__mapped_bl;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
let remove_me = node,
|
|
42
|
+
current_node = node.parent,
|
|
43
|
+
stashed_bl = _.noop();
|
|
44
|
+
|
|
45
|
+
let apportioned_bl =
|
|
46
|
+
node.data.__mapped_bl === undefined ? undefined : node.data.__mapped_bl * fraction;
|
|
47
|
+
|
|
48
|
+
stashed_bl = current_node.data.__mapped_bl;
|
|
49
|
+
|
|
50
|
+
current_node.data.__mapped_bl =
|
|
51
|
+
node.data.__mapped_bl === undefined
|
|
52
|
+
? undefined
|
|
53
|
+
: node.data.__mapped_bl - apportioned_bl;
|
|
54
|
+
|
|
55
|
+
node.data.__mapped_bl = apportioned_bl;
|
|
56
|
+
|
|
57
|
+
var remove_idx;
|
|
58
|
+
|
|
59
|
+
if (current_node.parent) {
|
|
60
|
+
|
|
61
|
+
new_json.children.push(current_node);
|
|
62
|
+
|
|
63
|
+
while (current_node.parent) {
|
|
64
|
+
|
|
65
|
+
remove_idx = current_node.children.indexOf(remove_me);
|
|
66
|
+
|
|
67
|
+
if (current_node.parent.parent) {
|
|
68
|
+
current_node.children.splice(remove_idx, 1, current_node.parent);
|
|
69
|
+
} else {
|
|
70
|
+
current_node.children.splice(remove_idx, 1);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let t = current_node.parent.data.__mapped_bl;
|
|
74
|
+
|
|
75
|
+
if (t !== undefined) {
|
|
76
|
+
current_node.parent.data.__mapped_bl = stashed_bl;
|
|
77
|
+
stashed_bl = t;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
remove_me = current_node;
|
|
81
|
+
current_node = current_node.parent;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
remove_idx = current_node.children.indexOf(remove_me);
|
|
85
|
+
current_node.children.splice(remove_idx, 1);
|
|
86
|
+
|
|
87
|
+
} else {
|
|
88
|
+
|
|
89
|
+
remove_idx = current_node.children.indexOf(remove_me);
|
|
90
|
+
current_node.children.splice(remove_idx, 1);
|
|
91
|
+
stashed_bl = current_node.data.__mapped_bl;
|
|
92
|
+
remove_me = new_json;
|
|
93
|
+
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// current_node is now old root, and remove_me is the root child we came up
|
|
97
|
+
// the tree through
|
|
98
|
+
if (current_node.children.length == 1) {
|
|
99
|
+
|
|
100
|
+
if (stashed_bl) {
|
|
101
|
+
current_node.children[0].data.__mapped_bl += stashed_bl;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
remove_me.children = remove_me.children.concat(current_node.children);
|
|
105
|
+
|
|
106
|
+
} else {
|
|
107
|
+
|
|
108
|
+
let new_node = new d3.hierarchy({ name: "__reroot_top_clade", __mapped_bl: stashed_bl });
|
|
109
|
+
_.extendOwn (new_json.children[0], node);
|
|
110
|
+
new_node.data.__mapped_bl = stashed_bl;
|
|
111
|
+
new_node.children = current_node.children.map(function(n) {
|
|
112
|
+
n.parent = new_node;
|
|
113
|
+
return n;
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
new_node.parent = remove_me;
|
|
117
|
+
remove_me.children.push(new_node);
|
|
118
|
+
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// need to traverse the nodes and update parents
|
|
124
|
+
this.update(new_json);
|
|
125
|
+
|
|
126
|
+
this.traverse_and_compute(n => {
|
|
127
|
+
_.each (n.children, (c) => {c.parent = n;})
|
|
128
|
+
}, "pre-order");
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
if(!_.isUndefined(this.display)) {
|
|
132
|
+
|
|
133
|
+
// get options
|
|
134
|
+
let options = this.display.options;
|
|
135
|
+
// get container
|
|
136
|
+
d3.select(this.display.container).select('svg').remove()
|
|
137
|
+
|
|
138
|
+
// retain selection
|
|
139
|
+
let selectionName = this.display.selection_attribute_name
|
|
140
|
+
|
|
141
|
+
delete this.display;
|
|
142
|
+
|
|
143
|
+
let rendered_tree = this.render(options);
|
|
144
|
+
rendered_tree.selectionLabel(selectionName);
|
|
145
|
+
rendered_tree.update();
|
|
146
|
+
d3.select(rendered_tree.container).node().appendChild(rendered_tree.show());
|
|
147
|
+
d3.select(this.display.container).dispatch('reroot');
|
|
148
|
+
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return this;
|
|
152
|
+
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function rootpath(attr_name, store_name) {
|
|
156
|
+
|
|
157
|
+
attr_name = attr_name || "attribute";
|
|
158
|
+
store_name = store_name || "y_scaled";
|
|
159
|
+
|
|
160
|
+
if ("parent" in this) {
|
|
161
|
+
let my_value = parseFloat(this[attr_name]);
|
|
162
|
+
|
|
163
|
+
this[store_name] =
|
|
164
|
+
this.parent[store_name] + (isNaN(my_value) ? 0.1 : my_value);
|
|
165
|
+
|
|
166
|
+
} else {
|
|
167
|
+
|
|
168
|
+
this[store_name] = 0.0;
|
|
169
|
+
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return this[store_name];
|
|
173
|
+
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function pathToRoot(node) {
|
|
177
|
+
let selection = [];
|
|
178
|
+
while (node) {
|
|
179
|
+
selection.push(node);
|
|
180
|
+
node = node.parent;
|
|
181
|
+
}
|
|
182
|
+
return selection;
|
|
183
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
function handleRotate(only_self, this_node) {
|
|
2
|
+
// TODO: This might need tested
|
|
3
|
+
tree.resortChildren( // eslint-disable-line
|
|
4
|
+
function(node1, node2) {
|
|
5
|
+
return node2.ordering - node1.ordering;
|
|
6
|
+
},
|
|
7
|
+
this_node,
|
|
8
|
+
only_self
|
|
9
|
+
? function(node) {
|
|
10
|
+
return node == this_node;
|
|
11
|
+
}
|
|
12
|
+
: null
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
this_node.children.forEach(function(n, i) {
|
|
16
|
+
n.ordering = i;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default handleRotate;
|
package/src/traversal.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import * as _ from "lodash";
|
|
2
|
+
|
|
3
|
+
export function postOrder(node, callback, backtrack) {
|
|
4
|
+
|
|
5
|
+
let nodes = [node],
|
|
6
|
+
next = [],
|
|
7
|
+
children,
|
|
8
|
+
i,
|
|
9
|
+
n;
|
|
10
|
+
|
|
11
|
+
while ((node = nodes.pop())) {
|
|
12
|
+
if (!(backtrack && backtrack(node))) {
|
|
13
|
+
next.push(node), (children = node.children);
|
|
14
|
+
if (children)
|
|
15
|
+
for (i = 0, n = children.length; i < n; ++i) {
|
|
16
|
+
nodes.push(children[i]);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
while ((node = next.pop())) {
|
|
22
|
+
callback(node);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return node;
|
|
26
|
+
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function preOrder(node, callback, backtrack) {
|
|
30
|
+
let nodes = [node],
|
|
31
|
+
children,
|
|
32
|
+
i;
|
|
33
|
+
|
|
34
|
+
while ((node = nodes.pop())) {
|
|
35
|
+
if (!(backtrack && backtrack(node))) {
|
|
36
|
+
callback(node), (children = node.children);
|
|
37
|
+
if (children)
|
|
38
|
+
for (i = children.length - 1; i >= 0; --i) {
|
|
39
|
+
nodes.push(children[i]);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return node;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export default function inOrder(node, callback, backtrack) {
|
|
48
|
+
let current,
|
|
49
|
+
next = [node],
|
|
50
|
+
children,
|
|
51
|
+
i,
|
|
52
|
+
n;
|
|
53
|
+
|
|
54
|
+
do {
|
|
55
|
+
(current = next.reverse()), (next = []);
|
|
56
|
+
while ((node = current.pop())) {
|
|
57
|
+
if (!(backtrack && backtrack(node))) {
|
|
58
|
+
callback(node), (children = node.children);
|
|
59
|
+
if (children)
|
|
60
|
+
for (i = 0, n = children.length; i < n; ++i) {
|
|
61
|
+
next.push(children[i]);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
} while (next.length);
|
|
66
|
+
|
|
67
|
+
return node;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Traverses a tree that represents left-child right-sibling
|
|
72
|
+
* @param {Object} tree -- the phylotree.js tree object
|
|
73
|
+
* @return {Object} An edge list that represents the original multi-way tree
|
|
74
|
+
*
|
|
75
|
+
*/
|
|
76
|
+
export function leftChildRightSibling(root) {
|
|
77
|
+
|
|
78
|
+
let declareTrueParent = function(n) {
|
|
79
|
+
|
|
80
|
+
if(n.children) {
|
|
81
|
+
// left child is the child
|
|
82
|
+
n.children[0].data.multiway_parent = n;
|
|
83
|
+
|
|
84
|
+
// right child is the sibling
|
|
85
|
+
n.children[1].data.multiway_parent = n.parent;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// First decorate each node with its true parent node
|
|
91
|
+
postOrder(root, declareTrueParent);
|
|
92
|
+
|
|
93
|
+
// return edge list
|
|
94
|
+
let edge_list = _.map(root.descendants(), n => {
|
|
95
|
+
|
|
96
|
+
let source = n.data.multiway_parent;
|
|
97
|
+
let name = "unknown";
|
|
98
|
+
|
|
99
|
+
if(source) {
|
|
100
|
+
name = source.data.name;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// In order to get the true name of the infector/infectee, we must traverse
|
|
104
|
+
// the tree from the multiway_parents node.
|
|
105
|
+
|
|
106
|
+
return {"source" : n.data.multiway_parent, "target" : n, "name": name } });
|
|
107
|
+
|
|
108
|
+
// Construct edge list by new parent-child listing
|
|
109
|
+
return edge_list;
|
|
110
|
+
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|