-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproview_plots.js
executable file
·467 lines (374 loc) · 12.6 KB
/
proview_plots.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
var dataset = [];
var traits = [];
var vis1;
var vis2;
var brush1; // For 1st canvas
var brush_container1;
var brush2; // For 2nd canvas
var brush_container2;
var nSize; // Number of columns inputted in CSV file
// Set some dynamic dimensions and scale axis
var w=$("#plots").width() / 2 - 15;
var h=$("#iview").height();
var padding = 50;
var xScale_1=d3.scale.linear().range([padding,w-padding/2]);
var xScale_2=d3.scale.linear().range([padding,w-padding/2]);
var yScale_1=d3.scale.linear().range([h-padding,padding/2]);
var yScale_2=d3.scale.linear().range([h-padding,padding/2]);
var xAxis_1 = d3.svg.axis().scale(xScale_1).orient("bottom").ticks(5);
var yAxis_1 = d3.svg.axis().scale(yScale_1).orient("left").ticks(10);
var xAxis_2 = d3.svg.axis().scale(xScale_2).orient("bottom").ticks(5);
var yAxis_2 = d3.svg.axis().scale(yScale_2).orient("left").ticks(10);
// Rescale axis to a new set of data
function rescale_axis(axis,column){
axis.domain([0, d3.max(dataset,function(d){return d[column]})]);
}
// Add variables to the drop-down box
function AddVariable(){
// Grab the form input
var userInput=$("#transform_name").val();
var chosenFunc=$("#transform_op").val();
var chosenVar=$("#transform_var").val();
// Calculate the transform
dataset.forEach(function(d){
var newVal = d[chosenVar];
// Log transform
if (chosenFunc == 1){
newVal = Math.log(newVal > 0 ? Math.abs(newVal) : 0);
}
// Square root transform
else if(chosenFunc == 2){
newVal = Math.sqrt(newVal > 0 ? Math.abs(newVal) : 0);
}
// Absolute value transform
else if(chosenFunc == 3){
newVal = Math.abs(newVal);
}
// Splice the new value into the dataset
d.splice(nSize,0,newVal);
})
// Push the new variable into the trait list
traits.push(userInput);
var newOpt = $("<option></option>").attr("value",traits.length-1).text(userInput);
$("[id^=opts]").append(newOpt);
$("#transform_var").append(newOpt);
// Update the number of traits counter
nSize=traits.length;
// Show the alert
$("#transform_alert").toggleClass("hidden");
setTimeout(function(){$("#transform_alert").toggleClass("hidden")},4000);
}
// Create a placeholder table for the summary properties
function setup_summary(){
var def_list = $("<dl></dl>").addClass("dl-horizontal");
for (trait in traits){
var term = $("<dt></dt>").text(traits[trait]);
var val = $("<dd></dd>").attr("id","trait"+trait);
def_list.append(term);
def_list.append(val);
}
$("#summary").append(def_list);
}
// Populate the summary box with proper summary statistics
function populate_summary(){
// Initialize the summary table
var summaries = [];
var num_selected = 0;
for (trait in traits){
if(trait > 0){
summaries[trait] = 0;
}else{
summaries[trait] = [];
}
}
// If nothing is selected then summarize all
select_all = !dataset.some(function(d){return d[nSize]});
// Add up all the values
dataset.forEach(function(d){
if(select_all || d[nSize]){
num_selected++;
for(trait in traits){
if(trait > 0){
summaries[trait] += d[trait];
}else{
summaries[trait].push(d[trait]);
}
}
}
})
// Take an average
for (trait in traits){
if(trait > 0){
summaries[trait] = summaries[trait] / num_selected;
}
}
// Cleaner text
if(select_all){
summaries[0] = "All";
}else{
summaries[0] = summaries[0].join(" ");
}
// Append the value to the right box
for (trait in traits){
$("#trait"+trait).text(trait > 0 ? summaries[trait].toFixed(2) : summaries[trait]);
}
}
// Redraw 1st canvas from dropdown box !!
function PutOnCanvas1(){
var selopt_1_can1=document.getElementById("opts1_1").value;
var selopt_2_can1=document.getElementById("opts1_2").value;
reDrawVis1(selopt_1_can1,selopt_2_can1);
}
// Redraw 2nd canvas from dropdown box
function PutOnCanvas2(){
var selopt_1_can2=document.getElementById("opts2_1").value;
var selopt_2_can2=document.getElementById("opts2_2").value;
reDrawVis2(selopt_1_can2,selopt_2_can2);
}
// Parse the CSV file and populate the global dataset
function load_dataset(csv){
var data = d3.csv.parse(csv);
traits = d3.keys(data[0]);
for (var i=0; i<traits.length; i++){
d3.select("#opts1_1").append("option").text(traits[i]).attr("value",i);
d3.select("#opts1_2").append("option").text(traits[i]).attr("value",i);
d3.select("#opts2_1").append("option").text(traits[i]).attr("value",i);
d3.select("#opts2_2").append("option").text(traits[i]).attr("value",i);
d3.select("#transform_var").append("option").text(traits[i]).attr("value",i);
}
nSize = traits.length;
last_trait_plotted = traits.length > 2 ? 2 : 1;
d3.select("#opts1_1 option[value='0']").node().selected=true;
d3.select("#opts1_2 option[value='1']").node().selected=true;
d3.select("#opts2_1 option[value='0']").node().selected=true;
d3.select("#opts2_2 option[value='" + last_trait_plotted + "']").node().selected=true;
data.forEach(function(d){
var point = d3.values(d).map(parseFloat);
point.push(false);
dataset.push(point);
});
// Draw the initial plots
reDrawVis1(0,1);
reDrawVis2(0,last_trait_plotted);
// Create the summary view
setup_summary();
populate_summary();
}
// Reset the old view
function reset_view(){
dataset = [];
d3.selectAll("circle").classed("selected",false);
$("[id^=opts]").empty();
$("#transform_var").empty();
$("#summary").empty();
clearHighlight3D();
}
// Highlight points when cliked on by mouse
function click_select(d){
if (!d[nSize]){
dataset.forEach(function(d){d[nSize]=false});
d[nSize]=true;
d3.select("#plot1").selectAll("circle").classed("selected",function(d){
if (d[nSize] == true){return true;}
else {return false;}
})
d3.select("#plot2").selectAll("circle").classed("selected",function(d){
if (d[nSize] == true){return true;}
else {return false;}
})
// Highlight the residue on the 3D structure
highlight_residues();
// Update the data summary
populate_summary();
}
else {
d3.selectAll(".selected").classed("selected",false);
d[nSize]=false;
}
}
// Color the circles which are within the brushed box for 1st canvas
function brushmove_1(column1,column2){
brush_container2.call(brush2.clear());
var e = brush1.extent();
dataset.forEach(function(d){d[nSize]=false});
d3.select("#plot1")
.selectAll("circle")
.classed("selected",function(d){
if (e[0][0] <= d[column1] &&
d[column1] <= e[1][0] &&
e[0][1] <= d[column2] &&
d[column2] <= e[1][1]) {
d[nSize]=true;
return true}
else {return false}
})
d3.select("#plot2")
.selectAll("circle")
.classed("selected",function(d){
if (d[nSize]==true) {return true}
else {return false}
})
populate_summary();
}
// Brush function for 2nd canvas
function brushmove_2(column1,column2){
brush_container1.call(brush1.clear());
var e = brush2.extent();
dataset.forEach(function(d){d[nSize]=false});
var selected = [];
d3.select("#plot2")
.selectAll("circle")
.classed("selected",function(d){
if (e[0][0] <= d[column1] &&
d[column1] <= e[1][0] &&
e[0][1] <= d[column2] &&
d[column2] <= e[1][1]) {
d[nSize]=true;
return true}
else {return false}
})
d3.select("#plot1")
.selectAll("circle")
.classed("selected",function(d){
if (d[nSize]==true) {return true}
else {return false}
})
}
// Highlight residues on the 3D vis
function highlight_residues(){
var selected = [];
dataset.forEach(function(d){
if(d[nSize]){
selected.push(d[0]);
}
})
highlight3D(selected);
}
// Append appropriate canvases to the DOM for vis1
function setup_vis1(){
vis1=d3.select("#plot1")
.append("svg")
.attr("width",w)
.attr("height",h);
vis1.append("text")
.attr("x",-h/2)
.attr("y",15)
.attr("transform","rotate(-90)")
.attr("id","ylab_1");
vis1.append("text")
.attr("x",w/2)
.attr("y",h-15)
.attr("id","xlab_1");
vis1.insert("g")
.attr("class","y axis");
vis1.insert("g")
.attr("class","x axis");
brush1 = d3.svg.brush()
.x(xScale_1)
.y(yScale_1);
brush_container1 = vis1.append("g")
.attr("class","brush")
.call(brush1); // Use vis1 !!
}
// Append appropriate canvases to the DOM for vis2
function setup_vis2(){
vis2=d3.select("#plot2")
.append("svg")
.attr("width",w)
.attr("height",h);
vis2.append("text")
.attr("x",-h/2)
.attr("y",15)
.attr("transform","rotate(-90)")
.attr("id","ylab_2");
vis2.append("text")
.attr("x",w/2)
.attr("y",h-15)
.attr("id","xlab_2");
vis2.insert("g")
.attr("class","y axis");
vis2.insert("g")
.attr("class","x axis");
brush2 = d3.svg.brush()
.x(xScale_2)
.y(yScale_2);
brush_container2 = vis2.append("g")
.attr("class","brush")
.call(brush2); // Use vis2 !!
}
// Draw the new datapoints for vis1
function reDrawVis1(column1,column2){
rescale_axis(xScale_1,column1);
rescale_axis(yScale_1,column2);
var dataPointX=vis1.selectAll("circle").data(dataset);
dataPointX.enter().append("circle");
dataPointX.exit().remove();
dataPointX
.attr("cx",function(d) {return xScale_1(d[column1])})
.attr("cy",function(d){return yScale_1(d[column2])})
.attr("r",function(d) {return 3})
.classed("regular",true)
.on("click",click_select);
d3.select("#xlab_1").text(traits[column1]);
d3.select("#ylab_1").text(traits[column2]);
vis1.selectAll(".x.axis")
.attr("transform","translate(0,"+(h-padding)+")")
.call(xAxis_1);
vis1.select(".y.axis")
.attr("transform","translate("+padding+",0)")
.call(yAxis_1);
brush1.clear();
brush1.on("brush",function(d) {brushmove_1(column1,column2)});
brush1.on("brushend",highlight_residues);
brush_container1.call(brush1);
}
// Draw the new datapoints for vis2
function reDrawVis2(column1,column2){
rescale_axis(xScale_2,column1);
rescale_axis(yScale_2,column2);
var dataPointX=vis2.selectAll("circle").data(dataset);
dataPointX.enter().append("circle");
dataPointX.exit().remove();
dataPointX
.attr("cx",function(d) {return xScale_2(d[column1])})
.attr("cy",function(d){return yScale_2(d[column2])})
.attr("r",function(d) {return 3})
.classed("regular",true)
.on("click",click_select);
d3.select("#xlab_2").text(traits[column1]);
d3.select("#ylab_2").text(traits[column2]);
vis2.selectAll(".x.axis")
.attr("transform","translate(0,"+(h-padding)+")")
.call(xAxis_2);
vis2.select(".y.axis")
.attr("transform","translate("+padding+",0)")
.call(yAxis_2);
brush2.clear();
brush2.on("brush",function(d) {brushmove_2(column1,column2)});
brush2.on("brushend",highlight_residues);
brush_container2.call(brush2);
}
// Attach a listener to the CSV uploader
$("#csvInput").change(function() {
var file = this.files[0];
if (file === undefined) return;
var reader = new FileReader();
reader.onload = function () {
//Reset the current visualization
reset_view();
// Load in the new CSV dataset
load_dataset(reader.result);
};
reader.readAsText(file);
});
// Initialize the plots
setup_vis1();
setup_vis2();
$.get("pdb/ke70.csv",function(csv){
load_dataset(csv);
})
// Assign canvas changes to replot
$("[id^='opts1']").change(PutOnCanvas1);
$("[id^='opts2']").change(PutOnCanvas2);
// Assign thransform changes to apply the transform
$("#transform_apply").click(AddVariable);