@microbiome-informatics/genome-properties-viewer 0.0.0-stage → 1.0.0-beta.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.
@@ -0,0 +1,387 @@
1
+ import * as d3 from "./d3";
2
+ import TaxonomyNodeManager from "./gp-tax-node";
3
+
4
+ export default class GenomePropertiesTaxonomy {
5
+ constructor({
6
+ path,
7
+ x = 0,
8
+ y = 0,
9
+ width = 200,
10
+ height = 600,
11
+ show_tree = true,
12
+ }) {
13
+ this.nodes = null;
14
+ this.root = null;
15
+ this.path = path;
16
+ this.x = x;
17
+ this.y = y;
18
+ this.width = width;
19
+ this.cell_side = 20;
20
+ this.height = height;
21
+ this.current_order = [];
22
+ this.organisms = [];
23
+ this.svg = null;
24
+ this.collapse_tree = true;
25
+ this.show_tree = show_tree;
26
+ this.dataOffsetX = 0;
27
+ this.dipatcher = d3.dispatch(
28
+ "changeOrder",
29
+ "spaciesRequested",
30
+ "multipleSpaciesRequested",
31
+ "changeWidth",
32
+ "taxonomyLoaded",
33
+ "removeSpacies"
34
+ );
35
+ this.node_r = 6;
36
+ this.tax_label_type = "name";
37
+ this.node_manager = new TaxonomyNodeManager(this, this.node_r);
38
+ return this;
39
+ }
40
+
41
+ load_taxonomy() {
42
+ fetch(this.path)
43
+ .then((response) => {
44
+ if (!response.ok)
45
+ throw new Error(`${response.status} ${response.statusText}`);
46
+ return response.json;
47
+ })
48
+ .then((data) => {
49
+ this.load_taxonomy_obj(data);
50
+ });
51
+ return this;
52
+ }
53
+
54
+ load_taxonomy_obj(data) {
55
+ this.root = data;
56
+ this.root.parent = null;
57
+ this.nodes = this.load_nodes(this.root);
58
+ this.root.expanded = true;
59
+ this.dipatcher.call("taxonomyLoaded", this, this.root);
60
+ this.update_tree(500);
61
+ }
62
+
63
+ load_nodes(node) {
64
+ if (this.nodes === null) this.nodes = {};
65
+ node.id = node.id || node.taxid;
66
+ this.nodes[node.taxid] = node;
67
+ this.nodes[node.taxid].expanded = false;
68
+ if (node.children === null || node.children.length < 1)
69
+ this.organisms.push(node.taxid);
70
+ node.children.forEach((child) => {
71
+ this.load_nodes(child);
72
+ });
73
+ return this.nodes;
74
+ }
75
+
76
+ draw_tree_panel(svg, treeClipId = null) {
77
+ this.svg = svg;
78
+ this.tree_layer = svg.append("g").attr("class", "taxon_tree_layer");
79
+ if (treeClipId) {
80
+ this.tree_layer.attr("clip-path", `url(#${treeClipId})`);
81
+ }
82
+ this.tree_g = this.tree_layer
83
+ .append("g")
84
+ .attr("class", "taxon_tree")
85
+ .attr("transform", `translate(${this.x}, ${this.y})`);
86
+ this.node_manager.tree_g = this.tree_g;
87
+ }
88
+
89
+ // Walks the tree and calculates x,y values for each node.
90
+ // Leaves run along X so they line up with species columns; depth runs down Y.
91
+ tree(node, deepness = 0) {
92
+ let avg = 0;
93
+
94
+ if (node.has_loaded_leaves) {
95
+ if (node.children) {
96
+ for (const child of node.children) {
97
+ this.tree(child, deepness + 1);
98
+ avg += child.x;
99
+ node.deepness =
100
+ !node.deepness || child.deepness > node.deepness
101
+ ? child.deepness
102
+ : node.deepness;
103
+ }
104
+ node.x = avg / node.children.length;
105
+ const leafY = Math.max(0, this.height - this.y - this.cell_side);
106
+ node.y = (deepness / (node.deepness || 1)) * leafY;
107
+ } else {
108
+ node.x =
109
+ (this.dataOffsetX || 0) +
110
+ this.cell_side / 3 +
111
+ this.cell_side *
112
+ this.current_order.indexOf(this.organisms.indexOf(node.data.taxid));
113
+ node.y = Math.max(0, this.height - this.y - this.cell_side);
114
+ node.deepness = deepness;
115
+ }
116
+ }
117
+ }
118
+
119
+ prune_inner_nodes(tree, depth = 0) {
120
+ if (!tree.label) tree.label = tree.data.name;
121
+ if (!tree.taxid) tree.taxid = tree.data.taxid;
122
+ tree.depth = depth;
123
+ if (tree.children) {
124
+ if (tree.children.length === 1) {
125
+ tree.label = tree.children[0].data.name;
126
+ tree.height = tree.children[0].height;
127
+ tree.data = tree.children[0].data;
128
+ tree.children = tree.children[0].children;
129
+ if (tree.children)
130
+ for (const child of tree.children) child.parent = tree;
131
+ this.prune_inner_nodes(tree, depth);
132
+ } else
133
+ for (const child of tree.children)
134
+ this.prune_inner_nodes(child, depth + 1);
135
+ }
136
+ }
137
+
138
+ mark_branch_for_loaded_leaves(node) {
139
+ node.has_loaded_leaves = true;
140
+ if (node.parent) this.mark_branch_for_loaded_leaves(node.parent);
141
+ }
142
+
143
+ filter_collapsed_nodes(node) {
144
+ if (node.data.expanded) {
145
+ node.children = node.children ? node.children : node._children;
146
+ node._children = null;
147
+ } else {
148
+ node._children = node.children;
149
+ if (node.has_loaded_leaves && node._children) {
150
+ node.children = node._children.filter((d) => d.has_loaded_leaves);
151
+ } else {
152
+ node.children = null;
153
+ }
154
+ }
155
+ if (node.children) {
156
+ node.children.sort((a) => (a.has_loaded_leaves ? -1 : 1));
157
+ node.children.forEach((n) => {
158
+ n.data.expanded = node.data.expanded && n.data.expanded;
159
+ this.filter_collapsed_nodes(n);
160
+ });
161
+ }
162
+ }
163
+
164
+ requestAll(tree) {
165
+ tree.expanded = true;
166
+ if (!tree.children || tree.children.length === 0) {
167
+ this.dipatcher.call("spaciesRequested", this, tree.taxid);
168
+ }
169
+ if (tree.children) {
170
+ tree.children.forEach((d) => this.requestAll(d));
171
+ }
172
+ }
173
+
174
+ getLeaves(tree) {
175
+ if (!tree.children || tree.children.length === 0) {
176
+ return [tree.taxid];
177
+ }
178
+ if (tree.children) {
179
+ const leaves = [];
180
+ for (const child of tree.children) {
181
+ leaves.splice(0, 0, ...this.getLeaves(child));
182
+ }
183
+ return leaves;
184
+ }
185
+ return null;
186
+ }
187
+
188
+ set_organisms_loaded(tax_id, isFromFile) {
189
+ if (tax_id in this.nodes) this.nodes[tax_id].loaded = true;
190
+ else {
191
+ this.nodes[tax_id] = {
192
+ id: tax_id,
193
+ loaded: true,
194
+ taxid: tax_id,
195
+ name: tax_id,
196
+ isFromFile,
197
+ };
198
+ this.root.children.push(this.nodes[tax_id]);
199
+ }
200
+ // this.organisms.sort((a,b)=>{
201
+ // return tax_loaded.indexOf(tax_loaded.indexOf(String(this.nodes[a].taxid))-tax_loaded.indexOf(String(this.nodes[b].taxid)));
202
+ // });
203
+ }
204
+
205
+ // A fake tree is created when the taxonomy is hidden.
206
+ // The new tree is only the root and the loaded leaves.
207
+ // This method either contructs the fake tree or uses the taxonomy one.
208
+ get_tree_to_show() {
209
+ const leaves = Object.values(this.nodes).filter((d) => d.loaded);
210
+ leaves.forEach((l) => {
211
+ if (this.show_tree) {
212
+ l.parent = l.parent === "fake-root" ? l._parent : l.parent;
213
+ l._parent = null;
214
+ } else {
215
+ l._parent = l.parent === "fake-root" ? l._parent : l.parent;
216
+ l.parent = "fake-root";
217
+ }
218
+ });
219
+ if (this.show_tree) {
220
+ return this.root;
221
+ }
222
+ return {
223
+ children: leaves,
224
+ expanded: true,
225
+ id: "fake-root",
226
+ lineage: "",
227
+ number_of_leaves: leaves.length,
228
+ parent: null,
229
+ rank: null,
230
+ name: "root",
231
+ taxid: "root",
232
+ taxonomy: "",
233
+ };
234
+ }
235
+
236
+ update_tree(time = 0, cell_side = null) {
237
+ if (this.root === null || !this.tree_g) return;
238
+ if (cell_side !== null) this.cell_side = cell_side;
239
+ this.tree_g.attr("transform", `translate(${this.x}, ${this.y})`);
240
+
241
+ const root = d3.hierarchy(this.get_tree_to_show());
242
+
243
+ if (this.show_tree && this.collapse_tree) this.prune_inner_nodes(root);
244
+ else
245
+ root.descendants().forEach((e) => {
246
+ e.label = e.data.name || e.data.taxid;
247
+ });
248
+
249
+ root
250
+ .leaves()
251
+ .filter((d) => d.data.loaded)
252
+ .forEach((d) => this.mark_branch_for_loaded_leaves(d));
253
+ this.filter_collapsed_nodes(root);
254
+ root.sort((a) => (a.has_loaded_leaves ? -1 : 1));
255
+ root.eachBefore((node) => {
256
+ let height = 0;
257
+ do {
258
+ node.height = height;
259
+ node = node.parent;
260
+ } while (node && node.height < ++height);
261
+ });
262
+
263
+ const leaves = root
264
+ .leaves()
265
+ .filter((d) => d.data.loaded)
266
+ .sort((a, b) => a.data.taxid - b.data.taxid);
267
+ const ol = leaves.length;
268
+ this.organisms = leaves.map((n) => n.data.taxid);
269
+ this.dataOffsetX = Math.max(
270
+ 0,
271
+ this.width - this.organisms.length * this.cell_side
272
+ );
273
+
274
+ // Precompute the orders.
275
+ this.orders = {
276
+ tax_id: d3
277
+ .range(ol)
278
+ .sort((a, b) => leaves[a].data.taxid - leaves[b].data.taxid),
279
+ org_name: d3
280
+ .range(ol)
281
+ .sort((a, b) => (leaves[a].data.name > leaves[b].data.name ? 1 : -1)),
282
+ tree1: d3
283
+ .range(ol)
284
+ .sort((a, b) =>
285
+ leaves[a].data.lineage > leaves[b].data.lineage ? 1 : -1
286
+ ),
287
+ tree2: d3
288
+ .range(ol)
289
+ .sort((a, b) =>
290
+ leaves[a].data.lineage > leaves[b].data.lineage ? -1 : 1
291
+ ),
292
+ };
293
+ if (!this.current_order || this.current_order.length !== leaves.length)
294
+ this.current_order = this.orders.tree1;
295
+ const layoutHeight = Math.max(
296
+ this.cell_side,
297
+ this.height - (this.y || 0) - this.cell_side
298
+ );
299
+ const tree_f = d3
300
+ .tree()
301
+ .size([Math.max(this.cell_side, this.width), layoutHeight]);
302
+ tree_f(root);
303
+ this.tree(root);
304
+ const t = this.tree_g.transition().duration(time).delay(100);
305
+ const visible_nodes = root.descendants().filter((d) => {
306
+ if (!d.parent) return true;
307
+ return (
308
+ d.data.expanded || d.parent.data.expanded || d.parent.has_loaded_leaves
309
+ );
310
+ });
311
+
312
+ const link = this.tree_g
313
+ .selectAll(".link")
314
+ .data(root.links(), (d) =>
315
+ d.source.data.id > d.target.data.id
316
+ ? d.source.data.id + d.target.data.id
317
+ : d.target.data.id + d.source.data.id
318
+ );
319
+
320
+ link
321
+ .style("stroke-dashoffset", 0)
322
+ .transition(t)
323
+ .attr(
324
+ "d",
325
+ (d) =>
326
+ `M${d.source.x},${d.source.y}V${d.source.y + 10}H${d.target.x}V${
327
+ d.target.y
328
+ }`
329
+ );
330
+
331
+ link.exit().transition(t).attr("stroke-dashoffset", -1000).remove();
332
+ link
333
+ .enter()
334
+ .append("path")
335
+ .attr("class", "link")
336
+ .attr(
337
+ "d",
338
+ (d) =>
339
+ `M${d.source.x},${d.source.y}V${d.source.y + 10}H${d.target.x}V${
340
+ d.target.y
341
+ }`
342
+ )
343
+ .style("stroke", (d) =>
344
+ d.target.data.isFromFile ||
345
+ d.target.data.parent === "fake-root" ||
346
+ (d.target.data.parent &&
347
+ d.target.data.parent.data &&
348
+ d.target.data.parent.data.taxid === "fake-root")
349
+ ? "transparent"
350
+ : null
351
+ )
352
+ .attr("stroke-dasharray", 1000)
353
+ .attr("stroke-dashoffset", -1000)
354
+ .transition(t)
355
+ .attr("stroke-dashoffset", 0);
356
+
357
+ this.node_manager.draw_nodes(visible_nodes, t);
358
+ }
359
+
360
+ get_tax_list() {
361
+ return this.organisms.map((d) => this.nodes[d].taxid);
362
+ }
363
+
364
+ on(typename, callback) {
365
+ this.dipatcher.on(typename, callback);
366
+ return this;
367
+ }
368
+
369
+ change_tax_label(type) {
370
+ this.tax_label_type = type;
371
+ this.update_tree();
372
+ }
373
+
374
+ remove_organism_loaded(tax_id, isFromFile) {
375
+ this.nodes[tax_id].loaded = false;
376
+ if (isFromFile) {
377
+ const i = this.root.children.indexOf(this.nodes[tax_id]);
378
+ this.root.children.splice(i, 1);
379
+ delete this.nodes[tax_id];
380
+ }
381
+ }
382
+
383
+ sortBy(method) {
384
+ this.current_order = this.orders[method];
385
+ this.dipatcher.call("changeOrder", this, this.current_order);
386
+ }
387
+ }
@@ -0,0 +1,209 @@
1
+ import * as d3 from "./d3";
2
+
3
+ export const drawTotalPerOrganismPanel = (viewer) => {
4
+ viewer.total_g = viewer.mainGroup
5
+ .append("g")
6
+ .attr("class", "total-group")
7
+ .attr("transform", "translate(0,0)");
8
+ };
9
+
10
+ export const drawRowTotalPanel = (viewer) => {
11
+ viewer.row_total_g = viewer.mainGroup
12
+ .append("g")
13
+ .attr("class", "row-total-group");
14
+ };
15
+
16
+ export const refreshGPTotals = (viewer) => {
17
+ for (const prop of viewer.props) {
18
+ const total = {
19
+ NO: 0,
20
+ YES: 0,
21
+ PARTIAL: 0,
22
+ };
23
+ for (const org of viewer.organisms) {
24
+ total[prop.values[org]]++;
25
+ }
26
+ prop.values.TOTAL = total;
27
+ }
28
+ };
29
+
30
+ const refreshOrganismTotals = (viewer) => {
31
+ for (const o of Object.keys(viewer.organism_totals))
32
+ viewer.organism_totals[o] = { YES: 0, NO: 0, PARTIAL: 0 };
33
+ viewer.props.forEach((e) => {
34
+ for (const v of Object.keys(viewer.organism_totals))
35
+ viewer.organism_totals[v][e.values[v]]++;
36
+ });
37
+ };
38
+
39
+ export const updateTotalPerOrganismPanel = (viewer) => {
40
+ viewer.options.total_panel_height = viewer.options.cell_side;
41
+ const ph = viewer.options.total_panel_height;
42
+ const heatmapBottom =
43
+ viewer.heatmapTop +
44
+ Math.min(
45
+ viewer.heatmapHeight(),
46
+ viewer.y(viewer.props?.length || 0) || viewer.heatmapHeight()
47
+ );
48
+
49
+ viewer.total_g.attr(
50
+ "transform",
51
+ `translate(${
52
+ viewer.heatmapAnchor + viewer.current_scroll.x
53
+ }, ${heatmapBottom})`
54
+ );
55
+
56
+ const arc_f = d3
57
+ .arc()
58
+ .outerRadius(ph * 0.4)
59
+ .innerRadius(0);
60
+
61
+ const pie_f = d3.pie().value((d) => d.value);
62
+
63
+ refreshOrganismTotals(viewer);
64
+
65
+ const cells_t = viewer.total_g.selectAll(".total_cell_org").data(
66
+ d3
67
+ .entries(viewer.organism_totals)
68
+ .sort(
69
+ (a, b) =>
70
+ viewer.organisms.indexOf(a.key) - viewer.organisms.indexOf(b.key)
71
+ ),
72
+ (d) => d.key
73
+ );
74
+
75
+ const newX = d3
76
+ .scaleBand()
77
+ .range([0, viewer.organisms.length * viewer.options.cell_side])
78
+ .domain(viewer.current_order || []);
79
+
80
+ cells_t.attr(
81
+ "transform",
82
+ (d, i) =>
83
+ `translate(${newX(i) + viewer.options.cell_side / 2}, ${
84
+ viewer.options.cell_side / 2
85
+ })`
86
+ );
87
+ cells_t.exit().remove();
88
+
89
+ const g_e = cells_t
90
+ .enter()
91
+ .append("g")
92
+ .attr("class", "total_cell_org")
93
+ .attr(
94
+ "transform",
95
+ (d, i) =>
96
+ `translate(${newX(i) + viewer.options.cell_side / 2}, ${
97
+ viewer.options.cell_side / 2
98
+ })`
99
+ )
100
+ .on("mouseover", (event, p) => {
101
+ d3.selectAll(".node--leaf text").classed(
102
+ "active",
103
+ () => viewer.textContent === p.key
104
+ );
105
+ viewer.controller.draw_tooltip(event, {
106
+ Organism: p.key,
107
+ });
108
+ viewer.controller.draw_legends(p.value);
109
+ })
110
+ .on("mouseout", () => {
111
+ viewer.controller.draw_legends();
112
+ viewer.controller.draw_tooltip();
113
+ d3.selectAll(".node--leaf text").classed("active", false);
114
+ });
115
+
116
+ const group = g_e.size() ? g_e : cells_t;
117
+ const arcs = group.selectAll(".arc").data((d) => pie_f(d3.entries(d.value)));
118
+
119
+ arcs.attr("d", arc_f).attr("transform", "scale(1)");
120
+
121
+ arcs
122
+ .enter()
123
+ .append("path")
124
+ .attr("class", "arc")
125
+ .attr("d", arc_f)
126
+ .style("fill", (d) => viewer.c[d.data.key]);
127
+ };
128
+
129
+ export const updateRowTotalPanel = (viewer) => {
130
+ if (!viewer.row_total_g) return;
131
+ const side = viewer.options.cell_side;
132
+ const dataWidth = (viewer.organisms?.length || 0) * side;
133
+ const pieOffset = viewer.organisms?.length
134
+ ? Math.min(dataWidth, viewer.heatmapWidth())
135
+ : 0;
136
+
137
+ viewer.row_total_g.attr(
138
+ "transform",
139
+ `translate(${viewer.heatmapAnchor + pieOffset + side * 0.5}, ${
140
+ viewer.current_scroll.y
141
+ })`
142
+ );
143
+
144
+ const header = viewer.row_total_g
145
+ .selectAll("text.total_title")
146
+ .data(viewer.organisms?.length ? ["TOTAL"] : []);
147
+ header.exit().remove();
148
+ header
149
+ .enter()
150
+ .append("text")
151
+ .attr("class", "total_title")
152
+ .merge(header)
153
+ .attr("transform", `translate(0, ${viewer.heatmapTop - 6}) rotate(-70)`)
154
+ .attr("text-anchor", "start")
155
+ .text("TOTAL");
156
+
157
+ const props = viewer.current_props || [];
158
+ const dy = props.length ? Math.max(0, viewer.props.indexOf(props[0])) : 0;
159
+ const localY = (d, i) => viewer.heatmapTop + viewer.y(i + dy) + side * 0.5;
160
+
161
+ const cells_t = viewer.row_total_g
162
+ .selectAll(".total_cell")
163
+ .data(props, (d) => d.property);
164
+
165
+ cells_t.exit().remove();
166
+ cells_t.attr("transform", (d, i) => `translate(0, ${localY(d, i)})`);
167
+
168
+ const mouseover = (event, gp) => {
169
+ viewer.controller.draw_legends(gp.values.TOTAL);
170
+ viewer.controller.draw_tooltip(event, {
171
+ Property: gp.property,
172
+ Name: gp.name,
173
+ Organism: "All",
174
+ Value: `TOTAL: {YES: ${gp.values.TOTAL.YES}, PARTIAL: ${gp.values.TOTAL.PARTIAL}, NO: ${gp.values.TOTAL.NO}}`,
175
+ });
176
+ };
177
+ const mouseout = () => {
178
+ viewer.controller.draw_legends();
179
+ viewer.controller.draw_tooltip();
180
+ };
181
+
182
+ const entered = cells_t
183
+ .enter()
184
+ .append("g")
185
+ .attr("class", "total_cell")
186
+ .attr("transform", (d, i) => `translate(0, ${localY(d, i)})`)
187
+ .on("mouseover", mouseover)
188
+ .on("mouseout", mouseout);
189
+
190
+ const arc_f = d3
191
+ .arc()
192
+ .outerRadius(side * 0.4)
193
+ .innerRadius(0);
194
+ const pie_f = d3.pie().value((d) => d.value);
195
+
196
+ const group = entered.merge(cells_t);
197
+ const arcs = group.selectAll(".arc").data(
198
+ (d) => pie_f(d3.entries(d.values.TOTAL)),
199
+ (d) => d.data.key
200
+ );
201
+
202
+ arcs.attr("d", arc_f);
203
+ arcs
204
+ .enter()
205
+ .append("path")
206
+ .attr("class", "arc")
207
+ .attr("d", arc_f)
208
+ .style("fill", (d) => viewer.c[d.data.key]);
209
+ };