This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
1310 lines (1201 loc) · 57.5 KB
/
script.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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
const { invoke } = window.__TAURI__.tauri;
var assets_folder = undefined;
function sortTable(id, column_number, type) {
var table, rows, switching, i, x, y, shouldSwitch, dir = 0;
table = document.getElementById(id);
switching = true;
dir = "desc";
let headers = table.getElementsByTagName("tr")[0].getElementsByTagName("th");
var sorted = false;
if (headers[column_number].getAttribute('data-sort-order') == "asc") {
dir = "desc";
sorted = true
}
if (headers[column_number].getAttribute('data-sort-order') == "desc") {
dir = "asc";
sorted = true
}
for (var j = 0; j < headers.length; j++) {
headers[j].setAttribute('data-sort-order', "");
}
headers[column_number].setAttribute('data-sort-order', dir);
rows = Array.from(table.getElementsByTagName("tr"));
var values = [null];
for (i = 1; i < rows.length; i++) {
x = rows[i].getElementsByTagName("td")[column_number]
switch (type) {
case "string":
values.push(x.innerHTML.toLowerCase())
break
case "id":
values.push(x.firstChild.innerHTML.toLowerCase())
break
case "number":
values.push(Number(x.innerHTML))
break
}
}
if (sorted) {
rows = rows.reverse()
rows.splice(0, 0, rows[rows.length - 1])
rows.pop()
switching = false
} else {
switching = true
}
while (switching) {
switching = false;
/* Loop through all table rows (except the
first, which contains table headers): */
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
if (typeof values[i] == "string") {
if (values[i].localeCompare(values[i + 1], undefined, { numeric: true, sensitivity: "base" }) == 1) {
shouldSwitch = true;
break;
}
} else if (values[i] < values[i + 1]) {
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
var el = rows[i + 1]
rows.splice(i + 1, 1)
rows.splice(i, 0, el)
el = values[i + 1]
values.splice(i + 1, 1)
values.splice(i, 0, el)
//rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
// Remove old rows and append all new rows
while (table.lastChild) {
table.removeChild(table.lastChild);
}
var frag = document.createDocumentFragment();
for (var i = 0; i < rows.length; ++i) {
frag.appendChild(rows[i]);
}
table.appendChild(frag);
}
function Select(id) {
window.location.href = assets_folder + "/contigs/" + id.replace(':', '-') + ".html";
}
var dragging = false;
function get(name) {
if (name = (new RegExp('[?&]' + encodeURIComponent(name) + '=([^&]*)')).exec(location.search))
return decodeURIComponent(name[1]);
}
var t;
function Setup() {
if (assets_folder != undefined) {
var back_button = document.getElementById("back-button");
var ref = window.name.split('|').pop();
if (back_button && ref) {
back_button.href = ref;
var id = ref.split('/').pop().split('.')[0].replace(/-/g, ':');
back_button.innerText = id;
if (id != assets_folder.replace(/-/g, ':')) back_button.style = "display: inline-block;"
}
window.name = window.name + "|" + window.location.href
if (window.name.split('|').pop().split('/').pop().split('.')[0].replace(/-/g, ':') == assets_folder.replace(/-/g, ':')) window.name = window.location.href;
}
if (get('pos')) {
var pos = parseInt(get('pos'));
var text = document.getElementsByClassName('aside-seq')[0].innerText;
document.getElementsByClassName('aside-seq')[0].innerHTML = text.substring(0, pos) + "<span class='highlight'>" + text.substring(pos, pos + 1) + "</span>" + text.substring(pos + 1);
}
var elements = document.getElementsByClassName("copy-data")
for (var i = 0; i < elements.length; i++) {
elements[i].addEventListener("click", CopyGraphData)
var example = elements[i].getElementsByClassName("example")[0]
if (example)
example.innerHTML = GetGraphDataExample(elements[i]);
}
var elements = document.getElementsByClassName("user-help")
for (var i = 0; i < elements.length; i++) {
if (!elements[i].classList.contains("copy-data")) {
elements[i].addEventListener("click", (event) => { event.stopPropagation() })
}
elements[i].addEventListener("focusin", openHelp)
elements[i].addEventListener("focusout", closeHelp)
elements[i].addEventListener("mouseenter", openHelp)
elements[i].addEventListener("mouseleave", closeHelp)
}
// Highlight the correct nodes and table row on the homepage after clicking on the point graph
var split = window.location.href.split('#')
if (split.length == 2) {
var parts = split[1].split('_');
var target = parts[parts.length - 1];
var elements = document.querySelectorAll("[id$=\"" + target + "\"]")
for (var i = 0; i < elements.length; i++) {
elements[i].classList.add("highlight");
}
}
SpectrumSetUp()
}
function openHelp(event) {
var content = event.target.children[1]
content.classList.add("focus");
var box = content.getBoundingClientRect()
if (box.x < 0) {
content.style.marginLeft = -box.x + "px";
}
if (box.right > window.innerWidth) {
content.style.marginLeft = (window.innerWidth - box.right - 20) + "px";
}
if (box.bottom > window.innerHeight) {
content.style.marginTop = (window.innerHeight - box.bottom) + "px";
}
}
function closeHelp(event) {
var content = event.target.children[1]
content.classList.remove("focus")
}
function pauseEvent(e) {
if (e.stopPropagation) e.stopPropagation();
if (e.preventDefault) e.preventDefault();
e.cancelBubble = true;
e.returnValue = false;
return false;
}
function GoBack() {
var history = window.name.split('|');
history.pop(); //remove current
var url = history.pop();
window.name = history.join('|');
window.location.href = url;
}
function AlignmentDetails(number) {
AlignmentDetailsClear();
document.getElementById("alignment-details-" + number).className += " active";
}
function AlignmentDetailsClear() {
document.getElementById("index-menus").childNodes.forEach(a => a.className = "alignment-details");
}
// Copy the graph data for the clicked on graph
function CopyGraphData(event) {
var t = event.target;
if (event.target.classList.contains("mark"))
t = event.target.parentElement.parentElement;
if (event.target.classList.contains("copy-data"))
t = event.target.parentElement;
var elements = t.getElementsByClassName("graph-data");
for (var i = 0; i < elements.length; i++) {
elements[i].select();
elements[i].setSelectionRange(0, 99999);
navigator.clipboard.writeText(elements[i].value);
return;
}
}
/** Get the first four lines of the data to work as an example of how the data looks.
* @param {Element} element the .copy-data element to get the example for
*/
function GetGraphDataExample(element) {
var children = element.children;
if (element.classList.contains("mark"))
children = element.parentElement.parentElement.children;
if (element.classList.contains("copy-data"))
children = element.parentElement.children;
var example = undefined;
for (var i = 0; i < children.length; i++) {
if (children[i].classList.contains("graph-data")) {
example = children[i].value.split("\n").slice(0, 4);
break;
}
}
if (example == undefined) {
return "No data found";
} else {
var output = "";
for (var line in example) {
output += example[line].substring(0, Math.min(30, example[line].length)).replace(/ /gi, "<span class='sp'> </span>").replace(/\t/gi, "<span class='tab'>\t</span>")
if (example[line].length <= 30) {
output += "<span class='nl'></span>\n";
} else {
output += "...\n";
}
}
return output;
}
}
function ToggleCDRReads() {
document.querySelector(".alignment-body").classList.toggle("cdr");
}
function ToggleAlignmentComic() {
document.querySelector(".alignment-body").classList.toggle("comic");
}
/** Update the reads alignment to only show the read supporting the given position. Or if only this position is shown remove the highlight.
* @param {String} position the class of the position eg for position 42 this would be "a42".
* @param {Number} alignment_position the position of this node in the template alignment.
*/
function HighlightAmbiguous(position, alignment_position) {
var body = document.querySelector(".alignment-body")
if (body.classList.contains("highlight") && body.dataset.position == position) {
body.style.setProperty("--highlight-pos", "-1ch");
body.dataset.position = undefined;
document.querySelectorAll(".highlight").forEach(e => e.classList.remove("highlight")); // Removes highlight from all reads, the .node, and .alignment-body
} else {
document.querySelectorAll(".highlight").forEach(e => e.classList.remove("highlight"));
body.style.setProperty("--highlight-pos", String(alignment_position) + "ch");
body.dataset.position = position;
body.classList.add("highlight")
document.querySelector(".higher-order-graphs").classList.add("highlight")
document.querySelectorAll("." + position).forEach(e => e.classList.add("highlight")); // Highlights all reads and the .node
}
}
/* Spectrum viewer code */
function SpectrumSetUp() {
// Settings
var elements = document.querySelectorAll("#spectrum-wrapper .graphics-settings input");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("change", GraphicsSettings);
}
var elements = document.querySelectorAll("#spectrum-wrapper .graphics-settings button");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("click", GraphicsSettings);
}
document.querySelectorAll("#spectrum-wrapper .error-graph-settings input")
.forEach(ele => ele.addEventListener("change", ErrorGraphSettings));
var elements = document.querySelectorAll("#spectrum-wrapper .error-graph-settings .manual-zoom input");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("change", ManualZoomSpectrumGraph);
}
var elements = document.querySelectorAll("#spectrum-wrapper .peptide-settings input");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("change", PeptideSettings);
}
var elements = document.querySelectorAll("#spectrum-wrapper .peptide-settings button");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("click", PeptideSettings);
}
var elements = document.querySelectorAll("#spectrum-wrapper .spectrum-settings input");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("change", SpectrumSettings);
}
var elements = document.querySelectorAll("#spectrum-wrapper .spectrum-settings button");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("click", SpectrumSettings);
}
var elements = document.querySelectorAll("#spectrum-wrapper .spectrum-settings .manual-zoom input");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("change", ManualZoom);
}
var elements = document.querySelectorAll("#spectrum-wrapper .spectrum-settings .reset-zoom");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("mouseup", spectrumZoomOut)
elements[i].addEventListener("keyup", e => { if (e.key == "Enter") spectrumZoomOut(e) })
}
// Legend
var elements = document.querySelectorAll("#spectrum-wrapper .legend .ion");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("mouseenter", e => { ToggleHighlight(e.target, false, true) });
elements[i].addEventListener("mouseleave", e => { ToggleHighlight(e.target, false, false) });
elements[i].addEventListener("focusin", e => { ToggleHighlight(e.target, false, true) });
elements[i].addEventListener("focusout", e => { ToggleHighlight(e.target, false, false) });
elements[i].addEventListener("mouseup", e => { ToggleHighlight(e.target, true) });
elements[i].addEventListener("keyup", e => { if (e.key == "Enter") e.target.click() });
}
var elements = document.querySelectorAll("#spectrum-wrapper .legend .unassigned");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("keyup", e => { if (e.key == "Enter") e.target.click() });
}
var elements = document.querySelectorAll("#spectrum-wrapper .legend .legend-peptide");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("keyup", e => { if (e.key == "Enter") e.target.click() });
}
var elements = document.querySelectorAll("#spectrum-wrapper .legend input");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("change", SpectrumInputChange);
}
// Peptide
var elements = document.querySelectorAll("#spectrum-wrapper .peptide span");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("mouseenter", e => { SequenceElementEvent(e, false, true) });
elements[i].addEventListener("mouseleave", e => { SequenceElementEvent(e, false, false) });
elements[i].addEventListener("focusin", e => { SequenceElementEvent(e, false, true) });
elements[i].addEventListener("focusout", e => { SequenceElementEvent(e, false, false) });
elements[i].addEventListener("mousedown", e => { SequenceElementEvent(e, false, true) });
elements[i].addEventListener("mouseup", e => { SequenceElementEvent(e, true) });
elements[i].addEventListener("keyup", e => { if (e.key == "Enter") e.target.click() });
}
// Spectrum
document.querySelectorAll("#spectrum-wrapper .wrapper").forEach(element => {
element.addEventListener("mousedown", spectrumDragStart)
element.addEventListener("mousemove", spectrumDrag)
element.addEventListener("mouseup", spectrumDragEnd)
element.addEventListener("mouseout", spectrumDragOut)
})
document.querySelectorAll("#spectrum-wrapper .peak").forEach(element => {
element.addEventListener("mousedown", spectrumDistanceStart)
element.addEventListener("mouseup", spectrumDistanceEnd)
})
document.querySelectorAll("#spectrum-wrapper .canvas-wrapper").forEach(element => {
var d = element.dataset;
d.minMz = 0;
d.maxMz = d.initialMaxMz;
d.maxIntensity = d.initialMaxIntensity;
})
document.querySelectorAll("#spectrum-wrapper .peak:not(.fragment)").forEach(element => {
element.addEventListener("click", ForceShowLabels)
})
document.querySelectorAll("#spectrum-wrapper .canvas-spectrum").forEach(element => {
element.addEventListener("wheel", spectrumScroll)
})
document.addEventListener("keyup", e => {
if (e.key == "0" && e.ctrlKey) {
spectrumZoomOut(e)
} else if ((e.key == "=" || e.key == "-") && e.ctrlKey) {
const wrapper = document.querySelector(".canvas-wrapper");
const minMz = Number(wrapper.dataset.minMz);
const maxMz = Number(wrapper.dataset.maxMz);
const delta = 0.05 * (maxMz - minMz);
const direction = (e.key == "-") ? -1 : 1;
Zoom(wrapper, Math.max(0, minMz + direction * delta * 0.5), maxMz - direction * delta * 0.5, Number(wrapper.dataset.maxIntensity));
}
})
// Spectrum graph
var elements = document.querySelectorAll("#spectrum-wrapper .error-graph");
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener("mousemove", SpectrumGraphMouseMove)
}
// Make the axes look nice
UpdateSpectrumAxes(document.querySelector("#spectrum-wrapper .canvas-wrapper"));
// Update error graph
UpdateErrorGraph()
}
/**
* Force show the labels of a peak (click event)
* @param {MouseEvent} e The event
*/
function ForceShowLabels(e) {
let spectrum = document.getElementById("spectrum-wrapper");
if (spectrum.classList.contains("force-show-label")) {
if ("showLabelManually" in e.target.dataset) {
delete e.target.dataset.showLabelManually;
} else {
e.target.dataset.showLabelManually = "true";
}
} else if (spectrum.classList.contains("force-show-m-z")) {
if ("showMZManually" in e.target.dataset) {
delete e.target.dataset.showMZManually;
} else {
e.target.dataset.showMZManually = "true";
}
} else if (spectrum.classList.contains("force-show-hide")) {
e.target.dataset.showLabelManually = "false";
e.target.dataset.showMZManually = "false";
}
}
/**
* @type {EventTarget}
*/
let sequence_element_start = undefined;
/**
* Do the event for a sequence element (one element in the peptide)
* @param {MouseEvent} e The event
* @param {boolean} permanent If it will be applied permanently (true if clicked, false if hovered)
* @param {boolean?} turn_on If this is the turns on (true) or turns off (false) the event, the default is toggle (null)
*/
function SequenceElementEvent(e, permanent, turn_on = null) {
// if (selected_colour != "highlight") {
if (e.type == "mouseup" && sequence_element_start != undefined && sequence_element_start.dataset.pos != e.target.dataset.pos) {
let state = sequence_element_start.classList.contains(selected_colour);
let start = sequence_element_start.dataset.pos.split("-");
let end = e.target.dataset.pos.split("-");
if (start[0] == end[0]) {
let range = [Math.min(Number(start[1]), Number(end[1])), Math.max(Number(start[1]), Number(end[1]))];
document.querySelectorAll(".spectrum .peptide>span[title]").forEach(element => {
let pos = element.dataset.pos.split("-");
element.classList.remove("select");
if (pos[0] == start[0] && Number(pos[1]) >= range[0] && Number(pos[1]) <= range[1]) {
element.classList.remove("red", "green", "blue", "yellow", "purple", "default", "highlight");
ToggleHighlight(element, true, (selected_colour == "remove" ? false : !state), selected_colour);//, ".p" + pos[0] + "-" + pos[1]);
if (selected_colour != "remove") {
element.classList.toggle(selected_colour, !state);
element.classList.toggle("highlight", !state);
}
}
})
}
sequence_element_start = undefined;
} else if (permanent) {
let state = e.target.classList.contains(selected_colour);
e.target.classList.remove("red", "green", "blue", "yellow", "purple", "default", "highlight");
ToggleHighlight(e.target, true, (selected_colour == "remove" ? false : !state), selected_colour);
if (selected_colour != "remove") {
e.target.classList.toggle(selected_colour, !state);
e.target.classList.toggle("highlight", !state);
}
sequence_element_start = undefined;
} else if (e.type == "mousedown") {
sequence_element_start = e.target;
} else if (e.type == "mouseenter" && sequence_element_start != undefined) {
let start = sequence_element_start.dataset.pos.split("-");
let end = e.target.dataset.pos.split("-");
if (start[0] == end[0]) {
let range = [Math.min(Number(start[1]), Number(end[1])), Math.max(Number(start[1]), Number(end[1]))];
document.querySelectorAll(".spectrum .peptide>span[title]").forEach(element => {
let pos = element.dataset.pos.split("-");
if (pos[0] == start[0] && Number(pos[1]) >= range[0] && Number(pos[1]) <= range[1]) {
element.classList.add("select");
} else {
element.classList.remove("select");
}
})
}
} else if (sequence_element_start == undefined && e.type == "mouseenter") {
let state = e.target.classList.contains(selected_colour);
ToggleHighlight(e.target, false, true, selected_colour);
} else if (sequence_element_start == undefined && e.type == "mouseleave") {
let state = e.target.classList.contains(selected_colour);
ToggleHighlight(e.target, false, false, selected_colour);
}
}
export function SetUpSpectrumInterface() {
document.getElementById("spectrum-wrapper").classList.add("error-graph-relative");
SpectrumSetUp();
var event = new Event('change');
document.getElementById("error-graph-y-max").dispatchEvent(event);
}
var highlight;
/**
* Toggle a highlight
* @param {Element} t The target
* @param {boolean} permanent If it will be applied permanently (true if clicked, false if hovered)
* @param {boolean?} state If this is the apply (true) or clear (false) operation
* @param {string} selected_colour For peptide highlights the colour selected or "default"
*/
function ToggleHighlight(t, permanent, state, selected_colour) {
let current_state = t.classList.contains("permanent");
if (permanent) t.classList.toggle("permanent", state);
if (t.classList.contains("permanent") && !permanent) return;
highlight = t;
var container = document.getElementById("spectrum-wrapper");
// Set the correct state for a single peak
function toggle(element) {
// Make sure the n is set, this tracks the number of reasons why a peak should be highlighted
if (element.dataset.n == undefined) element.dataset.n = 0;
if (element.dataset.n < 0) element.dataset.n = 0;
// Clear any colour set for this peak
element.classList.remove("red", "green", "blue", "yellow", "purple", "default");
if (permanent) {
// A permanent highlight, where this position or ion type is clicked
if (state === true) {
// If this position in the peptide was already highlighted do not add one to n
if (current_state != state) element.dataset.n = Number(element.dataset.n) + 1;
// Set up the classes
if (element.dataset.n == 1) element.classList.add("highlight");
element.classList.add(selected_colour);
}
else if (state === false) {
if (current_state != state) element.dataset.n = Number(element.dataset.n) - 1;
// Set up the classes
if (element.dataset.n <= 0) element.classList.remove("highlight");
element.classList.remove(selected_colour);
} else {
console.error("When using 'permanent' the state should be set");
}
} else {
// A temporary highlight, while hovering over this position or ion type
if (element.dataset.n == undefined || element.dataset.n == 0) {
if (state === true) {
element.classList.add("highlight", selected_colour);
} else if (state === null) {
element.classList.toggle("highlight", selected_colour);
} else {
element.classList.remove("highlight", selected_colour);
}
}
}
}
// Select all peaks for this operation
container.querySelectorAll(".canvas").forEach(canvas => {
let selector = "";
if (t.classList.contains("n-term")) {
selector = ":is(.a, .b, .c, .d, .v)";
} else if (t.classList.contains("c-term")) {
selector = ":is(.w, .x, .y, .z)";
} else if (t.classList.contains("ion")) {
var cl = t.classList[1];
selector = "." + cl;
} else if (t.classList.contains("name")) {
selector = ".p" + t.dataset.pos;
} else {
selector = ".p" + t.dataset.pos;
}
canvas.querySelectorAll(selector).forEach(element => toggle(element))
})
// Set the state for the whole canvas (is there anything highlighted still)
if (state === true || container.querySelector(".permanent") != null) {
container.querySelectorAll(".canvas").forEach(canvas => {
canvas.classList.add("highlight");
})
} else {
container.querySelectorAll(".canvas").forEach(canvas => {
canvas.classList.remove("highlight");
})
}
}
/** The mouse moved in the spectrum graph.
* @param {MouseEvent} event
*/
function SpectrumGraphMouseMove(event) {
var data = event.target;
if (data.classList.contains("point")) data = data.parentElement;
var spectrum = data.parentElement;
var baseline = data.getBoundingClientRect().top;
var offset = event.clientY - baseline;
var el = spectrum.querySelector(".ruler");
el.style.top = offset + "px";
var el = spectrum.querySelector("#ruler-value");
let style = getComputedStyle(spectrum);
let min = Number(style.getPropertyValue(`--y-min`));
let max = Number(style.getPropertyValue(`--y-max`));
el.innerText = fancyRound(max, min, offset / data.clientHeight * - 1 * (max - min) + max, 1);
}
/** Zoom the spectrum manually.
* @param {Event} event
*/
function ManualZoom(event) {
var spectrum = event.target.parentElement.parentElement;
var min = Number(spectrum.querySelector(".mz-min").value);
var max = Number(spectrum.querySelector(".mz-max").value);
var maxI = Number(spectrum.querySelector(".intensity-max").value);
spectrum.querySelectorAll(".canvas-wrapper").forEach(canvas => Zoom(canvas, min, max, maxI));
}
/** Zoom the spectrum manually.
* @param {Event} event
*/
function ManualZoomSpectrumGraph(event) {
var min_y = Number(document.getElementById("error-graph-y-min").value);
var max_y = Number(document.getElementById("error-graph-y-max").value);
document.getElementById("spectrum-wrapper")
.querySelectorAll(".error-graph").forEach(canvas => ZoomSpectrumGraph(canvas, min_y, max_y));
}
/** Setup properties of the spectrum for publication
* @param {Event} event
*/
function GraphicsSettings(event) {
var cl = event.target.className;
let spectrum_wrapper = document.getElementById("spectrum-wrapper");
if (cl == "width") {
spectrum_wrapper.style.setProperty("--width", event.target.value);
} else if (cl == "height") {
spectrum_wrapper.style.setProperty("--height", event.target.value);
} else if (cl == "fs-peptide") {
spectrum_wrapper.style.setProperty("--fs-peptide", event.target.value);
} else if (cl == "stroke-peptide") {
spectrum_wrapper.style.setProperty("--stroke-peptide", event.target.value);
} else if (cl == "fs-spectrum") {
spectrum_wrapper.style.setProperty("--fs-spectrum", event.target.value);
} else if (cl == "stroke-spectrum") {
spectrum_wrapper.style.setProperty("--stroke-spectrum", event.target.value);
} else if (cl == "stroke-spectrum-unassigned") {
spectrum_wrapper.style.setProperty("--stroke-spectrum-unassigned", event.target.value);
}
}
/** Setup properties of the spectrum for publication
* @param {Event} event
*/
function ErrorGraphSettings(event) {
var id = event.target.id;
let spectrum_wrapper = document.getElementById("spectrum-wrapper");
if (id == "error-graph-y-type-absolute") {
spectrum_wrapper.classList.toggle("error-graph-absolute");
spectrum_wrapper.classList.remove("error-graph-relative");
UpdateErrorGraph()
} else if (id == "error-graph-y-type-relative") {
spectrum_wrapper.classList.remove("error-graph-absolute");
spectrum_wrapper.classList.toggle("error-graph-relative");
UpdateErrorGraph()
} else if (id == "error-graph-intensity") {
spectrum_wrapper.classList.toggle("error-graph-intensity");
} else {
UpdateErrorGraph()
}
}
/**
* Update for the whole error graph the --y values for all points
*/
function UpdateErrorGraph() {
let spectrum_wrapper = document.getElementById("spectrum-wrapper");
let graph = document.getElementById("error-graph");
let points = graph.querySelectorAll(".point");
let relative = spectrum_wrapper.classList.contains("error-graph-relative");
let assigned_mode = document.getElementById("error-graph-x-type-assigned").checked;
let use_a = document.getElementById("error-graph-ion-a").checked;
let use_b = document.getElementById("error-graph-ion-b").checked;
let use_c = document.getElementById("error-graph-ion-c").checked;
let use_x = document.getElementById("error-graph-ion-x").checked;
let use_y = document.getElementById("error-graph-ion-y").checked;
let use_z = document.getElementById("error-graph-ion-z").checked;
let show_assigned = document.getElementById("error-graph-show-assigned").checked;
document.getElementById("error-graph-y-title").innerText =
"The error for all " +
(assigned_mode ? "assigned " : (show_assigned ? "" : "unassigned ")) +
"peaks as compared to " +
(assigned_mode ? "the annotation" : "the closest theoretical ion") +
" (" + (relative ? "ppm" : "Da") + ")";
let points_y = Array();
points.forEach((point) => {
let y = Infinity;
let label = "";
if (assigned_mode) {
if (relative) {
y = Number(point.dataset.aRel);
} else {
y = Number(point.dataset.aAbs);
}
} else {
if (!show_assigned && point.dataset.aRel != "undefined") {
// hide
} else if (relative) {
if (use_a && Math.abs(y) > Math.abs(Number(point.dataset.uARelValue))) {
y = Number(point.dataset.uARelValue);
label = point.dataset.uARelFragment
}
if (use_b && Math.abs(y) > Math.abs(Number(point.dataset.uBRelValue))) {
y = Number(point.dataset.uBRelValue);
label = point.dataset.uBRelFragment
}
if (use_c && Math.abs(y) > Math.abs(Number(point.dataset.uCRelValue))) {
y = Number(point.dataset.uCRelValue);
label = point.dataset.uCRelFragment
}
if (use_x && Math.abs(y) > Math.abs(Number(point.dataset.uXRelValue))) {
y = Number(point.dataset.uXRelValue);
label = point.dataset.uXRelFragment
}
if (use_y && Math.abs(y) > Math.abs(Number(point.dataset.uYRelValue))) {
y = Number(point.dataset.uYRelValue);
label = point.dataset.uYRelFragment
}
if (use_z && Math.abs(y) > Math.abs(Number(point.dataset.uZRelValue))) {
y = Number(point.dataset.uZRelValue);
label = point.dataset.uZRelFragment
}
} else {
if (use_a && Math.abs(y) > Math.abs(Number(point.dataset.uAAbsValue))) {
y = Number(point.dataset.uAAbsValue);
label = point.dataset.uAAbsFragment
}
if (use_b && Math.abs(y) > Math.abs(Number(point.dataset.uBAbsValue))) {
y = Number(point.dataset.uBAbsValue);
label = point.dataset.uBAbsFragment
}
if (use_c && Math.abs(y) > Math.abs(Number(point.dataset.uCAbsValue))) {
y = Number(point.dataset.uCAbsValue);
label = point.dataset.uCAbsFragment
}
if (use_x && Math.abs(y) > Math.abs(Number(point.dataset.uXAbsValue))) {
y = Number(point.dataset.uXAbsValue);
label = point.dataset.uXAbsFragment
}
if (use_y && Math.abs(y) > Math.abs(Number(point.dataset.uYAbsValue))) {
y = Number(point.dataset.uYAbsValue);
label = point.dataset.uYAbsFragment
}
if (use_z && Math.abs(y) > Math.abs(Number(point.dataset.uZAbsValue))) {
y = Number(point.dataset.uZAbsValue);
label = point.dataset.uZAbsFragment
}
}
}
if (Number.isNaN(y) || y == Infinity) {
point.classList.toggle("hidden", true);
} else {
point.classList.toggle("hidden", false);
point.style.setProperty("--y", y);
point.dataset.label = label;
points_y.push(y);
}
})
// Call
invoke("density_graph", { data: points_y }).then((result) => {
document.getElementById("error-graph-density").innerHTML = result;
}).catch((error) => {
console.error("density estimation wrong", error)
})
}
/** Setup properties of the spectrum for publication
* @param {Event} event
*/
let selected_colour = "default";
function PeptideSettings(event) {
var t = event.target;
let spectrum_wrapper = document.getElementById("spectrum-wrapper");
if (t.id == "spectrum-compact") {
spectrum_wrapper.classList.toggle("compact");
} else if (t.name == "highlight") {
selected_colour = t.value;
} else if (t.id == "clear-colour") {
spectrum_wrapper.querySelectorAll(".peptide>span").forEach(item => item.classList.remove("red", "green", "blue", "yellow", "purple", "default", "highlight"))
spectrum_wrapper.querySelectorAll(".ion-series .permanent").forEach(item => item.classList.remove("permanent"))
spectrum_wrapper.querySelectorAll(".peak").forEach(item => { item.classList.remove("red", "green", "blue", "yellow", "purple", "default", "highlight"); item.dataset.n = 0; })
spectrum_wrapper.querySelectorAll(".point").forEach(item => { item.classList.remove("red", "green", "blue", "yellow", "purple", "default", "highlight"); item.dataset.n = 0; })
spectrum_wrapper.querySelectorAll(".canvas").forEach(item => item.classList.remove("highlight"))
}
}
/** Setup properties of the spectrum for publication
* @param {Event} event
*/
function SpectrumSettings(event) {
var t = event.target;
var cl = t.className;
let spectrum_wrapper = document.getElementById("spectrum-wrapper");
let canvas = spectrum_wrapper.querySelector(".canvas-wrapper");
if (cl == "theoretical") {
spectrum_wrapper.classList.toggle("theoretical");
} else if (t.id == "peak-colour-ion") {
spectrum_wrapper.classList.add("legend-ion");
spectrum_wrapper.classList.remove("legend-peptide", "legend-peptidoform", "legend-none");
} else if (t.id == "peak-colour-peptide") {
spectrum_wrapper.classList.remove("legend-ion", "legend-peptidoform", "legend-none");
spectrum_wrapper.classList.add("legend-peptide");
} else if (t.id == "peak-colour-peptidoform") {
spectrum_wrapper.classList.remove("legend-ion", "legend-peptide", "legend-none");
spectrum_wrapper.classList.add("legend-peptidoform");
} else if (t.id == "peak-colour-none") {
spectrum_wrapper.classList.remove("legend-ion", "legend-peptide", "legend-peptidoform");
spectrum_wrapper.classList.add("legend-none");
} else if (t.id == "peak-distance") {
if (spectrum_wrapper.classList.contains("ruler")) {
spectrum_wrapper.classList.remove("ruler");
ruler_mode = false;
} else {
spectrum_wrapper.classList.add("ruler");
ruler_mode = true;
}
} else if (t.id == "force-show-none") {
spectrum_wrapper.classList.remove("force-show-label", "force-show-m-z", "force-show-hide");
} else if (t.id == "force-show-label") {
spectrum_wrapper.classList.remove("force-show-m-z", "force-show-hide");
spectrum_wrapper.classList.add("force-show-label");
} else if (t.id == "force-show-m-z") {
spectrum_wrapper.classList.remove("force-show-label", "force-show-hide");
spectrum_wrapper.classList.add("force-show-m-z");
} else if (t.id == "force-show-hide") {
spectrum_wrapper.classList.remove("force-show-label", "force-show-m-z");
spectrum_wrapper.classList.add("force-show-hide");
} else if (t.id == "force-show-clear") {
spectrum_wrapper.querySelectorAll(".peak").forEach(element => { delete element.dataset.showLabelManually; delete element.dataset.showMZManually; });
} else if (cl == "unassigned") {
if (t.checked) { // Will be adding all background peaks
spectrum_wrapper.classList.add('show-unassigned');
if (Number(canvas.dataset.maxIntensity) == Number(canvas.dataset.initialMaxIntensityAssigned)) {
canvas.dataset.maxIntensity = canvas.dataset.initialMaxIntensity;
canvas.style.setProperty("--max-intensity", canvas.dataset.maxIntensity);
UpdateSpectrumAxes(canvas)
}
} else { // Will be removing all background peaks
spectrum_wrapper.classList.remove('show-unassigned');
if (canvas.dataset.maxIntensity == canvas.dataset.initialMaxIntensity) {
canvas.dataset.maxIntensity = canvas.dataset.initialMaxIntensityAssigned;
canvas.style.setProperty("--max-intensity", canvas.dataset.maxIntensity);
UpdateSpectrumAxes(canvas)
}
}
} else if (t.id == "spectrum-label-charge") {
spectrum_wrapper.classList.toggle("show-charge", t.checked);
} else if (t.id == "spectrum-label-series") {
spectrum_wrapper.classList.toggle("show-series", t.checked);
} else if (t.id == "spectrum-label-glycan-id") {
spectrum_wrapper.classList.toggle("show-glycan-id", t.checked);
} else if (t.id == "spectrum-label-peptide-id") {
spectrum_wrapper.classList.toggle("show-peptide-id", t.checked);
} else if (t.id == "spectrum-label-neutral-losses") {
spectrum_wrapper.classList.toggle("show-neutral-losses", t.checked);
} else if (t.id == "spectrum-label-cross-links") {
spectrum_wrapper.classList.toggle("show-cross-links", t.checked);
} else if (t.id == "spectrum-label-ambiguous-amino-acids") {
spectrum_wrapper.classList.toggle("show-ambiguous-amino-acids", t.checked);
} else if (t.id == "spectrum-label-modifications") {
spectrum_wrapper.classList.toggle("show-modifications", t.checked);
} else if (t.id == "spectrum-label-charge-carriers") {
spectrum_wrapper.classList.toggle("show-charge-carriers", t.checked);
} else if (t.id == "rotate-label") {
spectrum_wrapper.classList.toggle("rotate-label", t.checked);
} else if (t.id == "spectrum-mz-min") {
canvas.dataset.minMz = Number(t.value);
canvas.style.setProperty("--min-mz", canvas.dataset.minMz);
UpdateSpectrumAxes(canvas)
} else if (t.id == "spectrum-mz-max") {
canvas.dataset.maxMz = Number(t.value);
canvas.style.setProperty("--max-mz", canvas.dataset.maxMz);
UpdateSpectrumAxes(canvas)
} else if (t.id == "spectrum-intensity-max") {
canvas.dataset.maxIntensity = Number(t.value);
canvas.style.setProperty("--max-intensity", canvas.dataset.maxIntensity);
UpdateSpectrumAxes(canvas)
} else if (t.id == "spectrum-ticks-y" || t.id == "spectrum-ticks-x") {
UpdateSpectrumAxes(canvas)
} else if (t.type == "range" || t.type == "number") { // Peak labels + masses
var ele = document.getElementById(
(t.type == "number") ?
t.id.substring(0, t.id.length - 6)
: t.id + "-value");
ele.value = t.value;
spectrum_wrapper.querySelectorAll(".canvas-spectrum").forEach(
canvas => SpectrumUpdateLabels(canvas))
} else if (t.id == "y-sqrt") {
spectrum_wrapper.classList.toggle("y-sqrt", t.checked);
UpdateSpectrumAxes(canvas)
} else if (t.id == "y-percentage") {
spectrum_wrapper.classList.toggle("y-percentage", t.checked);
UpdateSpectrumAxes(canvas)
}
}
/** Update the spectrum to only show the label for peaks within the given percentage group
* @param {Element} canvas the canvas ('.canvas') to work on
*/
function SpectrumUpdateLabels(canvas) {
const max = Number(canvas.parentElement.style.getPropertyValue("--max-intensity"));
const label_value = Number(document.getElementById("spectrum-label-value").value);
const mz_value = Number(document.getElementById("spectrum-m-z-value").value);
const label_threshold = (100 - label_value) / 100 * max;
const mz_threshold = (100 - mz_value) / 100 * max;
setTimeout(() => {
for (let index = 1; index < canvas.children.length; index++) {
const peak = canvas.children[index];
const intensity = Number(peak.style.getPropertyValue("--intensity"));
// Update shown labels
if (label_value != 0 && intensity >= label_threshold) {
peak.dataset.showLabel = "true";
} else {
peak.dataset.showLabel = "";
}
if (mz_value != 0 && intensity >= mz_threshold) {
peak.dataset.showMZ = "true";
} else {
peak.dataset.showMZ = "";
}
// Update cut peaks
if (intensity > max) {
peak.dataset.cut = "true";
} else {
peak.dataset.cut = "";
}
}
}, 0)
}
var startPoint;
var selection;
var linked_selection;
var last_selection;
var ruler_mode = false;
function spectrumDragStart(event) {
var wrapper = event.target;
if (wrapper.classList.contains("peptide") || wrapper.classList.contains("canvas-wrapper")) wrapper = wrapper.parentElement;
if (wrapper.classList.contains("canvas") || wrapper.classList.contains("y-axis") || wrapper.classList.contains("x-axis")) wrapper = wrapper.parentElement.parentElement;
if (wrapper.classList.contains("wrapper")) {
var canvas = wrapper.querySelector(".canvas");
startPoint = event.pageX - wrapper.getBoundingClientRect().x;
selection = canvas.querySelector(".selection");
if (wrapper.classList.contains("first")) {
linked_selection = wrapper.parentElement.querySelector(".selection.second")
} else if (wrapper.classList.contains("second")) {
linked_selection = wrapper.parentElement.querySelector(".selection.first")
}
wrapper.classList.add("dragging")
selection.hidden = false;
if (linked_selection != undefined) {
linked_selection.hidden = false;
linked_selection.classList.add("linked");
}
updateSelections(wrapper, canvas, startPoint, event.pageY - wrapper.getBoundingClientRect().y, startPoint);
}
}
function updateSelections(wrapper, canvas, x, y, end_x) {
var wrapper_box = wrapper.getBoundingClientRect();
var canvas_box = canvas.getBoundingClientRect();
var new_x = Math.min(Math.max(0, x - (canvas_box.left - wrapper_box.left)), canvas_box.width);
var new_y = Math.min(Math.max(1, y - (canvas_box.top - wrapper_box.top)), canvas_box.height);
var new_end_x = Math.min(Math.max(0, end_x - (canvas_box.left - wrapper_box.left)), canvas_box.width);
var new_w = Math.min(Math.abs(new_end_x - new_x), canvas_box.width);
var new_x = Math.min(new_x, new_end_x);
if (selection != undefined) updateSelection(selection, new_x, new_y, new_w);
if (linked_selection != undefined) updateSelection(linked_selection, new_x, new_y, new_w);
}
function updateSelection(select, offsetX, offsetY, width) {
select.style.setProperty("left", offsetX + "px")
select.style.setProperty("width", width + "px")
select.style.setProperty("height", select.parentElement.getBoundingClientRect().height - offsetY + "px")