phylotree 1.0.17 → 1.0.18

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phylotree",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
4
4
  "main": "dist/phylotree.js",
5
5
  "unpkg": "dist/phylotree.js",
6
6
  "jsdelivr": "dist/phylotree.js",
@@ -59,6 +59,7 @@
59
59
  "dist/phylotree.css",
60
60
  "dist/phylotree.js",
61
61
  "dist/phylotree.min.js",
62
- "bin"
62
+ "bin",
63
+ "src"
63
64
  ]
64
65
  }
@@ -0,0 +1,156 @@
1
+ import * as _ from "underscore";
2
+
3
+ // These methods are part of the Phylotree object
4
+
5
+ export function setPartitions(partitions) {
6
+ this.partitions = partitions;
7
+ }
8
+
9
+ export function getPartitions(attributes) {
10
+ return this.partitions;
11
+ }
12
+
13
+ /**
14
+ * Returns T/F whether every branch in the tree has a branch length
15
+ *
16
+ * @returns {Object} true if every branch in the tree has a branch length
17
+ */
18
+ export default function hasBranchLengths() {
19
+
20
+ let bl = this.branch_length;
21
+
22
+ if (bl) {
23
+ return _.every(this.nodes.descendants(), function(node) {
24
+ return !node.parent || !_.isUndefined(bl(node));
25
+ });
26
+
27
+ }
28
+
29
+ return false;
30
+ }
31
+
32
+ /**
33
+ * Returns branch lengths
34
+ *
35
+ * @returns {Array} array of branch lengths
36
+ */
37
+ export function getBranchLengths() {
38
+
39
+ let bl = this.branch_length;
40
+ return _.map(this.nodes.descendants(), node => { return bl(node)});
41
+
42
+ }
43
+
44
+
45
+ export function defBranchLengthAccessor(_node, new_length) {
46
+
47
+ let _node_data = _node.data;
48
+ //let _node_data = _node;
49
+
50
+ if (
51
+ "attribute" in _node_data &&
52
+ _node_data["attribute"] &&
53
+ _node_data["attribute"].length
54
+ ) {
55
+
56
+ if(new_length > 0) {
57
+ _node_data["attribute"] = String(new_length);
58
+ }
59
+
60
+ let bl = parseFloat(_node_data["attribute"]);
61
+
62
+ if (!isNaN(bl)) {
63
+ return Math.max(0, bl);
64
+ }
65
+
66
+ }
67
+
68
+ // Allow for empty branch length at root
69
+ if(_node_data.name == "root") {
70
+ return 0;
71
+ }
72
+
73
+ console.warn('Undefined branch length at ' + _node_data.name + '!');
74
+
75
+ return undefined;
76
+
77
+ }
78
+
79
+ /**
80
+ * Get or set branch length accessor.
81
+ *
82
+ * @param {Function} attr Empty if getting, or new branch length accessor if setting.
83
+ * @returns {Object} The branch length accessor if getting, or the current this if setting.
84
+ */
85
+ export function setBranchLength(attr) {
86
+ if (!arguments.length) return this.branch_length_accessor;
87
+ this.branch_length_accessor = attr ? attr : defBranchLengthAccessor;
88
+ return this;
89
+ }
90
+
91
+ /**
92
+ * Normalizes branch lengths
93
+ */
94
+ export function normalize(attr) {
95
+
96
+ let bl = this.branch_length;
97
+
98
+ let branch_lengths = _.map(this.nodes.descendants(), function(node) {
99
+ if(bl(node)) {
100
+ return bl(node);
101
+ } else {
102
+ return null;
103
+ }
104
+ });
105
+
106
+ const max_bl = _.max(branch_lengths);
107
+ const min_bl = _.min(branch_lengths);
108
+
109
+ let scaler = function (x) {
110
+ return (x - min_bl)/(max_bl - min_bl);
111
+ }
112
+
113
+ _.each(this.nodes.descendants(), (node) => {
114
+ let len = bl(node);
115
+ if(len) {
116
+ bl(node, scaler(len));
117
+ }
118
+ });
119
+
120
+ return this;
121
+
122
+ }
123
+
124
+
125
+ /**
126
+ * Scales branch lengths
127
+ *
128
+ * @param {Function} function that scales the branches
129
+ */
130
+ export function scale(scale_by) {
131
+
132
+ let bl = this.branch_length;
133
+
134
+ _.each(this.nodes.descendants(), (node) => {
135
+ let len = bl(node);
136
+ if(len) {
137
+ bl(node, scale_by(len));
138
+ }
139
+ });
140
+
141
+ return this;
142
+
143
+ }
144
+
145
+ /**
146
+ * Get or set branch name accessor.
147
+ *
148
+ * @param {Function} attr (Optional) If setting, a function that accesses a branch name
149
+ * from a node.
150
+ * @returns The ``nodeLabel`` accessor if getting, or the current ``this`` if setting.
151
+ */
152
+ export function branchName(attr) {
153
+ if (!arguments.length) return this.nodeLabel;
154
+ this.nodeLabel = attr;
155
+ return this;
156
+ }
@@ -0,0 +1,115 @@
1
+ import * as _ from "underscore";
2
+
3
+ /**
4
+ * Implements a linear time / space version of the Cluster picker algorithm
5
+ *
6
+ * @param tree -- the tree object
7
+ * @param bootstrap_thresold -- value in [0,1] that sets the stringency of bootstrap support
8
+ * @param diameter_threshold -- value >=0 that defines the maximum allowable pairwise distance in a cluster
9
+ * @param root_node -- if specified, traverse the tree starting here (useful for only looking at parts of the tree),
10
+ * tree root by default
11
+ * @param missing_bootstrap_value -- if a branch is missing bootstrap support value, use this value instead
12
+ * (1.0 by default)
13
+ *
14
+ * @return an array of clusters, where cluster =
15
+ * \{
16
+ * 'root' : [root node of cluster],
17
+ * 'members' : [list of leaf. nodes],
18
+ * 'diameter' : max distance in the cluster,
19
+ * 'bootstrap' : bootstrap support at the root node
20
+ * \}
21
+ */
22
+ function clusterPicker(
23
+ tree,
24
+ bootstrap_threshold,
25
+ diameter_threshold,
26
+ root_node,
27
+ missing_bootstrap_value
28
+ ) {
29
+ root_node = root_node || tree.getRootNode();
30
+ missing_bootstrap_value = _.isNumber(missing_bootstrap_value)
31
+ ? missing_bootstrap_value
32
+ : 1;
33
+
34
+ // perform a bottom-up pass of the tree
35
+ // where each internal node will receive a floating point value
36
+ // that stores the longest path from the internal node to any of its descendants,
37
+ // the diameter of the cluster, is then the sum of longest paths of all of its children
38
+ let bl = tree.branch_length;
39
+
40
+ // initialize member variables
41
+ tree.traverse_and_compute(function(n) {
42
+ if (n.parent) {
43
+ n._computed_length = bl(n);
44
+ if (!_.isNumber(n._computed_length)) {
45
+ throw "clusterPicker cannot be run on trees with missing branch lengths";
46
+ }
47
+ n.max_path_length = 0;
48
+ }
49
+ });
50
+
51
+ tree.traverse_and_compute(function(n) {
52
+ if (n.parent) {
53
+ n.parent.max_path_length = Math.max(
54
+ n.parent.max_path_length,
55
+ n.max_path_length + n._computed_length
56
+ );
57
+ }
58
+ });
59
+
60
+ var clusters = [];
61
+
62
+ tree.traverse_and_compute(_.noop, "pre-order", root_node, function(n) {
63
+ if (!tree.isLeafNode(n)) {
64
+ var bs = _.isString(n.data.bootstrap_values)
65
+ ? +n.data.bootstrap_values
66
+ : missing_bootstrap_value;
67
+
68
+ if (bs >= bootstrap_threshold) {
69
+ var my_diameter = _.reduce(
70
+ n.children,
71
+ function(c, n) {
72
+ return n.max_path_length + n._computed_length + c;
73
+ },
74
+ 0
75
+ );
76
+
77
+ if (my_diameter <= diameter_threshold) {
78
+ clusters.push({ root: n, diameter: my_diameter, bootstrap: bs });
79
+ return true;
80
+ }
81
+ }
82
+ }
83
+
84
+ return false;
85
+ });
86
+
87
+ // clean up member variables
88
+ tree.traverse_and_compute(
89
+ function(n) {
90
+ if (n.parent) {
91
+ delete n._computed_length;
92
+ delete n.max_path_length;
93
+ }
94
+ },
95
+ "post-order",
96
+ root_node
97
+ );
98
+
99
+ _.each(clusters, function(cluster) {
100
+ cluster["members"] = [];
101
+ tree.traverse_and_compute(
102
+ function(n) {
103
+ if (tree.isLeafNode(n)) {
104
+ cluster["members"].push(n);
105
+ }
106
+ },
107
+ "post-order",
108
+ cluster["root"]
109
+ );
110
+ });
111
+
112
+ return clusters;
113
+ }
114
+
115
+ export default clusterPicker;
@@ -0,0 +1,238 @@
1
+ import * as _ from "underscore";
2
+
3
+ /*
4
+ *Implements a linear time / space version of the Phylopart algorithm
5
+ *Nature Communications volume 2, Article number: 321 (2011)
6
+ *
7
+ *@param tree -- the tree object
8
+ *
9
+ *@param bootstrap_thresold -- value in [0,1] that sets the stringency of bootstrap support
10
+ *
11
+ *@param percentile threshold -- a value in 0-1, which is used to delineate how clusters
12
+ * are defined; the actual criterion used is
13
+ * [approximate] median pairwise distance in a cluster <= %-le of tree-wide distribution
14
+ *
15
+ *@param missing_bootstrap_value -- if a branch is missing bootstrap support value, use this value instead
16
+ * (1.0 by default)
17
+ *
18
+ *@param resolution -- the width of the bin for the histogram; by default is set to be the
19
+ * minimum of 0.001, branch length range / 100,
20
+ * however, the maximum number of histogram bins is capped at 500 for
21
+ * performance reasons
22
+ *
23
+ *@return an array of clusters, where cluster =
24
+ *{
25
+ * 'root' : [root node of cluster]
26
+ * 'members' : [list of leaf. nodes]
27
+ * 'median' : approximate median cluster distance,
28
+ * 'bootstrap' : bootstrap support at the root node
29
+ *}
30
+ */
31
+
32
+ import { default as pairwise_distances } from "../metrics/pairwise-distances";
33
+
34
+ function phylopart(
35
+ tree,
36
+ bootstrap_threshold,
37
+ percentile_threshold,
38
+ missing_bootstrap_value,
39
+ resolution
40
+ ) {
41
+ /** TODO SLKP 20180817 : this implementation does not compute pairwise distances correctly at the moment;
42
+ instead it computes root-to-tip distances */
43
+ missing_bootstrap_value = _.isNumber(missing_bootstrap_value)
44
+ ? missing_bootstrap_value
45
+ : 1;
46
+
47
+ var leaf_count = pairwise_distances(tree);
48
+
49
+ /** first, decide on the domain of branch lengths **/
50
+
51
+ var core_node = tree.getRootNode().children[0];
52
+
53
+ var min_bl = Number.MAX_VALUE,
54
+ min_bl2 = Number.MAX_VALUE;
55
+
56
+ if (!(percentile_threshold > 0 && percentile_threshold < 1)) {
57
+ throw "Invalid percentile threshold in perform_phylopart";
58
+ }
59
+
60
+ tree.traverse_and_compute(function(n) {
61
+ if (tree.isLeafNode(n)) {
62
+ if (n.cot_computed_length < min_bl) {
63
+ if (min_bl < min_bl2) {
64
+ min_bl2 = min_bl;
65
+ }
66
+ min_bl = n.cot_computed_length;
67
+ } else {
68
+ if (n.cot_computed_length < min_bl2) {
69
+ min_bl2 = n.cot_computed_length;
70
+ }
71
+ }
72
+ }
73
+ });
74
+
75
+ min_bl += min_bl2;
76
+
77
+ // pairwise distances are bounded below by the sum of two shortest terminal branches
78
+
79
+ // compute the upper bound
80
+ var max_path_length =
81
+ _.reduce(
82
+ core_node.cot_path_to_leaves_below,
83
+ function(c, n) {
84
+ return n > c ? n : c;
85
+ },
86
+ 0
87
+ ) +
88
+ _.reduce(
89
+ core_node.cot_path_to_leaves_above,
90
+ function(c, n) {
91
+ return n > c ? n : c;
92
+ },
93
+ 0
94
+ ) +
95
+ core_node.cot_computed_length;
96
+
97
+ var domain = max_path_length - min_bl;
98
+
99
+ if (_.isUndefined(resolution)) {
100
+ resolution = Math.min(1e-3, domain / 100);
101
+ }
102
+
103
+ var number_of_bins = ((domain / resolution) >> 0) + 1;
104
+ if (number_of_bins > 500) {
105
+ number_of_bins = 500;
106
+ resolution = domain / number_of_bins;
107
+ }
108
+
109
+ var root_node = tree.getRootNode();
110
+
111
+ root_node.paths_to_leaves = new Array(leaf_count);
112
+
113
+ _.each(root_node.children, function(cn) {
114
+ _.each(root_node.cot_path_to_leaves_below, function(v, i) {
115
+ root_node.paths_to_leaves[i] = v + cn.cot_computed_length;
116
+ });
117
+ });
118
+
119
+ tree.traverse_and_compute(function(n) {
120
+ if (!tree.isLeafNode(n)) {
121
+ n.histogram = new Array(number_of_bins);
122
+ for (var i = 0; i < number_of_bins; i++) {
123
+ n.histogram[i] = 0;
124
+ }
125
+ if (n.parent) {
126
+ var index = 0;
127
+ n.paths_to_leaves = [];
128
+ _.each(n.cot_path_to_leaves_below, function(v, i) {
129
+ n.paths_to_leaves[index++] = v;
130
+ });
131
+ }
132
+ }
133
+ delete n.cot_path_to_leaves_above;
134
+ delete n.cot_path_to_leaves_below;
135
+ });
136
+
137
+ /**
138
+ for each internal node, produce a histogram of pairwise distances on sequences that are defined
139
+ by the subtree at that node
140
+
141
+ this could be approximated (I think), by merging histograms of children
142
+ */
143
+
144
+ tree.traverse_and_compute(function(n) {
145
+ if (!tree.isLeafNode(n)) {
146
+ for (var n1 = 0; n1 < n.paths_to_leaves.length; n1++) {
147
+ for (var n2 = n1 + 1; n2 < n.paths_to_leaves.length; n2++) {
148
+ var sum = n.paths_to_leaves[n1] + n.paths_to_leaves[n2];
149
+ n.histogram[((sum - min_bl) / resolution) >> 0]++;
150
+ }
151
+ }
152
+ n.leaf_count = n.paths_to_leaves.length;
153
+
154
+ delete n.paths_to_leaves;
155
+ }
156
+ });
157
+
158
+ // compute the percentile distance cutoff
159
+
160
+ var total_comparisons =
161
+ (leaf_count - 1) * leaf_count / 2 * percentile_threshold;
162
+ var bin_i = 0;
163
+ for (
164
+ ;
165
+ bin_i < number_of_bins - 1 &&
166
+ total_comparisons > root_node.histogram[bin_i];
167
+ bin_i++
168
+ ) {
169
+ total_comparisons -= root_node.histogram[bin_i];
170
+ }
171
+
172
+ var median_threshold =
173
+ min_bl +
174
+ (bin_i +
175
+ (root_node.histogram[bin_i] - total_comparisons) /
176
+ root_node.histogram[bin_i]) *
177
+ resolution;
178
+
179
+ var clusters = [];
180
+
181
+ tree.traverse_and_compute(_.noop, "pre-order", null, function(n) {
182
+ if (!tree.isLeafNode(n)) {
183
+ var bs = _.isString(n.data.bootstrap_values)
184
+ ? +n.data.bootstrap_values
185
+ : missing_bootstrap_value;
186
+ if (bs >= bootstrap_threshold) {
187
+ var total_comparisons = n.leaf_count * (n.leaf_count - 1) * 0.25;
188
+
189
+ var bin_i = 0;
190
+ for (
191
+ ;
192
+ bin_i < number_of_bins - 1 && total_comparisons > n.histogram[bin_i];
193
+ bin_i++
194
+ ) {
195
+ total_comparisons -= n.histogram[bin_i];
196
+ }
197
+
198
+ var my_median =
199
+ min_bl +
200
+ (bin_i +
201
+ (n.histogram[bin_i] - total_comparisons) / n.histogram[bin_i]) *
202
+ resolution;
203
+
204
+ if (my_median <= median_threshold) {
205
+ clusters.push({ root: n, median: my_median, bootstrap: bs });
206
+ return true;
207
+ }
208
+ }
209
+ }
210
+ return false;
211
+ });
212
+
213
+ tree.traverse_and_compute(function(n) {
214
+ if (!tree.isLeafNode(n)) {
215
+ if ("histogram" in n) {
216
+ delete n.histogram;
217
+ delete n.leaf_count;
218
+ }
219
+ }
220
+ });
221
+
222
+ _.each(clusters, function(cluster) {
223
+ cluster["members"] = [];
224
+ tree.traverse_and_compute(
225
+ function(n) {
226
+ if (tree.isLeafNode(n)) {
227
+ cluster["members"].push(n);
228
+ }
229
+ },
230
+ "post-order",
231
+ cluster["root"]
232
+ );
233
+ });
234
+
235
+ return clusters;
236
+ }
237
+
238
+ export default phylopart;
package/src/export.js ADDED
@@ -0,0 +1,25 @@
1
+ import * as _ from "underscore";
2
+
3
+ /**
4
+ * Return CSV of nodes sorted by longest branches.
5
+ *
6
+ * @returns {Array} An array of all tips and associated lengths of the form :
7
+ * [{
8
+ * name : <tip_name>,
9
+ * length: <tip_length>
10
+ * }, ...]
11
+ */
12
+
13
+ export default function getTipLengths() {
14
+
15
+ // Get nodes and branch lengths
16
+ let self = this;
17
+ let tips = self.getTips();
18
+
19
+ // Transform to name, attribute key-pair and sort by attribute length, descending
20
+ let toExport = _.map(tips, d => { return {'name' : d.data.name, 'length' : parseFloat(d.data.attribute) } });
21
+ toExport = _.sortBy(toExport, d=> -d.length)
22
+ return toExport;
23
+
24
+
25
+ }
@@ -0,0 +1,68 @@
1
+ import * as d3 from "d3";
2
+
3
+ const default_date_converter = d3.timeParse("%Y%m%d");
4
+
5
+ const default_regexp = /([0-9]{4}).?([0-9]{2}).?([0-9]{2})$/g;
6
+
7
+ const default_date_getter = function(node) {
8
+ if (d3.layout.phylotree.isLeafNode(node)) {
9
+ if ("name" in node) {
10
+ var location = default_regexp.exec(node.name);
11
+ if (location) {
12
+ return location[1] + location[2] + location[3];
13
+ }
14
+ }
15
+ }
16
+ return null;
17
+ };
18
+
19
+ /*
20
+ * Extracts dates from nodes using a provided callback (defaults supplied),
21
+ * and also converts them to decimal dates; missing dates are allowed; if desired, missing dates
22
+ * can throw exceptions
23
+ *
24
+ * @param tree : the tree object
25
+ *
26
+ * @param date_getter : a function that extracts date strings from nodes (e.g. by parsing the name),
27
+ * default is to extract from the end of the node name, using [YYYY] optional sep [MM] optional sep [DD] format;
28
+ * default is implemented in phylotree_extensions.extract_dates.date_getter ()
29
+ *
30
+ * @param date_converter : if provided, will be used to parse the date string; default is %Y%m%d implemented in
31
+ * phylotree_extensions.extract_dates.date_converter
32
+ *
33
+ *
34
+ * @return tree with date-annotated nodes, i.e. each node will have
35
+ *
36
+ * n.date_value (date object, e.g. 2018-08-17); null for missing
37
+ * n.decimal_date_value (decimal object, e.g. 2018.72)
38
+ *
39
+ */
40
+ const extract_dates = function(tree, date_getter, date_converter=default_date_converter) {
41
+
42
+ date_getter = date_getter || default_date_getter;
43
+
44
+ tree.traverse_and_compute(function(n) {
45
+ var d_string = date_getter(n);
46
+ if (d_string) {
47
+ try {
48
+ n.data.date_value = date_converter(d_string);
49
+ var full_year = n.data.date_value.getFullYear();
50
+ var year_start = new Date(full_year, 0, 1),
51
+ year_start_p1 = new Date(full_year + 1, 0, 1);
52
+
53
+ n.data.decimal_date_value =
54
+ full_year +
55
+ (n.data.date_value - year_start) / (year_start_p1 - year_start);
56
+ return;
57
+ } catch (e) {
58
+ // for conversion failures
59
+ }
60
+ }
61
+ n.data.date_value = null;
62
+ n.data.decimal_date_value = null;
63
+ });
64
+
65
+ return tree;
66
+ };
67
+
68
+ export default extract_dates;
@@ -0,0 +1,29 @@
1
+ import newickParser from "./newick";
2
+
3
+ export default function(newick, options) {
4
+ options.left_delimiter = '[';
5
+ options.right_delimiter = ']';
6
+ const parsed_newick = newickParser(newick, options);
7
+ function parseBeastNode(node) {
8
+ if(node.annotation) {
9
+ node.beast = {};
10
+ const tokens = node.annotation.split(/=|,|{|}/)
11
+ .filter(token => token);
12
+ for(var i = 0; i < tokens.length; i+=2) {
13
+ let key = tokens[i].replace(/&|%/g, '');
14
+ if(/[a-df-zA-DF-Z]+/.test(tokens[i+2])) {
15
+ node.beast[key] = +tokens[i+1];
16
+ } else {
17
+ node.beast[key] = [+tokens[i+1], +tokens[i+2]];
18
+ i++;
19
+ }
20
+ }
21
+ }
22
+ node.annotation = undefined;
23
+ if(node.children) {
24
+ node.children.forEach(parseBeastNode);
25
+ }
26
+ }
27
+ parseBeastNode(parsed_newick.json);
28
+ return parsed_newick;
29
+ }