-
Notifications
You must be signed in to change notification settings - Fork 2
/
Biojs.Sequence.js
1404 lines (1246 loc) · 40.4 KB
/
Biojs.Sequence.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
/**
* Sequence component
*
* @class
* @extends Biojs
*
* @author <a href="mailto:[email protected]">John Gomez</a>, <a href="mailto:[email protected]">Jose Villaveces</a>
* @version 1.0.0
* @category 3
*
* @requires <a href='http://blog.jquery.com/2011/09/12/jquery-1-6-4-released/'>jQuery Core 1.6.4</a>
* @dependency <script language="JavaScript" type="text/javascript" src="../biojs/dependencies/jquery/jquery-1.4.2.min.js"></script>
*
* @requires <a href='http://jqueryui.com/download'>jQuery UI 1.8.16</a>
* @dependency <script language="JavaScript" type="text/javascript" src="../biojs/dependencies/jquery/jquery-ui-1.8.2.custom.min.js"></script>
*
* @requires <a href='Biojs.Tooltip.css'>Biojs.Tooltip</a>
* @dependency <script language="JavaScript" type="text/javascript" src="src/Biojs.Tooltip.js"></script>
*
* @param {Object} options An object with the options for Sequence component.
*
* @option {string} target
* Identifier of the DIV tag where the component should be displayed.
*
* @option {string} sequence
* The sequence to be displayed.
*
* @option {string} [id]
* Sequence identifier if apply.
*
* @option {string} [format="FASTA"]
* The display format for the sequence representation.
*
* @option {Object[]} [highlights]
* For highlighting multiple regions.
* <pre class="brush: js" title="Syntax:">
* [
* // Highlight aminoacids from 'start' to 'end' of the current strand using the specified 'color' (optional) and 'background' (optional).
* { start: <startVal1>, end: <endVal1> [, id:<idVal1>] [, color: <HTMLColor>] [, background: <HTMLColor>]},
* //
* // Any others highlights
* ...,
* //
* { start: <startValN>, end: <endValN> [, id:<idValN>] [, color: <HTMLColor>] [, background: <HTMLColor>]}
* ]</pre>
*
* <pre class="brush: js" title="Example:">
* highlights : [
* { start:30, end:42, color:"white", background:"green", id:"spin1" },
* { start:139, end:140 },
* { start:631, end:633, color:"white", background:"blue" }
* ]
* </pre>
*
* @option {Object} [columns={size:40,spacedEach:10}]
* Options for displaying the columns. Syntax: { size: <numCols>, spacedEach: <numCols>}
*
* @option {Object} [selection]
* Positions for the current selected region. Syntax: { start: <startValue>, end: <endValue>}
*
* @option {Object[]} [annotations]
* Set of overlapping annotations. Must be an array of objects following the syntax:
* <pre class="brush: js" title="Syntax:">
* [
* // An annotation:
* { name: <name>,
* html: <message>,
* color: <color_code>,
* regions: [{ start: <startVal1>, end: <endVal1> color: <HTMLColor>}, ...,{ start: <startValN>, end: <endValN>, color: <HTMLColor>}]
* },
*
* // ...
* // more annotations here
* // ...
* ]
* </pre>
* where:
* <ul>
* <li><b>name</b> is the unique name for the annotation</li>
* <li><b>html</b> is the message (can be HTML) to be displayed in the tool tip.</li>
* <li><b>color</b> is the default HTML color code for all the regions.</li>
* <li><b>regions</b> array of objects defining the intervals which belongs to the annotation.</li>
* <li><b>regions[i].start</b> is the starting character for the i-th interval.</li>
* <li><b>regions[i].end</b> is the ending character for the i-th interval.</li>
* <li><b>regions[i].color</b> is an optional color for the i-th interval.
* </ul>
*
* @option {Object} [formatOptions={title:true, footer:true}]
* Options for displaying the title. by now just affecting the CODATA format.
* <pre class="brush: js" title="Syntax:">
* formatOptions : {
* title:false,
* footer:false
* }
* </pre>
*
* @example
* var theSequence = "METLCQRLNVCQDKILTHYENDSTDLRDHIDYWKHMRLECAIYYKAREMGFKHINHQVVPTLAVSKNKALQAIELQLTLETIYNSQYSNEKWTLQDVSLEVYLTAPTGCIKKHGYTVEVQFDGDICNTMHYTNWTHIYICEEAojs SVTVVEGQVDYYGLYYVHEGIRTYFVQFKDDAEKYSKNKVWEVHAGGQVILCPTSVFSSNEVSSPEIIRQHLANHPAATHTKAVALGTEETQTTIQRPRSEPDTGNPCHTTKLLHRDSVDSAPILTAFNSSHKGRINCNSNTTPIVHLKGDANTLKCLRYRFKKHCTLYTAVSSTWHWTGHNVKHKSAIVTLTYDSEWQRDQFLSQVKIPKTITVSTGFMSI";
* var mySequence = new Biojs.Sequence({
* sequence : theSequence,
* target : "YourOwnDivId",
* format : 'CODATA',
* id : 'P918283',
* annotations: [
* { name:"CATH",
* color:"#F0F020",
* html: "Using color code #F0F020 ",
* regions: [{start: 122, end: 135}]
* },
* { name:"TEST",
* html:"<br> Example of <b>HTML</b>",
* color:"green",
* regions: [
* {start: 285, end: 292},
* {start: 293, end: 314, color: "#2E4988"}]
* }
* ],
* highlights : [
* { start:30, end:42, color:"white", background:"green", id:"spin1" },
* { start:139, end:140 },
* { start:631, end:633, color:"white", background:"blue" }
* ]
* });
*
*/
Biojs.Sequence = Biojs.extend(
/** @lends Biojs.Sequence# */
{
constructor: function (options) {
var self = this;
this._container = jQuery( "#" + this.opt.target );
// Lazy initialization
this._container.ready(function() {
self._initialize();
});
},
/**
* Default values for the options
* @name Biojs.Sequence-opt
*/
opt : {
sequence : "",
id : "",
target : "",
format : "FASTA",
selection: { start: 0, end: 0 },
columns: { size: 35, spacedEach: 10 },
highlights : [],
annotations: [],
sequenceUrl: 'http://www.ebi.ac.uk/das-srv/uniprot/das/uniprot/sequence',
// Styles
selectionColor : 'Yellow',
selectionFontColor : 'black',
highlightFontColor : 'red',
highlightBackgroundColor : 'white',
fontFamily: '"Andale mono", courier, monospace',
fontSize: '12px',
fontColor : 'inherit',
backgroundColor : 'inherit',
width: undefined,
height: undefined,
formatSelectorVisible: true
},
/**
* Array containing the supported event names
* @name Biojs.Sequence-eventTypes
*/
eventTypes : [
/**
* @name Biojs.Sequence#onSelectionChanged
* @event
* @param {function} actionPerformed An function which receives an {@link Biojs.Event} object as argument.
* @eventData {Object} source The component which did triggered the event.
* @eventData {string} type The name of the event.
* @eventData {int} start A number indicating the start of the selection.
* @eventData {int} end A number indicating the ending of selection.
* @example
* mySequence.onSelectionChanged(
* function( objEvent ) {
* alert("Selected: " + objEvent.start + ", " + objEvent.end );
* }
* );
*
* */
"onSelectionChanged",
/**
* @name Biojs.Sequence#onSelectionChange
* @event
* @param {function} actionPerformed An function which receives an {@link Biojs.Event} object as argument.
* @eventData {Object} source The component which did triggered the event.
* @eventData {string} type The name of the event.
* @eventData {int} start A number indicating the start of the selection.
* @eventData {int} end A number indicating the ending of selection.
* @example
* mySequence.onSelectionChange(
* function( objEvent ) {
* alert("Selection in progress: " + objEvent.start + ", " + objEvent.end );
* }
* );
*
*
* */
"onSelectionChange",
/**
* @name Biojs.Sequence#onAnnotationClicked
* @event
* @param {function} actionPerformed An function which receives an {@link Biojs.Event} object as argument.
* @eventData {Object} source The component which did triggered the event.
* @eventData {string} type The name of the event.
* @eventData {string} name The name of the selected annotation.
* @eventData {int} pos A number indicating the position of the selected amino acid.
* @example
* mySequence.onAnnotationClicked(
* function( objEvent ) {
* alert("Clicked " + objEvent.name + " on position " + objEvent.pos );
* }
* );
*
* */
"onAnnotationClicked"
],
// internal members
_headerDiv : null,
_contentDiv : null,
// Methods
_initialize: function () {
if ( this.opt.width !== undefined ) {
this._container.width( this.opt.width );
}
if ( this.opt.height !== undefined ) {
this._container.height( this.opt.height );
}
// Disable text selection
this._container.css({
'-moz-user-select':'none',
'-webkit-user-select':'none',
'user-select':'none'
});
// DIV for the format selector
this._buildFormatSelector();
// DIV for the sequence
this._contentDiv = jQuery('<div></div>').appendTo(this._container);
this._contentDiv.css({
'font-family': this.opt.fontFamily,
'font-size': this.opt.fontSize,
'text-align': 'left'
});
// Initialize highlighting
this._highlights = this.opt.highlights;
// Initialize annotations
this._annotations = this.opt.annotations;
//Initialize tooltip
jQuery('<div id="sequenceTip' + this.getId() + '"></div>')
.css({
'position': "absolute",
'z-index': "999999",
'color': "#fff",
'font-size': "12px",
'width': "auto",
'display': 'none'
})
.addClass("tooltip")
.appendTo("body")
.hide();
if ( ! Biojs.Utils.isEmpty(this.opt.sequence) ) {
this._redraw();
} else if ( ! Biojs.Utils.isEmpty(this.opt.id) ) {
this._requestSequence( this.opt.id );
} else {
this.clearSequence("No sequence available", "../biojs/css/images/warning_icon.png");
}
},
/**
* Shows the columns indicated by the indexes array.
* @param {string} seq The sequence strand.
* @param {string} [identifier] Sequence identifier.
*
* @example
* mySequence.setSequence("P99999");
*
*/
setSequence: function ( seq, identifier ) {
if ( seq.match(/^([A-N,R-Z][0-9][A-Z][A-Z, 0-9][A-Z, 0-9][0-9])|([O,P,Q][0-9][A-Z, 0-9][A-Z, 0-9][A-Z, 0-9][0-9])(\.\d+)?$/i) ) {
this._requestSequence( arguments[0] );
} else {
this.opt.sequence = seq;
this.opt.id = identifier;
this._highlights = [];
this._highlightsCount = 0;
this.opt.selection = { start: 0, end: 0 };
this._annotations = [];
this._contentDiv.children().remove();
this._redraw();
}
},
_requestSequence: function ( accession ) {
var self = this;
Biojs.console.log("Requesting sequence for: " + accession );
jQuery.ajax({
url: self.opt.sequenceUrl,
dataType: "xml",
data: { segment: accession },
success: function ( xml ) {
try {
var sequenceNode = jQuery(xml).find('SEQUENCE:first');
self.setSequence( sequenceNode.text(), sequenceNode.attr("id"), sequenceNode.attr("label") );
} catch (e) {
Biojs.console.log("Error decoding response data: " + e.message );
self.clearSequence("No sequence available", "../biojs/css/images/warning_icon.png");
}
},
error: function (jqXHR, textStatus, errorThrown) {
Biojs.console.log("Error decoding response data: " + textStatus );
self.clearSequence("Error requesting the sequence to the server " + this.url , "../biojs/css/images/warning_icon.png");
}
});
},
/**
* Shows the columns indicated by the indexes array.
* @param {string} [showMessage] Message to be showed.
* @param {string} [icon] Icon to be showed a side of the message
*
* @example
* mySequence.clearSequence("No sequence available", "../biojs/css/images/warning_icon.png");
*
*/
clearSequence: function ( showMessage, icon ) {
var message = undefined;
this.opt.sequence = "";
this.opt.id = "";
this._highlights = [];
this._highlightsCount = 0;
this.opt.selection = { start: 0, end: 0 };
this._annotations = [];
this._contentDiv.children().remove();
this._headerDiv.hide();
if ( undefined !== showMessage ) {
message = jQuery('<div>' + showMessage + '</div>')
.appendTo(this._contentDiv)
.addClass("message");
if ( undefined !== icon ) {
message.css({
'background': 'transparent url("' + icon + '") no-repeat center left',
'padding-left': '20px'
});
}
}
},
/**
* Set the current selection in the sequence causing the event {@link Biojs.Sequence#onSelectionChanged}
*
* @example
* // set selection from the position 100 to 150
* mySequence.setSelection(100, 150);
*
* @param {int} start The starting character of the selection.
* @param {int} end The ending character of the selection
*/
setSelection : function(start, end) {
if(start > end) {
var aux = end;
end = start;
start = aux;
}
if(start != this.opt.selection.start || end != this.opt.selection.end) {
this._setSelection(start, end);
this.raiseEvent(
Biojs.Sequence.EVT_ON_SELECTION_CHANGED,
{ "start" : start, "end" : end }
);
}
},
_buildFormatSelector: function () {
var self = this;
this._headerDiv = jQuery('<div></div>').appendTo(this._container);
this._headerDiv.css({
'font-family': '"Heveltica Neue", Arial, "sans serif"',
'font-size': '14px'
}).append('Format: ');
this._formatSelector = jQuery('<select> '+
'<option value="FASTA">FASTA</option>'+
'<option value="CODATA">CODATA</option>'+
'<option value="PRIDE">PRIDE</option>'+
'<option value="RAW">RAW</option></select>').appendTo(self._headerDiv);
this._formatSelector.change(function(e) {
self.opt.format = jQuery(this).val();
self._redraw();
});
this._formatSelector.val(self.opt.format);
this.formatSelectorVisible( this.opt.formatSelectorVisible );
},
/**
* Highlights a region using the font color defined in {Biojs.Protein3D#highlightFontColor} by default is red.
*
* @deprecated use addHighlight instead.
*
* @param {int} start The starting character of the highlighting.
* @param {int} end The ending character of the highlighting.
* @param {string} [color] HTML color code.
* @param {string} [background] HTML color code.
* @param {string} [id] Custom identifier.
*
* @return {int} representing the id of the highlight on the internal array. Returns -1 on failure
*/
highlight : function (start, end, color, background, id ) {
return this.addHighlight({ "start": start, "end": end, "color": color, "background": background, "id": id });
},
/**
* Highlights a region using the font color defined in {Biojs.Sequence#highlightFontColor} by default is red.
*
* @example
* // highlight the characters within the position 100 to 150, included.
* mySequence.addHighlight( { "start": 100, "end": 150, "color": "white", "background": "red", "id": "aaa" } );
*
* @param {Object} h The highlight defined as follows:
*
*
* @return {int} representing the id of the highlight on the internal array. Returns -1 on failure
*/
addHighlight : function ( h ) {
var id = '-1';
var color = "";
var background = "";
var highlight = {};
if ( h instanceof Object && h.start <= h.end ) {
color = ( "string" == typeof h.color )? h.color : this.opt.highlightFontColor;
background = ( "string" == typeof h.background )? h.background : this.opt.highlightBackgroundColor;
id = ( "string" == typeof h.id )? h.id : (new Number(this._highlightsCount++)).toString();
highlight = { "start": h.start, "end": h.end, "color": color, "background": background, "id": id };
this._highlights.push(highlight);
this._applyHighlight(highlight);
this._restoreSelection(h.start,h.end);
}
return id;
},
/*
* Function: Biojs.Sequence._applyHighlight
* Purpose: Apply the specified color and background to a region between 'start' and 'end'.
* Returns: -
* Inputs: highlight -> {Object} An object containing the fields start (int), end (int),
* color (HTML color string) and background (HTML color string).
*/
_applyHighlight: function ( highlight ) {
var seq = this._contentDiv.find('.sequence');
for ( var i = highlight.start - 1; i < highlight.end; i++ ){
zindex = jQuery(seq[i]).css("z-index");
if (zindex=="auto"){
z = 1;
o = 1;
}
else{
z = 0;
o = 0.5;
}
jQuery(seq[i])
.css({
"color": highlight.color,
"background-color": highlight.background,
"z-index": z,
"opacity": o
})
.addClass("highlighted");
}
},
/*
* Function: Biojs.Sequence._applyHighlights
* Purpose: Apply the specified highlights.
* Returns: -
* Inputs: highlights -> {Object[]} An array containing the highlights to be applied.
*/
_applyHighlights: function ( highlights ) {
for ( var i in highlights ) {
this._applyHighlight(highlights[i]);
}
},
/*
* Function: Biojs.Sequence._restoreHighlights
* Purpose: Repaint the highlights in the specified region.
* Returns: -
* Inputs: start -> {int} Start of the region to be restored.
* end -> {int} End of the region to be restored.
*/
_restoreHighlights: function ( start, end ) {
var h = this._highlights;
// paint the region using default blank settings
this._applyHighlight({
"start": start,
"end": end,
"color": this.opt.fontColor,
"background": this.opt.backgroundColor
});
// restore highlights in that region
for ( var i in h ) {
// interval intersects with highlight i ?
if ( !( h[i].start > end || h[i].end < start ) ) {
a = ( h[i].start < start ) ? start : h[i].start;
b = ( h[i].end > end ) ? end : h[i].end;
this._applyHighlight({
"start": a,
"end": b,
"color": h[i].color,
"background": h[i].background
});
}
}
},
/*
* Function: Biojs.Sequence._restoreSelection
* Purpose: Repaint the current selection in the specified region.
* It is used in the case of any highlight do overriding of the current selection.
* Returns: -
* Inputs: start -> {int} Start of the region to be restored.
* end -> {int} End of the region to be restored.
*/
_restoreSelection: function ( start, end ) {
var sel = this.opt.selection;
// interval intersects with current selection ?
// restore selection
if ( !( start > sel.end || end < sel.start ) ) {
a = ( start < sel.start ) ? sel.start : start;
b = ( end > sel.end ) ? sel.end : end;
this._applyHighlight({
"start": a,
"end": b,
"color": this.opt.selectionFontColor,
"background": this.opt.selectionColor,
});
}
},
/**
* Clear a highlighted region using.
*
* @deprecated use removeHighlight instead.
*
* @param {int} id The id of the highlight on the internal array. This value is returned by method highlight.
*/
unHighlight : function (id) {
this.removeHighlight(id);
},
/**
* Remove a highlight.
*
* @example
* // Clear the highlighted characters within the position 100 to 150, included.
* mySequence.removeHighlight("spin1");
*
* @param {string} id The id of the highlight on the internal array. This value is returned by method highlight.
*/
removeHighlight : function (id) {
var h = this._highlights;
for ( i in h ) {
if ( h[i].id == id ) {
start = h[i].start;
end = h[i].end;
h.splice(i,1);
this._restoreHighlights(start,end);
this._restoreSelection(start,end);
break;
}
}
},
/**
* Clear the highlights of whole sequence.
* @deprecated use removeAllHighlights instead.
*/
unHighlightAll : function () {
this.removeAllHighlights();
},
/**
* Remove all the highlights of whole sequence.
*
* @example
* mySequence.removeAllHighlights();
*/
removeAllHighlights : function () {
this._highlights = [];
this._restoreHighlights(1,this.opt.sequence.length);
this._restoreSelection(1,this.opt.sequence.length);
},
/**
* Changes the current displaying format of the sequence.
*
* @example
* // Set format to 'FASTA'.
* mySequence.setFormat('FASTA');
*
* @param {string} format The format for the sequence to be displayed.
*/
setFormat : function(format) {
if ( this.opt.format != format.toUpperCase() ) {
this.opt.format = format.toUpperCase();
this._redraw();
}
var self = this;
// Changes the option in the combo box
this._headerDiv.find('option').each(function() {
if(jQuery(this).val() == self.opt.format.toUpperCase()) {
jQuery(this).attr('selected', 'selected');
}
});
},
/**
* Changes the current number of columns in the displayed sequence.
*
* @example
* // Set the number of columns to 70.
* mySequence.setNumCols(70);
*
* @param {int} numCols The number of columns.
*/
setNumCols : function(numCols) {
this.opt.columns.size = numCols;
this._redraw();
},
/**
* Set the visibility of the drop-down list of formats.
*
* @param {boolean} visible true: show; false: hide.
*/
formatSelectorVisible : function (visible){
if (visible) {
this._headerDiv.show();
} else {
this._headerDiv.hide();
}
},
/**
* This is similar to a {Biojs.Protein3D#formatSelectorVisible} with the 'true' argument.
*
* @example
* // Shows the format selector.
* mySequence.showFormatSelector();
*
*/
showFormatSelector : function() {
this._headerDiv.show();
},
/**
* This is similar to a {Biojs.Protein3D#formatSelectorVisible} with the 'false' argument.
*
* @example
* // Hides the format selector.
* mySequence.hideFormatSelector();
*
*/
hideFormatSelector : function() {
this._headerDiv.hide();
},
/**
* Hides the whole component.
*
*/
hide : function () {
this._headerDiv.hide();
this._contentDiv.hide();
},
/**
* Shows the whole component.
*
*/
show : function () {
this._headerDiv.show();
this._contentDiv.show();
},
/*
* Function: Biojs.Sequence._setSelection
* Purpose: Update the current selection.
* Returns: -
* Inputs: start -> {int} Start of the region to be selected.
* end -> {int} End of the region to be selected.
*/
_setSelection : function(start, end) {
//alert("adsas");
var current = this.opt.selection;
var change = {};
// Which is the change on selection?
if ( current.start == start ) {
// forward?
if ( current.end < end ) {
change.start = current.end;
change.end = end;
} else {
this._restoreHighlights(end+1, current.end);
}
} else if ( current.end == end ) {
// forward?
if ( current.start > start ) {
change.start = start;
change.end = current.start;
} else {
this._restoreHighlights(current.start, start-1);
}
} else {
this._restoreHighlights(current.start, current.end);
change.start = start;
change.end = end;
}
current.start = start;
current.end = end;
if ( change.start != undefined ) {
this._applyHighlight({
"start": change.start,
"end": change.end,
"color": this.opt.selectionFontColor,
"background": this.opt.selectionColor
});
}
},
/*
* Function: Biojs.Sequence._repaintSelection
* Purpose: Repaint the whole current selection.
* Returns: -
* Inputs: -
*/
_repaintSelection: function(){
var s = Biojs.Utils.clone(this.opt.selection);
this._setSelection(0,0);
this._setSelection(s.start,s.end);
},
/*
* Function: Biojs.Sequence._redraw
* Purpose: Repaint the current sequence.
* Returns: -
* Inputs: -
*/
_redraw : function() {
var i = 0;
var self = this;
// Reset the content
//this._contentDiv.text('');
this._contentDiv.children().remove();
// Rebuild the spans of the sequence
// according to format
if(this.opt.format == 'RAW') {
this._drawRaw();
} else if(this.opt.format == 'CODATA') {
this._drawCodata();
} else if (this.opt.format == 'FASTA'){
this._drawFasta();
} else {
this.opt.format = 'PRIDE';
this._drawPride();
}
// Restore the highlighted regions
this._applyHighlights(this._highlights);
this._repaintSelection();
this._addSpanEvents();
},
/*
* Function: Biojs.Sequence._drawFasta
* Purpose: Repaint the current sequence using FASTA format.
* Returns: -
* Inputs: -
*/
_drawFasta : function() {
var self = this;
var a = this.opt.sequence.toUpperCase().split('');
var pre = jQuery('<pre></pre>').appendTo(this._contentDiv);
var i = 1;
var arr = [];
var str = '>' + this.opt.id + ' ' + a.length + ' bp<br/>';
/* Correct column size in case the sequence is as small peptide */
var numCols = this.opt.columns.size;
if ( this.opt.sequence.length < this.opt.columns.size ) {
numCols = this.opt.sequence.length;
}
var opt = {
numCols: numCols,
numColsForSpace: 0
};
str += this._drawSequence(a, opt);
pre.html(str);
this._drawAnnotations(opt);
},
/*
* Function: Biojs.Sequence._drawCodata
* Purpose: Repaint the current sequence using CODATA format.
* Returns: -
* Inputs: -
*/
_drawCodata : function() {
var self = this;
var a = this.opt.sequence.toUpperCase().split('');
var pre = jQuery('<pre style="white-space:pre"></pre>').appendTo(this._contentDiv);
var i = 0;
var str = 'ENTRY ' + this.opt.id + '<br/>';
str += 'SEQUENCE<br/>';
if ( this.opt.formatOptions !== undefined ){
if(this.opt.formatOptions.title !== undefined ){
if (this.opt.formatOptions.title == false) {
str = '';
}
}
}
/* Correct column size in case the sequence is as small peptide */
var numCols = this.opt.columns.size;
if ( this.opt.sequence.length < this.opt.columns.size ) {
numCols = this.opt.sequence.length;
}
var opt = {
numLeft: true,
numLeftSize: 7,
numLeftPad:' ',
numTop: true,
numTopEach: 5,
numCols: numCols,
numColsForSpace: 0,
spaceBetweenChars: true
};
str += this._drawSequence(a, opt);
var footer = '<br/>///';
if (this.opt.formatOptions !== undefined) {
if (this.opt.formatOptions.footer !== undefined) {
if (this.opt.formatOptions.footer == false) {
footer = '';
}
}
}
str += footer;
pre.html(str);
this._drawAnnotations(opt);
},
/*
* Function: Biojs.Sequence._drawAnnotations
* Purpose: Paint the annotations on the sequence.
* Returns: -
* Inputs: settings -> {object}
*/
_drawAnnotations: function ( settings ){
var self = this;
var a = this.opt.sequence.toLowerCase().split('');
var annotations = this._annotations;
var leftSpaces = '';
var row = '';
var annot = '';
// Index at the left?
if ( settings.numLeft ) {
leftSpaces += this._formatIndex(' ', settings.numLeftSize+2, ' ');
}
for ( var i = 0; i < a.length; i += settings.numCols ){
row = '';
for ( var key in annotations ){
annotations[key].id = this.getId() + "_" + key;
annot = this._getHTMLRowAnnot(i+1, annotations[key], settings);
if (annot.length > 0) {
row += '<br/>';
row += leftSpaces;
row += annot;
row += '<br/>';
}
}
var numCols = settings.numCols;
var charRemaining = a.length-i;
if(charRemaining < numCols){
numCols = charRemaining;
}
if ( settings.numRight ) {
jQuery(row).insertAfter('div#'+self.opt.target+' div pre span#numRight_' + this.getId() + '_' + (i + numCols) );
} else {
jQuery(row).insertAfter('div#'+self.opt.target+' div pre span#'+ this.getId() + '_' + (i + numCols) );
}
}
// add tool tips and background' coloring effect
jQuery(this._contentDiv).find('.annotation').each( function(){
self._addToolTip( this, function() {
return self._getAnnotationString( jQuery(this).attr("id") );
});
jQuery(this).mouseover(function(e) {
jQuery('.annotation.'+jQuery(e.target).attr("id")).each(function(){
jQuery(this).css("background-color", jQuery(this).attr("color") );
});
}).mouseout(function() {
jQuery('.annotation').css("background-color", "transparent");
}).click(function(e) {
self.raiseEvent( Biojs.Sequence.EVT_ON_ANNOTATION_CLICKED, {
"name": self._annotations[ jQuery(e.target).attr("id") ].name,
"pos": parseInt( jQuery(e.target).attr("pos") )
});
});
});
},
/*
* Function: Biojs.Sequence._getAnnotationString
* Purpose: Get the annotation text message for the tooltip
* Returns: {string} Annotation text for the annotation
* Inputs: id -> {int} index of the internal annotation array
*/
_getAnnotationString: function ( id ) {
var annotation = this._annotations[id.substr(id.indexOf("_") + 1)];
return annotation.name + "<br/>" + ((annotation.html)? annotation.html : '');
},
/*
* Function: Biojs.Sequence._getHTMLRowAnnot
* Purpose: Build an annotation