forked from Merck/TraceTrack
-
Notifications
You must be signed in to change notification settings - Fork 2
/
trace_viewer.js
449 lines (375 loc) · 15.2 KB
/
trace_viewer.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
function TraceViewer(data) {
/**
DATA
**/
// list of position dictionaries with:
// cov: coverage (int)
// mut: type of mutation (str)
// ref: corresponding position in unaligned reference (int)
this.positions = data.positions
// Extracted list where alignedRefPositions[mutatedPos] = refPos
this.alignedRefPositions = this.positions.map(d => d.ref)
// list of trace sequence dictionaries with:
// id: ID based on trace filename
// alignedPositions: list where alignedPositions[mutatedPos] = traceSeqPos
// sequence: original unaligned trace sequence (str)
// locations: list of trace x-coordinates of each sequence base
// traces: list of trace value dictionaries with:
// base: A, C, T or G
// values: list of trace values
this.sequences = data.sequences
// mutated result sequence (str)
this.mutated = data.mutated
// original unaligned reference sequence (str)
this.reference = data.reference
// translation of reference sequence (str)
this.translation = data.translation
// ID of reference sequence
this.refid = data.refid
/**
PLOT PROPERTIES
**/
this.refRange = 10
this.traceRange = 112
this.margin = {top: 20, right: 20, bottom: 40, left: 40};
this.totalWidth = +d3.select('#tracePlot').style('width').slice(0, -2);
if (!this.totalWidth) {
// TODO
this.totalWidth = 780
}
this.width = this.totalWidth - this.margin.left - this.margin.right;
this.height = 75;
var viewer = this
/**
DRAW NAVIGATION BAR
**/
this.updateNavArrow = function(mutatedPos) { // Add interactivity
var mutBase = viewer.mutated[mutatedPos]
var refPos = viewer.positions[mutatedPos].ref
var textPos = viewer.positions[mutatedPos].pos
var text = ''
if (mutatedPos < viewer.positions.length - 1 && refPos == viewer.positions[mutatedPos + 1].ref) {
text += textPos + ' insert: '+mutBase
} else {
var refBase = viewer.reference[refPos]
text += (refPos + 1) + " " + refBase
if (mutBase != refBase) {
if (mutBase == '-') {
text += ' (deletion)'
} else {
text += ' (mut '+mutBase+')'
}
}
}
viewer.navArrow
.attr("transform", "translate(" + viewer.xScaleNav(mutatedPos) + ",10)")
.select("text")
.text(text)
}
this.xScaleNav = d3.scaleLinear()
.domain([0, this.positions.length])
.range([15, this.totalWidth-15]); // output
// add navigator svg
this.navPlot = d3.select('#navPlot').append("svg")
.attr("width", this.totalWidth)
.attr("height", 80)
.append("g")
.attr("transform", "translate(" + this.margin.left + "," + this.margin.top + ")")
this.navPlot.on("mouseout", function(d, i) {
viewer.updateNavArrow(viewer.shownPos)
})
this.navPlot.append("text")
.text("Navigator")
.attr("x", this.width/2).attr("y", -5)
.attr("class", "plotTitle");
// add navigator rectangles
var navRectWidth = Math.max(1, this.totalWidth / this.positions.length + 0.1)
this.navPlot
.append("g")
.attr("transform", "translate(0, 15)")
.selectAll("rect")
.data(this.positions)
.enter()
.append("rect")
.attr("width", navRectWidth)
.attr("height", function(d, i){ return viewer.mutated[i] == viewer.mutated[i].toUpperCase() ? 30 : 24 })
.attr("y", function(d, i){ return viewer.mutated[i] == viewer.mutated[i].toUpperCase() ? 0 : 3 })
.attr("x", function(d, i){ return viewer.xScaleNav(i) - navRectWidth/2 })
.attr("class", function(d){ return d.mut + ' cov' + Math.min(d.cov, 4) })
.on("mouseover", function(d, i) { viewer.updateNavArrow(i) })
.on("click", function(d, i) {
viewer.show(i)
})
this.navArrow = viewer.navPlot.append("g")
.attr("class", "navArrow")
this.navArrow.append("text")
this.navArrow.append("line")
.attr("x1", 0)
.attr("y1", 2)
.attr("x2", 0)
.attr("y2", 35)
.style("stroke-width", 1)
.style("stroke", "black")
.style("fill", "none")
/**
DRAW REFERENCE
**/
this.xScaleRef = d3.scaleLinear()
.range([0, this.width]); // output
// Add SVG to the page to "ref" div
this.refPlot = d3.select("#refPlot").append("svg")
.attr("width", this.totalWidth)
.attr("height", 75)
.append("g")
.attr("transform", "translate(" + this.margin.left + "," + this.margin.top + ")")
// add text label with file name
this.refPlot.append("text")
.attr("x", this.width/2).attr("y", -10)
.text('Reference '+this.refid)
.attr("class", "plotTitle");
var refTicks = this.refPlot.append('g')
.selectAll('g.refTick')
.data(this.reference.split(''))
.enter()
.append('g')
.attr('class', function(d, i){ return 'refTick tick tick_'+d.toUpperCase() });
refTicks.insert('rect')
.attr('transform', 'translate(-10 -10)')
.attr('width', 20)
.attr('height', 20)
.attr('rx', 4)
.on("click", function(d, refPos){
var mutatedPos = viewer.alignedRefPositions.lastIndexOf(refPos)
viewer.show(mutatedPos)
})
refTicks.insert('text')
.attr("x", 0).attr("y", -20)
.attr("dominant-baseline", "central")
.text(function(d, i){ return i + 1 })
.attr("class", "refPosition");
refTicks.insert('text')
.attr("x", 0).attr("y", -1)
.attr("dominant-baseline", "central")
.text(function(d, i){ return d });
amino_acid = function(d, i) {
return viewer.translation[i]
}
refTicks.insert('text')
.attr("x", 0).attr("y", 20)
.attr("dominant-baseline", "central")
.text(amino_acid)
.attr("class", "refTranslation");
// Add SVG to the page to "ref" div
this.navArrowL = d3.select("#refPlot").append("svg")
.attr("width", 20)
.attr("height", 20)
.style("z-index", 1000)
.style("position", "relative")
.attr("transform", "translate(" + -5 + "," + 0 + ")")
this.navArrowL.append('text')
.text("◀")
.attr("x", 10).attr("y", 15)
.attr("class", "plotTitle")
.style("cursor", "pointer")
.on("click", function(){
viewer.show(viewer.shownPos - (viewer.refRange*2))
})
this.navArrowR = d3.select("#refPlot").append("svg")
.attr("width", 20)
.attr("height", 20)
.attr("transform", "translate(" + 760 + "," + 0 + ")")
.style("z-index", 1000)
.style("position", "relative")
this.navArrowR.append('text')
.text("▶")
.attr("x", 10).attr("y", 15)
.attr("class", "plotTitle")
.style("cursor", "pointer")
.on("click", function(){
viewer.show(viewer.shownPos + (viewer.refRange*2))
})
/**
DRAW TRACE PLOTS
**/
this.yScale = d3.scaleLinear()
.domain([0, getMaxTrace(this.sequences)]) // input
.range([this.height, 0]); // output
// append svg for each trace sequence
this.plots = d3.select('#tracePlot').selectAll("svg")
.data(this.sequences)
.enter()
// enter an svg element for each tracefile
.append("svg")
.attr("width", this.totalWidth)
.attr("height", this.height + this.margin.top + this.margin.bottom)
.append("g")
.attr("transform", "translate(" + this.margin.left + "," + this.margin.top + ")")
.attr("class", "main")
// initialize each trace plot
this.plots.each(function(d, i){
// i is the trace file number (index in the list of dictionaries) d is dict
var svg = d3.select(this);
// add clipping rectangle mask
svg.append("clipPath")
.attr("id", "clipid")
.append("rect")
.attr("x", 0)
.attr("y", -5)
.attr("width", viewer.width)
.attr("height", viewer.height+30);
// add empty trace paths
var paths = svg.append("g")
.attr("class", "paths")
.selectAll('path')
.data(d.traces)
.enter()
.append('path')
.attr('class', function(d){ return 'tracePath line_'+d.base })
.attr("clip-path", "url(#clipid)");
// add dashed line in center
svg.append("line")
.attr("x1", viewer.width / 2)
.attr("y1", 0)
.attr("x2", viewer.width / 2)
.attr("y2", viewer.height)
.style("stroke-dasharray", ("3, 3"))
.style("stroke-width", 1)
.style("stroke", "#aaaaaa")
.style("fill", "none");
// append empty x axis group
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + viewer.height + ")");
// append empty y axis group
svg.append("g")
.attr("class", "y axis")
.call(d3.axisLeft(viewer.yScale));
// add text label with file name
svg.append("text")
.attr("x", viewer.width/2).attr("y", 0)
.text('Trace '+d.id)
.attr("class", "plotTitle");
});
var transition = d3.transition()
.delay(0)
.duration(400)
this.show = function(mutatedPos){
this.shownPos = mutatedPos
this.updateNavArrow(mutatedPos)
var refPos = this.positions[mutatedPos].ref
var opacity = 1
if (mutatedPos < this.positions.length - 1) {
// for insertions, center between two residues
var nextRefPos = this.positions[mutatedPos+1].ref
if (refPos == nextRefPos) {
refPos = refPos - 0.5
opacity = 0.5
}
}
this.xScaleRef
.domain([ refPos - this.refRange, refPos + this.refRange ])
this.refPlot.selectAll("g.refTick")
.transition(transition)
.style("opacity", opacity)
.attr("transform", function(d, i) { return "translate(" + viewer.xScaleRef(i) + ",25)" })
this.plots.each(function(d, i){ // i is the trace file number (index in the list of dictionaries) d is dict
// for each trace file edit svg and bind data
var svg = d3.select(this)
var tracePos = d.alignedPositions[mutatedPos]
var nextTracePos = d.alignedPositions[mutatedPos+1]
var traceCenter = d.locations[tracePos]
var opacity = 1
if (tracePos == nextTracePos) {
// gap in trace, show with half opacity and in middle of previous two positions
if (tracePos == 0) {
traceCenter = -10
} else if (tracePos == d.locations.length) {
traceCenter = d.locations[d.locations.length-1] + 10
} else {
traceCenter = (d.locations[tracePos] + d.locations[tracePos-1]) / 2
}
opacity = 0.5
}
var traceLength = d.traces[0].values.length
var traceDrawMargin = 20
var traceStart = traceCenter - viewer.traceRange
var traceSliceStart = Math.max(0, traceStart - traceDrawMargin)
var traceEnd = traceCenter + viewer.traceRange
var xScale = d3.scaleLinear()
.domain([ traceStart, traceEnd ]) // input
.range([0, viewer.width]) // output
// filter out base positions between start and end -> axis tick labels
var ticks = d.locations.filter(function(number) {
return number > traceStart && number <= traceEnd;
});
var seqStart = d.locations.indexOf(ticks[0]);
var seqEnd = seqStart + ticks.length
var tickLabels = d.sequence.split("").slice(seqStart, seqEnd)
var xAxisGenerator = d3.axisBottom(xScale)
.tickValues(ticks)
.tickFormat((d,i) => tickLabels[i]);
var line = d3.line()
.x(function(d, i) { return xScale(traceSliceStart + i); }) // set the x values for the line generator
.y(function(d) { return viewer.yScale(d); }) // set the y values for the line generator
.curve(d3.curveMonotoneX) // apply smoothing to the line
// trace line plot
var paths = svg.selectAll('path.tracePath')
.style("opacity", opacity)
.attr("d", function(d){
return line(d.values.slice(traceSliceStart, traceEnd + traceDrawMargin))
});
// Call the x axis in a group tag
var xAxis = svg.select("g.x.axis")
.call(xAxisGenerator)
xAxis.selectAll('g.tick')
.style("opacity", opacity)
.each(function(unused, i){
// note: This only works if there is no transition
// because extra elements at the edge need to be added and animated
var g = d3.select(this);
var base = g.select('text').text()
var tick_class = "tick_uni"
if (base.toUpperCase() == "N"){
tick_class = "tick_N"
}
if (!g.select('rect').size()) {
var mutatedPos = d.alignedPositions.lastIndexOf(seqStart + i)
g.insert('rect', ':first-child')
.attr('transform', 'translate(-8 5)')
.attr('width', 16)
.attr('height', 17)
.attr('rx', 4)
.on("click", function(){
viewer.show(mutatedPos)
})
var refPos = viewer.alignedRefPositions[mutatedPos]
if (refPos && base.toUpperCase() != viewer.reference[refPos].toUpperCase()) {
tick_class = 'tick_'+base.toUpperCase()
}
g.attr('class', 'traceTick tick '+tick_class)
var prevMutPos = d.alignedPositions.lastIndexOf(seqStart + i - 1)
var prevRefPos = viewer.alignedRefPositions[prevMutPos]
if (refPos > prevRefPos + 1) {
g.insert('line', ':first-child')
.attr('transform', 'translate(-26 9)')
.attr('x1', 1)
.attr('y1', 6)
.attr('x2', 10)
.attr('y2', 6)
.attr('stroke-width', 2)
.attr('stroke', 'red')
}
}
})
});
}
}
function getMaxTrace(sequences){
var ys = []
for (dict of sequences) {
for (var i=0; i<4; i+=1) {
ys.push(d3.max(dict.traces[i].values));
}
}
return d3.max(ys);
}