@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,218 @@
1
+ import { symbol, symbolCross } from "d3-shape";
2
+ import * as d3 from "./d3";
3
+
4
+ const displayStepsModal = (viewer, gp) => {
5
+ const organisms = viewer.gp_taxonomy.current_order.map(
6
+ (i) => viewer.organisms[i]
7
+ );
8
+
9
+ const html = `<h3>Steps for ${gp.property}</h3>
10
+ <table>
11
+ <tr>
12
+ <th>Step</th>
13
+ <th>Name</th>
14
+ ${organisms
15
+ .map(
16
+ (o) =>
17
+ `<th>${viewer.getOrganismNameFromTaxId(
18
+ o
19
+ )}<br/>(${o})</th>`
20
+ )
21
+ .join("")}
22
+ </tr>
23
+ ${gp.steps
24
+ .map(
25
+ (step) => `
26
+ <tr>
27
+ <td>${step.step}</td>
28
+ <td>${step.step_name}</td>
29
+ ${organisms
30
+ .map(
31
+ (o) => `
32
+ <td class="gp-${gp.values[o]}">
33
+ <div class="step-popup ${
34
+ step.values[o] ? "passed" : "failed"
35
+ }" />
36
+ </td>`
37
+ )
38
+ .join("")}
39
+ </tr>
40
+ `
41
+ )
42
+ .join("")}
43
+ </table>
44
+ `;
45
+ viewer.modal.showContent(html);
46
+ };
47
+
48
+ const updateStepDetailsButton = (viewer, gp, element, cellSide) => {
49
+ const x0 = cellSide * 0.1;
50
+ const side = cellSide * 0.8;
51
+ const top = cellSide * 0.1;
52
+ const half = side / 2;
53
+
54
+ const buttonG = d3
55
+ .select(element)
56
+ .selectAll(".step-details-button")
57
+ .data(gp.isShowingSteps ? [gp] : [], (d) => d.property);
58
+
59
+ buttonG.exit().remove();
60
+
61
+ const button = buttonG
62
+ .enter()
63
+ .append("g")
64
+ .attr("class", "step-details-button")
65
+ .on("click", () => displayStepsModal(viewer, gp));
66
+
67
+ button.append("circle");
68
+
69
+ button.append("path").attr("fill", "white");
70
+
71
+ buttonG
72
+ .merge(button)
73
+ .attr("transform", `translate(${-cellSide}, ${cellSide})`)
74
+ .selectAll("circle")
75
+ .transition()
76
+ .attr("cx", x0 + half)
77
+ .attr("cy", top + half)
78
+ .attr("r", half);
79
+
80
+ const symbol1 = symbol()
81
+ .type(symbolCross)
82
+ .size((cellSide / 2) ** 2);
83
+ buttonG
84
+ .merge(button)
85
+ .selectAll("path")
86
+ .transition()
87
+ .attr("d", symbol1())
88
+ .attr("transform", `translate(${x0 + half}, ${top + half})`);
89
+ };
90
+
91
+ export const updateSteps = (viewer, gp, element, cellSide, yScale) => {
92
+ const side = cellSide * 0.6;
93
+ const p = cellSide * 0.2;
94
+
95
+ const steps = d3
96
+ .select(element)
97
+ .selectAll(".step")
98
+ .data(gp.isShowingSteps ? gp.steps : []);
99
+
100
+ steps.exit().remove();
101
+
102
+ const step_g = steps
103
+ .enter()
104
+ .append("g")
105
+ .attr("class", "step")
106
+ .merge(steps)
107
+ .attr("transform", (d, i) => `translate(0, ${(i + 1) * cellSide + p})`);
108
+
109
+ const onMouseOver = (event, d) => {
110
+ viewer.controller.draw_tooltip(event, {
111
+ Property: gp.property,
112
+ Name: gp.name,
113
+ Step: `${d.step}. ${d.stepName}`,
114
+ Passed: d.value ? "YES" : "NO",
115
+ Organism: d.organism,
116
+ });
117
+ };
118
+ const onMouseOut = () => {
119
+ viewer.controller.draw_tooltip();
120
+ };
121
+ const stepPerSpecie = step_g
122
+ .merge(steps)
123
+ .selectAll(".step-per-specie")
124
+ .data(
125
+ (d, i) =>
126
+ viewer.organisms.map((organism) => ({
127
+ organism,
128
+ value: gp.steps[i].values[organism],
129
+ step: gp.steps[i].step,
130
+ stepName: gp.steps[i].step_name,
131
+ key: `${organism}__${i}`,
132
+ })),
133
+ (d) => d.key
134
+ );
135
+ stepPerSpecie.exit().remove();
136
+ stepPerSpecie
137
+ .enter()
138
+ .append("rect")
139
+ .attr("class", "step-per-specie")
140
+ .attr("stroke", "black")
141
+ .on("mouseover", onMouseOver)
142
+ .on("mouseout", onMouseOut)
143
+ .merge(stepPerSpecie)
144
+ .attr("x", (d, i) => yScale(i) + p)
145
+ .attr("y", 0)
146
+ .transition()
147
+ .attr("width", side)
148
+ .attr("height", side)
149
+ .attr("fill", (d) =>
150
+ d.value ? "rgba(100,250,100,0.7)" : "rgba(200,200,200,0.3)"
151
+ );
152
+ };
153
+
154
+ export const updateStepToggler = (viewer, gp, element, cellSide) => {
155
+ const onClick = (event, id) => {
156
+ viewer.data[id].isShowingSteps = !viewer.data[id].isShowingSteps;
157
+ viewer.update_viewer();
158
+ };
159
+ const toggler = d3
160
+ .select(element)
161
+ .selectAll(".step-toggler")
162
+ .data([gp.property], (d) => d);
163
+
164
+ const x0 = 0.1;
165
+ const side = cellSide * 0.8;
166
+ const top = cellSide * 0.1;
167
+ const half = side / 2;
168
+ const fifth = side / 5;
169
+
170
+ const expand =
171
+ `M${x0 + half},${top + 2 * fifth}L${x0 + side},${top + 2 * fifth}L${
172
+ x0 + half
173
+ },${top}L${x0},${top + 2 * fifth}L${x0 + half},${top + 2 * fifth}` +
174
+ `L${x0 + half},${top + 3 * fifth}` +
175
+ `L${x0 + side},${top + 3 * fifth}L${x0 + half},${top + side}L${x0},${
176
+ top + 3 * fifth
177
+ }L${x0 + half},${top + 3 * fifth}Z`;
178
+
179
+ const collapse =
180
+ `M${x0 + half},${top + 2 * fifth}L${x0 + side},${top}L${x0},${top}L${
181
+ x0 + half
182
+ },${top + 2 * fifth}` +
183
+ `L${x0 + half},${top + 3 * fifth}` +
184
+ `L${x0 + side},${top + side}L${x0},${top + side}L${x0 + half},${
185
+ top + 3 * fifth
186
+ }Z`;
187
+
188
+ const newG = toggler
189
+ .enter()
190
+ .append("g")
191
+ .attr("class", "step-toggler")
192
+ .on("click", onClick);
193
+
194
+ newG.append("circle").attr("fill", "rgba(30,60,90,0)");
195
+
196
+ newG.append("path").attr("fill", "rgba(30,60,90,0.2)");
197
+
198
+ newG
199
+ .merge(toggler)
200
+ .attr("transform", `translate(${-cellSide}, 0)`)
201
+ .attr(
202
+ "class",
203
+ (id) => `step-toggler ${viewer.data[id].isShowingSteps ? "expanded" : ""}`
204
+ )
205
+ .selectAll("path")
206
+ .transition()
207
+ .attr("d", (id) => (viewer.data[id].isShowingSteps ? collapse : expand));
208
+
209
+ newG
210
+ .merge(toggler)
211
+ .selectAll("circle")
212
+ .transition()
213
+ .attr("cx", x0 + half)
214
+ .attr("cy", top + half)
215
+ .attr("r", half);
216
+
217
+ updateStepDetailsButton(viewer, gp, element, cellSide);
218
+ };
@@ -0,0 +1,266 @@
1
+ import * as d3 from "./d3";
2
+
3
+ export default class TaxonomyNodeManager {
4
+ constructor(tax_component, r = 6) {
5
+ this.tree_g = null;
6
+ this.main = tax_component;
7
+ this.t = null;
8
+ this.r = r;
9
+ }
10
+
11
+ draw_nodes(visible_nodes, t) {
12
+ this.t = t;
13
+ const move_leaf = (d, d_col) => {
14
+ const current_i = this.main.organisms.indexOf(d.data.id);
15
+ const current_o = this.main.current_order.indexOf(current_i);
16
+ d_col = current_o + d_col < 0 ? -current_o : d_col;
17
+ if (d_col !== 0) {
18
+ const e = this.main.current_order.splice(current_o, 1);
19
+ this.main.current_order.splice(current_o + d_col, 0, e[0]);
20
+ this.main.update_tree(1000);
21
+ this.main.dipatcher.call(
22
+ "changeOrder",
23
+ this.main,
24
+ this.main.current_order
25
+ );
26
+ }
27
+ };
28
+ const move_tree = (d, d_col) => {
29
+ if (!d.children) {
30
+ move_leaf(d, d_col);
31
+ } else {
32
+ d.children.sort((a, b) => (d_col > 0 ? b.x - a.x : a.x - b.x));
33
+ for (const child of d.children) move_tree(child, d_col);
34
+ }
35
+ };
36
+
37
+ const node = this.tree_g
38
+ .selectAll(".node")
39
+ .data(visible_nodes, (d) => d.data.id);
40
+
41
+ node
42
+ .attr(
43
+ "class",
44
+ (d) =>
45
+ `node ${d.children ? " node--internal" : " node--leaf"}${
46
+ d.data.loaded ? " loaded" : ""
47
+ }`
48
+ )
49
+ .style("fill-opacity", (d) => (d.data.id === "fake-root" ? 0 : null))
50
+ .transition(t)
51
+ .attr("transform", (d) => `translate(${d.x},${d.y})`)
52
+ .each((d, i, c) => this.update_node(d, i, c));
53
+ node.exit().remove();
54
+ node
55
+ .enter()
56
+ .append("g")
57
+ .attr(
58
+ "class",
59
+ (d) =>
60
+ `node ${d.children ? " node--internal" : " node--leaf"}${
61
+ d.data.loaded ? " loaded" : ""
62
+ }`
63
+ )
64
+ .style("fill-opacity", (d) => (d.data.id === "fake-root" ? 0 : null))
65
+ .on("mouseover", (event, d) => {
66
+ d3.select("#info_organism").text(
67
+ `${d.label}${d.data.taxid ? ` - ${d.data.taxid}` : ""}`
68
+ );
69
+ d3.select(event.currentTarget)
70
+ .selectAll("circle")
71
+ .transition(300)
72
+ .attr("r", this.r + 2);
73
+ })
74
+ .on("mouseout", (event) => {
75
+ d3.select("#info_organism").text("");
76
+ d3.select(event.currentTarget)
77
+ .selectAll("circle")
78
+ .transition(300)
79
+ .attr("r", (d) => {
80
+ if (d.children || d._children) return this.r;
81
+ if (d.data.loaded) return this.r / 2;
82
+ return this.r - 2;
83
+ });
84
+ })
85
+ .on("click", (event, d) => {
86
+ if (
87
+ !d.data.loaded &&
88
+ (!d.data.children || d.data.children.length === 0)
89
+ ) {
90
+ // Only leaves have taxId attached
91
+ this.main.dipatcher.call("spaciesRequested", this.main, d.data.taxid);
92
+ }
93
+ if (d.parent) {
94
+ setTimeout(() => {
95
+ d.data.expanded = !d.data.expanded;
96
+ this.main.update_tree(500);
97
+ }, 200);
98
+ }
99
+ })
100
+ .on("dblclick", (event, d) => {
101
+ if (!d.data.children || d.data.children.length === 0) {
102
+ // Only leaves have taxId attached
103
+ this.main.dipatcher.call("spaciesRequested", this.main, d.data.taxid);
104
+ }
105
+ if (d.parent) {
106
+ this.main.dipatcher.call(
107
+ "multipleSpaciesRequested",
108
+ this.main,
109
+ this.main.getLeaves(d.data)
110
+ );
111
+ }
112
+ })
113
+ .call(
114
+ d3
115
+ .drag()
116
+ .subject((event) => {
117
+ const g = d3.select(event.sourceEvent.target.closest("g.node"));
118
+ const transform = g
119
+ .attr("transform")
120
+ .match(/translate\((.*),(.*)\)/);
121
+ const pos = {
122
+ x: Number(transform[1]), // + Number(g.attr("x")),
123
+ y: Number(transform[2]), // + Number(g.attr("y")),
124
+ };
125
+ return pos;
126
+ })
127
+ .on("drag", (event, d) => {
128
+ event.sourceEvent.stopPropagation();
129
+ const g = d3.select(event.sourceEvent.target.closest("g.node"));
130
+ if (d.has_loaded_leaves) {
131
+ g.attr("transform", (p) => `translate(${event.x},${p.y})`);
132
+ }
133
+ })
134
+ .on("end", (event, d) => {
135
+ if (d.has_loaded_leaves) {
136
+ const g = d3.select(event.sourceEvent.target.closest("g.node"));
137
+ const w = this.main.cell_side;
138
+ const dx = event.x - d.x;
139
+ const d_col = Math.round(dx / w);
140
+ move_tree(d, d_col);
141
+ const time = d3.transition().duration(500);
142
+ g.transition(time).attr(
143
+ "transform",
144
+ (p) => `translate(${p.x},${p.y})`
145
+ );
146
+ }
147
+ })
148
+ )
149
+ .each((d, i, c) => this.draw_node(d, i, c));
150
+ }
151
+
152
+ draw_node(node, i, context) {
153
+ const g = d3.select(context[i]);
154
+ g.append("circle");
155
+ // g.attr("transform", d => "translate(" + d.x + "," + d.y + ")scale(0)")
156
+ g.attr("transform", (d) => `translate(${d.x},${d.y})scale(0)`)
157
+ .transition(this.t)
158
+ .delay(300)
159
+ .attr("transform", (d) => `translate(${d.x},${d.y})scale(1)`);
160
+
161
+ g.append("text")
162
+ .attr("class", "label-leaves")
163
+ .attr("x", -this.r)
164
+ .attr("y", -this.r)
165
+ .style("text-anchor", "end")
166
+ .text((d) =>
167
+ d.data.number_of_leaves > 1 ? d.data.number_of_leaves : ""
168
+ );
169
+
170
+ g.append("text")
171
+ .attr("class", "label-species")
172
+ .attr("x", this.r)
173
+ .attr("y", this.r)
174
+ .style("text-anchor", "start")
175
+ .attr("transform", (d) =>
176
+ d.children || d._children
177
+ ? `translate(0,${-this.r - 2})`
178
+ : "rotate(-70)"
179
+ )
180
+ .style("fill", (d) => (d.data.isFromFile ? "darkred" : null))
181
+ .text((d) => {
182
+ let label = "";
183
+ if (d.label !== "ROOT") {
184
+ if (d.children || d._children) label = d.label;
185
+ else {
186
+ switch (this.main.tax_label_type) {
187
+ case "id":
188
+ label = d.data.taxid;
189
+ break;
190
+ case "both":
191
+ label = `${d.data.taxid}: ${d.label}`;
192
+ break;
193
+ default:
194
+ break;
195
+ }
196
+ }
197
+ }
198
+ return label;
199
+ });
200
+
201
+ g.append("path")
202
+ .attr("class", "node-type")
203
+ .attr("fill", "white")
204
+ .on("click", (event, d) => {
205
+ if (d.data.loaded)
206
+ this.main.dipatcher.call(
207
+ "removeSpacies",
208
+ this.main,
209
+ event,
210
+ d.data.taxid
211
+ );
212
+ });
213
+
214
+ this.update_node(node, i, context);
215
+ }
216
+
217
+ update_node(node, i, context) {
218
+ const g = d3.select(context[i]);
219
+ const { r } = this;
220
+
221
+ g.selectAll("circle").attr("r", (d) => {
222
+ if (d.children || d._children) return r;
223
+ if (d.data.loaded) return r / 2;
224
+ return r - 2;
225
+ });
226
+
227
+ g.selectAll(".node-type")
228
+ .attr("d", (d) => {
229
+ const s = d3.symbol().size((4 * r * r) / 5);
230
+ if (d.data.loaded) {
231
+ s.type(d3.symbolCross);
232
+ return s();
233
+ }
234
+ if (d.children || d._children) {
235
+ s.type(d.data.expanded ? d3.symbolTriangle : d3.symbolCross);
236
+ } else {
237
+ s.type(d3.symbolCircle);
238
+ }
239
+ return s();
240
+ })
241
+ .attr("transform", (d) => (d.data.loaded ? "rotate(45)" : null))
242
+ .attr("fill", (d) => (d.data.loaded ? "rgb(183, 83, 84)" : "white"));
243
+ g.selectAll(".label-species")
244
+ .style("pointer-events", (d) => (d.data.loaded ? "auto" : "none"))
245
+ .style("user-select", (d) => (d.data.loaded ? "auto" : "none"))
246
+ .text((d) => {
247
+ let label = "";
248
+ if (d.label !== "ROOT") {
249
+ label = d.label;
250
+ if (d.data.taxid) {
251
+ switch (this.main.tax_label_type) {
252
+ case "id":
253
+ label = d.data.taxid;
254
+ break;
255
+ case "both":
256
+ label = `${d.data.taxid}: ${d.label}`;
257
+ break;
258
+ default:
259
+ break;
260
+ }
261
+ }
262
+ }
263
+ return label;
264
+ });
265
+ }
266
+ }
@@ -0,0 +1,86 @@
1
+ export default class TaxonomySortButton {
2
+ constructor({
3
+ x = 0,
4
+ y = 0,
5
+ centerX = 17,
6
+ top = 30,
7
+ r = 10,
8
+ function_sort = null,
9
+ // padding = 3,
10
+ // scrollH = 40,
11
+ // scrollW = 10,
12
+ container = null,
13
+ // domain = [0, 100]
14
+ }) {
15
+ this.x = x;
16
+ this.y = y;
17
+ this.r = r;
18
+ this.centerX = centerX;
19
+ this.top = top;
20
+ this.container = container;
21
+ this.modes = ["tree1", "tree2", "tax_id", "org_name"];
22
+ this.texts = ["", "", "0 9", "A Z"];
23
+ this.function_sort = function_sort;
24
+ this.currentMode = 0;
25
+ }
26
+
27
+ draw() {
28
+ this.group = this.container.append("g").attr("class", "gpv-sorter");
29
+ this.group
30
+ .append("circle")
31
+ .attr("cx", this.centerX)
32
+ .attr("cy", this.top)
33
+ .attr("r", this.r)
34
+ .on("click", () => {
35
+ this.currentMode = (this.currentMode + 1) % this.modes.length;
36
+ this.function_sort(this.modes[this.currentMode]);
37
+ this.fakeData.sort((a, b) =>
38
+ a[this.modes[this.currentMode]] > b[this.modes[this.currentMode]]
39
+ ? 1
40
+ : -1
41
+ );
42
+ this.refresh();
43
+ });
44
+ this.text = this.group
45
+ .append("text")
46
+ .style("font-size", "0.8em")
47
+ .attr("x", this.centerX)
48
+ .attr("y", this.top);
49
+
50
+ this.fakeData = [
51
+ { id: 1, tree1: 1, tree2: 4, tax_id: 2, org_name: 4 },
52
+ { id: 2, tree1: 2, tree2: 3, tax_id: 4, org_name: 1 },
53
+ { id: 3, tree1: 3, tree2: 2, tax_id: 1, org_name: 3 },
54
+ { id: 4, tree1: 4, tree2: 1, tax_id: 3, org_name: 2 },
55
+ ];
56
+
57
+ this.refresh();
58
+ }
59
+
60
+ refresh() {
61
+ this.group.attr("transform", `translate(${this.x}, ${this.y})`);
62
+ this.bars = this.group
63
+ .selectAll("line.bar")
64
+ .data(this.fakeData, (d) => d.id);
65
+ const barBottom = this.top * 1.2;
66
+ const barMaxH = 12;
67
+ this.bars
68
+ .enter()
69
+ .append("line")
70
+ .attr("class", "bar")
71
+ .attr("y1", barBottom)
72
+ .attr("y2", (d) => barBottom - barMaxH * (d.id / this.modes.length))
73
+ .merge(this.bars)
74
+ // .transition()
75
+ .attr(
76
+ "opacity",
77
+ this.modes[this.currentMode].indexOf("tree") >= 0 ? 0.7 : 0.2
78
+ )
79
+ .attr("x1", (d, i) => ((i + 3.6) * this.r) / 3)
80
+ .attr("x2", (d, i) => ((i + 3.6) * this.r) / 3);
81
+
82
+ this.text
83
+ // .transition()
84
+ .text(this.texts[this.currentMode]);
85
+ }
86
+ }