-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
1310 lines (1199 loc) · 54.4 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="author" content="Joe Brown" />
<title>bedqc</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Fraunces&display=swap" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css" />
<link rel="stylesheet"
href="https://cdn.datatables.net/v/bs5/dt-1.11.3/b-2.2.1/b-html5-2.2.1/fh-3.2.1/datatables.min.css" />
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/10.0.2/math.min.js"></script>
<script src="https://cdn.datatables.net/v/bs5/dt-1.11.3/b-2.2.1/b-html5-2.2.1/fh-3.2.1/datatables.min.js"></script>
<script src="https://cdn.plot.ly/plotly-2.2.0.min.js"></script>
<script src="https://biowasm.com/cdn/v3/aioli.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.1/papaparse.min.js"></script>
<style>
body {
height: 100vh;
margin: 0px;
padding: 0px;
}
footer {
min-height: 200px;
}
.brand {
font-family: "Fraunces", serif;
font-size: 3rem;
letter-spacing: -0.05em;
color: #d91e30 !important;
}
.form-control-lg {
font-size: 2rem;
}
.form-control-lg::file-selector-button {
min-height: 120px;
}
.input-group-display {
min-height: 122px;
}
#url-upload {
font-size: 2rem;
width: 155px;
}
#url-input {
font-size: 2rem;
}
label {
cursor: pointer;
}
.dataTable>thead>tr>th[class*="sort"]:before,
.dataTable>thead>tr>th[class*="sort"]:after {
content: "" !important;
}
div.dataTables_wrapper div.dataTables_info {
padding-top: 0 !important;
}
.info-card {
min-height: 165px;
}
.plot-card {
min-height: 362px;
}
.tab-content>.tab-pane:not(.active),
.pill-content>.pill-pane:not(.active) {
display: block;
height: 0;
overflow-y: hidden;
}
table {
white-space: pre;
font-size: 1.2rem;
}
.btn-circle {
width: 18px;
height: 18px;
text-align: center;
padding: 4px 0;
font-size: 14px;
line-height: 0.5;
border-radius: 15px;
font-weight: bold;
}
.text-primary code {
color: rgba(var(--bs-primary-rgb), var(--bs-text-opacity)) !important;
}
.btn-floating {
line-height: 1.95;
font-size: 1.4rem;
}
.feature-icon {
font-size: 3rem;
}
</style>
</head>
<nav>
<div class="px-4 py-5 my-5 text-center">
<h1 class="display-5 fw-bold brand">bedqc</h1>
<div class="col-lg-6 mx-auto">
<p class="lead mb-4">A tool to validate and profile genomic intervals in BED format.</p>
</div>
<div class="col-12 col-lg-8 mx-auto p-3">
<div class="" id="local-file-upload">
<input class="form-control form-control-lg" type="file" id="file-upload" />
</div>
<div class="" id="remote-file-upload" hidden>
<div class="input-group input-group-display">
<button class="btn btn-outline-secondary text-dark" type="button" id="url-upload">Upload</button>
<input type="text" class="form-control" id="url-input"
placeholder="https://GRCh38_refseq_genes.bed">
</div>
</div>
<div class="col-lg-8 mx-auto pt-1 text-muted" hidden>
<button class="btn btn-link p-0 align-baseline" id="local-file-btn">Browse for a local file</button>
or
<button class="btn btn-link p-0 align-baseline" id="remote-file-btn">paste in a URL</button>.
100 MB file size limit.
</div>
</div>
<div class="col-lg-8 mx-auto text-danger pt-2" id="file-size-warning" hidden></div>
</div>
<div id="introduction">
<div class="container px-4 py-5">
<!-- <h2 class="pb-2 border-bottom">Hanging icons</h2> -->
<div class="row g-4 py-5 row-cols-1 row-cols-lg-3">
<div class="col d-flex align-items-start">
<div class="icon-square text-dark flex-shrink-0 me-3">
<i class="bi bi-lock feature-icon"></i>
</div>
<div>
<h2>Secure processing</h2>
<p>Your files are processed by your browser on your local machine using javascript and tools
from the <a href="https://github.com/biowasm/aioli" class="link-dark">biowasm project</a>.
No data is transferred from your local machine.</p>
</div>
</div>
<div class="col d-flex align-items-start">
<div class="icon-square text-dark flex-shrink-0 me-3">
<i class="bi bi-check2-all feature-icon"></i>
</div>
<div>
<h2>BED validation</h2>
<p>Your file is validated against <a href="https://samtools.github.io/hts-specs/BEDv1.pdf"
class="link-dark">BEDv1 file specifications</a>. File delimiter,
newline character, 0-length intervals, and other common issues are reported.</p>
</div>
</div>
<div class="col d-flex align-items-start">
<div class="icon-square text-dark flex-shrink-0 me-3">
<i class="bi bi-bar-chart-line feature-icon"></i>
</div>
<div>
<h2>Try an example</h2>
<p>We have prepared two examples to discover bedqc features</p>
<ul>
<li>
<button type="button" class="btn btn-link" id="load-example-cgpislands">CpG islands,
GRCh38</button>
</li>
<li>
<button type="button" class="btn btn-link" id="load-example-truncatedexons">Truncated
file, GRCh38</button>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="container col-xxl-10 px-4 pt-5 pb-0">
<h1 class="display-5 fw-bold lh-1 mb-3">Analyzing your BED file</h1>
</div>
<div class="container col-xxl-10 px-4 py-5">
<div class="row flex-lg-row-reverse align-items-center g-5 py-5">
<div class="col-10 col-sm-12 col-lg-8">
<img src="img/expected.png" class="d-block mx-lg-auto img-fluid" alt="typical interval density"
width="900" height="700" loading="lazy" />
</div>
<div class="col-lg-4">
<h1 class="display-5 fw-bold lh-1 mb-3">Check interval density by chromosome</h1>
<p class="lead">You can look at the interval density or count by chromosome compared to what would
be expected based on the relative length of each chromosome. For example, segmental duplications
have a much higher density than expected on chr16 in the human genome.</p>
</div>
</div>
</div>
<div class="container col-xxl-10 px-4 py-5">
<div class="row flex-lg-row-reverse align-items-center g-5 py-5">
<div class="col-lg-4">
<h1 class="display-5 fw-bold lh-1 mb-3">Find truncated files</h1>
<p class="lead">Use interval counts plot to diagnose truncated or incomplete files based on the
expected chromosomes of a genome build.</p>
</div>
<div class="col-10 col-sm-12 col-lg-8">
<img src="img/truncated.png" class="d-block mx-lg-auto img-fluid" alt="typical interval density"
width="900" height="700" loading="lazy" />
</div>
</div>
</div>
<div class="px-4 py-5 my-5 text-center">
<h1 class="display-5 fw-bold">More complex analyses</h1>
<div class="col-lg-6 mx-auto">
<p class="lead mb-4">For instance, since most annotations will not overlap gaps in the same genome
build, you can use this tool to help determine your file's genome build comparing to gaps from
various builds to find which has the fewest intersecting hits.</p>
</div>
</div>
</div>
<div class="col-lg-8 mx-auto px-2 py-5" id="example-loading" hidden>
<div class="d-flex justify-content-center">
<div class="spinner-border text-dark" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
</div>
</nav>
<div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvas-interval-density"
aria-labelledby="offcanvas-interval-density-label">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvas-interval-density-label">Interpreting interval density plots</h5>
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<div class="row pb-3">
<div class="card-body">
<h3 class="card-subtitle mb-2 text-success">Normal</h3>
<div class="card-img-top" id="interval-density-normal"></div>
<p class="card-text pt-2">Based on the known chromosome length and total interval count, the intervals
are spread evenly among the chromosomes.</p>
</div>
</div>
<div class="row pb-3">
<div class="card-body">
<h3 class="card-subtitle mb-2 text-warning">Potential issue</h3>
<div class="card-img-top" id="interval-density-issue"></div>
<p class="card-text">Unequal distribution among chromosomes could mean a filter has been applied
unevenly or possible file truncation.</p>
</div>
</div>
</div>
</div>
<div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvas-interval-counts"
aria-labelledby="offcanvas-interval-counts-label">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvas-interval-counts-label">Interpreting interval count plots</h5>
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<div class="row pb-3">
<div class="card-body">
<h3 class="card-subtitle mb-2 text-success">Normal</h3>
<div class="card-img-top" id="interval-counts-normal"></div>
<p class="card-text pt-2">Based on the known chromosome length and total interval count, the intervals
are spread evenly among the chromosomes.</p>
</div>
</div>
<div class="row pb-3">
<div class="card-body">
<h3 class="card-subtitle mb-2 text-warning">Potential issue</h3>
<div class="card-img-top" id="interval-counts-issue"></div>
<p class="card-text">Unequal distribution among chromosomes could mean a filter has been applied
unevenly or possible file truncation.</p>
</div>
</div>
</div>
</div>
<div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvas-Intersect"
aria-labelledby="offcanvas-intersect-gap-label">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvas-intersect-gap-label">Interpreting intersection plots</h5>
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<div class="row pb-3">
<div class="card-body">Intersecting a BED with known intervals can help us determine the genome by way of
elimination (using gaps). It can also instruct us whether or not our intervals hit genes, exons, or
introns, which can help sanity check intervals by genome build or per your science question. For
instance, if you assume your intervals are exonic then one would expect zero hits with Ensembl introns.
</div>
</div>
<div class="row pb-3">
<div class="card-body">
<h3 class="card-subtitle mb-2 text-success">Normal</h3>
<div class="card-img-top" id="intersect-gap-normal"></div>
<p class="card-text pt-2">Our intervals, in the case of intersecting with Gaps, do not overlap
structures like telomeres and centromeres of the selected genome build, suggesting the selected
genome build is <span class="fw-bold fst-italic">not incorrect</span>.</p>
</div>
</div>
<div class="row pb-3">
<div class="card-body">
<h3 class="card-subtitle mb-2 text-warning">Potential issue</h3>
<div class="card-img-top" id="intersect-gap-issue"></div>
<p class="card-text">If we intersect our intervals with Gaps and observe many overlaps, our intervals
may not be from the selected genome build as we usually anticipate little overlap with centromeres.
</p>
</div>
</div>
</div>
</div>
<main class="container-fluid" id="content"></main>
<div class="container">
<footer class="d-flex flex-wrap justify-content-between align-items-center">
<div class="col-md-4 d-flex align-items-center">
<!-- <a class="navbar-brand brand d-flex align-items-center mb-md-0 me-md-auto text-decoration-none" -->
<!-- href="https://github.com/quinlan-lab/bedqc">bedqc</a> -->
<!-- <span class="text-muted">© 2021</span> -->
</div>
<!-- <ul class="nav col-md-4 justify-content-end list-unstyled d-flex"> -->
<!-- <li class="ms-3"><a class="text-muted" href="https://github.com/quinlan-lab/bedqc"><svg -->
<!-- xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" -->
<!-- class="bi bi-github" viewBox="0 0 16 16"> -->
<!-- <path -->
<!-- d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z" /> -->
<!-- </svg></a></li> -->
<!-- </ul> -->
</footer>
</div>
<script type="module">
const plot_config = {
displaylogo: false,
toImageButtonOptions: {
format: "svg", // one of png, svg, jpeg, webp
filename: "bedqc",
height: 600,
width: 1200,
scale: 1, // Multiply title/legend/axis/canvas sizes by this factor
},
responsive: true,
};
const aesthetics = {
height: 375,
palette: ["#4e79a7", "#f28e2c", "#e15759", "#76b7b2", "#59a14f", "#edc949", "#af7aa1", "#ff9da7", "#9c755f", "#bab0ab"],
tickfont: {
family: "Courier New, monospace",
size: 16,
color: "#000",
},
labelfont: {
family: "Raleway, sans-serif",
size: 12,
},
};
const sort_alphanum = (a, b) => a.localeCompare(b, "en", { numeric: true });
const build_example_plot = ({ x, o, e, divid, ytitle }) => {
let traces = [{ x: x, y: o, type: "bar", marker: { color: aesthetics.palette[0], line: { color: "black", width: 1 } }, name: "Observed" }];
if (e.length > 0) {
traces.push({ x: x, y: e, type: "scatter", name: "Expected", mode: "markers", marker: { size: 12, symbol: "circle", color: "#f28e2c", line: { color: "rgb(255,255,255)", width: 1 } } });
}
let layout = { height: 200, font: aesthetics.labelfont, padding: { t: 0, r: 0, b: 0 }, margin: { t: 0, r: 0, b: 0 }, xaxis: { automargin: true, type: "category" }, yaxis: { title: ytitle, automargin: true }, legend: { font: { size: 12 } } };
Plotly.newPlot(divid, traces, layout, { staticPlot: true, responsive: true });
};
build_example_plot({
x: ["chr1", "chr2", "chr3"],
o: [0.075, 0.079, 0.06],
e: [0.08, 0.07, 0.06],
divid: "interval-density-normal",
ytitle: "Density",
});
build_example_plot({
x: ["chr1", "chr2", "chr3"],
o: [0.075, 0.13, 0.025],
e: [0.08, 0.07, 0.06],
divid: "interval-density-issue",
ytitle: "Density",
});
build_example_plot({
x: ["chr1", "chr2", "chr3"],
o: [100, 90, 80],
e: [95, 94, 78],
divid: "interval-counts-normal",
ytitle: "Density",
});
build_example_plot({
x: ["chr1", "chr2", "chr3"],
o: [100, 132, 18],
e: [95, 94, 78],
divid: "interval-counts-issue",
ytitle: "Density",
});
build_example_plot({
x: ["chr1", "chr2", "chr3"],
o: [0, 0, 0],
e: [],
divid: "intersect-gap-normal",
ytitle: "Count",
});
build_example_plot({
x: ["chr1", "chr2", "chr3"],
o: [50, 57, 22],
e: [],
divid: "intersect-gap-issue",
ytitle: "Count",
});
const add_plot_div = (track, title, pb = 2) => {
let d = document.getElementById("content");
let exists = document.getElementById(`${track}-row`);
if (!exists) {
d.insertAdjacentHTML(
"beforeend",
`<div class="container-fluid row mx-auto pb-${pb}">
<div class="col-lg-8 mx-auto p-0">
<div class="row" id="${track}-row">
<div class="col-12">
<h5>${title}</h5>
</div>
</div>
<div class="row pt-1" id="${track}-plot-row">
<div class="col-12">
<div id="${track}-plot"></div>
</div>
</div>
</div>
</div>
`
);
}
return Promise.resolve();
};
const add_table_row = ({ title, value, warn, validation }) => {
let divid = title.replace(" ", "-");
let d = document.getElementById("metadata-table");
let exists = document.getElementById(divid);
let text_color = warn ? "text-danger" : "text-primary";
let validation_col = "";
if (validation) {
validation_col = warn ? `<td class="text-danger"><i class="bi bi-exclamation-triangle-fill"></i></td>` : `<td class="text-success"><i class="bi bi-check-circle-fill"></i></td>`;
}
if (!exists) {
d.insertAdjacentHTML(
"beforeend",
`<tr>
<td>${title}</td>
<td id="${divid}" class="${text_color}"><pre>${value}</pre></td>
${validation_col}
</tr>`
);
} else {
$(`#${divid}`).html(value);
}
};
let CLI = await new Aioli("bedtools/2.29.2", { printInterleaved: false });
const output = await CLI.exec("bedtools --version");
console.log(`Loaded ${output.stdout}`);
const strip_prefix = (str) => {
if (str.startsWith("chr")) {
return str.slice(3);
}
return str;
};
let chromosome_lengths = {};
let prefix = true;
const to_title = (str) => {
return str.replace(/\w\S*/g, (txt) => {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
};
const scroll_to = (id) => {
const yOffset = -10;
const element = document.getElementById(id);
const y = element.getBoundingClientRect().top + window.pageYOffset + yOffset;
window.scrollTo({ top: y, behavior: "smooth" });
};
const bedtools_intersect = async (genome, track, chromosomes, prefix) => {
let table;
let cmd = "bedtools intersect -sorted";
if (track == "gap") {
table = `${genome}_gap.bed.gz`;
} else if (track.startsWith("Ensembl")) {
cmd += " -u";
if (track.endsWith("whole)")) {
table = `${genome}_ensembl_gene_wholegene.bed.gz`;
} else if (track.endsWith("exon)")) {
table = `${genome}_ensembl_gene_exon.bed.gz`;
} else if (track.endsWith("intron)")) {
table = `${genome}_ensembl_gene_intron.bed.gz`;
} else if (track.endsWith("CDS)")) {
table = `${genome}_ensembl_gene_cds.bed.gz`;
}
} else {
console.log("selected table was not accounted for in `bedtools intersect`");
}
let remote_file = `bedqc.s3.amazonaws.com/${table}`;
if (!global_data.chr_prefix) {
remote_file = remote_file.replace(".bed.gz", "_noprefix.bed.gz");
}
try {
await CLI.mount(`https://${remote_file}`);
} catch (error) {
console.log("file likely already exists. ignoring error.");
console.error(error);
}
// console.log("ls:", await CLI.ls("."));
// intersect user upload and UCSC track
let o = await CLI.exec(`${cmd} -a a.bed -b ${remote_file.replace("/", "-")}`);
o.stdout = o.stdout.split("\n").map((p) => p.split("\t"));
let chr_count = {};
o.stdout.forEach((row) => {
chr_count[row[0]] = (chr_count[row[0]] || 0) + 1;
});
let content = document.getElementById("content");
let idx = global_data.input_test_count;
let primary = $(`#hypothesis-primary-${idx}`).val();
let secondary = $(`#hypothesis-secondary-${idx}`).val();
genome = $(`#hypothesis-genome-${idx}`).val();
let err_msg = o.stderr ? `<div class="col-12 pb-2"><div><code>${o.stderr}</code></div></div>` : "";
content.insertAdjacentHTML(
"beforeend",
`<div class="container-fluid row mx-auto pb-3">
<div class="d-flex col-1 justify-content-end"><code>Out[${idx}]</code></div>
<div class="col-lg-11 p-0">
<div class="d-flex flex-row">
<div class="pe-2">
<h6>${to_title(primary)} ${genome} ${secondary} count</h6>
</div>
<a class="btn btn-primary btn-circle" data-bs-toggle="offcanvas" href="#offcanvas-${primary}" role="button" aria-controls="offcanvas-${primary}">?</a>
</div>
<div class="row pt-1">
${err_msg}
<div class="col-12">
<div id="hypothesis-${idx}-plot"></div>
</div>
</div>
</div>
</div>
`
);
build_bar_plot(
chromosomes,
chromosomes.map((chr) => chr_count[chr]),
"Chromosome",
"Overlap count",
`hypothesis-${idx}-plot`
);
};
const delimiter_string = (sep) => {
if (sep == "\t") {
return "tab";
} else if (sep == " ") {
return "space";
} else {
return sep;
}
};
const newline_string = (nl) => {
if (nl == "\n") {
return "\\n";
} else if (nl == "\r\n") {
return "\\r\\n";
} else {
return `${nl}`;
}
};
const build_input_test = () => {
global_data.input_test_count += 1;
let idx = global_data.input_test_count;
let genome = global_data.previous_genome;
// defaults
let primary = global_data.previous_primary_action;
let secondary = global_data.previous_secondary_action;
let title = `${to_title(primary)} ${genome} ${secondary} count`;
let d = document.getElementById("content");
d.insertAdjacentHTML(
"beforeend",
`<div class="container-fluid row mx-auto pb-2">
<div class="d-flex col-1 text-primary justify-content-end">
<code>In [${idx}]</code>
</div>
<div class="col-lg-11 col-sm-12 bg-light border p-2" id="hypothesis-${idx}-anchor">
<div class="row">
<div class="col">
<div class="form-floating">
<select class="form-select hyp-test" id="hypothesis-primary-${idx}">
<option selected>Calculate interval densities</option>
<option>Calculate interval counts</option>
<option value="Intersect">Overlap intervals with table</option>
</select>
<label for="hypothesis-primary-${idx}">Action</label>
</div>
</div>
<div class="col">
<div class="form-floating">
<select class="form-select hyp-test" id="hypothesis-genome-${idx}">
<option>hg38</option>
<option>hg19</option>
</select>
<label for="hypothesis-genome-${idx}">Genome build</label>
</div>
</div>
<div class="col" id="hypothesis-secondary-wrapper-${idx}" hidden>
<div class="form-floating">
<select class="form-select hyp-test" id="hypothesis-secondary-${idx}">
<option selected>gap</option>
<option>Ensembl gene (whole)</option>
<option>Ensembl gene (exon)</option>
<option>Ensembl gene (intron)</option>
<option>Ensembl gene (CDS)</option>
</select>
<label for="hypothesis-secondary-${idx}">Table</label>
</div>
</div>
<div class="col-1">
<div class="d-grid gap-2">
<button type="button" class="btn btn-primary btn-floating position-relative hyp-test" id="hypothesis-select-action-${idx}">
Run
</button>
</div>
</div>
</div>
<div class="row">
<div class="col-12 py-2 text-secondary" id="hypothesis-blurb-${idx}">
</div>
</div>
</div>
</div>
`
);
$(`#hypothesis-primary-${idx}`).val(global_data.previous_primary_action);
$(`#hypothesis-secondary-${idx}`).val(global_data.previous_secondary_action);
$(`#hypothesis-genome-${idx}`).val(genome);
update_hypothesis_blurb();
if (!global_data.previous_primary_action.startsWith("Calculate")) {
// show all selects
$(`#hypothesis-secondary-wrapper-${idx}`).prop("hidden", false);
}
// 'Run' click
document.getElementById(`hypothesis-select-action-${idx}`).addEventListener("click", run_hypothesis_action);
// select input changes
let elements = document.getElementsByClassName("form-select hyp-test");
Array.from(elements).forEach(function (element) {
element.addEventListener("change", update_hypothesis_blurb);
});
return Promise.resolve();
};
const run_hypothesis_action = async () => {
let idx = global_data.input_test_count;
let action = $(`#hypothesis-primary-${idx}`).val();
let genome = $(`#hypothesis-genome-${idx}`).val();
let track = $(`#hypothesis-secondary-${idx}`).val();
global_data.previous_primary_action = action;
global_data.previous_secondary_action = track;
global_data.previous_genome = genome;
// disable the components of the current test
$(".hyp-test:enabled").prop("disabled", true);
let chromosome_lengths;
jQuery
.getJSON(`https://api.genome.ucsc.edu/list/chromosomes?genome=${genome}`, (d) => {
if (global_data.chr_prefix) {
chromosome_lengths = d.chromosomes;
} else {
// non-chr prefixed chroms
Object.keys(d.chromosomes).map((k) => (chromosome_lengths[k.substring(3)] = d.chromosomes[k]));
}
})
.then(async () => {
let genome_length = 0;
let display_chroms = Object.keys(chromosome_lengths).filter((k) => k.match(/fix|random|alt|Un|hap|MT|M/g) === null);
display_chroms.sort(sort_alphanum);
// length of genome with only primary chromosomes
display_chroms.map((x) => (genome_length += chromosome_lengths[x]));
let content = document.getElementById("content");
if (action == "Calculate interval densities") {
content.insertAdjacentHTML(
"beforeend",
`<div class="container-fluid row mx-auto pb-3">
<div class="d-flex col-1 justify-content-end"><code>Out[${idx}]</code></div>
<div class="col-lg-11 p-0">
<div class="d-flex flex-row">
<div class="pe-2">
<h6>Interval density across ${genome}</h6>
</div>
<a class="btn btn-primary btn-circle" data-bs-toggle="offcanvas" href="#offcanvas-interval-density" role="button" aria-controls="offcanvas-interval-density">?</a>
</div>
<div class="row pt-1">
<div class="col-12">
<div id="hypothesis-${idx}-plot"></div>
</div>
</div>
</div>
</div>
`
);
build_bar_plot(
display_chroms,
display_chroms.map((k) => global_data.chr_interval_count[k] / global_data.interval_count_filtered),
"Chromosome",
"Density",
`hypothesis-${idx}-plot`,
{
x: display_chroms,
y: display_chroms.map((x) => chromosome_lengths[x] / genome_length),
type: "scatter",
name: "Expected",
mode: "markers",
marker: {
size: 12,
symbol: "circle",
color: "#f28e2c",
line: {
color: "rgb(255,255,255)",
width: 1,
},
},
}
);
build_input_test();
} else if (action == "Calculate interval counts") {
content.insertAdjacentHTML(
"beforeend",
`<div class="container-fluid row mx-auto pb-3">
<div class="d-flex col-1 justify-content-end"><code>Out[${idx}]</code></div>
<div class="col-lg-11 p-0">
<div class="d-flex flex-row">
<div class="pe-2">
<h6>Interval counts across ${genome}</h6>
</div>
<a class="btn btn-primary btn-circle" data-bs-toggle="offcanvas" href="#offcanvas-interval-counts" role="button" aria-controls="offcanvas-interval-counts">?</a>
</div>
<div class="row pt-1">
<div class="col-12">
<div id="hypothesis-${idx}-plot"></div>
</div>
</div>
</div>
</div>
`
);
build_bar_plot(
display_chroms,
display_chroms.map((k) => global_data.chr_interval_count[k]),
"Chromosome",
"Count",
`hypothesis-${idx}-plot`,
{
x: display_chroms,
y: display_chroms.map((k) => global_data.interval_count_filtered * (chromosome_lengths[k] / genome_length)),
type: "scatter",
name: "Expected",
mode: "markers",
marker: {
size: 12,
symbol: "circle",
color: "#f28e2c",
line: {
color: "rgb(255,255,255)",
width: 1,
},
},
}
);
build_input_test();
} else if (action == "Intersect") {
await bedtools_intersect(genome, track, display_chroms, global_data.chr_prefix);
build_input_test();
} else {
console.log("Unaccounted for action selected.");
}
scroll_to(`hypothesis-${idx}-anchor`);
});
};
const update_hypothesis_blurb = (event) => {
let idx = global_data.input_test_count;
let filename = typeof global_data.file_obj === "string" || global_data.file_obj instanceof String ? global_data.file_obj.split(/[\\/]/).pop() : global_data.file_obj.name;
let primary = $(`#hypothesis-primary-${idx}`).val();
let secondary = $(`#hypothesis-secondary-${idx}`).val();
let genome = $(`#hypothesis-genome-${idx}`).val();
let str = "";
if (primary.startsWith("Calculate")) {
str = `Select the genome build of ${filename}.`;
// hide the secondary select box
$(`#hypothesis-secondary-wrapper-${idx}`).prop("hidden", true);
} else if (primary == "Intersect") {
$(`#hypothesis-secondary-wrapper-${idx}`).prop("hidden", false);
if (secondary == "gap") {
str = `If your intervals intersect with many gaps across chromosomes, ${genome} may not be the underlying genome build of ${filename}.`;
} else {
str = `Intersect the intervals in ${filename} with Ensembl gene elements.`;
}
}
$(`#hypothesis-blurb-${idx}`).html(str);
};
const update_button = (event) => {
$("#button-alert").prop("hidden", false);
};
let global_data = {};
const build_table = (arr) => {
let content = document.getElementById("content");
content.insertAdjacentHTML(
"beforeend",
`
<div class="container-fluid row mx-auto pb-5">
<div class="col-lg-8 mx-auto px-0">
<div class="row">
<div class="col-12">
<h5>Interval length statistics</h5>
</div>
</div>
<div class="table table-responsive">
<table id="chrom-stats-table" class="table table-sm table-hover" width="100%"></table>
</div>
</div>
</div>
`
);
let cols = [
{ data: "chr", title: "Chromosome", render: $.fn.dataTable.render.text() },
{ data: "min", title: "Minimum", render: $.fn.dataTable.number },
{ data: "max", title: "Maximum", render: $.fn.dataTable.number },
{ data: "mean", title: "Mean", render: $.fn.dataTable.number },
{ data: "median", title: "Median", render: $.fn.dataTable.number },
{ data: "mode", title: "Mode", render: $.fn.dataTable.number },
];
$("#chrom-stats-table").DataTable({
data: arr,
columns: cols,
scrollY: "400px",
scrollX: true,
scrollCollapse: true,
paging: false,
buttons: [
'copyHtml5',
],
// dom: 'rt<"clear">',
dom: '<"top row">rt<"bottom"<"col-12 p-0"i>><"clear">',
infoCallback: (oSettings, iStart, iEnd, iMax, iTotal, sPre) => {
return `
<div class="datatable-info p-2 pb-1 d-flex">
<div class="input-group input-group-sm justify-content-end">
<span class="input-group-text">${iTotal} rows</span>
<button type="button" class="btn btn-sm btn-outline-secondary" id="copy-button">Copy</button>
</div>
</div>
`
},
drawCallback: (settings) => {
document.getElementById("copy-button").addEventListener("click", copy_button_click);
},
order: [[0, "asc"]],
});
};
const copy_button_click = (e) => {
$("#chrom-stats-table").DataTable().button('.buttons-copy').trigger()
}
const process_remote_file = async ({ file }) => {
let bed = [];
let data = {
newline: "\n",
delimiter: "\t",
n_of_fields: [],
missing_data: 0,
interval_count: 0,
interval_length_total: 0,
interval_lengths: [],
chr_interval_length: {},
chr_interval_lengths: {},
filtered_intervals: 0,
zero_length_intervals: 0,
extends_beyond_chr: 0,
chr_prefix: true,
chromosomes: [],
all_chromosomes: [],
};
let url = new URL(file)
try {
await CLI.mount(url.href)
} catch (error) {
console.log("file likely already exists. ignoring error.");
console.error(error);
}
console.log("ls:", await CLI.ls("."));
let o = await CLI.cat(`${url.host}${url.pathname.replaceAll("/", "-")}`)
console.log("o", o)
process_local_file({ file: o, download: false })
}
const process_local_file = async ({ file, download, }) => {
let bed = [];
let file_stream_limit = 1000000
let stream_aborted = false;
let data = {
newline: "\n",
delimiter: "\t",
n_of_fields: [],
missing_data: 0,
interval_count: 0,
interval_length_total: 0,
interval_lengths: [],
chr_interval_length: {},
chr_interval_lengths: {},
filtered_intervals: 0,
zero_length_intervals: 0,
extends_beyond_chr: 0,
chr_prefix: true,
chromosomes: [],
all_chromosomes: [],
};
Papa.parse(file, {
download: download,
skipEmptyLines: "greedy",
header: false,
delimitersToGuess: ["\t", ",", " ", "|", ";", Papa.RECORD_SEP, Papa.UNIT_SEP],
encoding: "utf-8",
step: (res, parser) => {
data.interval_count += 1;
if (data.interval_count == file_stream_limit) {
// add warning that we've stopped file streaming prematurely
$("#file-size-warning").prop("hidden", false);
$("#file-size-warning").html(`File too large. Data streaming stopped at line ${file_stream_limit.toLocaleString()}.`);
stream_aborted = true;
parser.abort();
}