@microbiome-informatics/genome-properties-viewer 1.0.0-beta.1 → 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.
package/src/gp-totals.js CHANGED
@@ -1,17 +1,16 @@
1
1
  import * as d3 from "./d3";
2
2
 
3
3
  export const drawTotalPerOrganismPanel = (viewer) => {
4
- // const ph = viewer.options.total_panel_height;
5
4
  viewer.total_g = viewer.mainGroup
6
5
  .append("g")
7
6
  .attr("class", "total-group")
8
- .attr(
9
- "transform",
10
- `translate(${
11
- viewer.options.width - viewer.options.dimensions.total.short_side / 2
12
- }, ${viewer.options.dimensions.total.short_side / 2}
13
- )`
14
- );
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");
15
14
  };
16
15
 
17
16
  export const refreshGPTotals = (viewer) => {
@@ -40,13 +39,18 @@ const refreshOrganismTotals = (viewer) => {
40
39
  export const updateTotalPerOrganismPanel = (viewer) => {
41
40
  viewer.options.total_panel_height = viewer.options.cell_side;
42
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
+ );
43
48
 
44
49
  viewer.total_g.attr(
45
50
  "transform",
46
51
  `translate(${
47
- viewer.options.width - viewer.options.dimensions.total.short_side / 2
48
- }, ${viewer.options.dimensions.total.short_side / 2}
49
- )`
52
+ viewer.heatmapAnchor + viewer.current_scroll.x
53
+ }, ${heatmapBottom})`
50
54
  );
51
55
 
52
56
  const arc_f = d3
@@ -68,14 +72,17 @@ export const updateTotalPerOrganismPanel = (viewer) => {
68
72
  (d) => d.key
69
73
  );
70
74
 
71
- const newY = d3
75
+ const newX = d3
72
76
  .scaleBand()
73
77
  .range([0, viewer.organisms.length * viewer.options.cell_side])
74
- .domain(viewer.current_order);
78
+ .domain(viewer.current_order || []);
75
79
 
76
80
  cells_t.attr(
77
81
  "transform",
78
- (d, i) => `translate(0, ${newY(i) + viewer.options.cell_side})`
82
+ (d, i) =>
83
+ `translate(${newX(i) + viewer.options.cell_side / 2}, ${
84
+ viewer.options.cell_side / 2
85
+ })`
79
86
  );
80
87
  cells_t.exit().remove();
81
88
 
@@ -85,7 +92,10 @@ export const updateTotalPerOrganismPanel = (viewer) => {
85
92
  .attr("class", "total_cell_org")
86
93
  .attr(
87
94
  "transform",
88
- (d, i) => `translate(0, ${newY(i) + viewer.options.cell_side})`
95
+ (d, i) =>
96
+ `translate(${newX(i) + viewer.options.cell_side / 2}, ${
97
+ viewer.options.cell_side / 2
98
+ })`
89
99
  )
90
100
  .on("mouseover", (event, p) => {
91
101
  d3.selectAll(".node--leaf text").classed(
@@ -115,3 +125,85 @@ export const updateTotalPerOrganismPanel = (viewer) => {
115
125
  .attr("d", arc_f)
116
126
  .style("fill", (d) => viewer.c[d.data.key]);
117
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
+ };
@@ -5,6 +5,9 @@ import * as d3 from "./d3";
5
5
  * @param {GernomeProperiesViewer} viewer - The instance of the genome properites viewer
6
6
  */
7
7
  export const createGradient = (viewer) => {
8
+ const uid = `gpv-${Math.random().toString(36).slice(2, 8)}`;
9
+ viewer.heatmapClipId = `${uid}-heatmap`;
10
+ viewer.treeClipId = `${uid}-tree`;
8
11
  const defs = viewer.mainGroup.append("defs");
9
12
  const gradient_left = defs
10
13
  .append("linearGradient")
@@ -23,6 +26,23 @@ export const createGradient = (viewer) => {
23
26
  .attr("offset", "100%")
24
27
  .attr("stop-color", "#fff")
25
28
  .attr("stop-opacity", 0.5);
29
+ const gradient_top = defs
30
+ .append("linearGradient")
31
+ .attr("id", "gradienttop")
32
+ .attr("x1", "0%")
33
+ .attr("y1", "0%")
34
+ .attr("x2", "0%")
35
+ .attr("y2", "100%");
36
+ gradient_top
37
+ .append("stop")
38
+ .attr("offset", "85%")
39
+ .attr("stop-color", "#fff")
40
+ .attr("stop-opacity", 1);
41
+ gradient_top
42
+ .append("stop")
43
+ .attr("offset", "100%")
44
+ .attr("stop-color", "#fff")
45
+ .attr("stop-opacity", 0.5);
26
46
  const gradient_right = defs
27
47
  .append("linearGradient")
28
48
  .attr("id", "gradientright")
@@ -45,10 +65,50 @@ export const createGradient = (viewer) => {
45
65
  .attr("offset", "35%")
46
66
  .attr("stop-color", "#fff")
47
67
  .attr("stop-opacity", 1);
68
+ const gradient_bottom = defs
69
+ .append("linearGradient")
70
+ .attr("id", "gradientbottom")
71
+ .attr("x1", "0%")
72
+ .attr("y1", "0%")
73
+ .attr("x2", "0%")
74
+ .attr("y2", "100%");
75
+ gradient_bottom
76
+ .append("stop")
77
+ .attr("offset", "0%")
78
+ .attr("stop-color", "#fff")
79
+ .attr("stop-opacity", 0);
80
+ gradient_bottom
81
+ .append("stop")
82
+ .attr("offset", "15%")
83
+ .attr("stop-color", "#fff")
84
+ .attr("stop-opacity", 0.5);
85
+ gradient_bottom
86
+ .append("stop")
87
+ .attr("offset", "35%")
88
+ .attr("stop-color", "#fff")
89
+ .attr("stop-opacity", 1);
90
+
91
+ viewer.heatmapClipPath = defs
92
+ .append("clipPath")
93
+ .attr("id", viewer.heatmapClipId);
94
+ viewer.heatmapClipPath
95
+ .append("rect")
96
+ .attr("x", viewer.heatmapLeft)
97
+ .attr("y", viewer.treeHeight)
98
+ .attr("width", Math.max(1, viewer.heatmapWidth()))
99
+ .attr("height", Math.max(1, viewer.heatmapHeight()));
100
+ viewer.treeClipPath = defs.append("clipPath").attr("id", viewer.treeClipId);
101
+ viewer.treeClipPath
102
+ .append("rect")
103
+ .attr("x", 0)
104
+ .attr("y", 0)
105
+ .attr("width", Math.max(1, viewer.options.width))
106
+ .attr("height", Math.max(1, viewer.treeHeight));
48
107
  };
108
+
49
109
  /**
50
110
  * Appends a new group element into the mainGroup of the viewer.
51
- * The group contains masks as rectangles to give the effect of new GP fading-in/out while horizontal scrolling
111
+ * The group contains masks as rectangles to give the effect of fading around the heatmap
52
112
  * @param {GernomeProperiesViewer} viewer - The instance of the genome properites viewer
53
113
  */
54
114
  export const drawMasks = (viewer) => {
@@ -56,10 +116,18 @@ export const drawMasks = (viewer) => {
56
116
  viewer.masks
57
117
  .append("rect")
58
118
  .attr("class", "tree-background background")
119
+ .style("fill", "url(#gradienttop)")
120
+ .attr("x", 0)
121
+ .attr("y", 0)
122
+ .attr("width", viewer.options.width)
123
+ .attr("height", viewer.treeHeight);
124
+ viewer.masks
125
+ .append("rect")
126
+ .attr("class", "label-background background")
59
127
  .style("fill", "url(#gradientleft)")
60
128
  .attr("x", 0)
61
129
  .attr("y", 0)
62
- .attr("width", viewer.options.dimensions.tree.width)
130
+ .attr("width", viewer.labelWidth)
63
131
  .attr("height", viewer.options.height);
64
132
  viewer.masks
65
133
  .append("rect")
@@ -72,12 +140,32 @@ export const drawMasks = (viewer) => {
72
140
  .attr("y", 0)
73
141
  .attr("width", viewer.options.dimensions.total.short_side * 1.2)
74
142
  .attr("height", viewer.options.height);
143
+ viewer.masks
144
+ .append("rect")
145
+ .attr("class", "bottom-background background")
146
+ .style("fill", "url(#gradientbottom)")
147
+ .attr("x", 0)
148
+ .attr(
149
+ "y",
150
+ viewer.options.height - viewer.options.dimensions.total.short_side * 1.2
151
+ )
152
+ .attr("width", viewer.options.width)
153
+ .attr("height", viewer.options.dimensions.total.short_side * 1.2);
75
154
  };
155
+
76
156
  /**
77
157
  * Update the size and position of the masks
78
158
  * @param {GernomeProperiesViewer} viewer - The instance of the genome properites viewer
79
159
  */
80
160
  export const updateMasks = (viewer) => {
161
+ viewer.masks
162
+ .select(".tree-background")
163
+ .attr("width", viewer.options.width)
164
+ .attr("height", viewer.treeHeight);
165
+ viewer.masks
166
+ .select(".label-background")
167
+ .attr("width", viewer.heatmapAnchor)
168
+ .attr("height", viewer.options.height);
81
169
  viewer.masks
82
170
  .select(".total-background")
83
171
  .attr(
@@ -87,64 +175,87 @@ export const updateMasks = (viewer) => {
87
175
  .attr("width", viewer.options.dimensions.total.short_side * 1.2)
88
176
  .attr("height", viewer.options.height);
89
177
  viewer.masks
90
- .select(".tree-background")
91
- .attr("y", 0)
92
- .attr("width", viewer.options.dimensions.tree.width)
93
- .attr("height", viewer.options.height);
178
+ .select(".bottom-background")
179
+ .attr(
180
+ "y",
181
+ viewer.options.height - viewer.options.dimensions.total.short_side * 1.2
182
+ )
183
+ .attr("width", viewer.options.width)
184
+ .attr("height", viewer.options.dimensions.total.short_side * 1.2);
185
+ if (viewer.heatmapClipPath) {
186
+ viewer.heatmapClipPath
187
+ .select("rect")
188
+ .attr("x", viewer.heatmapLeft)
189
+ .attr("y", viewer.heatmapTop)
190
+ .attr("width", viewer.heatmapWidth())
191
+ .attr("height", viewer.heatmapHeight());
192
+ }
193
+ if (viewer.treeClipPath) {
194
+ viewer.treeClipPath
195
+ .select("rect")
196
+ .attr("x", 0)
197
+ .attr("y", 0)
198
+ .attr("width", viewer.options.width)
199
+ .attr("height", viewer.treeHeight);
200
+ }
201
+ viewer.mainGroup
202
+ .select(".height-dragger")
203
+ .attr("transform", `translate(0, ${viewer.treeHeight})`);
204
+ viewer.mainGroup.select(".height-sizer").attr("x2", viewer.options.width);
94
205
  };
95
206
 
96
207
  /**
97
- * Draws a draggable area ||| to redifine the widthassigned to the tree.
208
+ * Draws a draggable area to redefine the height assigned to the taxonomy tree.
98
209
  * @param {GernomeProperiesViewer} viewer - The instance of the genome properites viewer
99
210
  */
100
211
  export const drawDragArea = (viewer) => {
101
- const xLimit = 90;
102
- let dx = 0;
212
+ const yLimit = 80;
213
+ let dy = 0;
103
214
  const g = viewer.mainGroup
104
215
  .append("g")
105
216
  .attr("class", "height-dragger")
106
- .attr("transform", `translate(${viewer.options.dimensions.tree.width}, 0)`)
217
+ .attr("transform", `translate(0, ${viewer.treeHeight})`)
107
218
  .call(
108
219
  d3
109
220
  .drag()
110
221
  .on("drag", (event) => {
111
222
  const treeSpace = viewer.options.dimensions.tree.width;
112
- // Forces limits for the drag
113
- dx = Math.min(
114
- Math.max(-treeSpace + event.x, xLimit - treeSpace),
115
- viewer.options.width - treeSpace
223
+ dy = Math.min(
224
+ Math.max(-treeSpace + event.y, yLimit - treeSpace),
225
+ viewer.options.height - treeSpace - 80
116
226
  );
117
- g.attr("transform", `translate(${dx + treeSpace}, 0)`);
227
+ g.attr("transform", `translate(0, ${dy + treeSpace})`);
118
228
  })
119
229
  .on("end", () => {
120
230
  const treeSpace = viewer.options.dimensions.tree.width;
121
- const new_width = treeSpace + dx;
122
- if (Number.isNaN(new_width)) return;
231
+ const new_height = treeSpace + dy;
232
+ if (Number.isNaN(new_height)) return;
123
233
  viewer.gp_taxonomy.dipatcher.call(
124
234
  "changeWidth",
125
235
  viewer.gp_taxonomy,
126
- new_width
236
+ new_height
127
237
  );
128
- g.attr("transform", `translate(${new_width}, 0)`);
129
- viewer.gp_taxonomy.width = new_width;
238
+ g.attr("transform", `translate(0, ${new_height})`);
239
+ viewer.gp_taxonomy.height = new_height;
130
240
  viewer.gp_taxonomy.update_tree();
131
241
  })
132
242
  );
133
243
  const side = viewer.options.cell_side / 2;
134
244
  g.append("rect")
135
- .attr("x", -side / 2)
136
- .attr("y", 0)
137
- .attr("width", side)
138
- .attr("height", side * 2)
245
+ .attr("x", 0)
246
+ .attr("y", -side / 2)
247
+ .attr("width", side * 2)
248
+ .attr("height", side)
139
249
  .style("fill", "transparent");
140
250
  for (let index = -1; index < 2; index++) {
141
251
  g.append("line")
142
- .attr("y1", side * 2)
143
- .attr("x1", (index * side) / 2)
144
- .attr("x2", (index * side) / 2)
252
+ .attr("x1", 0)
253
+ .attr("x2", side * 2)
254
+ .attr("y1", (index * side) / 2)
255
+ .attr("y2", (index * side) / 2)
145
256
  .style("stroke", "#333");
146
257
  }
147
258
  g.append("line")
148
259
  .attr("class", "height-sizer")
149
- .attr("y1", viewer.options.height);
260
+ .attr("x2", viewer.options.width);
150
261
  };
@@ -152,14 +152,18 @@ export class FileGetter {
152
152
  this.viewer = viewer;
153
153
  this.activeGauge = 0;
154
154
  this.activeGauges = [];
155
- setInterval(
156
- (_this) => {
157
- _this.activeGauge =
158
- (_this.activeGauge + 1) % Object.values(_this.activeGauges).length;
159
- },
160
- 3000,
161
- this
162
- );
155
+ this.progressTimer = setInterval(() => {
156
+ const n = this.activeGauges.length;
157
+ if (n) this.activeGauge = (this.activeGauge + 1) % n;
158
+ }, 3000);
159
+ }
160
+
161
+ destroy() {
162
+ this.isActive = false;
163
+ if (this.progressTimer) {
164
+ clearInterval(this.progressTimer);
165
+ this.progressTimer = null;
166
+ }
163
167
  }
164
168
 
165
169
  getJSON(path) {
@@ -176,6 +180,7 @@ export class FileGetter {
176
180
  if (!this.isActive) this.createProgressContent(modal);
177
181
  // this.activeGauge = path;
178
182
  const response = await fetch(path);
183
+ if (this.viewer.destroyed) return null;
179
184
  this.files[path].request = response;
180
185
  const total = response.headers.get("content-length");
181
186
  const reader = response.body && response.body.getReader();
@@ -192,6 +197,7 @@ export class FileGetter {
192
197
  // eslint-disable-next-line no-await-in-loop
193
198
  const { done, value } = await reader.read();
194
199
  if (done) break;
200
+ if (this.viewer.destroyed) return null;
195
201
  loaded += value.length;
196
202
  this.files[path].progress = total ? loaded / total : null;
197
203
  this.files[path].event = {
@@ -273,10 +279,13 @@ export class FileGetter {
273
279
  `
274
280
  );
275
281
 
276
- const w = this.gaugeSVG.node().getBoundingClientRect().width;
282
+ const gaugeNode = this.gaugeSVG && this.gaugeSVG.node();
283
+ if (!gaugeNode) return;
284
+ const w = gaugeNode.getBoundingClientRect().width || 0;
285
+ if (w <= 0) return;
277
286
 
278
287
  const gauges = this.gaugeSVG.selectAll("g.gauge").data(this.activeGauges);
279
- const current = this.activeGauge;
288
+ const current = Number.isFinite(this.activeGauge) ? this.activeGauge : 0;
280
289
 
281
290
  const gauge = gauges
282
291
  .enter()
@@ -284,7 +293,7 @@ export class FileGetter {
284
293
  .attr("class", "gauge")
285
294
  .attr("transform", (d, i) => `translate(0,${w * (i - current)})`);
286
295
 
287
- const r = w / 2 - 10;
296
+ const r = Math.max(0, w / 2 - 10);
288
297
 
289
298
  const circunferencia = 2 * Math.PI * r;
290
299
  gauge