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 |
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1 | /*jshint laxbreak: true */
/*jshint laxcomma: true */
var _ = require("underscore");
var DataLoader = require("./dataLoader");
var CategoryFactory = require("./CategoryFactory");
var d3 = require("d3");
var ViewerHelper = require("./ViewerHelper");
var FeatureFactory = require("./FeatureFactory");
var VariantFilterDialog = require("./VariantFilterDialog");
var CategoryFilterDialog = require("./CategoryFilterDialog");
var TooltipFactory = require('./TooltipFactory');
var PinPad = require("biojs-vis-pinpad");
var updateZoomFromChart = function(fv) {
fv.zoom.x(fv.xScale);
// remove if no zoom
var fullDomain = fv.maxPos - 1,
currentDomain = fv.xScale.domain()[1] - fv.xScale.domain()[0];
var minScale = currentDomain / fullDomain,
maxScale = minScale * Math.floor(fv.sequence.length/fv.maxZoomSize);
fv.zoom.scaleExtent([minScale, maxScale]);
//end remove
};
var updateViewportFromChart = function (fv) {
fv.viewport.extent(fv.xScale.domain());
d3.select('.up_pftv_viewport').call(fv.viewport);
fv.viewport.updateTrapezoid();
};
var update = function(fv) {
fv.aaViewer.update();
fv.aaViewer2.update();
_.each(fv.categories, function(category) {
category.update();
});
};
var updateZoomButton = function(currentClass, newClass, newTitle) {
try {
var zoomBtn = d3.select('.' + currentClass);
zoomBtn.classed(currentClass, false);
zoomBtn.classed(newClass, true);
zoomBtn.attr('title', newTitle);
} catch(er) {
console.log('updateZoomButton error: ' + er);
}
};
var zoomIn = function(fv) {
fv.xScale.domain([
1,
fv.maxZoomSize
]);
if (fv.selectedFeature) {
var domain = fv.xScale.domain();
var max = domain[domain.length-1];
var ftMiddle = +fv.selectedFeature.begin +
(fv.selectedFeature.end ? Math.floor((+fv.selectedFeature.end - +fv.selectedFeature.begin)/2) : 0);
var init = (ftMiddle - max/2) < 1 ? 1 : ftMiddle - max/2;
if ((init + max) > fv.sequence.length) {
init = fv.sequence.length - max;
}
fv.xScale.domain([
init,
init + max
]);
}
update(fv);
updateViewportFromChart(fv);
updateZoomFromChart(fv);
updateZoomButton('icon-zoom-in', 'icon-zoom-out', 'Zoom out to overview');
};
var resetZoom = function(fv) {
update(fv);
updateViewportFromChart(fv);
updateZoomFromChart(fv);
};
var zoomOut = function(fv) {
fv.xScale.domain([
1,
fv.maxPos
]);
resetZoom(fv);
updateZoomButton('icon-zoom-out', 'icon-zoom-in', 'Zoom in to sequence view');
};
var resetZoomAndSelection = function(fv) {
fv.xScale.domain([
1,
fv.maxPos
]);
if (fv.selectedFeature) {
ViewerHelper.selectFeature(fv.selectedFeature, fv.selectedFeatureElement, fv);
}
resetZoom(fv);
updateZoomButton('icon-zoom-out', 'icon-zoom-in', 'Zoom in to sequence view');
};
var createZoom = function(fv) {
var zoom = d3.behavior.zoom()
.x(fv.xScale)
// .scaleExtent([1,1])
.on('zoom', function() {
if (fv.xScale.domain()[0] < 1) {
var tempX = zoom.translate()[0] - fv.xScale(1) + fv.xScale.range()[0];
zoom.translate([tempX, 0]);
} else if (fv.xScale.domain()[1] > fv.maxPos) {
var translatedX = zoom.translate()[0] - fv.xScale(fv.maxPos) + fv.xScale.range()[1];
zoom.translate([translatedX, 0]);
}
update(fv);
updateViewportFromChart(fv);
});
return zoom;
};
var closeTooltipAndPopup = function(fv) {
if (!fv.overFeature && !fv.overTooltip) {
var tooltipContainer = d3.selectAll('.up_pftv_tooltip-container')
.transition(20)
.style('opacity', 0)
.style('display', 'none');
tooltipContainer.remove();
}
if (!fv.overCatFilterDialog) {
CategoryFilterDialog.closeDialog();
}
};
var createNavRuler = function(fv, container) {
var navHeight = 40, navWithTrapezoid = 50;
var navXScale = d3.scale.linear()
.domain([1,fv.maxPos])
.range([fv.padding.left, fv.width - fv.padding.right]);
var svg = container
.append('div')
.attr('class', 'up_pftv_navruler')
.append('svg')
.attr('id','up_pftv_svg-navruler')
.attr('width', fv.width)
.attr('height', (navWithTrapezoid));
var navXAxis = d3.svg.axis()
.scale(fv.xScale)
.orient('bottom');
svg.append('g')
.attr('class', 'x axis')
.call(navXAxis);
var viewport = d3.svg.brush()
.x(navXScale)
.on("brush", function() {
var s = d3.event.target.extent();
if((s[1] - s[0]) < fv.maxZoomSize) {
d3.event.target.extent([s[0],s[0] + fv.maxZoomSize]);
d3.event.target(d3.select(this));
}
fv.xScale.domain(viewport.empty() ? navXScale.domain() : viewport.extent());
update(fv);
viewport.updateTrapezoid();
});
viewport.on("brushstart", function () {
closeTooltipAndPopup(fv);
});
viewport.on("brushend", function () {
updateZoomFromChart(fv);
var navigator = d3.select('.up_pftv_navruler .extent');
if (+navigator.attr('width') >= fv.width - fv.padding.left - fv.padding.right) {
updateZoomButton('icon-zoom-out', 'icon-zoom-in', 'Zoom in to sequence view');
}
});
var arc = d3.svg.arc()
.outerRadius(navHeight / 4)
.startAngle(0)
.endAngle(function(d, i) { return i ? -Math.PI : Math.PI; });
svg.append("g")
.attr("class", "up_pftv_viewport")
.call(viewport)
.selectAll("rect")
.attr("height", navHeight);
viewport.trapezoid = svg.append("g")
.selectAll("path")
.data([0]).enter().append("path")
.classed("up_pftv_trapezoid", true);
viewport.domainStartLabel = svg.append("text")
.attr('class', 'domain-label')
.attr('x',0)
.attr('y',navHeight);
viewport.domainEndLabel = svg.append("text")
.attr('class', 'domain-label')
.attr('x',fv.width)
.attr('y',navHeight)
.attr('text-anchor','end');
svg.selectAll(".resize").append("path")
.attr("transform", "translate(0," + ((navHeight / 2) - 5) + ")")
.attr('class','handle')
.attr("d", arc);
viewport.updateTrapezoid = function() {
var begin = d3.select(".up_pftv_navruler .extent").attr("x");
var tWidth = d3.select(".up_pftv_navruler .extent").attr("width");
var end = (+begin) + (+tWidth);
var path = "M0," + (navWithTrapezoid) + "L0" + "," + (navWithTrapezoid-2)
+ "L" + begin + "," + (navHeight-12) + "L" + begin + "," + navHeight
+ "L" + end + "," + navHeight + "L" + end + "," + (navHeight-12)
+ "L" + fv.width + "," + (navWithTrapezoid-2) + "L" + fv.width + "," + (navWithTrapezoid) + "Z";
this.trapezoid.attr("d", path);
this.domainStartLabel.text(Math.round(fv.xScale.domain()[0]));
this.domainEndLabel.text(Math.min(Math.round(fv.xScale.domain()[1]), fv.maxPos));
};
viewport.clearTrapezoid = function() {
this.trapezoid.attr("d", "M0,0");
};
return viewport;
};
var createButtons = function(fv, data, container) {
var buttons = container.append('div')
.attr('class','up_pftv_buttons');
buttons.append('span')
.attr('class','icon-cog')
.attr('title','Hide/Show tracks')
.on('click', function(){
CategoryFilterDialog.displayDialog(fv, data, buttons);
});
buttons.append('span')
.attr('class','icon-arrows-cw')
.attr('title','Reset view')
.on('click', function(){
resetZoomAndSelection(fv);
});
buttons.append('span')
.attr('class','icon-zoom-in')
.attr('title','Zoom in to sequence view')
.on('click', function(){
if ( d3.select(this).classed('icon-zoom-in')) {
zoomIn(fv);
} else {
zoomOut(fv);
}
});
};
var createAAViewer = function(fv, container, sequence) {
var aaViewer = {}, aaViewWidth = fv.width, aaViewHeight = 30;
var svg = container
.append('div')
.attr('class','up_pftv_aaviewer')
.append('svg')
.attr('width', aaViewWidth)
.attr('height',aaViewHeight);
//amino acids selector
var aaSelectorPlot = function(){
var series, aminoAcids;
var aaSelectorPlot = function(selection) {
selection.each(function(data) {
series = d3.select(this);
aminoAcids = series.selectAll('.up_pftv_amino_acid_selector').data(data);
aminoAcids.enter().append('path');
aminoAcids
.attr('d', function(d) {
return ViewerHelper.shadowPath(d.feature, fv, aaViewHeight);
})
.attr('transform', function(d) {
return 'translate(' + fv.xScale(d.feature.begin) + ',0)';
})
.classed('up_pftv_amino_acid_selector', true);
aminoAcids.exit().remove();
});
};
return aaSelectorPlot;
};
var selectorSeries = aaSelectorPlot();
var selectorGroup = svg.append('g')
.attr('clip-path','url(#aaSelectorViewClip)')
.style('opacity',1);
selectorGroup.datum([{"feature": {"begin": -10, "end": -10}}])
.call(selectorSeries);
//scale
var xAxis = d3.svg.axis()
.scale(fv.xScale);
var gAxis = svg.append("g")
.attr("class", "x axis")
.attr('transform','translate(0, -7)')
.call(xAxis);
//amino acids
var aaPlot = function(){
var series, aminoAcids;
var aaPlot = function(selection) {
selection.each(function(data) {
series = d3.select(this);
aminoAcids = series.selectAll('.up_pftv_amino-acid').data(data);
aminoAcids.enter().append('text')
.style('text-anchor','middle')
.attr('y', aaViewHeight / 2)
.text(function(d) {
return d.toUpperCase();
})
.attr('title', function(d, i) {
return (i+1);
})
.attr('class','up_pftv_amino-acid');
aminoAcids
.attr('x', function(d, i) {
return fv.xScale(i+1);
});
aminoAcids.exit().remove();
});
};
return aaPlot;
};
var series = aaPlot();
var g = svg.append('g')
.attr('class','up_pftv_aa-text')
.attr('clip-path','url(#aaViewClip)')
.attr('transform','translate(0,' + aaViewHeight/5 + ')')
.style('opacity',0);
g.datum(sequence.split('')).call(series);
aaViewer.update = function() {
gAxis.call(xAxis);
selectorGroup.call(selectorSeries);
var count = fv.xScale.domain()[1] - fv.xScale.domain()[0];
if (count > 70) {
g.transition(50).style('opacity',0);
} else {
g.call(series);
g.transition(50).style('opacity',1);
}
};
aaViewer.selectFeature = function() {
if (fv.selectedFeature) {
selectorGroup.datum([{"feature": fv.selectedFeature}]).call(selectorSeries);
} else {
selectorGroup.datum([{"feature": {"begin": -10, "end": -10}}]).call(selectorSeries);
}
};
return aaViewer;
};
var findFeature = function(fv, ftType, begin, end, altSequence) {
var lookup, varLookup;
_.find(fv.data, function(datum) {
if (datum.features) {
lookup = _.find(datum.features, function(feature) {
var ftEnd = feature.end ? feature.end : feature.begin;
if (feature.variants && (feature.type.name === 'VARIANT')) {
varLookup = _.find(feature.variants, function(variant) {
var varEnd = variant.end ? variant.end : variant.begin;
return (+variant.begin === +begin) && (+varEnd === +end) && (variant.mutation === altSequence);
});
return varLookup;
} else if (feature.type.name === 'CONFLICT'){
return (+feature.begin === +begin) && (+ftEnd === +end) && (feature.alternativeSequence === altSequence);
} else if (feature.type.name === 'MUTAGEN') {
return (+feature.begin === +begin) && (+ftEnd === +end) && (feature.mutation === altSequence);
} else {
return (feature.type.name === ftType) && (+feature.begin === +begin) && (+ftEnd === +end);
}
});
return lookup;
} else {
return false;
}
});
return varLookup ? varLookup : lookup;
};
var FeaturesViewer = function(opts) {
var fv = this;
fv.dispatcher = d3.dispatch("featureSelected", "featureDeselected", "ready", "noData", "noFeatures", "notFound");
fv.width = 760;
fv.maxZoomSize = 30;
fv.selectedFeature = undefined;
fv.selectedFeatureElement = undefined;
fv.sequence = "";
fv.categoryOrderAndType = {
domainsAndSites: {
type: 'basic', filter: false, title: 'Domains & sites',
ftTypes: ['REGION', 'COILED', 'MOTIF', 'REPEAT', 'CA_BIND', 'DNA_BIND', 'DOMAIN', 'ZN_FING', 'NP_BIND',
'METAL', 'SITE', 'BINDING', 'ACT_SITE']
}, moleculeProcessing: {
type: 'basic', filter: false, title: 'Molecule processing',
ftTypes: ['CHAIN', 'TRANSIT', 'INIT_MET', 'PROPEP', 'PEPTIDE', 'SIGNAL']
}, ptm: {
type: 'basic', filter: false, title: 'Post translational modifications',
ftTypes: ['MOD_RES', 'LIPID', 'CARBOHYD', 'DISULFID', 'CROSSLNK']
}, seqInfo: {
type: 'basic', filter: false, title: 'Sequence information',
ftTypes: ['COMPBIAS', 'CONFLICT', 'NON_CONS', 'NON_TER', 'UNSURE', 'NON_STD']
}, structural: {
type: 'basic', filter: false, title: 'Structural features',
ftTypes: ['HELIX', 'STRAND', 'TURN']
}, topology: {
type: 'basic', filter: false, title: 'Topology',
ftTypes: ['TOPO_DOM', 'TRANSMEM', 'INTRAMEM']
}, mutagenesis: {
type: 'basic', filter: false, title: 'Mutagenesis',
ftTypes: ['MUTAGEN']
}, variants: {
type: 'variant', filter: true, title: 'Variants',
ftTypes: ['VARIANT', 'MISSENSE', 'MS_DEL', 'INSDEL', 'STOP_LOST', 'STOP_GAINED', 'INIT_CODON']
}
};
fv.filterTypes = fv.categoryOrderAndType.variants.ftTypes;
fv.categories = [];
fv.filterCategories = [];
fv.padding = {top:2, right:10, bottom:2, left:10};
fv.data = undefined;
fv.load = function() {
opts.proxy = opts.proxy ? opts.proxy : "";
var dataLoader = DataLoader.get(opts.proxy, opts.uniprotacc);
dataLoader.done(function(d) {
fv.data = d;
if (d.totalFeatureCount === 0) {
d3.select(opts.el)
.text('No features available for protein ' + opts.uniprotacc);
fv.dispatcher.noFeatures(d);
} else {
fv.init(opts, d);
}
}).fail(function(e) {
d3.select(opts.el)
.text(e.responseText);
fv.dispatcher.noData({error: e});
});
};
fv.load();
};
FeaturesViewer.prototype.getCategoryTitle = function(type) {
var fv = this;
var category = _.find(fv.categoryOrderAndType, function(cat) {
if (_.contains(cat.ftTypes, type.toUpperCase())) {
return true;
}
return false;
});
return category ? category.title : undefined;
};
FeaturesViewer.prototype.applyFilter = function() {
var fv = this;
_.each(fv.filterCategories, function(category) {
category.update();
});
if (fv.selectedFeature && _.contains(fv.filterTypes, fv.selectedFeature.type.name) &&
!VariantFilterDialog.displayFeature(fv.selectedFeature) ) {
ViewerHelper.selectFeature(fv.selectedFeature, fv.selectedFeatureElement, fv);
}
};
FeaturesViewer.prototype.updateFeatureSelector = function() {
this.aaViewer.selectFeature();
this.aaViewer2.selectFeature();
};
FeaturesViewer.prototype.getDispatcher = function() {
return this.dispatcher;
};
FeaturesViewer.prototype.selectFeature = function(ftType, start, end, altSequence) {
var fv = this;
ftType = ftType.toUpperCase();
altSequence = altSequence ? altSequence.toUpperCase() : altSequence;
var catTitle = fv.getCategoryTitle(ftType);
var category = _.find(fv.categories, function(cat) {
return cat.data.label === catTitle;
});
var feature = findFeature(fv, ftType, +start, +end, altSequence);
if (!feature) {
fv.dispatcher.notFound({ftType: ftType, begin: start, end: end});
return undefined;
}
var elem = d3.select('[name="' + feature.internalId + '"]');
if (category && feature && elem && !elem.classed('up_pftv_variant_hidden')) {
var container = category.viewerContainer.style('display') === 'none'
? category.tracksContainer : category.viewerContainer;
if (elem.classed('up_pftv_variant')) {
var varTrack = d3.select('.up_pftv_category-name[title="' + catTitle + '"]');
if (varTrack.classed('up_pftv_arrow-right')) {
category.toggle();
}
}
var elemRect = elem.node().getBoundingClientRect();
var contRect = container.node().getBoundingClientRect();
var coordinates = {x: elemRect.x - contRect.x, y: elemRect.y - contRect.y};
if (fv.selectedFeature) {
if (fv.selectedFeature.internalId !== feature.internalId) {
ViewerHelper.selectFeature(feature, elem.node(), fv);
} else {
fv.dispatcher.featureSelected({feature: fv.selectedFeature, color: elem.style("fill")});
}
} else {
ViewerHelper.selectFeature(feature, elem.node(), fv);
}
TooltipFactory.createTooltip(fv, catTitle, feature, container, coordinates);
return feature;
} else {
fv.dispatcher.notFound({ftType: ftType, begin: start, end: end});
return undefined;
}
};
FeaturesViewer.prototype.init = function(opts, d) {
var fv = this;
fv.sequence = d.sequence;
fv.accession = d.accession;
fv.maxPos = d.sequence.length;
fv.xScale = d3.scale.linear()
.domain([1, d.sequence.length + 1])
.range([fv.padding.left, fv.width - fv.padding.right]);
//remove any previous text
var genericContainer = d3.select(opts.el).text('');
var fvContainer = genericContainer
.append('div')
.attr('class', 'up_pftv_container')
.on('mousedown', function() {
closeTooltipAndPopup(fv);
});
if (opts.pinPad === true) {
fvContainer.style('display', 'inline-block');
var pinPadId = '_' + new Date().getTime();
genericContainer.append('div')
.classed('up_ptfv_pp-container', true)
.attr('id', pinPadId);
fv.pinPad = new PinPad({
ordering: ['type', 'start', 'end'],
options: {
el: '#' + pinPadId
, width: '220px'
, height: '320px'
, highlightColor: 'green'
}
});
}
fv.viewport = createNavRuler(fv, fvContainer);
createButtons(fv, d, fvContainer);
fv.aaViewer = createAAViewer(fv, fvContainer, d.sequence);
fv.zoom = createZoom(fv);
fv.container = fvContainer
.append('div')
.attr('class', 'up_pftv_category-container');
_.each(fv.categoryOrderAndType, function(value, category) {
if ( (!_.contains(opts.exclusions, category)) && (d[category].features.length !== 0) ){
var cat = CategoryFactory.createCategory(d[category], fv.categoryOrderAndType[category].type, fv);
fv.categories.push(cat);
if (fv.categoryOrderAndType[category].filter) {
fv.filterCategories.push(cat);
}
}
});
var bottomAAViewerContainer = fvContainer.append('div').attr('class','bottom-aa-container');
fv.aaViewer2 = createAAViewer(fv, bottomAAViewerContainer, d.sequence);
updateViewportFromChart(fv);
updateZoomFromChart(fv);
fv.dispatcher.ready(d);
};
module.exports = FeaturesViewer; |