Skip to content
Navigation Menu
{{ message }}
forked from ryanlayer/samplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsamplot_vcf.html
More file actions
626 lines (576 loc) · 24 KB
/
Copy pathsamplot_vcf.html
File metadata and controls
626 lines (576 loc) · 24 KB
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
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>samplot</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.9.2/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crossfilter2/1.4.7/crossfilter.min.js"
type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dc/3.0.12/dc.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"
type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/viewerjs/1.5.0/viewer.min.js" type="text/javascript"></script>
<script src="https://cdn.datatables.net/v/bs4/dt-1.10.20/b-1.6.1/b-html5-1.6.1/sc-2.0.1/sl-1.3.1/datatables.min.js"
type="text/javascript"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/viewerjs/1.5.0/viewer.min.css" rel="stylesheet"
type="text/css" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.2/css/all.min.css" rel="stylesheet"
type="text/css" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"
type="text/css" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/dc/3.0.12/dc.min.css" rel="stylesheet" type="text/css" />
<link href="https://cdn.datatables.net/v/bs4/dt-1.10.20/b-1.6.1/b-html5-1.6.1/sc-2.0.1/sl-1.3.1/datatables.min.css"
rel="stylesheet" type="text/css" />
<style type="text/css">
#filter-menu .dropdown-menu {
min-height: 100px;
max-height: 100vh;
overflow-y: auto;
overflow-x: hidden;
background-color: #edf0f2;
}
span.no-show {
display: none;
}
span.show-ellipsis:after {
content: "...";
}
.datatable-info {
font-size: .9em;
}
#variant-table_info {
padding-top: 8px;
}
table.dataTable thead th.sorting:after,
table.dataTable thead th.sorting_asc:after,
table.dataTable thead th.sorting_desc:after,
table.dataTable thead th.sorting:before,
table.dataTable thead th.sorting_asc:before,
table.dataTable thead th.sorting_desc:before {
font-family: FontAwesome !important;
}
.modal-content {
width: 610px;
}
h7 {
font-size: .95rem;
}
body {
height: 100vh
}
div.dts div.dataTables_scrollBody {
background: white;
}
</style>
</head>
<body>
<nav class="navbar navbar-dark bg-dark p-0 pl-2">
<a class="navbar-brand text-light p-0" href="https://github.com/ryanlayer/samplot">samplot</a>
</nav>
<div class="modal fade" id="filter-modal" tabindex="-1" role="dialog" aria-labelledby="filter-modal"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<div class="flex-column">
<h5 class="modal-title" id="filter-modal">Filters</h5>
<h7 class="pl-2 text-secondary" id="variant-count">
<a href="javascript:dc.filterAll(); dc.renderAll();">Reset All</a>
</h7>
</div>
</div>
<div class="modal-body">
<div class="container">
<div class="row pt-2">
<div class="col">
<h5>Sample</h5>
</div>
</div>
<div class="row pb-3">
<div class="col-12">
<div id="sample-search"></div>
</div>
</div>
<div class="row" id="nsamples-chart">
<div class="col-4">
<h5># of Samples</h5>
</div>
<div class="col-8 text-right">
<span class="reset text-muted" style="display: none;">[<span
class="filter"></span>]</span>
<a class="reset" href="javascript:nsamplesChart.filterAll();dc.redrawAll();"
style="display: none;">Reset</a>
</div>
</div>
<div class="row" id="size-chart">
<div class="col-4">
<h5>Size</h5>
</div>
<div class="col-8 text-right">
<span class="reset text-muted" style="display: none;">[<span
class="filter"></span>]</span>
<a class="reset" href="javascript:sizeChart.filterAll();dc.redrawAll();"
style="display: none;">Reset</a>
</div>
</div>
<div class="row" id="type-chart">
<div class="col-4">
<h5>SV Type</h5>
</div>
<div class="col-8 text-right">
<span class="reset text-muted" style="display: none;">[<span
class="filter"></span>]</span>
<a class="reset" href="javascript:typeChart.filterAll();dc.redrawAll();"
style="display: none;">Reset</a>
</div>
</div>
<div class="row" id="chrom-chart">
<div class="col-4">
<h5>Chromosome</h5>
</div>
<div class="col-8 text-right">
<span class="reset text-muted" style="display: none;">[<span
class="filter"></span>]</span>
<a class="reset" href="javascript:chromChart.filterAll();dc.redrawAll();"
style="display: none;">Reset</a>
</div>
</div>
<div class="row" id="overlaps-chart" hidden>
<div class="col-4">
<h5>SV Overlaps</h5>
</div>
<div class="col-8 text-right">
<span class="reset text-muted" style="display: none;">[<span
class="filter"></span>]</span>
<a class="reset" href="javascript:overlapsChart.filterAll();dc.redrawAll();"
style="display: none;">Reset</a>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-secondary" data-dismiss="modal"
onclick="javascript:dc.filterAll(); dc.renderAll();"
title="Clear selection and close">Cancel</button>
<button type="button" class="btn btn-primary" data-dismiss="modal"
title="Apply filters">Apply</button>
</div>
</div>
</div>
</div>
<div class="container-fluid h-90">
<div class="row" id="variant-table-placeholder">
<div class="col-12">
<div style="height:415px">
<div class="d-flex justify-content-center align-items-center text-muted h-100">
<div class="d-flex flex-column">
<i class="fas fa-10x fa-table"></i>
</div>
</div>
</div>
</div>
</div>
<div class="row pb-1" id="variant-table-div" hidden>
<div class="col-12">
<div class="table-responsive">
<table id="variant-table" class="table table-hover display nowrap" width="100%"></table>
</div>
</div>
</div>
</div>
</body>
<script>
const data = {{ data|tojson }}
const plot_type = "{{plot_type}}"
const annotation = {{ gff3 }}
const denovo = {{ denovo }}
dc.config.defaultColors(d3.schemeSet1)
// plot constraints
const plotw = 585
const ploth = 150
// table filters
var searchInput = dc.textFilterWidget("#sample-search")
var nsamplesChart = dc.barChart("#nsamples-chart")
var sizeChart = dc.barChart("#size-chart")
var typeChart = dc.barChart("#type-chart")
var chromChart = dc.barChart("#chrom-chart")
var overlapsChart
// shows filter impact in modal header
var variantCount = dc.dataCount("#variant-count")
// used to access filtered table data
var chromDimension
// datatables obj
var variant_table
// crossfilter obj
var ndx
$('#filter-modal').on('hidden.bs.modal', function () {
update_table()
})
const table_click = (selection, table) => {
table.$('tr.selected').removeClass('selected')
$(selection).addClass('selected')
let current = $('tr.selected')
let next = current.next()
let prev = current.prev()
let row = table.rows('.selected').data()[0]
let img = new Image()
img.src = `${row.svtype}_${row.chrom}_${row.start}_${row.end}.${plot_type}`
if ( row.chrom2) {
img.src = `${row.svtype}_${row.chrom}_${row.start}_${row.chrom2}_${row.end}.${plot_type}`
}
let viewer = new Viewer(img, {
hidden: function () {
viewer.destroy()
},
title: function () {
return `${row.svtype} on chromosome ${row.chrom} at ${row.start}-${row.end}`
},
toolbar: {
zoomIn: 4,
zoomOut: 4,
oneToOne: 4,
reset: 4,
prev: {
show: prev.length > 0 ? true : false,
size: "large",
click: function () {
viewer.destroy()
table_click(prev, table)
}
},
play: { show: false },
next: {
show: next.length > 0 ? true : false,
size: "large",
click: function () {
viewer.destroy()
table_click(next, table)
}
},
rotateLeft: { show: false },
rotateRight: { show: false },
flipHorizontal: { show: false },
flipVertical: { show: false },
},
transition: false,
navbar: false,
})
viewer.show()
}
function build_table(data) {
// hide the placeholder and show the datatable
d3.select('#variant-table-placeholder').property("hidden", true)
d3.select('#variant-table-div').property("hidden", false)
let cols = [
{ data: 'chrom', title: 'Chrom' },
{ data: 'start', title: 'Start' },
{ data: 'end', title: 'End' },
{ data: 'svlength', title: 'Size' },
{ data: 'svtype', title: 'SV Type' },
{ data: 'nsamples', title: '# of Samples' },
{ data: 'samples', title: 'Samples' },
]
if (annotation) {
d3.select('#overlaps-chart').property("hidden", false)
cols.push({ data: 'overlaps', title: 'Overlaps' })
}
if (denovo) {
cols.push({ data: 'dn', title: 'De novo' })
}
variant_table = $("#variant-table").DataTable({
data: data,
columns: cols,
deferRender: true,
scrollY: '80vh',
scrollCollapse: true,
scroller: true,
info: true,
buttons: [
'copyHtml5', 'csvHtml5'
],
infoCallback: (oSettings, iStart, iEnd, iMax, iTotal, sPre) => {
return `
<span class="datatable-info">
<span class="pr-2">Showing <b>${iStart}</b> - <b>${iEnd}</b> of <b>${iTotal}</b> records</span>
<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#filter-modal" title="Show filters">
<span class="fas fa-filter"></span>
</button>
<span class="dropup">
<button type="button" class="btn btn-sm btn-primary dropdown-toggle" id="download-menu" title="Save table" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="fas fa-save"></span>
</button>
<span class="dropdown-menu" aria-labelledby="download-menu">
<h6 class="dropdown-header">Save ${iTotal} rows as:</h6>
<button class="dropdown-item" type="button" id="csv-button-download" onclick="csv_button_click()">
CSV
</button>
<button class="dropdown-item" type="button" id="copy-button-download" onclick="copy_button_click()">
Copy
</button>
</span>
</span>
</span>
`
},
columnDefs: [
{
targets: (annotation ? [0, 1, 2, 3, 4, 5, 7] : [0, 1, 2, 3, 4, 5]),
width: '15%'
},
// https://datatables.net/blog/2016-02-26
{
targets: 6,
render: function (data, type, row) {
if (type === 'display' && data != null) {
data = data.replace(/<(?:.|\n)*?>/gm, '');
if (data.length > 40) {
return '<span class="show-ellipsis">' + data.substr(0, 40) + '</span><span class="no-show">' + data.substr(40) + '</span>';
} else {
return data;
}
} else {
return data;
}
}
}
],
// search is applied using crossfilter
searching: false,
lengthChange: false,
order: [[0, 'asc'], [1, 'asc']],
})
// register table clicks on sample_column
variant_table.on('click', 'tr', function () {
table_click(this, variant_table)
})
}
function csv_button_click() {
variant_table.button('.buttons-csv').trigger()
}
function copy_button_click() {
variant_table.button('.buttons-copy').trigger()
}
function update_table() {
variant_table.clear()
variant_table.rows.add(chromDimension.top(Infinity))
variant_table.draw()
}
function remove_empty_bins(source_group) {
return {
all: function () {
return source_group.all().filter(function (d) {
return d.value != 0
})
}
}
}
// https://jsfiddle.net/gordonwoodhull/g34Ldwaz/8/
// https://github.com/dc-js/dc.js/issues/348
function index_group(group) {
return {
all: function () {
return group.all().map(function (kv, i) {
return { key: i, value: kv.value }
})
}
}
}
$(document).ready(function () {
ndx = crossfilter(data)
var all = ndx.groupAll()
chromDimension = ndx.dimension((d) => { return d.chrom })
build_table(chromDimension.top(Infinity))
var chromGroup = chromDimension.group().reduceCount()
var nonEmptyChromGroup = remove_empty_bins(chromGroup)
var searchDimension = ndx.dimension(function (d) {
return d.samples
})
searchInput
.dimension(searchDimension)
.on('renderlet', function () {
d3.selectAll(".dc-text-filter-input")
.classed("form-control", true)
d3.selectAll("#sample-search.dc-chart")
.classed("col-12", true)
})
var sizeDimension = ndx.dimension(function (d) {
var round
if (d.svlength < 100) {
round = 100
} else if (d.svlength < 1000) {
round = 100
} else if (d.svlength < 10000) {
round = 1000
} else if (d.svlength < 100000) {
round = 10000
} else if (d.svlength < 1000000) {
round = 100000
} else if (d.svlength < 10000000) {
round = 1000000
} else {
round = 10000000
}
return Math.round(d.svlength / round) * round
})
var sizeGroup = sizeDimension.group().reduceCount()
var nonEmptySizeGroup = remove_empty_bins(sizeGroup)
// for brushing, need to track keys at numeric indexes
var sizeKeys = nonEmptySizeGroup.all().map(dc.pluck('key')).slice()
var typeDimension = ndx.dimension((d) => { return d.svtype })
var typeGroup = typeDimension.group().reduceCount()
var nonEmptyTypeGroup = remove_empty_bins(typeGroup)
var nsamplesDimension = ndx.dimension((d) => { return d.nsamples })
var nsamplesDimension = ndx.dimension(function (d) {
var round
if (d.nsamples < 10) {
round = 1
} else if (d.nsamples < 100) {
round = 10
} else if (d.nsamples < 1000) {
round = 100
} else if (d.nsamples < 10000) {
round = 1000
} else {
round = 10000
}
return Math.round(d.nsamples / round) * round
})
var nsamplesGroup = nsamplesDimension.group().reduceCount()
var nonEmptyNsamplesGroup = remove_empty_bins(nsamplesGroup)
var nsamplesKeys = nonEmptyNsamplesGroup.all().map(dc.pluck('key')).slice()
// number of samples
nsamplesChart
.width(plotw).height(ploth).gap(1)
.margins({ top: 10, right: 50, bottom: 30, left: 40 })
.x(d3.scaleLinear().domain([0, nsamplesKeys.length]))
.round(Math.floor)
.brushOn(true)
.elasticX(true)
.dimension(nsamplesDimension)
.group(index_group(nonEmptyNsamplesGroup))
.elasticY(true)
.yAxisLabel('Count')
.filterPrinter(function (filters) {
var filter = filters[0]
return nsamplesKeys[filter[0]] + ' - ' + nsamplesKeys[filter[1]]
})
// limit the number of labels along x-axis
nsamplesChart.xAxis().ticks(20)
nsamplesChart.yAxis().ticks(5)
// update labels from keys
nsamplesChart.xAxis().tickFormat(function (v) {
return nsamplesKeys[v]
})
nsamplesChart.filterHandler(function (dimension, filters) {
if (filters.length === 0) {
// the empty case (no filtering)
dimension.filter(null)
} else {
dimension.filterRange([nsamplesKeys[filters[0][0]], nsamplesKeys[filters[0][1]]])
}
return filters
})
// SV length
sizeChart
.width(plotw).height(ploth).gap(1)
.margins({ top: 10, right: 50, bottom: 30, left: 40 })
.x(d3.scaleLinear().domain([0, sizeKeys.length]))
.round(Math.floor)
.brushOn(true)
.elasticX(true)
.dimension(sizeDimension)
.group(index_group(nonEmptySizeGroup))
.elasticY(true)
.yAxisLabel('Count')
.filterPrinter(function (filters) {
var filter = filters[0]
return sizeKeys[filter[0]] + ' - ' + sizeKeys[filter[1]]
})
// adds left padding to plots inside filtering panel
.on('renderlet', function () {
d3.selectAll("svg")
.classed("pl-3", true)
})
// limit the number of labels along x-axis
sizeChart.xAxis().ticks(10)
sizeChart.yAxis().ticks(5)
// update labels from keys
sizeChart.xAxis().tickFormat(function (v) {
return sizeKeys[v]
})
// update the status format for this chart
sizeChart.filterHandler(function (dimension, filters) {
if (filters.length === 0) {
// the empty case (no filtering)
dimension.filter(null)
} else {
dimension.filterRange([sizeKeys[filters[0][0]], sizeKeys[filters[0][1]]])
}
return filters
})
// sv type
typeChart
.width(plotw).height(ploth).gap(1)
.margins({ top: 10, right: 50, bottom: 30, left: 40 })
.x(d3.scaleBand())
.xUnits(dc.units.ordinal)
.elasticX(true)
.elasticY(true)
.dimension(typeDimension)
.group(nonEmptyTypeGroup)
.yAxisLabel('Count')
typeChart.yAxis().ticks(5)
// chromosome
chromChart
.width(plotw).height(ploth).gap(1)
.margins({ top: 10, right: 50, bottom: 30, left: 40 })
.x(d3.scaleBand())
.xUnits(dc.units.ordinal)
.yAxisLabel('Count')
.elasticX(true)
.elasticY(true)
.dimension(chromDimension)
.group(nonEmptyChromGroup)
.ordering((d) => {
v = parseInt(d.key)
if (v) {
return v
} else {
return d.key
}
})
chromChart.yAxis().ticks(5)
// overlaps
if (annotation) {
var overlapsDimension = ndx.dimension((d) => { return d.overlaps })
var overlapsGroup = overlapsDimension.group().reduceCount()
var nonEmptyOverlapsGroup = remove_empty_bins(overlapsGroup)
overlapsChart = dc.barChart("#overlaps-chart")
overlapsChart
.width(plotw).height(ploth).gap(1)
.margins({ top: 10, right: 50, bottom: 30, left: 40 })
.x(d3.scaleBand())
.xUnits(dc.units.ordinal)
.elasticX(true)
.elasticY(true)
.dimension(overlapsDimension)
.group(nonEmptyOverlapsGroup)
.yAxisLabel('Count')
overlapsChart.yAxis().ticks(5)
}
variantCount
.crossfilter(ndx)
.groupAll(all)
// (_optional_) `.html` sets different html when some records or all records are selected.
// `.html` replaces everything in the anchor with the html given using the following function.
// `%filter-count` and `%total-count` are replaced with the values obtained.
.html({
some: '<strong>%filter-count</strong> selected out of <strong>%total-count</strong> records' +
' | <a href=\'javascript:dc.filterAll(); dc.renderAll();\'>Reset All</a>',
all: '<strong>%total-count</strong> records'
});
dc.renderAll()
})
</script>
</html>
You can’t perform that action at this time.
