-
Notifications
You must be signed in to change notification settings - Fork 199
/
DraggableHTMLFeatures.js
1176 lines (1070 loc) · 55.8 KB
/
DraggableHTMLFeatures.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
define( [
'dojo/_base/declare',
'dojo/_base/array',
'JBrowse/View/Track/HTMLFeatures',
'WebApollo/FeatureSelectionManager',
'dijit/Menu',
'dijit/MenuItem',
'dijit/CheckedMenuItem',
'dijit/Dialog',
'jquery',
'jqueryui/draggable',
'JBrowse/Util',
'JBrowse/Model/SimpleFeature'
],
function( declare, array, HTMLFeatureTrack, FeatureSelectionManager, dijitMenu, dijitMenuItem, dijitCheckedMenuItem, dijitDialog, $, draggable, Util, SimpleFeature ) {
/* Subclass of FeatureTrack that allows features to be selected,
and dragged and dropped into the annotation track to create annotations.
WARNING:
for selection to work for features that cross block boundaries, z-index of feature style MUST be set, and must be > 0
otherwise what happens is:
feature div inherits z-order from parent, so same z-order as block
so feature div pixels may extend into next block, but next block draws ON TOP OF IT (assuming next block added
to parent after current block). So events over part of feature div that isn't within it's parent block will never
reach feature div but instead be triggered on next block
This issue will be more obvious if blocks have background color set since then not only will selection not work but
part of feature div that extends into next block won't even be visible, since next block background will render over it
*/
var debugFrame = false;
//var DraggableFeatureTrack = declare( HTMLFeatureTrack,
var draggableTrack = declare( HTMLFeatureTrack,
{
// so is dragging
dragging: false,
_defaultConfig: function() {
return Util.deepUpdate(
dojo.clone( this.inherited(arguments) ),
{
style: {
className: "feature",
// renderClassName: 'DraggableFeatureTrack' ???
// setting minSubfeatureWidth to 1 insures subfeatures will almost always get drawn,
minSubfeatureWidth: 1
},
events: {
// need to map click to a null-op, to override default JBrowse click behavior for click on features
// (JBrowse default is feature detail popup)
click: function(event) {
// not quite a null-op, also need to suprress propagation of click recursively up through parent divs,
// in order to stop default JBrowse behavior for click on tracks (which is to recenter view at click point)
event.stopPropagation();
}
// WebApollo can't set up mousedown --> onFeatureMouseDown() in config.events,
// because dojo.on used by JBrowse config-based event setup doesn't play nice with
// JQuery event retriggering via _mousedown() for feature drag bootstrapping
// also, JBrowse only sets these events for features, and WebApollo needs them to trigger for subfeatures as well
// , mousedown: dojo.hitch( this, 'onFeatureMouseDown' ),
// , dblclick: dojo.hitch( this, 'onFeatureDoubleClick' )
}
}
);
},
constructor: function( args ) {
console.log("DragableFeatureTrack constructor called");
this.gview = this.browser.view;
// get a handle to on the main WA object
this.browser.getPlugin( 'WebApollo', dojo.hitch( this, function(p) {
this.webapollo = p;
}));
// if calculated feature pixel width would be less than minFeatWidth, then set width to minFeatWidth instead
// setting to 0.1 avoids problem with small feature divs getting expanded during zoom in, then shrinking back to
// scale-appropriate size at end of zoom
this.minFeatWidth = 0.1;
// DraggableFeatureTracks all share the same FeatureSelectionManager
// if want subclasses to have different selection manager,
// call this.setSelectionManager in subclass (after calling parent constructor)
this.setSelectionManager( this.webapollo.featSelectionManager );
// CSS class for selected features
// override if want subclass to have different CSS class for selected features
this.selectionClass = "selected-feature";
// DraggableFeatureTrack.selectionManager.addListener(this);
this.last_whitespace_mousedown_loc = null;
this.last_whitespace_mouseup_time = new Date(); // dummy timestamp
this.prev_selection = null;
this.verbose = false;
this.verbose_selection = false;
this.verbose_selection_notification = false;
this.verbose_drag = false;
this.drag_enabled = true;
this.feature_context_menu = null;
/** hack to determine which tracks to apply edge matching to
would rather do a check for whether track is instance of DraggableHTMLFeatures (or possibly HTMLFeatures),
but use of dojo.declare() for classes means track object's class is actually base Object.
*/
this.edge_matching_enabled = true;
},
loadSuccess: function(trackInfo) {
/* if subclass indicates it has custom context menu, do not initialize default feature context menu */
if (! this.has_custom_context_menu) {
this.initFeatureContextMenu();
this.initFeatureDialog();
}
this.inherited( arguments );
},
setSelectionManager: function(selman) {
if (this.selectionManager) {
this.selectionManager.removeListener(this);
}
this.selectionManager = selman;
// FeatureSelectionManager listeners must implement
// selectionAdded() and selectionRemoved() response methods
this.selectionManager.addListener(this);
return selman;
},
/**
* only called once, during track setup ???
*
* doublclick in track whitespace is used by JBrowse for zoom
* but WebApollo/JBrowse uses single click in whitespace to clear selection
*
* so this sets up mousedown/mouseup/doubleclick
* kludge to restore selection after a double click to whatever selection was before
* initiation of doubleclick (first mousedown/mouseup)
*
*/
setViewInfo: function(genomeView, numBlocks,
trackDiv, labelDiv,
widthPct, widthPx, scale) {
this.inherited( arguments );
var $div = $(this.div);
var track = this;
// this.scale = scale; // scale is in pixels per base
// setting up mousedown and mouseup handlers to enable click-in-whitespace to clear selection
// (without conflicting with JBrowse drag-in-whitespace to scroll)
$div.bind('mousedown', function(event) {
var target = event.target;
if (! (target.feature || target.subfeature)) {
track.last_whitespace_mousedown_loc = [ event.pageX, event.pageY ];
}
} );
$div.bind('mouseup', function(event) {
var target = event.target;
if (! (target.feature || target.subfeature)) { // event not on feature, so must be on whitespace
var xup = event.pageX;
var yup = event.pageY;
// if click in whitespace without dragging (no movement between mouse down and mouse up,
// and no shift modifier,
// then deselect all
if (this.verbose_selection) { console.log("mouse up on track whitespace"); }
var eventModifier = event.shiftKey || event.altKey || event.metaKey || event.ctrlKey;
if (track.last_whitespace_mousedown_loc &&
xup === track.last_whitespace_mousedown_loc[0] &&
yup === track.last_whitespace_mousedown_loc[1] &&
(! eventModifier )) {
var timestamp = new Date();
var prev_timestamp = track.last_whitespace_mouseup_time;
track.last_whitespace_mouseup_time = timestamp;
// if less than half a second, probably a doubleclick (or triple or more click...)
var probably_doubleclick = ((timestamp.getTime() - prev_timestamp.getTime()) < 500);
if (probably_doubleclick) {
if (this.verbose_selection) { console.log("mouse up probably part of a doubleclick"); }
// don't record selection state, want to keep prev_selection set
// to selection prior to first mouseup of doubleclick
}
else {
track.prev_selection = track.selectionManager.getSelection();
if (this.verbose_selection) {
console.log("recording prev selection");
console.log(track.prev_selection);
}
}
if (this.verbose_selection) { console.log("clearing selection"); }
track.selectionManager.clearSelection();
}
else {
track.prev_selection = null;
}
}
// regardless of what element it's over, mouseup clears out tracking of mouse down
track.last_whitespace_mousedown_loc = null;
} );
// kludge to restore selection after a double click to whatever selection was before
// initiation of doubleclick (first mousedown/mouseup)
$div.bind('dblclick', function(event) {
var target = event.target;
// because of dblclick bound to features, will only bubble up to here on whitespace,
// but doing feature check just to make sure
if (! (target.feature || target.subfeature)) {
if (this.verbose_selection) {
console.log("double click on track whitespace");
console.log("restoring selection after double click");
console.log(track.prev_selection);
}
if (track.prev_selection) {
var plength = track.prev_selection.length;
// restore selection
for (var i = 0; i<plength; i++) {
track.selectionManager.addToSelection(track.prev_selection[i]);
}
}
}
track.prev_selection = null;
} );
/* track click diagnostic (and example of how to add additional track mouse listener?) */
$div.bind("click", function(event) {
// console.log("track click, base position: " + track.gview.getGenomeCoord(event));
var target = event.target;
if (target.feature || target.subfeature) {
event.stopPropagation();
}
} );
},
selectionAdded: function( rec, smanager) {
var track = this;
if( rec.track === track) {
var featdiv = track.getFeatDiv( rec.feature );
if( track.verbose_selection_notification ) {
console.log("DFT.selectionAdded called: ");
console.log( rec );
console.log( featdiv );
}
if( featdiv ) {
var jq_featdiv = $(featdiv);
if (!jq_featdiv.hasClass(track.selectionClass)) {
jq_featdiv.addClass(track.selectionClass);
}
// track.showEdgeMatches(feat);
}
}
},
selectionCleared: function(selected, smanager) {
var track = this;
if (track.verbose_selection_notification) {
console.log("DFT.selectionCleared called");
}
var slength = selected.length;
for (var i=0; i<slength; i++) {
var rec = selected[i];
track.selectionRemoved( rec );
}
},
selectionRemoved: function( rec, smanager) {
var track = this;
if( rec.track === track ) {
var featdiv = track.getFeatDiv( rec.feature );
if( track.verbose_selection_notification ) {
console.log("DFT.selectionRemoved called");
console.log( rec );
console.log( featdiv );
}
if( featdiv ) {
var jq_featdiv = $(featdiv);
if (jq_featdiv.hasClass(track.selectionClass)) {
jq_featdiv.removeClass(track.selectionClass);
}
if (jq_featdiv.hasClass("ui-draggable")) {
jq_featdiv.draggable("destroy");
}
if (jq_featdiv.hasClass("ui-multidraggable")) {
jq_featdiv.multidraggable("destroy");
}
}
}
},
/**
* overriding renderFeature to add event handling for mouseover, mousedown, mouseup
*/
renderFeature: function(feature, uniqueId, block, scale, labelScale, descriptionScale,
containerStart, containerEnd) {
var featdiv = this.inherited( arguments );
if( featdiv ) { // just in case featDiv doesn't actually get created
var $featdiv = $(featdiv);
$featdiv.bind("mousedown", dojo.hitch( this, 'onFeatureMouseDown') );
$featdiv.bind("dblclick", dojo.hitch( this, 'onFeatureDoubleClick') );
if (this.feature_context_menu && (! this.has_custom_context_menu)) {
this.feature_context_menu.bindDomNode(featdiv);
}
// if renderClassName field exists in trackData.json for this track, then add a child
// div to the featdiv with class for CSS styling set to renderClassName value
var rclass = this.config.style.renderClassName;
if (rclass) {
// console.log("in FeatureTrack.renderFeature, creating annot div");
var rendiv = document.createElement("div");
rendiv.className = rclass;
if (Util.is_ie6) rendiv.appendChild(document.createComment());
featdiv.appendChild(rendiv);
}
}
return featdiv;
},
renderSubfeature: function( feature, featDiv, subfeature,
displayStart, displayEnd, block ) {
var subfeatdiv = this.inherited( arguments );
if (subfeatdiv) { // just in case subFeatDiv doesn't actually get created
var $subfeatdiv = $(subfeatdiv);
// adding pointer to track for each subfeatdiv
// (could get this by DOM traversal, but shouldn't take much memory, and having it with each subfeatdiv is more convenient)
subfeatdiv.track = this;
subfeatdiv.subfeature = subfeature;
$subfeatdiv.bind("mousedown", dojo.hitch( this, 'onFeatureMouseDown' ) );
$subfeatdiv.bind("dblclick", dojo.hitch( this, 'onFeatureDoubleClick') );
}
return subfeatdiv;
},
_subfeatSorter: function( a, b ) {
var as = a.get('start');
var bs = b.get('start');
if ( as == bs ) { return 0; }
else if ( as > bs ) { return 1; }
else if ( as < bs ) { return -1; }
else { return 0; /* shouldn't fall through to here */ }
},
/**
* if feature has translated region (CDS, wholeCDS, start_codon, ???),
* reworks feature's subfeatures for more annotation-editing-friendly selection
*
* Assumes:
* if translated, will either have
* CDS-ish term for each coding segment
* wholeCDS from start of translation to end of translation (so already pre-processed)
* mutually exclusive (either have CDS, or wholeCDS, but not both)
* if wholeCDS present, then pre-processed (no UTRs)
* if any exon-ish types present, then _all_ exons are present with exon-ish types
*/
_processTranslation: function( feature ) {
var track = this;
var feat_type = feature.get('type');
// most very dense genomic feature tracks do not have CDS. Trying to minimize overhead for that case --
// keep list of types that NEVER have CDS children (match, alignment, repeat, etc.)
// (WARNING in this case not sorting, but sorting (currently) only needed for features with CDS (for reading frame calcs))
if (this.webapollo.neverHasCDS[feat_type]) {
feature.normalized = true;
return;
}
var subfeats = feature.get('subfeatures');
// var cds = subfeats.filter( function(feat) { return feat.get('type') === 'CDS'; } );
var cds = subfeats.filter( function(feat) {
return track.webapollo.cdsTerms[feat.get('type')];
} );
var wholeCDS = subfeats.filter( function(feat) { return feat.get('type') === 'wholeCDS'; } );
// most very dense genomic feature tracks do not have CDS. Trying to minimize overhead for that case --
// if no CDS, no wholeCDS, consider normalized
// (WARNING in this case not sorting, but sorting (currently) only needed for features with CDS (for reading frame calcs))
//
if (cds.length === 0 && wholeCDS.length === 0) {
feature.normalized = true;
return;
}
var newsubs;
// wholeCDS is specific to WebApollo, if seen can assume no CDS, and UTR/exon already normalized
if (wholeCDS.length > 0) {
// extract wholecds from subfeats, then sort subfeats
feature.wholeCDS = wholeCDS[0];
newsubs = subfeats.filter( function(feat) { return feat.get('type') !== 'wholeCDS'; } );
}
// if has a CDS, remove CDS from subfeats and sort exons
else if (cds.length > 0) {
cds.sort(this._subfeatSorter);
var cdsmin = cds[0].get('start');
var cdsmax = cds[cds.length-1].get('end');
feature.wholeCDS = new SimpleFeature({ parent: feature,
data: { start: cdsmin, end: cdsmax, type: 'wholeCDS',
strand: feature.get('strand') }
} );
var hasExons = false;
for (var i=0; i<subfeats.length; i++) {
// if (subfeats[i].get('type') === 'exon') { hasExons = true; break; }
if (track.webapollo.exonTerms[subfeats[i].get('type')]) { hasExons = true; break; }
}
if (hasExons) {
// filter out UTR and CDS
newsubs = subfeats.filter( function(feat) {
var ftype = feat.get('type');
return (! (track.webapollo.utrTerms[ftype] || track.webapollo.cdsTerms[ftype]) );
} );
}
else { // no exons, but at least one CDS, possibly UTR
// create exons by joining abutting UTR/CDS
var sortedsubs = subfeats.slice(); // shallow copy subfeats array
sortedsubs.sort(this._subfeatSorter);
newsubs = [];
// since cds.length > 0, guaranteed to have at least one CDS
var exonCount = 0;
var prevStart, prevEnd;
// scan through sorted subfeats, joining abutting UTR/CDS regions
for (var i=0; i<sortedsubs.length; i++) {
var subfeat = sortedsubs[i];
var ftype = subfeat.get('type');
var curStart = subfeat.get('start');
var curEnd = subfeat.get('end');
if (this.webapollo.utrTerms[ftype] || this.webapollo.cdsTerms[ftype] ) {
if (! prevStart) { // first UTR/CDS, just initialize first exon
prevStart = subfeat.get('start');
prevEnd = subfeat.get('end');
}
else { // compare to previous UTR/CDS
// abutting, extend previous exon
if (curStart == prevEnd) {
prevEnd = curEnd;
}
// not abutting, create previous exon and start new one
else {
var exon = new SimpleFeature({ parent: feature,
id: feature.id() + "-exon-" + exonCount++,
data: { start: prevstart, end: prevend, type: 'exon',
strand: feature.get('strand') }
} );
console.log(exon);
newsubs.push(exon);
prevStart = curStart;
prevEnd = curEnd;
}
}
}
else { // not a CDS or UTR, just add to new subfeats array
newsubs.push(subfeat);
}
}
// add last exon after exiting loop
var exon = new SimpleFeature({ parent: feature,
id: feature.id() + "-exon-" + exonCount++,
data: { start: prevstart, end: prevend, type: 'exon',
strand: feature.get('strand') }
} );
newsubs.push(exon);
}
}
// ensure that subfeatures are sorted by ascending start (regardless of feature orientation)
// may want to revisit later and sort subfeatures of minus strand in descending order ??
// but if do this must make sure to change reading frame calcs to reflect this
newsubs.sort(this._subfeatSorter);
feature.filteredsubs = newsubs;
feature.normalized = true;
},
/**
* overriding handleSubFeatures for customized handling of UTR/CDS-segment rendering within exon divs
*/
handleSubFeatures: function( feature, featDiv,
displayStart, displayEnd, block ) {
var subfeats = feature.get('subfeatures');
if (! subfeats) { return; }
if (! feature.normalized ) {
this._processTranslation( feature );
}
var wholeCDS = feature.wholeCDS;
var parentId = this.getId(feature);
// if processing resulted in filtered subfeats, render with those instead of unfiltered subfeats
if (feature.filteredsubs) { subfeats = feature.filteredsubs; }
var slength = subfeats.length;
var subfeat;
var subtype;
if (wholeCDS) {
var cdsStart = wholeCDS.get('start');
var cdsEnd = wholeCDS.get('end');
// current convention is start = min and end = max regardless of strand, but checking just in case
var cdsMin = Math.min(cdsStart, cdsEnd);
var cdsMax = Math.max(cdsStart, cdsEnd);
if (this.verbose_render) { console.log("wholeCDS:"); console.log(wholeCDS); }
}
var priorCdsLength = 0;
if (debugFrame) { console.log("====================================================="); }
var strand = feature.get('strand');
var reverse = false;
if (strand === -1 || strand === '-') {
reverse = true;
}
/* WARNING: currently assuming children are ordered by ascending min
* (so if on minus strand then need to calc frame starting with the last exon)
*/
for (var i = 0; i < slength; i++) {
if (reverse) {
subfeat = subfeats[slength-i-1];
}
else {
subfeat = subfeats[i];
}
var uid = this.getId(subfeat);
subtype = subfeat.get('type');
// don't render "wholeCDS" type
// although if subfeatureClases is properly set up, wholeCDS would also be filtered out in renderFeature?
// if (subtype == "wholeCDS") { continue; }
var subDiv = this.renderSubfeature( feature, featDiv, subfeat, displayStart, displayEnd, block);
if( subDiv )
subDiv.subfeature = subfeat;
// if subfeat is of type "exon", add CDS/UTR rendering
// if (subDiv && wholeCDS && (subtype === "exon")) {
// if (wholeCDS && (subtype === "exon")) { // pass even if subDiv is null (not drawn), in order to correctly calc downstream CDS frame
// CHANGED to call renderExonSegments even if no wholeCDS --
// non wholeCDS means undefined cdsMin, which will trigger creation of UTR div for entire exon
if (subtype === "exon") { // pass even if subDiv is null (not drawn), in order to correctly calc downstream CDS frame
priorCdsLength = this.renderExonSegments(subfeat, subDiv, cdsMin, cdsMax, displayStart, displayEnd, priorCdsLength, reverse);
}
if (this.verbose_render) {
console.log("in DraggableFeatureTrack.handleSubFeatures, subDiv: ");
console.log(subDiv);
}
}
},
/**
* TODO: still need to factor in truncation based on displayStart and displayEnd???
From: http://mblab.wustl.edu/GTF22.html
Frame is calculated as (3 - ((length-frame) mod 3)) mod 3.
(length-frame) is the length of the previous feature starting at the first whole codon (and thus the frame subtracted out).
(length-frame) mod 3 is the number of bases on the 3' end beyond the last whole codon of the previous feature.
3-((length-frame) mod 3) is the number of bases left in the codon after removing those that are represented at the 3' end of the feature.
(3-((length-frame) mod 3)) mod 3 changes a 3 to a 0, since three bases makes a whole codon, and 1 and 2 are left unchanged.
*/
renderExonSegments: function( subfeature, subDiv, cdsMin, cdsMax,
displayStart, displayEnd, priorCdsLength, reverse) {
var subStart = subfeature.get('start');
var subEnd = subfeature.get('end');
var subLength = subEnd - subStart;
var UTRclass, CDSclass;
// if (debugFrame) { console.log("exon: " + subStart); }
// if the feature has been truncated to where it doesn't cover
// this subfeature anymore, just skip this subfeature
// GAH: was OR, but should be AND?? var render = ((subEnd > displayStart) && (subStart < displayEnd));
var render = subDiv && (subEnd > displayStart) && (subStart < displayEnd);
// look for UTR and CDS subfeature class mapping from trackData
// if can't find, then default to parent feature class + "-UTR" or "-CDS"
if( render ) {
UTRclass = this.config.style.subfeatureClasses["UTR"];
CDSclass = this.config.style.subfeatureClasses["CDS"];
if (! UTRclass) { UTRclass = this.className + "-UTR"; }
if (! CDSclass) { CDSclass = this.className + "-CDS"; }
}
// if ((subEnd <= displayStart) || (subStart >= displayEnd)) { return undefined; }
var segDiv;
// console.log("render sub frame");
// whole exon is untranslated (falls outside wholeCDS range, or no CDS info found)
if( (cdsMin === undefined && cdsMax === undefined) ||
(cdsMax <= subStart || cdsMin >= subEnd)) {
if( render ) {
segDiv = document.createElement("div");
// not worrying about appending "plus-"/"minus-" based on strand yet
dojo.addClass(segDiv, "webapollo-UTR");
dojo.addClass(segDiv, UTRclass);
if (Util.is_ie6) segDiv.appendChild(document.createComment());
segDiv.style.cssText =
"left: " + (100 * ((subStart - subStart) / subLength)) + "%;"
+ "top: 0px;"
+ "width: " + (100 * ((subEnd - subStart) / subLength)) + "%;";
subDiv.appendChild(segDiv);
}
}
/*
Frame is calculated as (3 - ((length-frame) mod 3)) mod 3.
(length-frame) is the length of the previous feature starting at the first whole codon (and thus the frame subtracted out).
(length-frame) mod 3 is the number of bases on the 3' end beyond the last whole codon of the previous feature.
3-((length-frame) mod 3) is the number of bases left in the codon after removing those that are represented at the 3' end of the feature.
(3-((length-frame) mod 3)) mod 3 changes a 3 to a 0, since three bases makes a whole codon, and 1 and 2 are left unchanged.
*/
// whole exon is translated
else if (cdsMin <= subStart && cdsMax >= subEnd) {
var overhang = priorCdsLength % 3; // number of bases overhanging from previous CDS
var relFrame = (3 - (priorCdsLength % 3)) % 3;
var absFrame, cdsFrame, initFrame;
if (reverse) {
initFrame = (cdsMax - 1) % 3;
absFrame = (subEnd - 1) % 3;
cdsFrame = (3 + absFrame - relFrame) % 3;
}
else {
initFrame = cdsMin % 3;
absFrame = (subStart % 3);
cdsFrame = (absFrame + relFrame) % 3;
}
if (debugFrame) {
console.log("whole exon: " + subStart + " -- ", subEnd, " initFrame: ", initFrame,
", overhang: " + overhang + ", relFrame: ", relFrame, ", absFrame: ", absFrame,
", cdsFrame: " + cdsFrame);
}
if (render) {
segDiv = document.createElement("div");
// not worrying about appending "plus-"/"minus-" based on strand yet
dojo.addClass(segDiv, "webapollo-CDS");
dojo.addClass(segDiv, CDSclass);
if (Util.is_ie6) segDiv.appendChild(document.createComment());
segDiv.style.cssText =
"left: " + (100 * ((subStart - subStart) / subLength)) + "%;"
+ "top: 0px;"
+ "width: " + (100 * ((subEnd - subStart) / subLength)) + "%;";
if (this.config.style.colorCdsFrame || this.webapollo.colorCdsByFrame) {
dojo.addClass(segDiv, "cds-frame" + cdsFrame);
}
subDiv.appendChild(segDiv);
}
priorCdsLength += subLength;
}
// partial translation of exon
else {
// calculate 5'UTR, CDS segment, 3'UTR
var cdsSegStart = Math.max(cdsMin, subStart);
var cdsSegEnd = Math.min(cdsMax, subEnd);
var overhang = priorCdsLength % 3; // number of bases overhanging
var absFrame, cdsFrame, initFrame;
if (priorCdsLength > 0) {
var relFrame = (3 - (priorCdsLength % 3)) % 3;
if (reverse) {
// cdsFrame = ((subEnd-1) + ((3 - (priorCdsLength % 3)) % 3)) % 3; }
initFrame = (cdsMax - 1) % 3;
absFrame = (subEnd - 1) % 3;
cdsFrame = (3 + absFrame - relFrame) % 3;
}
else {
// cdsFrame = (subStart + ((3 - (priorCdsLength % 3)) % 3)) % 3;
initFrame = cdsMin % 3;
absFrame = (subStart % 3);
cdsFrame = (absFrame + relFrame) % 3;
}
if (debugFrame) { console.log("partial exon: " + subStart + ", initFrame: " + (cdsMin % 3) +
", overhang: " + overhang + ", relFrame: " + relFrame + ", subFrame: " + (subStart % 3) +
", cdsFrame: " + cdsFrame); }
}
else { // actually shouldn't need this? -- if priorCdsLength = 0, then above conditional collapses down to same calc...
if (reverse) {
cdsFrame = (cdsMax-1) % 3; // console.log("rendering reverse frame");
}
else {
cdsFrame = cdsMin % 3;
}
}
var utrStart;
var utrEnd;
// make left UTR (if needed)
if (cdsMin > subStart) {
utrStart = subStart;
utrEnd = cdsSegStart;
if (render) {
segDiv = document.createElement("div");
// not worrying about appending "plus-"/"minus-" based on strand yet
dojo.addClass(segDiv, "webapollo-UTR");
dojo.addClass(segDiv, UTRclass);
if (Util.is_ie6) segDiv.appendChild(document.createComment());
segDiv.style.cssText =
"left: " + (100 * ((utrStart - subStart) / subLength)) + "%;"
+ "top: 0px;"
+ "width: " + (100 * ((utrEnd - utrStart) / subLength)) + "%;";
subDiv.appendChild(segDiv);
}
}
if (render) {
// make CDS segment
segDiv = document.createElement("div");
// not worrying about appending "plus-"/"minus-" based on strand yet
dojo.addClass(segDiv, "webapollo-CDS");
dojo.addClass(segDiv, CDSclass);
if (Util.is_ie6) segDiv.appendChild(document.createComment());
segDiv.style.cssText =
"left: " + (100 * ((cdsSegStart - subStart) / subLength)) + "%;"
+ "top: 0px;"
+ "width: " + (100 * ((cdsSegEnd - cdsSegStart) / subLength)) + "%;";
if (this.config.style.colorCdsFrame || this.webapollo.colorCdsByFrame) {
dojo.addClass(segDiv, "cds-frame" + cdsFrame);
}
subDiv.appendChild(segDiv);
}
priorCdsLength += (cdsSegEnd - cdsSegStart);
// make right UTR (if needed)
if (cdsMax < subEnd) {
utrStart = cdsSegEnd;
utrEnd = subEnd;
if (render) {
segDiv = document.createElement("div");
// not worrying about appending "plus-"/"minus-" based on strand yet
dojo.addClass(segDiv, "webapollo-UTR");
dojo.addClass(segDiv, UTRclass);
if (Util.is_ie6) segDiv.appendChild(document.createComment());
segDiv.style.cssText =
"left: " + (100 * ((utrStart - subStart) / subLength)) + "%;"
+ "top: 0px;"
+ "width: " + (100 * ((utrEnd - utrStart) / subLength)) + "%;";
subDiv.appendChild(segDiv);
}
}
}
return priorCdsLength;
},
/*
* selection occurs on mouse down
* mouse-down on unselected feature -- deselect all & select feature
* mouse-down on selected feature -- no change to selection (but may start drag?)
* mouse-down on "empty" area -- deselect all
* (WARNING: this is preferred behavior, but conflicts with dblclick for zoom -- zoom would also deselect)
* therefore have mouse-click on empty area deselect all (no conflict with dblclick)
* shift-mouse-down on unselected feature -- add feature to selection
* shift-mouse-down on selected feature -- remove feature from selection
* shift-mouse-down on "empty" area -- no change to selection
*
* "this" should be a featdiv or subfeatdiv
*/
onFeatureMouseDown: function(event) {
// event.stopPropagation();
if( this.verbose_selection || this.verbose_drag ) { console.log("DFT.onFeatureMouseDown called"); }
console.log("genome coord: " + this.gview.absXtoBp(event.pageX));
// drag_create conditional needed in older strategy using trigger(event) for feature drag bootstrapping with JQuery 1.5,
// but not with with JQuery 1.7+ strategy using _mouseDown(event), since _mouseDown call doesn't lead to onFeatureMouseDown() call
// if (this.drag_create) { this.drag_create = null; return; }
this.handleFeatureSelection(event);
if (this.drag_enabled) {
this.handleFeatureDragSetup(event);
}
},
handleFeatureSelection: function( event ) {
var ftrack = this;
var selman = ftrack.selectionManager;
var featdiv = (event.currentTarget || event.srcElement);
var feat = featdiv.feature || featdiv.subfeature;
if( selman.unselectableTypes[feat.get('type')] ) {
return;
}
var already_selected = selman.isSelected( { feature: feat, track: ftrack } );
var parent_selected = false;
var parent = feat.parent();
if (parent) {
parent_selected = selman.isSelected( { feature: parent, track: ftrack } );
}
if (this.verbose_selection) {
console.log("DFT.handleFeatureSelection() called, actual mouse event");
console.log(featdiv);
console.log(feat);
console.log("already selected: " + already_selected + ", parent selected: " + parent_selected +
", shift: " + (event.shiftKey));
}
// if parent is selected, allow propagation of event up to parent,
// in order to ensure parent draggable setup and triggering
// otherwise stop propagation
if (! parent_selected) {
event.stopPropagation();
}
if (event.shiftKey) {
if (already_selected) { // if shift-mouse-down and this already selected, deselect this
selman.removeFromSelection( { feature: feat, track: this });
}
else if (parent_selected) {
// if shift-mouse-down and parent selected, do nothing --
// event will get propagated up to parent, where parent will get deselected...
// selman.removeFromSelection(parent);
}
else { // if shift-mouse-down and neither this or parent selected, select this
// children are auto-deselected by selection manager when parent is selected
selman.addToSelection({ feature: feat, track: this });
}
}
else if (event.altKey) {
}
else if (event.ctrlKey) {
}
else if (event.metaKey) {
}
else { // no shift modifier
if (already_selected) { // if this selected, do nothing (this remains selected)
if (this.verbose_selection) { console.log("already selected"); }
}
else {
if (parent_selected) {
// if this not selected but parent selected, do nothing (parent remains selected)
// event will propagate up (since parent_selected), so draggable check
// will be done in bubbled parent event
}
else { // if this not selected and parent not selected, select this
selman.clearSelection();
selman.addToSelection({ track: this, feature: feat});
}
}
}
},
handleFeatureDragSetup: function(event) {
var ftrack = this;
var featdiv = (event.currentTarget || event.srcElement);
if (this.verbose_drag) { console.log("called handleFeatureDragSetup()"); console.log(featdiv); }
var feat = featdiv.feature || featdiv.subfeature;
var selected = this.selectionManager.isSelected( { feature: feat, track: ftrack });
/* if (selected) { // simple version (no multiselect ghosting, no event retriggering for simultaneous select & drag)
var $featdiv = $(featdiv);
$featdiv.draggable( {
helper: 'clone',
opacity: 0.5,
axis: 'y',
} );
}
*/
/**
* ideally would only make $.draggable call once for each selected div
* but having problems with draggability disappearing from selected divs
* that $.draggable was already called on
* therefore whenever mousedown on a previously selected div also want to
* check that draggability and redo if missing
*/
if (selected) {
var $featdiv = $(featdiv);
if (! $featdiv.hasClass("ui-draggable")) {
if (this.verbose_drag) {
console.log("setting up dragability");
console.log(featdiv);
}
$featdiv.draggable( // draggable() adds "ui-draggable" class to div
{
// custom helper for pseudo-multi-drag ("pseudo" because multidrag is visual only --
// handling of draggable when dropped is already done through selection)
// strategy for custom helper is to make a "holder" div with same dimensionsas featdiv
// that's (mostly) a clone of the featdiv draggable is being called on
// (since draggable seems to like that),
// then add clones of all selected feature divs (including another clone of featdiv)
// to holder, with dimensions of each clone recalculated as pixels and set relative to
// featdiv that the drag is actually initiated on (and thus relative to the holder's
// dimensions)
// helper: 'clone',
helper: function() {
var $featdiv_copy = $featdiv.clone();
var $holder = $featdiv.clone();
$holder.removeClass();
$holder.addClass("custom-multifeature-draggable-helper");
var holder = $holder[0];
var featdiv_copy = $featdiv_copy[0];
var foffset = $featdiv.offset();
var fheight = $featdiv.height();
var fwidth = $featdiv.width();
var ftop = foffset.top;
var fleft = foffset.left;
if (this.verbose_drag) {
console.log("featdiv dimensions: ");
console.log(foffset); console.log("height: " + fheight + ", width: " + fwidth);
}
var selection = ftrack.selectionManager.getSelection();
var selength = selection.length;
for (var i=0; i<selength; i++) {
var srec = selection[i];
var strack = srec.track;
var sfeat = srec.feature;
var sfeatdiv = strack.getFeatDiv( sfeat );
// if (sfeatdiv && (sfeatdiv !== featdiv)) {
if (sfeatdiv) {
var $sfeatdiv = $(sfeatdiv);
var $divclone = $sfeatdiv.clone();
var soffset = $sfeatdiv.offset();
var sheight = $sfeatdiv.height();
var swidth =$sfeatdiv.width();
var seltop = soffset.top;
var sleft = soffset.left;
$divclone.width(swidth);
$divclone.height(sheight);
var delta_top = seltop - ftop;
var delta_left = sleft - fleft;
if (this.verbose_drag) {
console.log(sfeatdiv);
console.log("delta_left: " + delta_left + ", delta_top: " + delta_top);
}
// setting left and top by pixel, based on delta relative to moused-on feature
// tried using $divclone.position( { ...., "offset": delta_left + " " + delta_top } );,
// but position() not working for negative deltas? (ends up using absolute value)
// so doing more directly with "left and "top" css calls
$divclone.css("left", delta_left);
$divclone.css("top", delta_top);
var divclone = $divclone[0];
holder.appendChild(divclone);
}
}
if (this.verbose_drag) { console.log(holder); }
return holder;
},
opacity: 0.5,
axis: 'y'
// drag_create setting in create() needed by older drag bootstrapping strategy with JQuery 1.5,
// but not with different JQuery 1.7+ strategy
// , create: function(event, ui) { ftrack.drag_create = true; }
} );
// Want to be able to both make feature draggable and initiate actual dragging with the same mousedown event
// to do this need to retrigger/simulate the mousedown event again
// see http://bugs.jqueryui.com/ticket/3876 regarding switch from previous hacky approach using JQuery 1.5:
// $featdiv.trigger(event) and ftrack.drag_create
// to new hacky approach using JQuery 1.7+:
// data("draggable")._mouseDown(event);
// _mouseDown doesn't lead to another call to onFeatMouseDown, but does trigger the drag
//
// see also http://stackoverflow.com/questions/9634639/why-does-this-break-in-jquery-1-7-x
// for more explanation of event handling changes in JQuery 1.7
// _mouseDown(event) triggering boostrapping of feature drag
// $featdiv.data("draggable")._mouseDown(event);
$featdiv.draggable().data("draggable")._mouseDown(event);
// $featdiv.trigger(event);
}
}
},
onFeatureDoubleClick: function( event ) {
var ftrack = this;
var selman = ftrack.selectionManager;
// prevent event bubbling up to genome view and triggering zoom
event.stopPropagation();
var featdiv = (event.currentTarget || event.srcElement);
if (this.verbose_selection) {
console.log("DFT.featDoubleClick");
console.log(ftrack);
console.log(featdiv);
}
// only take action on double-click for subfeatures
// (but stop propagation for both features and subfeatures)
// GAH TODO: make this work for feature hierarchies > 2 levels deep
var subfeat = featdiv.subfeature;
// if (subfeat && (! unselectableTypes[subfeat.get('type')])) { // only allow double-click parent selection for selectable features
if( subfeat && selman.isSelected({ feature: subfeat, track: ftrack }) ) { // only allow double-click of child for parent selection if child is already selected
var parent = subfeat.parent();
// select parent feature
// children (including subfeat double-clicked one) are auto-deselected in FeatureSelectionManager if parent is selected
if( parent ) { selman.addToSelection({ feature: parent, track: ftrack }); }
}
},
/**
* returns first feature or subfeature div (including itself)
* found when crawling towards root from branch in
* feature/subfeature/descendants div hierachy
*/
getLowestFeatureDiv: function(elem) {
while (!elem.feature && !elem.subfeature) {