-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotterscript.js
executable file
·711 lines (659 loc) · 19.3 KB
/
plotterscript.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
// jshint esversion:6
let data;
let layout;
let numOfResiduesl;
let xpmFiles;
let xpmAxes;
let reader;
let fileNum;
let graphType;
let plot;
let coilColor;
let helixColor;
let sheetColor;
let titleFontFam;
let titleFontSize;
let titleFontColor;
let xFontFam;
let xFontSize;
let xFontColor;
// Methods to extract data from xpm files
function getXpmFiles(newgraphType) {
//Global Variable initiation every new plot
numOfResidues = null;
xpmFiles = [];
xpmAxes = [];
reader = new FileReader();
fileNum = 0;
graphType = newgraphType;
let inputID;
if (graphType == "line-time") {
inputID = "time-files";
} else if (graphType == "line-res") {
inputID = "res-files";
} else if (graphType == "colormap") {
inputID = "colormap-files";
}
xpmAxes = [];
xpmFiles = document.getElementById(inputID).files;
reader.onload = function(e) {
populateXpmAxes(xpmFiles.length, e);
};
reader.readAsText(xpmFiles[0]);
}
function makeXpmKey(fileLines) {
let xpmKey = {
cbt: [],
helix: [],
sheet: []
};
for (var lineNum = 0; lineNum < fileLines.length; lineNum++) {
let line = fileLines[lineNum];
if (line[0] == '"' && line.slice(line.length - 3, line.length).toString() == '*/,') {
let structureName = line.split('"')[3];
let structureSymbol = line.split('"')[1][0];
if (structureName.includes("Coil") || structureName.includes("Bend") || structureName.includes("Turn")) {
xpmKey.cbt.push(structureSymbol);
} else if (structureName.includes("B-")) {
xpmKey.sheet.push(structureSymbol);
} else if (structureName.includes("-Helix")) {
xpmKey.helix.push(structureSymbol);
}
}
//Dont read the rest of the file in this method
if (line.split(":")[0] == "/* x-axis") {
break;
}
}
return xpmKey;
}
function populateXpmAxes(totalFiles, e) {
xpmAxes.push([]);
let yDataReached = false;
let fileLines = e.target.result.split("\n");
let xpmKey = makeXpmKey(fileLines);
let resLineCount = 0;
// Populate xpmAxes
for (var lineNum = 0; lineNum < fileLines.length; lineNum++) {
let line = fileLines[lineNum];
// Everything after the yAxis line is yData and should be added to xpmAxes
if (yDataReached) {
let lineArr = line.split("");
//TODO the last residues has one less time value than the rest. print xpmAxes after populating to see.
if (resLineCount < numOfResidues - 1) {
xpmAxes[fileNum].push(lineArr.slice(1, lineArr.length - 2));
} else if (resLineCount == numOfResidues - 1) {
xpmAxes[fileNum].push(lineArr.slice(1, lineArr.length - 1));
}
resLineCount += 1;
}
//Find yData and numOfResidues count
if (line.split(":")[0] == "/* y-axis") {
let yAxisLineArray = line.split(" ");
numOfResidues = Number(yAxisLineArray[yAxisLineArray.length - 2]);
yDataReached = true;
}
}
// Clean xpmAxes by changing all values to "cbt", "helix" or "sheet"
for (var resNum = 0; resNum < xpmAxes[fileNum].length; resNum++) {
for (var time = 0; time < xpmAxes[fileNum][resNum].length; time++) {
let structure = xpmAxes[fileNum][resNum][time];
if (xpmKey.cbt.includes(structure)) {
xpmAxes[fileNum][resNum][time] = "cbt";
} else if (xpmKey.helix.includes(structure)) {
xpmAxes[fileNum][resNum][time] = "helix";
} else if (xpmKey.sheet.includes(structure)) {
xpmAxes[fileNum][resNum][time] = "sheet";
}
}
}
fileNum += 1;
if (fileNum < totalFiles) {
reader.readAsText(xpmFiles[fileNum]);
} else if (fileNum == totalFiles) {
switch (graphType) {
case "line-time":
plotLines("time");
break;
case "line-res":
plotLines("res");
break;
case "colormap":
plotColorMap();
break;
default:
}
}
}
//========== COLOR MAP ==========
function plotColorMap() {
let axes = makeColorAxes();
let colorArr = [];
for (let resNum = 0; resNum < axes.length; resNum++) {
colorArr.push([]);
let epochPercents = [];
for (let time = 0; time < axes[0].length; time++) {
epochPercents.push(axes[resNum][time]);
if (time % 100 == 0) {
colorArr[resNum].push(calcRgb(averageArr(epochPercents)));
epochPercents = [];
}
}
}
let plotTable = document.getElementById("plot-table");
plotTable.innerHTML = "";
for (var resNum = 0; resNum < colorArr.length; resNum++) {
let newRow = document.createElement("tr");
let newCell = document.createElement("td");
newCell.setAttribute("class", "data-cell");
newCell.setAttribute("id", "res-row-" + resNum);
newCell.innerHTML = ""+(resNum+1);
newRow.appendChild(newCell);
for (var time = 0; time < colorArr[0].length; time++) {
newCell = document.createElement("td");
newCell.setAttribute("class", "data-cell");
newCell.setAttribute("id", "cell-" + resNum + "-" + time);
newCell.style.backgroundColor = colorArr[resNum][time];
newCell.style.height = "15px";
newCell.style.width = "1px";
newRow.appendChild(newCell);
}
plotTable.appendChild(newRow);
}
// X-Axis Labels
let newRow = document.createElement("tr");
for (var x = 0; x < colorArr[0].length; x++) {
let newCell = document.createElement("td");
newCell.setAttribute("class", "data-cell");
newCell.setAttribute("id", "cell-xaxis-" + x);
newCell.style.borderRight = "1px solid black";
newCell.style.borderLeft = "1px solid black";
newCell.style.height = "15px";
newCell.style.width = "1px";
newCell.style.overflow = "scroll";
if (x % 100 == 0) {
newCell.innerHTML = x + "ns";
}
newRow.appendChild(newCell);
}
plotTable.appendChild(newRow);
}
function averageArr(arr) {
let avgC = 0;
let avgH = 0;
let avgS = 0;
let total = arr.length;
for (let i = 0; i < total; i++) {
avgC += arr[i][0];
avgH += arr[i][1];
avgS += arr[i][2];
}
avgC = avgC / total;
avgH = avgH / total;
avgS = avgS / total;
return [avgC, avgH, avgS];
}
rgb_ryb();
const RGB_MAX = 384;
function calcRgb(percents) {
let ryb = chs2ryb(percents[1], percents[0], percents[2]);
let r = ryb[0];
let y = ryb[1];
let b = ryb[2];
let color = new RgbRyb();
color.setRyb(r, y, b);
return color.getRgbText();
}
function makeColorAxes() {
let axes = [];
let res = 0;
let chainSepCount = 0;
while (res < xpmAxes[0].length) {
if (xpmAxes[0][res][0] == "=") {
for (let fileNum = 0; fileNum < xpmAxes.length; fileNum++) {
xpmAxes[fileNum].splice(res, 1);
}
chainSepCount++;
continue;
}
axes.push([]);
for (let time = 0; time < xpmAxes[0][0].length; time++) {
let c = 0;
let h = 0;
let s = 0;
for (let fileNum = 0; fileNum < xpmAxes.length; fileNum++) {
switch (xpmAxes[fileNum][res][time]) {
case "cbt":
c += 1;
break;
case "helix":
h += 1;
break;
case "sheet":
s += 1;
break;
default:
}
}
c = c / (xpmAxes.length);
h = h / (xpmAxes.length);
s = s / (xpmAxes.length);
axes[res].push([c, h, s]);
}
res++;
}
let strands = [];
for (let i = 0; i < axes.length; i += axes.length / (chainSepCount + 1)) {
strands.push(axes.slice(i, i + axes.length / (chainSepCount + 1)));
}
let avgAxes = [];
for (let res = 0; res < strands[0].length; res++) {
avgAxes.push([]);
for (let time = 0; time < strands[0][0].length; time++) {
avgAxes[res].push([]);
let avgC = 0;
let avgH = 0;
let avgS = 0;
for (let strandNum = 0; strandNum < strands.length; strandNum++) {
avgC += strands[strandNum][res][time][0];
avgH += strands[strandNum][res][time][1];
avgS += strands[strandNum][res][time][2];
}
avgC = avgC / strands.length;
avgH = avgH / strands.length;
avgS = avgS / strands.length;
avgAxes[res][time] = [avgC, avgH, avgS];
}
}
return avgAxes;
}
//========== LINE PLOT ===========
function plotLines() {
let plotArea = document.getElementById('plot');
plotArea.innerHTML = "";
let axes;
let plotTitle;
let avgStrands;
switch (graphType) {
case "line-time":
plotTitle = document.getElementById("time-plot-title").value;
avgStrands = document.getElementById("time-avg-strands").checked;
coilColor = document.getElementById("time-coil-color").value;
helixColor = document.getElementById("time-helix-color").value;
sheetColor = document.getElementById("time-sheet-color").value;
axes = makeTimeAxes(avgStrands);
break;
case "line-res":
plotTitle = document.getElementById("res-plot-title").value;
avgStrands = document.getElementById("res-avg-strands").checked;
coilColor = document.getElementById("res-coil-color").value;
helixColor = document.getElementById("res-helix-color").value;
sheetColor = document.getElementById("res-sheet-color").value;
axes = makeResidueAxes(avgStrands);
break;
default:
}
let xAxis = axes[0];
let yAxes = axes[1];
let cbtLine = {
x: xAxis,
y: yAxes[0],
mode: "lines",
name: "Coil, Bend or Turn",
line: {
color: coilColor,
width: 2
}
};
let sheetLine = {
x: xAxis,
y: yAxes[1],
mode: "lines",
name: " \u03B1-Helix",
line: {
color: helixColor,
width: 2
}
};
let helixLine = {
x: xAxis,
y: yAxes[2],
mode: "lines",
name: " \u03B2-Sheet",
line: {
color: sheetColor,
width: 2
}
};
data = [cbtLine, sheetLine, helixLine];
layout = {
title: plotTitle,
titlefont: {
family: "Montserrat",
size: 24,
color: "#7a7a7a"
},
xaxis: {
title: "X-Axis Title",
titlefont: {
family: 'Montserrat',
size: 18,
color: '#7a7a7a',
},
ticklen: 8,
tickwidth: 1,
/* Set the step in-between ticks*/
dtick: null,
// /* Set the values at which ticks on this axis appear */
tickvals: null,
// /* Set the text displayed at the ticks position via tickvals */
ticktext: null,
tickfont: {
size: 14,
color: "#7a7a7a"
},
ticks: 'outside',
showgrid: false,
showline: true
},
yaxis: {
title: "Secondary Structure %",
titlefont: {
family: 'Montserrat',
size: 18,
color: '#7a7a7a'
},
ticklen: 0,
tickwidth: 1,
/* Set the step in-between ticks*/
dtick: null,
// /* Set the values at which ticks on this axis appear */
tickvals: null,
// /* Set the text displayed at the ticks position via tickvals */
ticktext: null,
tickfont: {
size: 14,
color: "#7a7a7a"
},
ticks: 'outside',
showgrid: false,
showline: true,
range: [0, 100]
}
};
if (graphType == "line-time") {
layout.xaxis.title = "Time (ns)";
} else if (graphType == "line-res") {
layout.xaxis.title = "Residue Number";
}
plot = Plotly.newPlot('plot', data, layout, {
showSendToCloud: true
});
plotArea.style.width = "100%";
document.getElementById("line-width-input").value = 2;
let widthLabel = document.getElementById("line-width-label");
widthLabel.innerHTML = "2px";
}
//========== RESIDUE PLOT METHODS ==========
function makeResidueAxes(averageStrands) {
if (averageStrands) {
yAxes = [
[],
[],
[]
];
}
for (var resNum = 0; resNum < xpmAxes[0].length; resNum++) {
let structureVotes = [];
for (var time = 0; time < xpmAxes[0][0].length; time++) {
for (var fileNum = 0; fileNum < xpmAxes.length; fileNum++) {
structureVotes.push(xpmAxes[fileNum][resNum][time]);
}
}
if (averageStrands) {
let cbt = 0;
let helix = 0;
let sheet = 0;
let totalVotes = 0;
for (var votei = 0; votei < structureVotes.length; votei++) {
if (structureVotes[votei] == "cbt") {
cbt += 1;
totalVotes += 1;
} else if (structureVotes[votei] == "helix") {
helix += 1;
totalVotes += 1;
} else if (structureVotes[votei] == "sheet") {
sheet += 1;
totalVotes += 1;
}
}
yAxes[0].push(cbt / totalVotes * 100);
yAxes[1].push(helix / totalVotes * 100);
yAxes[2].push(sheet / totalVotes * 100);
}
}
let avgYsAxes = [
[],
[],
[]
];
let avgYsIndex = 0;
let chainSepCount = 0;
for (var yIndex = 0; yIndex < yAxes[0].length; yIndex++) {
if (isNaN(yAxes[0][yIndex])) {
chainSepCount += 1;
avgYsIndex = 0;
} else {
if (chainSepCount == 0) {
avgYsAxes[0].push(0);
avgYsAxes[1].push(0);
avgYsAxes[2].push(0);
}
avgYsAxes[0][avgYsIndex] += yAxes[0][yIndex];
avgYsAxes[1][avgYsIndex] += yAxes[1][yIndex];
avgYsAxes[2][avgYsIndex] += yAxes[2][yIndex];
// console.log("avgYsIndex: " + avgYsIndex);
// console.log("avgYsAxes[0][avgYsIndex]:");
// console.log(avgYsAxes[0][avgYsIndex]);
// console.log("yIndex: " + yIndex);
// console.log("yAxes[0][yIndex]: ");
// console.log(yAxes[0][yIndex]);
// console.log("\n");
avgYsIndex += 1;
}
}
for (var i = 0; i < avgYsAxes[0].length; i++) {
avgYsAxes[0][i] = avgYsAxes[0][i] / (chainSepCount + 1);
avgYsAxes[1][i] = avgYsAxes[1][i] / (chainSepCount + 1);
avgYsAxes[2][i] = avgYsAxes[2][i] / (chainSepCount + 1);
}
// Populate xAxis here so you don't count multiple strands
let xAxis = [];
for (var x = 1; x <= avgYsAxes[0].length; x++) {
xAxis.push(x);
}
// console.log("avgYsAxes and yAxes:");
// console.log(avgYsAxes);
// console.log(yAxes);
// console.log(xAxis);
yAxes = avgYsAxes;
checkAxes(xAxis, yAxes);
return ([xAxis, yAxes]);
}
//==========TIME PLOT METHODS===========
function makeTimeAxes(averageStrands) {
let xAxis = [];
if (averageStrands) {
yAxes = [
[],
[],
[]
];
}
for (var time = 0; time < xpmAxes[0][0].length; time++) {
let structureVotes = [];
for (var resNum = 0; resNum < xpmAxes[0].length; resNum++) {
for (var fileNum = 0; fileNum < xpmAxes.length; fileNum++) {
structureVotes.push(xpmAxes[fileNum][resNum][time]);
}
}
if (averageStrands) {
let cbt = 0;
let helix = 0;
let sheet = 0;
let totalVotes = 0;
for (var votei = 0; votei < structureVotes.length; votei++) {
if (structureVotes[votei] == "cbt") {
cbt += 1;
totalVotes += 1;
} else if (structureVotes[votei] == "helix") {
helix += 1;
totalVotes += 1;
} else if (structureVotes[votei] == "sheet") {
sheet += 1;
totalVotes += 1;
}
}
yAxes[0].push(cbt / totalVotes * 100);
yAxes[1].push(helix / totalVotes * 100);
yAxes[2].push(sheet / totalVotes * 100);
}
xAxis.push(time / 100);
}
checkAxes(xAxis, yAxes);
return [xAxis, yAxes];
}
function checkAxes(xAxis, yAxes) {
let check = true;
for (var s = 0; s < yAxes.length; s++) {
for (var x = 0; x < xAxis.length; x++) {
if (yAxes[s][x] > 100 || yAxes[s][x] < 0) {
pass = false;
}
}
}
if (!check) {
console.log("\nDid not pass axes check...\n");
}
}
//========== Plot Controls ==========
function updatePlot(targetID, newValue) {
let plotDiv = document.getElementById("plot");
let targetElem = document.getElementById(targetID);
switch (targetID) {
case "plot-title-input":
layout.title.text = targetElem.value;
break;
case "title-font-choice":
layout.title.font.family = newValue;
let titleFontFamLabel = document.getElementsByClassName("title-font-family-input")[0];
titleFontFamLabel.innerHTML = newValue + ' <i class="dropdown-arrow fas fa-caret-down"></i>';
break;
case "title-font-size-input":
layout.title.font.size = targetElem.value;
break;
case "title-color":
layout.title.font.color = targetElem.value;
break;
case "gen-coil-color":
data[0].line.color = targetElem.value;
break;
case "gen-helix-color":
data[1].line.color = targetElem.value;
break;
case "gen-sheet-color":
data[2].line.color = targetElem.value;
break;
case "line-width-input":
let w = targetElem.value;
let widthLabel = document.getElementById("line-width-label");
widthLabel.innerHTML = w + "px";
if (data == null) {
return;
}
data[0].line.width = w;
data[1].line.width = w;
data[2].line.width = w;
break;
case "xaxis-title-input":
layout.xaxis.title.text = targetElem.value;
break;
case "xaxis-title-font-choice":
layout.xaxis.title.font.family = newValue;
let xaxisTitleFontFamLabel = document.getElementsByClassName("title-font-family-input")[1];
xaxisTitleFontFamLabel.innerHTML = newValue + ' <i class="dropdown-arrow fas fa-caret-down"></i>';
break;
case "xaxis-title-font-size-input":
layout.xaxis.title.font.size = targetElem.value;
break;
case "xaxis-title-color":
layout.xaxis.title.font.color = targetElem.value;
layout.xaxis.tickfont.color = targetElem.value;
layout.xaxis.tickcolor = targetElem.value;
layout.xaxis.linecolor = targetElem.value;
break;
case "xaxis-ticks-outside":
if (newValue.checked) {
layout.xaxis.ticks = "outside";
}
break;
case "xaxis-ticks-inside":
if (newValue.checked) {
layout.xaxis.ticks = "inside";
}
break;
case "xaxis-tick-length-input":
layout.xaxis.ticklen = targetElem.value;
break;
case "xaxis-tick-width-input":
layout.xaxis.tickwidth = targetElem.value;
break;
case "xaxis-tick-distance-input":
layout.xaxis.dtick = targetElem.value;
break;
case "xaxis-tick-font-size-input":
layout.xaxis.tickfont.size = targetElem.value;
break;
case "yaxis-title-input":
layout.yaxis.title.text = targetElem.value;
break;
case "yaxis-title-font-choice":
layout.yaxis.title.font.family = newValue;
let yaxisTitleFontFamLabel = document.getElementsByClassName("title-font-family-input")[2];
yaxisTitleFontFamLabel.innerHTML = newValue + ' <i class="dropdown-arrow fas fa-caret-down"></i>';
break;
case "yaxis-title-font-size-input":
layout.yaxis.title.font.size = targetElem.value;
break;
case "yaxis-title-color":
layout.yaxis.title.font.color = targetElem.value;
layout.yaxis.tickfont.color = targetElem.value;
layout.yaxis.tickcolor = targetElem.value;
layout.yaxis.linecolor = targetElem.value;
break;
case "yaxis-ticks-outside":
if (newValue.checked) {
layout.yaxis.ticks = "outside";
}
break;
case "yaxis-ticks-inside":
if (newValue.checked) {
layout.yaxis.ticks = "inside";
}
break;
case "yaxis-tick-length-input":
layout.yaxis.ticklen = targetElem.value;
break;
case "yaxis-tick-width-input":
layout.yaxis.tickwidth = targetElem.value;
break;
case "yaxis-tick-distance-input":
layout.yaxis.dtick = targetElem.value;
break;
case "yaxis-tick-font-size-input":
layout.yaxis.tickfont.size = targetElem.value;
break;
default:
}
Plotly.react(plotDiv, data, layout);
}