@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.
- package/LICENSE +201 -0
- package/README.md +63 -2
- package/bin/d3.custom.min.js +9424 -0
- package/gp-style.css +528 -0
- package/index.js +30 -0
- package/package.json +74 -4
- package/src/d3.js +28 -0
- package/src/gp-controller.js +282 -0
- package/src/gp-filters.js +74 -0
- package/src/gp-hierarchy.js +143 -0
- package/src/gp-scroller.js +197 -0
- package/src/gp-steps.js +218 -0
- package/src/gp-tax-node.js +266 -0
- package/src/gp-taxonomy-sorter.js +86 -0
- package/src/gp-taxonomy.js +387 -0
- package/src/gp-totals.js +209 -0
- package/src/gp-ui-utils.js +261 -0
- package/src/gp-uploader.js +402 -0
- package/src/gp-viewer.js +734 -0
- package/src/modal.js +56 -0
- package/src/zoomer.js +128 -0
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import * as d3 from "./d3";
|
|
2
|
+
import { transformByScroll } from "./gp-scroller";
|
|
3
|
+
|
|
4
|
+
export default class GenomePropertiesController {
|
|
5
|
+
constructor({
|
|
6
|
+
gp_element_selector = null,
|
|
7
|
+
legends_element_selector = null,
|
|
8
|
+
tooltip_selector = ".info-tooltip",
|
|
9
|
+
gp_text_filter_selector = null,
|
|
10
|
+
gp_viewer = null,
|
|
11
|
+
gp_taxonomy = null,
|
|
12
|
+
gp_label_selector = null,
|
|
13
|
+
tax_label_selector = null,
|
|
14
|
+
tax_search_selector = null,
|
|
15
|
+
hierarchy_contorller = null,
|
|
16
|
+
width = 400,
|
|
17
|
+
}) {
|
|
18
|
+
this.gp_viewer = gp_viewer;
|
|
19
|
+
this.hierarchy_contorller = hierarchy_contorller;
|
|
20
|
+
this.gp_taxonomy = gp_taxonomy;
|
|
21
|
+
this.width = width;
|
|
22
|
+
this.dipatcher = d3.dispatch("textFilterChanged", "legendFilterChanged");
|
|
23
|
+
|
|
24
|
+
if (gp_element_selector) {
|
|
25
|
+
this.gp_component = d3.select(gp_element_selector);
|
|
26
|
+
|
|
27
|
+
if (this.hierarchy_contorller.root) this.draw_hierarchy_selector();
|
|
28
|
+
else
|
|
29
|
+
this.hierarchy_contorller.on("hierarchyLoaded", () =>
|
|
30
|
+
this.draw_hierarchy_selector()
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (legends_element_selector) {
|
|
35
|
+
const host = d3.select(legends_element_selector);
|
|
36
|
+
host.selectAll("ul").remove();
|
|
37
|
+
this.legends_component = host.append("ul");
|
|
38
|
+
this.draw_legends();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (tooltip_selector) {
|
|
42
|
+
this.tooltip_selector = tooltip_selector;
|
|
43
|
+
d3.select(tooltip_selector).selectAll("ul").remove();
|
|
44
|
+
this.draw_tooltip(null, null, true);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
this.text_filter = "";
|
|
48
|
+
if (gp_text_filter_selector) {
|
|
49
|
+
d3.select(gp_text_filter_selector).on("keyup", (event) => {
|
|
50
|
+
this.text_filter =
|
|
51
|
+
event.currentTarget.value.length > 2 ? event.currentTarget.value : "";
|
|
52
|
+
if (this.text_filter !== this.gp_viewer.filter_text) {
|
|
53
|
+
this.gp_viewer.filter_text = this.text_filter;
|
|
54
|
+
this.moveScrollUp();
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
if (gp_label_selector) {
|
|
59
|
+
d3.select(gp_label_selector).on("change", (event) => {
|
|
60
|
+
this.gp_viewer.change_gp_label(event.currentTarget.value);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
if (tax_label_selector && gp_taxonomy) {
|
|
64
|
+
d3.select(tax_label_selector).on("change", (event) => {
|
|
65
|
+
this.gp_taxonomy.change_tax_label(event.currentTarget.value);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (tax_search_selector && gp_taxonomy) {
|
|
69
|
+
this.search_options = [];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
moveScrollUp() {
|
|
74
|
+
this.gp_viewer.current_scroll.y = 0;
|
|
75
|
+
transformByScroll(this.gp_viewer);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
loadSearchOptions() {
|
|
79
|
+
this.search_options = this.gp_taxonomy.organisms.map(
|
|
80
|
+
(e) => `${e}: ${this.gp_taxonomy.nodes[e].name}`
|
|
81
|
+
);
|
|
82
|
+
// this.search_options.splice(0,0,...this.search_options.map(e=>this.gp_taxonomy.nodes[e].species))
|
|
83
|
+
// this.search_options = this.search_options.map(String);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
draw_tooltip(event, items = null, first_time = false, header = null) {
|
|
87
|
+
const parent = d3.select(this.tooltip_selector);
|
|
88
|
+
|
|
89
|
+
if (header) parent.insert("header", ":first-child").text(header);
|
|
90
|
+
else parent.select("header").remove();
|
|
91
|
+
|
|
92
|
+
if (first_time) this.tooltip_component = parent.append("ul");
|
|
93
|
+
|
|
94
|
+
parent.style("visibility", items == null ? "hidden" : "visible");
|
|
95
|
+
const info_item = this.tooltip_component
|
|
96
|
+
.selectAll("li")
|
|
97
|
+
.data(d3.entries(items), (d) => d.key);
|
|
98
|
+
info_item.exit().remove();
|
|
99
|
+
const li_e = info_item.enter().append("li");
|
|
100
|
+
li_e
|
|
101
|
+
.append("div")
|
|
102
|
+
.attr("class", "label")
|
|
103
|
+
.text((d) => d.key.toLowerCase());
|
|
104
|
+
li_e
|
|
105
|
+
.append("div")
|
|
106
|
+
.attr("class", "content")
|
|
107
|
+
.text((d) => d.value);
|
|
108
|
+
|
|
109
|
+
if (event) {
|
|
110
|
+
// const h = parent.node().getBoundingClientRect().height;
|
|
111
|
+
const top = this.gp_viewer.options.cell_side / 2 + event.pageY;
|
|
112
|
+
let left = Math.max(event.pageX - this.width / 2, 0);
|
|
113
|
+
if (left + this.width > this.gp_viewer.options.width)
|
|
114
|
+
left = this.gp_viewer.options.width - this.width;
|
|
115
|
+
|
|
116
|
+
parent
|
|
117
|
+
.style("width", `${this.width}px`)
|
|
118
|
+
.style("top", `${top}px`)
|
|
119
|
+
.style("left", `${left}px`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
draw_legends(total = { YES: 0, NO: 0, PARTIAL: 0 }) {
|
|
124
|
+
const legend_item = this.legends_component.selectAll("li").data(
|
|
125
|
+
d3.entries(total).sort((a, b) => (a.key > b.key ? -1 : 1)),
|
|
126
|
+
(d) => d.key
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
const legends_filter = { YES: "", NO: "", PARTIAL: "" };
|
|
130
|
+
const filter_symbols = ["", "∀", "∃", "∄"];
|
|
131
|
+
|
|
132
|
+
legend_item
|
|
133
|
+
.select(".color>div")
|
|
134
|
+
.html((d) => (d.value > 0 ? d.value : " "));
|
|
135
|
+
|
|
136
|
+
const li_e = legend_item.enter().append("li");
|
|
137
|
+
|
|
138
|
+
li_e.append("label").text((d) => d.key.toLowerCase());
|
|
139
|
+
li_e
|
|
140
|
+
.append("div")
|
|
141
|
+
.attr("class", "color")
|
|
142
|
+
.style("background", (d) => this.gp_viewer.c[d.key])
|
|
143
|
+
.style("color", (d) =>
|
|
144
|
+
d.key === "NO" ? "rgb(49, 130, 189)" : "rgb(230,230,230)"
|
|
145
|
+
)
|
|
146
|
+
.style("cursor", "pointer")
|
|
147
|
+
.attr("type", "")
|
|
148
|
+
.on("click", (event, d) => {
|
|
149
|
+
const e = d3.select(event.currentTarget); // Element
|
|
150
|
+
const cu = filter_symbols.indexOf(e.attr("type")); // Current
|
|
151
|
+
const n = (cu + 1) % filter_symbols.length; // Next
|
|
152
|
+
|
|
153
|
+
e.classed("filter", filter_symbols[n] !== "").attr(
|
|
154
|
+
"type",
|
|
155
|
+
filter_symbols[n]
|
|
156
|
+
);
|
|
157
|
+
legends_filter[d.key] = filter_symbols[n];
|
|
158
|
+
this.moveScrollUp();
|
|
159
|
+
this.dipatcher.call("legendFilterChanged", this, legends_filter);
|
|
160
|
+
})
|
|
161
|
+
.on("mouseover", (event, d) =>
|
|
162
|
+
this.draw_tooltip(
|
|
163
|
+
event,
|
|
164
|
+
{
|
|
165
|
+
"∀": `All the species in the row have the value (${d.key})`,
|
|
166
|
+
"∃": `There is at least one species in each row with the value (${d.key})`,
|
|
167
|
+
"∄": `There is not a single species in each row with the value (${d.key})`,
|
|
168
|
+
},
|
|
169
|
+
false,
|
|
170
|
+
"Click in this area to apply one of the following filters"
|
|
171
|
+
)
|
|
172
|
+
)
|
|
173
|
+
.on("mouseout", () => this.draw_tooltip())
|
|
174
|
+
.append("div")
|
|
175
|
+
.style("margin", "0 auto")
|
|
176
|
+
.style("padding", "2px");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
draw_hierarchy_selector() {
|
|
180
|
+
this.gp_component.html(`
|
|
181
|
+
<div class='current_status'>All</div>
|
|
182
|
+
<div class='options'>
|
|
183
|
+
<a class="all">All</a> | <a class="none">None</a><br/>
|
|
184
|
+
<ul>
|
|
185
|
+
</ul>
|
|
186
|
+
</div>`);
|
|
187
|
+
|
|
188
|
+
this.gp_component
|
|
189
|
+
.select(".options .all")
|
|
190
|
+
.on("click", () => this.update("ALL"));
|
|
191
|
+
this.gp_component
|
|
192
|
+
.select(".options .none")
|
|
193
|
+
.on("click", () => this.update("NONE"));
|
|
194
|
+
|
|
195
|
+
const tll = this.gp_component
|
|
196
|
+
.select(".options ul")
|
|
197
|
+
.selectAll(".top-level-option")
|
|
198
|
+
.data(this.hierarchy_contorller.hierarchy_switch, (d) => d.id);
|
|
199
|
+
|
|
200
|
+
const li = tll.enter().append("li").attr("class", "top-level-option");
|
|
201
|
+
li.append("div")
|
|
202
|
+
.style("width", "0.8em")
|
|
203
|
+
.style("height", "0.8em")
|
|
204
|
+
.style("margin-right", "5px")
|
|
205
|
+
.style("display", "inline-block")
|
|
206
|
+
.style("background", (d) => this.hierarchy_contorller.color(d.id))
|
|
207
|
+
.style(
|
|
208
|
+
"border",
|
|
209
|
+
(d) => `2px solid ${this.hierarchy_contorller.color(d.id)}`
|
|
210
|
+
)
|
|
211
|
+
.style("border-radius", "50%")
|
|
212
|
+
.on("click", (event, d) => this.update(d));
|
|
213
|
+
|
|
214
|
+
li.append("a")
|
|
215
|
+
.text((d) => this.hierarchy_contorller.nodes[d.id].name)
|
|
216
|
+
.on("click", (event, d) => this.update(d));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
update(d) {
|
|
220
|
+
let selected = [];
|
|
221
|
+
this.moveScrollUp();
|
|
222
|
+
if (d === "ALL" || d === "NONE") {
|
|
223
|
+
this.hierarchy_contorller.hierarchy_switch.forEach(
|
|
224
|
+
(e) => (e.enable = d === "ALL")
|
|
225
|
+
);
|
|
226
|
+
this.hierarchy_contorller.dipatcher.call(
|
|
227
|
+
"siwtchChanged",
|
|
228
|
+
this,
|
|
229
|
+
this.hierarchy_switch
|
|
230
|
+
);
|
|
231
|
+
this.gp_component.select(".current_status").html(d.toLowerCase());
|
|
232
|
+
} else {
|
|
233
|
+
this.hierarchy_contorller.toggle_switch(d);
|
|
234
|
+
this.gp_component.select(".current_status").text("");
|
|
235
|
+
selected = this.hierarchy_contorller.hierarchy_switch.filter(
|
|
236
|
+
(e) => e.enable
|
|
237
|
+
);
|
|
238
|
+
if (selected.length === 0)
|
|
239
|
+
this.gp_component.select(".current_status").text("none");
|
|
240
|
+
else if (
|
|
241
|
+
selected.length === this.hierarchy_contorller.hierarchy_switch.length
|
|
242
|
+
)
|
|
243
|
+
this.gp_component.select(".current_status").text("all");
|
|
244
|
+
else {
|
|
245
|
+
const samples = this.gp_component
|
|
246
|
+
.select(".current_status")
|
|
247
|
+
.selectAll("div.sample")
|
|
248
|
+
.data(selected, (item) => item.id);
|
|
249
|
+
samples
|
|
250
|
+
.enter()
|
|
251
|
+
.append("div")
|
|
252
|
+
.attr("class", "sample")
|
|
253
|
+
.style("width", "0.8em")
|
|
254
|
+
.style("height", "0.8em")
|
|
255
|
+
.style("margin-left", "2px")
|
|
256
|
+
.style("display", "inline-block")
|
|
257
|
+
.style("background", (item) =>
|
|
258
|
+
this.hierarchy_contorller.color(item.id)
|
|
259
|
+
)
|
|
260
|
+
.style("border-radius", "50%");
|
|
261
|
+
samples.exit().remove();
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
this.gp_component
|
|
265
|
+
.select(".options ul")
|
|
266
|
+
.selectAll(".top-level-option div")
|
|
267
|
+
.style("background", (item) =>
|
|
268
|
+
d.enable ? this.hierarchy_contorller.color(item.id) : "#e3e3e3"
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
on(typename, callback) {
|
|
273
|
+
this.dipatcher.on(typename, callback);
|
|
274
|
+
return this;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
destroy() {
|
|
278
|
+
if (this.legends_component) this.legends_component.remove();
|
|
279
|
+
if (this.tooltip_component) this.tooltip_component.remove();
|
|
280
|
+
if (this.gp_component) this.gp_component.html("");
|
|
281
|
+
}
|
|
282
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import * as d3 from "./d3";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @summary A cell in the heatmap can have 1 of 3 values: Yes, No or partial. Indicating if there is evidence that the GP is present in a given species.
|
|
5
|
+
* Given multiple species, this function filters the list of genome properties to which currentluy selected species match the given condition, For example:
|
|
6
|
+
*
|
|
7
|
+
* |Value | Filter | JSON | Explanation |
|
|
8
|
+
* |---------|--------|--------------------|------------------------------------------------------|
|
|
9
|
+
* | YES | ∀ | `{"YES": "∀"}` | All the species have this GP |
|
|
10
|
+
* | NO | ∃ | `{"NO": "∃"}` | There is at least 1 species with this GP |
|
|
11
|
+
* | PARTIAL | ∄ | `{"PARTIAL": "∄"}` | None of the species have partial evidence of this GP |
|
|
12
|
+
*
|
|
13
|
+
* The current set of filters is in `viewer.legend_filters` and the filtered props will be saved in `viewer.props`.
|
|
14
|
+
* @param {GernomeProperiesViewer} viewer - The instance of the genome properites viewer
|
|
15
|
+
*/
|
|
16
|
+
export const filterByLegend = (viewer) => {
|
|
17
|
+
if (viewer.legend_filters) {
|
|
18
|
+
for (const x of Object.keys(viewer.legend_filters)) {
|
|
19
|
+
if (viewer.legend_filters[x] === "∀") {
|
|
20
|
+
viewer.props = viewer.props.filter((prop) => {
|
|
21
|
+
const values = d3
|
|
22
|
+
.entries(prop.values)
|
|
23
|
+
.filter((e) => viewer.organisms.indexOf(e.key) !== -1);
|
|
24
|
+
return values.filter((e) => e.value === x).length === values.length;
|
|
25
|
+
});
|
|
26
|
+
} else if (viewer.legend_filters[x] === "∃") {
|
|
27
|
+
viewer.props = viewer.props.filter((prop) => {
|
|
28
|
+
const values = d3
|
|
29
|
+
.entries(prop.values)
|
|
30
|
+
.filter((e) => viewer.organisms.indexOf(e.key) !== -1);
|
|
31
|
+
return values.filter((e) => e.value === x).length > 0;
|
|
32
|
+
});
|
|
33
|
+
} else if (viewer.legend_filters[x] === "∄") {
|
|
34
|
+
viewer.props = viewer.props.filter((prop) => {
|
|
35
|
+
const values = d3
|
|
36
|
+
.entries(prop.values)
|
|
37
|
+
.filter((e) => viewer.organisms.indexOf(e.key) !== -1);
|
|
38
|
+
return values.filter((e) => e.value === x).length === 0;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Genome Properties are organised in a hierarchy. By invoking this function, the list of properties in `viewer.props` is filtered
|
|
47
|
+
* by only including those whose `parent_top_properties` are included in `viewer.gp_hierarchy`
|
|
48
|
+
* @param {GernomeProperiesViewer} viewer - The instance of the genome properites viewer
|
|
49
|
+
*/
|
|
50
|
+
export const filterByHierarchy = (viewer) => {
|
|
51
|
+
viewer.props = viewer.props.filter((e) => {
|
|
52
|
+
if (e.parent_top_properties === null) return true;
|
|
53
|
+
for (const p of e.parent_top_properties)
|
|
54
|
+
for (const tp of viewer.gp_hierarchy.hierarchy_switch)
|
|
55
|
+
if (p === tp.id && tp.enable) return true;
|
|
56
|
+
return false;
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Filters the list of properties in `viewer.props` by checnking if their name includes the text in `viewer.filter_text`
|
|
62
|
+
* @param {GernomeProperiesViewer} viewer - The instance of the genome properites viewer
|
|
63
|
+
*/
|
|
64
|
+
export const filterByText = (viewer) => {
|
|
65
|
+
if (viewer.filter_text) {
|
|
66
|
+
viewer.props = viewer.props.filter(
|
|
67
|
+
(e) =>
|
|
68
|
+
e.name.toLowerCase().indexOf(viewer.filter_text.toLowerCase()) !== -1 ||
|
|
69
|
+
String(e.property)
|
|
70
|
+
.toLowerCase()
|
|
71
|
+
.indexOf(viewer.filter_text.toLowerCase()) !== -1
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import * as d3 from "./d3";
|
|
2
|
+
/**
|
|
3
|
+
* Genome Properties are organized in a hierarchy.
|
|
4
|
+
* This Class manages the file that defines that hierarchy and uses is to allow filtering over the heatmap
|
|
5
|
+
*/
|
|
6
|
+
class GenomePropertiesHierarchy {
|
|
7
|
+
/**
|
|
8
|
+
* Initializes all the class attributes.
|
|
9
|
+
*/
|
|
10
|
+
constructor() {
|
|
11
|
+
/**
|
|
12
|
+
* Hashmap object from nodeId to its object.
|
|
13
|
+
* @type {Object}
|
|
14
|
+
* */
|
|
15
|
+
this.nodes = {};
|
|
16
|
+
/**
|
|
17
|
+
* Reference to the root of the GP hierarchy
|
|
18
|
+
* @type {Object}
|
|
19
|
+
* */
|
|
20
|
+
this.root = null;
|
|
21
|
+
/**
|
|
22
|
+
* List of top level GP indicating if its enabled or not for filtering purposes
|
|
23
|
+
* @type {Array}
|
|
24
|
+
* */
|
|
25
|
+
this.hierarchy_switch = [];
|
|
26
|
+
/**
|
|
27
|
+
* Event dispatcher using `d3.dispatch`
|
|
28
|
+
* @type {Object}
|
|
29
|
+
* */
|
|
30
|
+
this.dipatcher = d3.dispatch("siwtchChanged", "hierarchyLoaded");
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Fetches the hierarchy from a given URL, and triggers the processing of the file.
|
|
36
|
+
* @param {String} path - The URL where the hierarchy file is.
|
|
37
|
+
* @return {GenomePropertiesHierarchy} The curent instance for chaining methods.
|
|
38
|
+
*/
|
|
39
|
+
load_hierarchy_from_path(path) {
|
|
40
|
+
fetch(path)
|
|
41
|
+
.then((response) => {
|
|
42
|
+
if (!response.ok)
|
|
43
|
+
throw new Error(`${response.status} ${response.statusText}`);
|
|
44
|
+
return response.json;
|
|
45
|
+
})
|
|
46
|
+
.then((data) => {
|
|
47
|
+
this.load_hierarchy_from_data(data);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Defines all the atrributes by processing the JSON object
|
|
55
|
+
* @param {Object} data - Object representing the root of the Hierachy
|
|
56
|
+
*/
|
|
57
|
+
load_hierarchy_from_data(data) {
|
|
58
|
+
this.root = data;
|
|
59
|
+
this.nodes = {};
|
|
60
|
+
this.add_node_recursively(this.root);
|
|
61
|
+
this.color = d3
|
|
62
|
+
.scaleOrdinal()
|
|
63
|
+
.domain(this.root.children.map((d) => d.id))
|
|
64
|
+
.range(d3.schemeCategory20b);
|
|
65
|
+
this.hierarchy_switch = this.root.children.map((d) => ({
|
|
66
|
+
id: d.id,
|
|
67
|
+
enable: true,
|
|
68
|
+
}));
|
|
69
|
+
this.dipatcher.call("hierarchyLoaded", this, this.root);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Wlaks the tree, adding each node in `this.nodes` and generating a list of parents(in `node.parents`) for each node.
|
|
74
|
+
* @param {Object} node - each node has the shape `{id: <String>, name: <String>, children: [<Node>]}`.
|
|
75
|
+
*/
|
|
76
|
+
add_node_recursively(node, parent = null) {
|
|
77
|
+
if (!node.parents) node.parents = [];
|
|
78
|
+
if (parent) node.parents.push(parent);
|
|
79
|
+
if (!this.nodes[node.id]) {
|
|
80
|
+
this.nodes[node.id] = node;
|
|
81
|
+
} else {
|
|
82
|
+
this.nodes[node.id].parents.splice(0, 0, ...node.parents);
|
|
83
|
+
}
|
|
84
|
+
if (node.children && node.children.length > 0)
|
|
85
|
+
for (const child of node.children) this.add_node_recursively(child, node);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Gets the node from `this.nodes` and searches it top level properties
|
|
90
|
+
* @param {String} id - node present in this.nodes
|
|
91
|
+
* @return {Array} Array of ids of the top level properties.
|
|
92
|
+
*/
|
|
93
|
+
get_top_level_gp_by_id(id) {
|
|
94
|
+
if (id in this.nodes)
|
|
95
|
+
return [...this.get_top_level_gp(this.nodes[id])].map((d) => d.id);
|
|
96
|
+
return [];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Gets the top level genome properties associated with a given node
|
|
101
|
+
* @param {Object} node - node present in this.nodes
|
|
102
|
+
* @return {Set} Set of top level genome properties.
|
|
103
|
+
*/
|
|
104
|
+
get_top_level_gp(node) {
|
|
105
|
+
if (node === this.root) return null;
|
|
106
|
+
if (node.top_level_gp) return node.top_level_gp;
|
|
107
|
+
if (node.parents.indexOf(this.root) !== -1) {
|
|
108
|
+
node.top_level_gp = new Set([node]);
|
|
109
|
+
return node.top_level_gp;
|
|
110
|
+
}
|
|
111
|
+
node.top_level_gp = new Set();
|
|
112
|
+
for (const parent of node.parents) {
|
|
113
|
+
node.top_level_gp = new Set([
|
|
114
|
+
...node.top_level_gp,
|
|
115
|
+
...this.get_top_level_gp(parent),
|
|
116
|
+
]);
|
|
117
|
+
}
|
|
118
|
+
return node.top_level_gp;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* finds the top level property with the given id, toggles the value of `enable`, and then dispatches an event announceing the change.
|
|
123
|
+
* @param {String} id - Id of the top level genome property
|
|
124
|
+
*/
|
|
125
|
+
toggle_switch(id) {
|
|
126
|
+
this.hierarchy_switch.forEach((e) => {
|
|
127
|
+
if (e.id === id.id) e.enable = !e.enable;
|
|
128
|
+
});
|
|
129
|
+
this.dipatcher.call("siwtchChanged", this, this.hierarchy_switch);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* shortcut to add invoke a callback when one of the dispatched events gets trigger
|
|
134
|
+
* @param {String} typename - one of the dispatched events: "siwtchChanged", "hierarchyLoaded"
|
|
135
|
+
* @return {GenomePropertiesHierarchy} The curent instance for chaining methods.
|
|
136
|
+
*/
|
|
137
|
+
on(typename, callback) {
|
|
138
|
+
this.dipatcher.on(typename, callback);
|
|
139
|
+
return this;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export default GenomePropertiesHierarchy;
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import * as d3 from "./d3";
|
|
2
|
+
/**
|
|
3
|
+
* Updates this and other components as if a scroll event happened.
|
|
4
|
+
* It became the de-facto refresh of the whole viewer, as it groups the minimum necesary changes to trigger a full update.
|
|
5
|
+
* @param {GernomeProperiesViewer} viewer - The instance of the genome properites viewer
|
|
6
|
+
*/
|
|
7
|
+
export const transformByScroll = (viewer) => {
|
|
8
|
+
viewer.newRows.attr(
|
|
9
|
+
"transform",
|
|
10
|
+
() => `translate(${viewer.current_scroll.x}, ${viewer.current_scroll.y})`
|
|
11
|
+
);
|
|
12
|
+
viewer.newCols.attr(
|
|
13
|
+
"transform",
|
|
14
|
+
`translate(${viewer.current_scroll.x}, ${viewer.current_scroll.y})`
|
|
15
|
+
);
|
|
16
|
+
if (viewer.labelGroup) {
|
|
17
|
+
viewer.labelGroup.attr(
|
|
18
|
+
"transform",
|
|
19
|
+
`translate(0, ${viewer.current_scroll.y})`
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
viewer.options.dimensions.total.short_side = viewer.options.cell_side;
|
|
23
|
+
viewer.update_viewer();
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Append a group into the viewer's mainGroup.
|
|
28
|
+
* It contains the elements to draw a simple horizontal srollbar, and attach the dragging events to them.
|
|
29
|
+
* @param {GernomeProperiesViewer} viewer - The instance of the genome properites viewer
|
|
30
|
+
*/
|
|
31
|
+
export const drawScrollXBar = (viewer) => {
|
|
32
|
+
const scrollerShortSide = viewer.options.dimensions.scroller.short_side;
|
|
33
|
+
viewer.scrollbar_x_g = viewer.mainGroup
|
|
34
|
+
.append("g")
|
|
35
|
+
.attr("class", "gpv-scrollbar gpv-scrollbar-x")
|
|
36
|
+
.attr("opacity", 0);
|
|
37
|
+
|
|
38
|
+
viewer.scrollbar_x_bg = viewer.scrollbar_x_g
|
|
39
|
+
.append("rect")
|
|
40
|
+
.attr("class", "gpv-scrollbar-bg")
|
|
41
|
+
.attr("height", scrollerShortSide);
|
|
42
|
+
|
|
43
|
+
let selectedXBar = null;
|
|
44
|
+
viewer.scrollbar_x = viewer.scrollbar_x_g
|
|
45
|
+
.append("rect")
|
|
46
|
+
.attr("class", "gpv-scrollbar-handle")
|
|
47
|
+
.attr("height", scrollerShortSide - 2)
|
|
48
|
+
.attr("y", 1)
|
|
49
|
+
.attr("rx", scrollerShortSide / 2)
|
|
50
|
+
.attr("ry", scrollerShortSide / 2)
|
|
51
|
+
.style("cursor", "ew-resize")
|
|
52
|
+
.call(
|
|
53
|
+
d3
|
|
54
|
+
.drag()
|
|
55
|
+
.on("start", (event) => (selectedXBar = event.sourceEvent.target))
|
|
56
|
+
.on("end", () => {
|
|
57
|
+
viewer.skip_scroll_refreshing = false;
|
|
58
|
+
selectedXBar = null;
|
|
59
|
+
})
|
|
60
|
+
.on("drag", (event) => {
|
|
61
|
+
viewer.skip_scroll_refreshing = true;
|
|
62
|
+
event.sourceEvent.stopPropagation();
|
|
63
|
+
const speciesWidth =
|
|
64
|
+
(viewer.organisms?.length || 0) * viewer.options.cell_side;
|
|
65
|
+
const prevX = Number(selectedXBar.getAttribute("x"));
|
|
66
|
+
let nextX = prevX + event.dx;
|
|
67
|
+
const available_x = viewer.heatmapWidth();
|
|
68
|
+
|
|
69
|
+
nextX = Math.max(
|
|
70
|
+
0,
|
|
71
|
+
Math.min(nextX, available_x - selectedXBar.getAttribute("width"))
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
viewer.scrollbar_x.attr("x", nextX);
|
|
75
|
+
const dx = (-nextX * speciesWidth) / available_x;
|
|
76
|
+
viewer.current_scroll.x = Math.min(0, dx);
|
|
77
|
+
transformByScroll(viewer);
|
|
78
|
+
})
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
viewer.scrollbar_y_g = viewer.mainGroup
|
|
82
|
+
.append("g")
|
|
83
|
+
.attr("class", "gpv-scrollbar gpv-scrollbar-y")
|
|
84
|
+
.attr("opacity", 0);
|
|
85
|
+
|
|
86
|
+
viewer.scrollbar_y_bg = viewer.scrollbar_y_g
|
|
87
|
+
.append("rect")
|
|
88
|
+
.attr("class", "gpv-scrollbar-bg")
|
|
89
|
+
.attr("width", scrollerShortSide);
|
|
90
|
+
|
|
91
|
+
let selectedYBar = null;
|
|
92
|
+
viewer.scrollbar_y = viewer.scrollbar_y_g
|
|
93
|
+
.append("rect")
|
|
94
|
+
.attr("class", "gpv-scrollbar-handle")
|
|
95
|
+
.attr("width", scrollerShortSide - 2)
|
|
96
|
+
.attr("x", 1)
|
|
97
|
+
.attr("rx", scrollerShortSide / 2)
|
|
98
|
+
.attr("ry", scrollerShortSide / 2)
|
|
99
|
+
.style("cursor", "ns-resize")
|
|
100
|
+
.call(
|
|
101
|
+
d3
|
|
102
|
+
.drag()
|
|
103
|
+
.on("start", (event) => (selectedYBar = event.sourceEvent.target))
|
|
104
|
+
.on("end", () => {
|
|
105
|
+
viewer.skip_scroll_refreshing = false;
|
|
106
|
+
selectedYBar = null;
|
|
107
|
+
})
|
|
108
|
+
.on("drag", (event) => {
|
|
109
|
+
viewer.skip_scroll_refreshing = true;
|
|
110
|
+
event.sourceEvent.stopPropagation();
|
|
111
|
+
const propsHeight = viewer.y(viewer.props?.length || 0) || 0;
|
|
112
|
+
const prevY = Number(selectedYBar.getAttribute("y"));
|
|
113
|
+
let nextY = prevY + event.dy;
|
|
114
|
+
const available_y = viewer.heatmapHeight();
|
|
115
|
+
|
|
116
|
+
nextY = Math.max(
|
|
117
|
+
0,
|
|
118
|
+
Math.min(nextY, available_y - selectedYBar.getAttribute("height"))
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
viewer.scrollbar_y.attr("y", nextY);
|
|
122
|
+
const dy = (-nextY * propsHeight) / available_y;
|
|
123
|
+
viewer.current_scroll.y = Math.min(0, dy);
|
|
124
|
+
transformByScroll(viewer);
|
|
125
|
+
})
|
|
126
|
+
);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const updateScrollBarX = (viewer, visible_cols, current_col) => {
|
|
130
|
+
const total_cols = viewer.organisms.length;
|
|
131
|
+
const available_x = viewer.heatmapWidth();
|
|
132
|
+
viewer.scrollbar_x_g
|
|
133
|
+
.attr(
|
|
134
|
+
"transform",
|
|
135
|
+
`translate(${viewer.heatmapLeft}, ${
|
|
136
|
+
viewer.options.height - viewer.options.dimensions.scroller.short_side
|
|
137
|
+
})`
|
|
138
|
+
)
|
|
139
|
+
.attr("opacity", total_cols > 0 ? 1 : 0);
|
|
140
|
+
|
|
141
|
+
viewer.scrollbar_x
|
|
142
|
+
.transition()
|
|
143
|
+
.attr(
|
|
144
|
+
"width",
|
|
145
|
+
available_x *
|
|
146
|
+
Math.min(
|
|
147
|
+
1,
|
|
148
|
+
total_cols !== 0 ? visible_cols / Math.max(total_cols, 1) : 1
|
|
149
|
+
)
|
|
150
|
+
)
|
|
151
|
+
.attr("x", total_cols ? (current_col * available_x) / total_cols : 0);
|
|
152
|
+
viewer.scrollbar_x_bg.attr("width", available_x);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const updateScrollBarY = (viewer, visible_rows, current_row) => {
|
|
156
|
+
const total_rows = viewer.props.length;
|
|
157
|
+
const available_y = viewer.heatmapHeight();
|
|
158
|
+
viewer.scrollbar_y_g
|
|
159
|
+
.attr(
|
|
160
|
+
"transform",
|
|
161
|
+
`translate(${
|
|
162
|
+
viewer.options.width - viewer.options.dimensions.scroller.short_side
|
|
163
|
+
}, ${viewer.heatmapTop})`
|
|
164
|
+
)
|
|
165
|
+
.attr("opacity", total_rows > 0 ? 1 : 0);
|
|
166
|
+
|
|
167
|
+
viewer.scrollbar_y
|
|
168
|
+
.transition()
|
|
169
|
+
.attr(
|
|
170
|
+
"height",
|
|
171
|
+
available_y *
|
|
172
|
+
Math.min(
|
|
173
|
+
1,
|
|
174
|
+
total_rows !== 0 ? visible_rows / Math.max(total_rows, 1) : 1
|
|
175
|
+
)
|
|
176
|
+
)
|
|
177
|
+
.attr("y", total_rows ? (current_row * available_y) / total_rows : 0);
|
|
178
|
+
viewer.scrollbar_y_bg.attr("height", available_y);
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* A single method to trigger the update of all the scroll bars of the viewer.
|
|
183
|
+
* @param {GernomeProperiesViewer} viewer - The instance of the genome properites viewer
|
|
184
|
+
* @param {Number} visible_rows - Visible property rows in the heatmap.
|
|
185
|
+
* @param {Number} current_row - Index of the first visible property.
|
|
186
|
+
*/
|
|
187
|
+
export const updateScrollBars = (viewer, visible_rows, current_row) => {
|
|
188
|
+
const visible_cols = Math.min(
|
|
189
|
+
viewer.organisms.length,
|
|
190
|
+
Math.ceil(viewer.heatmapWidth() / viewer.options.cell_side)
|
|
191
|
+
);
|
|
192
|
+
const current_col = Math.round(
|
|
193
|
+
Math.abs(viewer.current_scroll.x) / viewer.options.cell_side
|
|
194
|
+
);
|
|
195
|
+
updateScrollBarX(viewer, visible_cols, current_col);
|
|
196
|
+
updateScrollBarY(viewer, visible_rows, current_row);
|
|
197
|
+
};
|