-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtree.js
391 lines (324 loc) · 12.6 KB
/
tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* numMutations: int
* phyloTree: dictionary
* colorRanges: array
* subset: True or False for phylogeny tree format handling
*/
function drawTree(numMutations, phyloTree, colorRanges, order, treeLabels, clones, cloneLabels) {
document.getElementById("tree").innerHTML = "<svg></svg>";
// Create the input graph
var g = new dagreD3.graphlib.Graph()
.setGraph({})
.setDefaultEdgeLabel(function() { return {}; });
var treeIdx = {}
//var index = 0;
// use order[index] to make sure tree colors and IDs
// are loaded accurately
for(var index = 0; index < numMutations; index++) {
/**
assumption:
last node in ORDER (bfsorder; all the mutations in the clone)
is the CURRENT node
**/
g.setNode(index, {
//label: treeLabels[order[index]],
labelType: "html",
labelStyle: "font-size: 10px",
label: clones[order[index]],
id: "tree-node-u"+index,
shape: "circle",
width: 25,
height: 25,
style: "fill: " + colorIdx[indexToColorIdx[index]]});
//style: "fill: " + colorRanges[index]});
treeIdx[order[index]] = index
// index += 1
}
//empty node
g.setNode("head", {
//label: treeLabels[order[index]],
label: " ",
id: "tree-node-u",
shape: "circle",
style: "fill: #fff; stroke-dash-array: 5,5" });
var rootLabel = ""
if(idxToLabel[order[0]]) {
var temp = idxToLabel[order[0]].replace(/\s+/g, '');
var newLabel = temp.split(",")
var displayLabels = []
for(var i in temp) {
if(htmlSelectedMutations.includes(newLabel[i])) {
// message += newLabel[i] += ", "
displayLabels.push(newLabel[i])
}
i+=1
}
if(displayLabels.length > 6) {
for(var i = 0; i < 6; i++) {
rootLabel += displayLabels[i] += " <br>" + " "
}
rootLabel+= "[" + (displayLabels.length-6) + " more] "
} else {
for(var i in displayLabels) {
rootLabel += displayLabels[i] += " <br>" + " "
}
}
}
//(Boolean_expression) ? do.somethingForTrue() : do.somethingForFalse();
g.setEdge("head", 0, {
labelType: "html",
label: clones[order[0]] + rootLabel
})
for(var node in phyloTree) {
for(var child in phyloTree[node]) {
//debugger
var label = ""
var temp = treeLabels[phyloTree[node][child]].replace(/\s+/g, '');
var newLabel = temp.split(",")
var displayLabels = []
var i = 0
//debugger
while(i < newLabel.length) {
if(htmlSelectedMutations.includes(newLabel[i])) {
// message += newLabel[i] += ", "
displayLabels.push(newLabel[i])
}
i+=1
}
if(displayLabels.length > 6) {
for(var i = 0; i < 6; i++) {
label += newLabel[i] += " <br>" + " "
}
label+= "[" + (displayLabels.length-6) + " more] "
} else {
for(var i in displayLabels) {
label += newLabel[i] += " <br>" + " "
}
}
g.setEdge(treeIdx[node], treeIdx[phyloTree[node][child]],
{
labelType: "html",
label: cloneLabels[phyloTree[node][child]] + " " + label,
arrowhead: "normal"
});
}
}
try {
g.nodes().forEach(function(v) {
var node = g.node(v);
// Round the corners of the nodes
node.rx = node.ry = 5;
});
} catch(e) {
console.log(e)
console.log("Error occured setting nodes and edges in tree.js")
}
// Create the renderer
var render = new dagreD3.render();
var svg = d3.selectAll("#tree")
.append("svg")
var svgGroup = svg.append("g");
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip-tree")
.style("display", "none");
render(d3.select("#tree svg g"), g);
// Center the graph
var xCenterOffset = (svg.attr("width") - g.graph().width) / 2;
//svgGroup.attr("transform", "translate(" + xCenterOffset + ", 20)");
svgGroup.attr("transform", "translate(20, 20)");
var height = g.graph().height + 30
var width = g.graph().width + 30
svg.attr("height", height);
svg.attr("width", width);
svg.attr("id", "tree-svg")
var viewbox = "0 0 " + width + " " + height
svg.attr("viewbox", viewbox)
var br = document.getElementById("tree")
br.innerHTML += " "
// for(var i = 0; i < numMutations; i++) {
// var color=colorIdx[indexToColorIdx[i]]
// svg.selectAll(".nodes #tree-node-u"+i)
// .on('mouseover', function (d) {
// //append to svg to change z-index
// // this.parentElement.parentElement.appendChild(g);
// // d3.selectAll("#remove-cloud")
// // .attr("stroke-width", 3)
// var cloudPath = d3.selectAll("#cloud-u"+d);
// var node = d3.select(this).select("circle");
// var boundary = d3.selectAll("#color-boundary-u"+d)
// if(cloudPath.classed('activated')) {
// node.style('stroke-width', 3)
// boundary.style('stroke-width', 3)
// }
// tooltip.transition()
// .duration(200)
// .style("display", "inline-block")
// var message = ""
// if(order[d] == "") {
// message = "Unselected ancestral mutations"
// } else {
// message = "Mutations: "
// for(var mut in order[d]) {
// if(message.length < 50) {
// message += idxToLabel[order[d][mut]] + "<br>"
// } else {
// message = message.substr(0, message.length-2)
// message += "..."
// }
// }
// message = message.substr(0, message.length-2)
// }
// tooltip.html(message)
// .style("left", (d3.event.pageX + 18)+"px")
// .style("top", (d3.event.pageY - 28)+"px")
// })
// .on('mouseout', function (d) {
// var cloudPath = d3.selectAll("#cloud-u"+d);
// var node = d3.select(this).selectAll("circle");
// var boundary = d3.selectAll("#color-boundary-u"+d)
// if(cloudPath.classed('activated')) {
// node.style('stroke-width', 1)
// // boundary.style('fill', d3.rgb(colorRanges[d]))
// boundary.style('stroke-width', 1)
// }
// tooltip.transition()
// .duration(500)
// .style("display", "none")
// })
// .on('click', function (d) {
// alert("clicked!")
// });
// }
// var symbolLegend = document.getElementById("symbols");
// symbolLegend.innerHTML = "";
// for (var index = 0; index < numMutations; index++) {
// var symbolLabel = treeLabels[order[index]];
// var showMore = ""
// if(symbolLabel.length > 30) {
// //<a href='javascript:void(0);' onclick='openMore(this); return false;'>more</a>
// showMore ="<span class = 'treelabel-showmore'>" + symbolLabel + "</span>"
// symbolLabel = symbolLabel.substr(0,30) + "..."
// }
// symbolLegend.innerHTML += "<p class = 'treelabel'><span>"+ cloneLabels[order[index]] + "</span>" + symbolLabel + showMore + " </p>"
// }
}
function addTreeInteractivity() {
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip-tree")
.style("display", "none");
// hovering effects
for(var i = 0; i < numMutations; i++) {
var color = colorIdx[indexToColorIdx[i]]
d3.select("#tree-svg").selectAll(".nodes #tree-node-u"+i)
.on('mouseover', function () {
var index = d3.select(this).attr("id")
index = index.replace(/\D/g,'');
var cloudPath = d3.selectAll("#cloud-u"+index);
var node = d3.select(this).select("circle");
var boundary = d3.selectAll("#color-boundary-u"+index)
if(cloudPath.classed('activated')) {
// Get associated cloudpath to HIGHLIGHT
var test= document.getElementsByClassName("#cloud-selector-u"+index)[0]
var g = test.cloneNode(true)
g.class = "temp-cloud"
g.id = "remove-cloud"
test.parentElement.parentElement.appendChild(g);
d3.selectAll("#remove-cloud")
.attr("stroke-width", 3)
//debugger
node.style('stroke-width', 3)
//boundary.style('fill', d3.rgb(colorRanges[d]).darker(1.0))
boundary.style('stroke-width', 3)
}
tooltip.transition()
.duration(200)
.style("display", "inline-block")
var message = ""
if(order[index] == "") {
message = "Unselected ancestral mutations"
} else {
message = "Mutations: ";
for(var mut in order[index]) {
var newLabel = idxToLabel[order[index][mut]]
newLabel = newLabel.replace(/\s+/g, '');
newLabel = newLabel.split(",")
newLabel = newLabel.filter(function (el) {
return el != "";
});
var displayLabels = []
var i = 0
//debugger
while(i < newLabel.length) {
if(htmlSelectedMutations.includes(newLabel[i])) {
// message += newLabel[i] += ", "
displayLabels.push(newLabel[i])
}
i+=1
}
if(displayLabels.length > 6) {
for(var i = 0; i < 6; i++) {
message += displayLabels[i] += ", "
}
message+= "[" + (displayLabels.length-6) + " more], "
} else {
for(var i in displayLabels) {
message += displayLabels[i] += ", "
}
message=message.substr(0, message.length-1)
}
}
if(message.substr(message.length-2, message.length) ==", ") {
message = message.substr(0, message.length-2)
}
}
tooltip.html(message)
.style("left", (d3.event.pageX + 18)+"px")
.style("top", (d3.event.pageY - 28)+"px")
})
.on('mouseout', function () {
var d = d3.select(this).attr("id")
d = d.replace(/\D/g,'');
var cloudPath = d3.selectAll("#cloud-u"+d);
var node = d3.select(this).selectAll("circle");
var boundary = d3.selectAll("#color-boundary-u"+d)
if(cloudPath.classed('activated')) {
node.style('stroke-width', 1)
// boundary.style('fill', d3.rgb(colorRanges[d]))
boundary.style('stroke-width', 1)
// cloudpath HIGHLIGHTING function: remove it
while (document.getElementById("remove-cloud")) {
var element = document.getElementById("remove-cloud")
element.parentNode.removeChild(element);
}
}
tooltip.transition()
.duration(500)
.style("display", "none")
})
.on('click', function () {
var d = d3.select(this).attr("id")
d = d.replace(/\D/g,'');
var node = d3.select(this).select("circle")
//the actual outlined clone path
var cloudPath = d3.selectAll("#cloud-u"+d);
// drawContours(currMatrixU, index)
if(cloudPath.classed('activated')) {
if(globalSliderIdx = d) {
globalSliderIdx = -1
drawNewSlider(-1)
}
cloudPath.style('display','none')
node.style('fill', '#fff')
node.style("stroke-width", 1)
activatedMutations["cloud-u"+d] = "deactivated"
cloudPath.classed('activated', false)
} else {
//slider.style.display = 'inline'
cloudPath.style('display','inline')
node.style('fill', d3.rgb(colorIdx[indexToColorIdx[d]]))
activatedMutations["cloud-u"+d] = "activated"
cloudPath.classed('activated', true)
}
});
}
}