-
Notifications
You must be signed in to change notification settings - Fork 5
/
JSAV.js
1994 lines (1734 loc) · 64.5 KB
/
JSAV.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
/** @preserve
@file
JSAV V1.7 23.09.15
Copyright: (c) Dr. Andrew C.R. Martin, UCL, 2014-2015
This program is distributed under the Gnu Public Licence (GPLv2)
*/
/** ***********************************************************************
Program: JSAV
File: JSAV.js
Version: V1.7
Date: 23.09.15
Function: JavaScript Sequence Alignment Viewier
Copyright: (c) Dr. Andrew C.R. Martin, UCL, 2014-2015
Author: Dr. Andrew C.R. Martin
Address: Institute of Structural and Molecular Biology
Division of Biosciences
University College
Gower Street
London
WC1E 6BT
EMail: andrew@bioinf.org.uk
**************************************************************************
This program is distributed under the Gnu Public licence (GPLv2 or
above)
Alternative licences for commercial use are available on request.
**************************************************************************
Description:
============
JSAV is a simple JavaScript protein sequence alignment viewer. It
allows you to select a region of the alignment and sort on that
region.
**************************************************************************
Revision History:
=================
V1.0 06.06.14 Original By: ACRM
V1.1 10.06.14 Code cleanup
Changed options to a hash
Further code cleanup - some code was assuming
sequences were stored in an array called 'sequences'
Added 'selectable' option
12.06.14 Added 'deletable' and 'border' options
Implemented sequence deletion
13.06.14 Checks that some sequences are selected before
deletion
Cleaned up comments/documentation
Cleaned up defaults in printJSAV
Changed some routine names
V1.2 13.06.14 Added highlight option
Added submit/submitLabel options
Added action/actionLabel options
V1.2.1 15.06.14 Added height option
Changed to use ACRM_alert()
V1.2.2 16.06.14 Changed to use ACRM_confirm()
V1.3 16.06.14 Added dotify/nocolour/toggleDotify/toggleNocolour
V1.4 17.06.14 Added fasta/fastaLabel export options
Added consensus sequence display and colourScheme
Added selectColour/selectColor and
colourChoices/colorChoices
Added refresh of options on reload
V1.5 18.06.14 Added tooltips
V1.5.1 19.06.14 Added callback option
V1.6 17.09.14 Changed to manipulate the DOM rather than writing
to the document By: JHN
V1.7 23.09.15 Added options.toggleDotifyLabel
Added options.toggleNocolourLabel and options.toggleNocolorLabel
Added options.deleteLabel
Added options.idSubmit
By: ACRM
TODO:
1. Bar display of conservation from entropy
2. Allow user to specify key sequence for sorting
3. Clean up passing of globals
*************************************************************************/
/**
This is the only routine called by a user. It takes an array of
sequence objects and displays them as a coloured sortable table
optionally with a slider and sort button, delete button, etc
@example
var Seqs = [];
Seqs.push({id :"id1b1.L", sequence :"SASSSVNYMYACREFGHIKLMNPTRSTVWY"});
Seqs.push({id :"id1a.L", sequence :"SASSSTNYMYACDEFGHIKLMNPQRSTVWY"});
var options = Array();
options.width = '400px';
options.sortable = true;
options.highlight = [3,5,10,14];
options.submit = "http://www.bioinf.org.uk/cgi-bin/echo.pl";
options.submitLabel = "Submit sequences";
options.action = "myAction";
options.actionLabel = "My Action";
options.consensus = true;
options.deletable = true;
options.fasta = true;
printJSAV('mySeqDisplay', sequenceArray, options);
Where 'mySeqDisplay' is the name of a div that will be created
sequenceArray is an array of sequence objects
options is an object describing options - see below
@param {object[]} sequences - Array of sequence objects
@param {string} divId - ID of div to print in
@param {Object} options - Options that can be provided - see Properties
@property {bool} options.sortable - Should the sorting options be displayed
(default: false)
@property {string} options.width - The width of the selection slider with
units (default: '400px')
@property {string} options.height - The height of the selection slider with
units (default: '6pt')
@property {bool} options.selectable - Should selection checkboxes be displayed
for each sequence
@property {bool} options.deletable - Makes it possible to delete sequences
@property {string} options.deleteLabel - Label for delete button
@property {int[]} options.highlight - Array of ranges to highlight
@property {string} options.submit - URL for submitting selected sequences
@property {string} options.submitLabel - Label for submit button
@property {string} options.idSubmit - URL for submitting a single sequence where its
id/label has been clicked
@property {bool} options.idSubmitClean - Remove non-alpha characters from sequence
before submitting
@property {string} options.action - Function to call using selected sequences.
This is passed the seqId and array of
currently selected sequence objects
@property {string} options.actionLabel - Label for action button
@property {bool} options.dotify - Repeated amino acids in the sequence are
replaced by a dot
@property {bool} options.nocolour - Dotified amino acids are not coloured
(except deletions)
@property {bool} options.toggleDotify - Create a check box for toggling dotify
@property {string} options.toggleDotifyLabel - Label for dotify checkbox toggle
@property {bool} options.toggleNocolour - Create a check box for toggling nocolour
@property {string} options.toggleNocolourLabel - Label for nocolout checkbox toggle
@property {bool} options.fasta - Create a FASTA export button
@property {string} options.fastaLabel - Label for FASTA export button
@property {bool} options.consensus - Display consensus sequence
@property {string} options.colourScheme - Default colour scheme - valid options
depend on the css, but are currently
taylor, clustal, zappo, hphob, helix,
strand, turn, buried. Note that this must be
specified in lower case
@property {bool} options.selectColour - Display a pull-down to choose the colour
scheme.
@property {string[]} options.colourChoices - Array of colour scheme names - only used
if the user has added to the CSS. This
can be in mixed case.
@property {bool} options.plainTooltips - Don't use JQuery tooltips
@property {callback} options.callback - Specify the name of a function to be
called whenever the display is refreshed.
This is passed the seqId
@author
- 29.05.14 Original By: ACRM
- 30.05.14 Now just calls JSAV_buildSequencesHTML() and prints the results
- 05.06.14 Added divId parameter and sortable
- 06.06.14 Added width
- 10.06.14 sortable and width parameters now replaced by 'options'
Added 'selectable' option
Stores sequence length in global array
- 11.06.14 Added deletable
- 13.06.14 Cleaned up use of defaults
- 13.06.14 Added highlight
- 13.06.14 Added submit and action, submitLabel and actionLabel
- 15.06.14 Added height
- 16.06.14 Added dotify, nocolour, toggleDotify, toggleNocolour
- 17.06.14 Added fasta, fastaLabel
Added consensus
Added colourScheme/colorScheme
Added selectColour/selectColor and colourChoices/colorChoices
- 18.06.14 Added tooltips and plainTooltips option
- 19.06.14 Added callback
- 02.09.14 Avoid using write and writeln. Rather use jQuery to insert into DOM.
Fixes overwrite issues with using after page closure. By: JHN
- 23.09.15 Added toggleDotifyLabel
Added toggleNocolourLabel/toggleNocolorLabel
Added deleteLabel
Move FASTA before submit and action buttons
Added idSubmit and idSubmitClean
By: ACRM
*/
function printJSAV(divId, sequences, options)
{
// Deal with options
if(options == undefined) { options = Array(); }
if(options.width == undefined) { options.width = "400px"; }
if(options.height == undefined) { options.height = "6pt"; }
if(options.submitLabel == undefined) { options.submitLabel = "Submit Sequences"; }
if(options.actionLabel == undefined) { options.actionLabel = "Process Sequences"; }
if(options.nocolor) { options.nocolour = true; }
if(options.toggleNocolor) { options.toggleNocolour = true; }
if(options.fastaLabel == undefined) { options.fastaLabel = "Export Selected"; }
if(options.colorScheme) { options.colourScheme = options.colorScheme; }
if(options.colourScheme == undefined) { options.colourScheme = "taylor"; }
if(options.selectColor) { options.selectColour = true; }
if(options.colorChoices != undefined) { options.colourChoices = options.colorChoices; }
if(options.deletable) { options.selectable = true; }
if(options.toggleDotifyLabel == undefined) { options.toggleDotifyLabel = "Dotify"; }
if(options.toggleNocolourLabel == undefined) { options.toggleNocolourLabel = "Do not colour dots"; }
if(options.toggleNocolorLabel != undefined) { options.toggleNocolourLabel = options.toggleNocolorLabel;}
if(options.deleteLabel == undefined) { options.deleteLabel = "Delete Selected"; }
// Initialize globals if not yet done
JSAV_init();
gOptions[divId] = options;
gSequences[divId] = sequences;
gSequenceLengths[divId] = sequences[0].sequence.length;
// Enable JQuery tooltips
if(!options.plainTooltips)
{
$(function() {
$(document).tooltip();
});
}
if(options.consensus)
{
gConsensus[divId] = JSAV_buildConsensus(sequences);
}
var div = '';
if($("#" + divId).length == 0) {
div = $('<div />').appendTo($('body'));
div.attr('id', divId);
}
else{
div = $("#" + divId);
}
var div_sortable = $('<div />').appendTo(div);
div_sortable.attr('id', divId + '_sortable');
div_sortable.attr('class', 'JSAVDisplay');
var html = JSAV_buildSequencesHTML(divId, sequences, options.sortable,
options.selectable, options.highlight,
options.dotify, options.nocolour, options.consensus);
div_sortable.append(html);
var div_controls = $('<div />').appendTo(div);
div_controls.attr('id', divId + '_controls');
div_controls.attr('class', 'JSAVControls');
if(options.sortable)
{
var start = 1;
var stop = gSequenceLengths[divId];
JSAV_printSlider(divId, stop, options.width, options.height);
var html = "<button type='button' class='tooltip sortbutton' title='Sort the sequences based on the range specified with the slider' onclick='JSAV_sortAndRefreshSequences(\"" + divId + "\", true, " + options.selectable + ", " + options.border + ")'>Sort</button>";
div_controls.append(html);
}
// 23.09.15 Pass label into JSAV_printDelete()
if(options.deletable)
{
JSAV_printDelete(divId, options.deleteLabel);
}
// 23.09.15 Move FASTA before submit and action buttons
if(options.fasta)
{
JSAV_printFASTA(divId);
}
if(options.submit != undefined)
{
JSAV_printSubmit(divId, options.submit, options.submitLabel);
}
if(options.action != undefined)
{
JSAV_printAction(divId, options.action, options.actionLabel);
}
// Colour related - on a new line
if(options.selectColour || options.toggleDotify)
{
div_controls.append("<br />");
}
if(options.selectColour)
{
JSAV_printColourSelector(divId, options);
}
if(options.toggleDotify)
{
JSAV_printToggleDotify(divId, options);
if(options.toggleNocolour)
{
JSAV_printToggleNocolour(divId, options);
}
}
if(options.border)
{
JSAV_modifyCSS(divId);
}
// Ensure buttons etc match the data
window.onload = function(){JSAV_refreshSettings(divId);};
if(options.callback != undefined)
{
window[options.callback](divId);
}
}
// ---------------------------------------------------------------------
/**
Updates the buttons to match the settings. This is called when the
window is reloaded
@param {string} divId - The ID of the div we are printing in
@author
- 17.06.14 Original By: ACRM
*/
function JSAV_refreshSettings(divId)
{
if(gOptions[divId].selectColour)
{
// Colour scheme
var tag = "#" + divId + "_selectColour";
$(tag).val(gOptions[divId].colourScheme);
}
if(gOptions[divId].toggleDotify)
{
// Dotify option
var tag = "#" + divId + "_toggleDotify";
if(gOptions[divId].dotify == undefined)
{
$(tag).prop('checked', false);
}
else
{
$(tag).prop('checked', gOptions[divId].dotify);
}
}
if(gOptions[divId].toggleNocolour)
{
// Dotify-nocolour option
var tag = "#" + divId + "_toggleNocolour";
if(gOptions[divId].nocolour == undefined)
{
$(tag).prop('checked', false);
}
else
{
$(tag).prop('checked', gOptions[divId].nocolour);
}
}
if(gOptions[divId].selectable)
{
// Selectable
var tag = "#" + divId + "_AllNone";
$(tag).prop('checked', false);
// JSAV_unselectAll(divId);
}
}
// ---------------------------------------------------------------------
/**
Prints a pulldown menu to select a colour scheme
@param {string} divId - The ID of the div we are printing in
@param {object} options - User options
@author
- 17.06.14 Original By: ACRM
- 18.06.14 Added tooltip
- 02.09.14 Modifies the DOM rather than writing to document By: JHN
*/
function JSAV_printColourSelector(divId, options)
{
// Initialize colour choices if not provided
if(options.colourChoices == undefined)
{
options.colourChoices = JSAV_initColourChoices();
}
var id = divId + "_selectColour";
var html = "<select class='tooltip' title='Select colour scheme' id = '" + id + "' onchange='JSAV_setColourScheme(\"" + divId + "\", this)'>";
for(var i=0; i<options.colourChoices.length; i++)
{
var lcChoice = options.colourChoices[i].toLowerCase();
var selected = "";
if(options.colourScheme == lcChoice)
{
selected = " selected='selected'";
}
html += "<option value='" + lcChoice + "'" + selected + ">" +
options.colourChoices[i] + "</option>";
}
html += "</select>";
var parrenttag = '#' + divId + '_controls';
$(parrenttag).append(html);
}
// ---------------------------------------------------------------------
/**
Called when the colour scheme selector is changed. Sets the selected
colour scheme and refreshes the display
@param {object} select - The select pull-down
@param {string} divId - The ID of the div we are working in
@author
- 17.06.14 Original By: ACRM
*/
function JSAV_setColourScheme(divId, select)
{
gOptions[divId].colourScheme = select.value;
var options = gOptions[divId];
if(options.sorted)
{
JSAV_sortAndRefreshSequences(divId, options.sortable, options.selectable, options.border)
}
else
{
JSAV_refresh(divId, gSequences[divId], options.sortable, options.selectable, options.border, gStartPos[divId]-1, gStopPos[divId]-1, options.highlight, options.dotify, options.nocolour, options.consensus);
}
}
// ---------------------------------------------------------------------
/**
Prints the button to allow FASTA export
@param {string} divId - The ID of the div we are printing in
@author
- 17.06.14 Original By: ACRM
- 18.06.14 Added tooltip
- 02.09.14 Modifies the DOM rather than printing to the document By: JHN
*/
function JSAV_printFASTA(divId)
{
var parrenttag = '#' + divId + '_controls';
var label = gOptions[divId].fastaLabel;
var html = "<button type='button' class='tooltip exportbutton' title='Export the selected sequences, or all sequences if none selected' onclick='JSAV_exportFASTA(\"" + divId + "\")'>"+label+"</button>";
$(parrenttag).append(html);
}
// ---------------------------------------------------------------------
/**
Exports the selected sequences as FASTA
@param {string} divId - The ID of the div we are printing in
@author
- 17.06.14 Original By: ACRM
*/
function JSAV_exportFASTA(divId)
{
var sequenceText = JSAV_buildFASTA(divId);
ACRM_dialog("FASTA Export", sequenceText, 600, true);
}
// ---------------------------------------------------------------------
/**
Print a checkbox for toggling dotify mode
@param {string} divId The div that we are working in
@param {object} options The options
@author
- 16.06.14 Original By: ACRM
- 18.06.14 Added tooltip
- 02.09.14 Modifies the DOM rather than printing to the document By: JHN
- 23.09.15 Dotify label now comes from options By: ACRM
*/
function JSAV_printToggleDotify(divId, options)
{
var html = "";
var checked = "";
if(options.dotify) { checked = " checked='checked'"; };
var id = divId + "_toggleDotify";
var idText = " id='" + id + "'";
var onclick = " onclick='JSAV_toggleOption(\"" + divId + "\", \"" + id + "\", \"dotify\")'";
var tooltip = "Replace repeated residues with dots";
html += "<span><input type='checkbox'" + idText + checked + onclick + "/><label for='"+id+"' class='tooltip' title='"+tooltip+"'>" + options.toggleDotifyLabel + "</label></span>";
var parrenttag = '#' + divId + '_controls';
$(parrenttag).append(html);
}
// ---------------------------------------------------------------------
/**
Print a checkbox for toggling nocolour-dotify mode
@param {string} divId The div that we are working in
@param {object} options The options
@author
- 16.06.14 Original By: ACRM
- 18.06.14 Added tooltip
- 02.09.14 Modifies the DOM rather than printing to the document By: JHN
- 23.09.15 Obtains label from options.toggleNocolourLabel By: ACRM
*/
function JSAV_printToggleNocolour(divId, options)
{
var html = "";
var checked = "";
if(options.nocolour) { checked = " checked='checked'"; };
var id = divId + "_toggleNocolour";
var idText = " id='" + id + "'";
var onclick = " onclick='JSAV_toggleOption(\"" + divId + "\", \"" + id + "\", \"nocolour\")'";
var tooltip = "Do not colour repeated residues";
html += "<span><input type='checkbox'" + idText + checked + onclick + "/><label for='"+id+"' class='tooltip' title='"+tooltip+"'>" + options.toggleNocolourLabel + "</label></span>";
var parrenttag = '#' + divId + '_controls';
$(parrenttag).append(html);
}
// ---------------------------------------------------------------------
/**
Read a checkbox and toggle the associated option, refreshing the display
@param {string} divId The div that we are working in
@param {string} theButton The ID of the checkbox we are looking at
@param {string} theOption The name of the option we are toggling
@author
- 16.06.14 Original By: ACRM
- 17.06.14 Added consensus
*/
function JSAV_toggleOption(divId, theButton, theOption)
{
var tag = "#" + theButton;
var options = gOptions[divId];
options[theOption] = $(tag).prop('checked');
if(options.sorted)
{
JSAV_sortAndRefreshSequences(divId, options.sortable, options.selectable, options.border)
}
else
{
JSAV_refresh(divId, gSequences[divId], options.sortable, options.selectable, options.border, gStartPos[divId]-1, gStopPos[divId]-1, options.highlight, options.dotify, options.nocolour, options.consensus);
}
}
// ---------------------------------------------------------------------
/**
Builds HTML for table rows that highlight a region in the alignment as
being important (e.g. CDRs of antibodies). Note that ranges for highlighting
count from zero.
@param {string} divId - The div we are working in
@param {int} seqLen - The length of the alignment
@param {bool} selectable - Are there sequences selection boxes
@param {int[]} highlight - Array of residue ranges to highlight
@author
- 13.06.14 Original By: ACRM
*/
function JSAV_buildHighlightHTML(divId, seqLen, selectable, highlight)
{
var html = "";
if(selectable)
{
html += "<tr class='highlightrow'><th></th>";
html += "<td></td>";
}
else
{
html += "<tr class='highlightrow'><td></td>";
}
for(var i=0; i<seqLen; i++)
{
var displayClass = 'unhighlighted';
for(var j=0; j<highlight.length; j+=2)
{
var start = highlight[j];
var stop = highlight[j+1];
if((i >= start) && (i <= stop))
{
displayClass = 'highlighted';
break;
}
}
html += "<td class='" + displayClass + "' /></td>";
}
html += "</tr>\n";
return(html);
}
// ---------------------------------------------------------------------
/**
Prints the delete button
@param {string} divId - The ID of the div to print in
@param {string} label - The label to print in the delete button
@author
- 12.06.14 Original By: ACRM
- 18.06.14 Added tooltip
- 02.09.14 Modifies the DOM rather than printing to the document By: JHN
- 23.09.15 Added label parameter and use of this label By: ACRM
*/
function JSAV_printDelete(divId, label)
{
var parrenttag = '#' + divId + '_controls';
var html = "<button type='button' class='tooltip delete' title='Delete the selected sequences' onclick='JSAV_deleteSelectedSequences(\"" + divId + "\")'>" + label + "</button>";
$(parrenttag).append(html);
}
// ---------------------------------------------------------------------
/**
Prints the submit button
@param {string} divId - The ID of the div to print in
@param {string} url - URL to which we are submitting
@param {string} label - Label for submit button
@author
- 12.06.14 Original By: ACRM
- 18.06.14 Added tooltip
- 02.09.14 Modifies the DOM rather than printing to the document By: JHN
*/
function JSAV_printSubmit(divId, url, label)
{
var parrenttag = '#' + divId + '_controls';
var html = "<button type='button' class='tooltip submitbutton' title='Submit the selected sequences, or all sequences if none selected' onclick='JSAV_submitSequences(\"" + divId + "\")'>" + label + "</button>";
$(parrenttag).append(html);
// Build a hidden sequences text box in the form to contain
var formId = divId + "_form";
var html = "<div style='display:none'><form id='" + formId + "' action='" + url + "' method='post'>";
var textId = divId + "_submit";
html += "<textarea id='" + textId + "' name='sequences'></textarea>";
html += "</form></div>";
$(parrenttag).append(html);
}
// ---------------------------------------------------------------------
/**
Prints the action button
@param {string} divId - The ID of the div to print in
@param {string} action - Function to call
@param {string} label - Label for action button
@author
- 13.06.14 Original By: ACRM
- 18.06.14 Added tooltip
*/
function JSAV_printAction(divId, action, label)
{
var parrenttag = '#' + divId + '_controls';
var html = "<button type='button' class='tooltip actionbutton' title='Process the selected sequences, or all sequences if none selected' onclick='JSAV_wrapAction(\"" + divId + "\", \"" + action + "\")'>" + label + "</button>";
$(parrenttag).append(html);
}
// ---------------------------------------------------------------------
/**
Wrap the action function. When the action button is clicked, this function
is called to extract the relevant sequence data and call the user specified
function, passing the divId and an array of sequence objects
@param {string} divId - The ID of the div to work in
@param {string} action - The name of the user function
@author
- 13.06.14 Original By: ACRM
*/
function JSAV_wrapAction(divId, action)
{
var selectedSequences = Array();
// See if any checkboxes are set
var count = 0;
var toSubmit = Array();
// Find the selected sequences
var tag = "#" + divId + " .selectCell input";
$(tag).each(function(index) {
if($(this).prop('checked'))
{
var id = $(this).parent().parent().attr('id');
toSubmit[id] = 1;
count++;
}
});
var textTag = "textarea#" + divId + "_submit";
var sequenceText = "";
var sequences = gSequences[divId];
for(var i=0; i<sequences.length; i++)
{
if((count == 0) || (count == sequences.length) || (toSubmit[sequences[i].id] == 1))
{
selectedSequences.push({ id: sequences[i].id, sequence: sequences[i].sequence});
}
}
window[action](divId, selectedSequences);
}
// ---------------------------------------------------------------------
/**
Handles the user clicking the submit button. Build a FASTA version of the
sequences that have been selected, fills them into a textarea in a form
and then submits the form.
@param {string} divId - The ID of the div we are working in
@author
- 13.06.14 Original By: ACRM
- 17.06.14 Split out the JSAV_buildFASTA() section
*/
function JSAV_submitSequences(divId)
{
var sequenceText = JSAV_buildFASTA(divId);
var textTag = "textarea#" + divId + "_submit";
$(textTag).text(sequenceText);
var formTag = "#" + divId + "_form";
$(formTag).submit();
}
// ---------------------------------------------------------------------
/**
Builds a FASTA version of the sequences that are currently selected, or all
sequences if none are selected
@param {string} divId - The ID of the div we are printing in
@author
- 17.06.14 Split out from JSAV_submitSequences() By: ACRM
*/
function JSAV_buildFASTA(divId)
{
// See if any checkboxes are set
var count = 0;
var toFASTA = Array();
// Find the selected sequences
var tag = "#" + divId + " .selectCell input";
$(tag).each(function(index) {
if($(this).prop('checked'))
{
var id = $(this).parent().parent().attr('id');
toFASTA[id] = 1;
count++;
}
});
var sequenceText = "";
var sequences = gSequences[divId];
for(var i=0; i<sequences.length; i++)
{
if((count == 0) || (count == sequences.length) || (toFASTA[sequences[i].id] == 1))
{
sequenceText += ">" + sequences[i].id + "\n";
sequenceText += sequences[i].sequence + "\n";
}
}
return(sequenceText);
}
// ---------------------------------------------------------------------
/**
Deletes a set of sequences that have been clicked
@param {string} divId - The ID of the div to work in
@author
- 12.06.14 Original By: ACRM
- 15.06.14 Changed from alert() to ACRM_alert()
- 16.06.14 Changed from confirm() to ACRM_confirm()
- 17.06.14 Added consensus
*/
function JSAV_deleteSelectedSequences(divId)
{
var count = 0;
var toDelete = Array();
// Find the selected sequences
var tag = "#" + divId + " .selectCell input";
$(tag).each(function(index) {
if($(this).prop('checked'))
{
var id = $(this).parent().parent().attr('id');
toDelete.push(id);
count++;
}
});
if(count == 0)
{
ACRM_alert("Error!","You must select some sequences!");
}
else if(count == gSequences[divId].length)
{
ACRM_alert("Error!","You can't delete all the sequences!");
}
else
{
var message = "Delete " + count + " selected sequences?";
ACRM_confirm("Confirm", message, function(confirm){
if(confirm)
{
// Run through the global sequence array deleting the selected objects
for(var i=0; i<toDelete.length; i++)
{
ACRM_deleteItemByLabel('id', toDelete[i], gSequences[divId]);
}
var options = gOptions[divId];
// Update the consensus
if(options.consensus)
{
gConsensus[divId] = JSAV_buildConsensus(gSequences[divId]);
}
// Refresh the display
JSAV_refresh(divId, gSequences[divId], options.sortable,
options.selectable, options.border,
gStartPos[divId]-1, gStopPos[divId]-1, options.highlight,
options.dotify, options.nocolour, options.consensus);
options.sorted = false;
}
});
}
}
// ---------------------------------------------------------------------
/**
Toggle selection of all sequence selection buttons
@param {string} divId - The ID of the div to work in
@author
- 09.06.14 Original By: ACRM
*/
function JSAV_selectAllOrNone(divId)
{
var tag = "#" + divId + "_AllNone";
if($(tag).prop('checked'))
{
JSAV_selectAll(divId);
}
else
{
JSAV_unselectAll(divId);
}
}
// ---------------------------------------------------------------------
/**
Select all sequence selection buttons
@param {string} divId - The ID of the div to work in
@author
- 09.06.14 Original By: ACRM
*/
function JSAV_selectAll(divId)
{
var tag = "#" + divId + " .selectCell input";
$(tag).prop('checked', true);
}
// ---------------------------------------------------------------------
/**
Unselect all sequence selection buttons
@param {string} divId - The ID of the div to work in
@author
- 09.06.14 Original By: ACRM
*/
function JSAV_unselectAll(divId)
{
var tag = "#" + divId + " .selectCell input";
$(tag).prop('checked', false);
}
// ---------------------------------------------------------------------
/**
Change the <td> elements to have a white border
@param {string} divId - The ID of the div to work in
@author
- 12.06.14 Original By: ACRM
*/
function JSAV_modifyCSS(divId)
{
var selector = "#" + divId + " .JSAV td";
$(selector).css("border", "1px solid white");
}
// ---------------------------------------------------------------------
/**
Builds the HTML for printing a sequence as a table row. The row
starts with the identifier and is followed by each amino acid in a
separate <td> tag with a class to indicate the amino acid type
(e.g. taylorW for a tryptophan in Willie Taylor scheme).
@param {string} id The identifier
@param {string} sequence A string containing the sequence
@param {string} prevSequence A string containing the previous sequence
@param {bool} selectable Display a selection checkbox
@param {bool} dotify Dotify the sequence
@param {bool} nocolour Don't colour dotified residues
@param {bool} isConsensus This is the consensus sequence
@param {string} idSubmit URL to visit when sequence label clicked
@param {bool} idSubmitClean Remove non-alpha characters from sequence
before submitting it
@returns {string} text HTML snippet
@author
- 30.05.14 Original By: ACRM
- 16.06.14 Added dotify and nocolour - now takes prevSequence parameter
- 17.06.14 Added isConsensus and colourScheme
- 18.06.14 Added tooltip
- 23.09.15 Added idSubmit/idSubmitClean
*/
function JSAV_buildASequenceHTML(id, sequence, prevSequence, selectable, dotify, nocolour,
isConsensus, colourScheme, idSubmit, idSubmitClean)
{
var seqArray = sequence.split("");
var prevSeqArray = undefined;
if(dotify && (prevSequence != undefined))
{
prevSeqArray = prevSequence.split("");
}
var tableLine = "";
if(isConsensus)
{
tableLine = "<tr class='tooltip consensusCell' title='The consensus shows the most frequent amino acid. This is lower case if ≤50% of the sequences have that residue.' id='" + id + "'>";
}
else
{
tableLine = "<tr id='" + id + "'>";
}
if(idSubmit == null)
{
tableLine += "<th class='idCell'>" + id + "</th>";
}
else
{
var url = idSubmit;
var submitSeq;
if(idSubmitClean)
{
submitSeq = sequence.replace(/[^A-Za-z]/g, '');
}
url += submitSeq;
tableLine += "<th class='idCell'><a href='" + url + "'>" + id + "</a></th>";
}
if(selectable)
{
if(isConsensus)
{
tableLine += "<th class='consensusSelectCell'></th>";
}
else
{
var name = "select_" + id;
tableLine += "<th class='selectCell'><input type='checkbox' name='" + name + "' /></th>";
}
}
var consensusClass = "";
if(isConsensus)
{
consensusClass = " consensusCell";
}
var nResidues = seqArray.length;
if(dotify && !isConsensus)
{
for(var i=0; i<nResidues; i++)
{
var aa = seqArray[i];
var prevAa = '#';
var colourClass = colourScheme + aa.toUpperCase();
if(nocolour)
{
if(aa == "-")