Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / jalview / appletgui / SequenceRenderer.js
1 Clazz.declarePackage ("jalview.appletgui");
2 Clazz.load (["jalview.api.SequenceRenderer"], "jalview.appletgui.SequenceRenderer", ["jalview.util.Comparison", "java.awt.Color", "$.Font"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.av = null;
5 this.fm = null;
6 this.renderGaps = true;
7 this.currentSequenceGroup = null;
8 this.allGroups = null;
9 this.resBoxColour = null;
10 this.graphics = null;
11 this.forOverview = false;
12 Clazz.instantialize (this, arguments);
13 }, jalview.appletgui, "SequenceRenderer", null, jalview.api.SequenceRenderer);
14 Clazz.makeConstructor (c$, 
15 function (av) {
16 this.av = av;
17 }, "jalview.appletgui.AlignViewport");
18 Clazz.defineMethod (c$, "prepare", 
19 function (g, renderGaps) {
20 this.graphics = g;
21 this.fm = g.getFontMetrics ();
22 this.renderGaps = renderGaps;
23 }, "java.awt.Graphics,~B");
24 Clazz.overrideMethod (c$, "getResidueBoxColour", 
25 function (seq, i) {
26 this.allGroups = this.av.getAlignment ().findAllGroups (seq);
27 if (this.inCurrentSequenceGroup (i)) {
28 if (this.currentSequenceGroup.getDisplayBoxes ()) {
29 this.getBoxColour (this.currentSequenceGroup.cs, seq, i);
30 }} else if (this.av.getShowBoxes ()) {
31 this.getBoxColour (this.av.getGlobalColourScheme (), seq, i);
32 }return this.resBoxColour;
33 }, "jalview.datamodel.SequenceI,~N");
34 Clazz.overrideMethod (c$, "getResidueColour", 
35 function (seq, position, fr) {
36 var col = this.getResidueBoxColour (seq, position);
37 if (fr != null) {
38 col = fr.findFeatureColour (col, seq, position);
39 }return col;
40 }, "jalview.datamodel.SequenceI,~N,jalview.api.FeatureRenderer");
41 Clazz.defineMethod (c$, "getBoxColour", 
42 function (cs, seq, i) {
43 if (cs != null) {
44 this.resBoxColour = cs.findColour (seq.getCharAt (i), i, seq);
45 } else if (this.forOverview && !jalview.util.Comparison.isGap (seq.getCharAt (i))) {
46 this.resBoxColour = java.awt.Color.lightGray;
47 } else {
48 this.resBoxColour = java.awt.Color.white;
49 }}, "jalview.schemes.ColourSchemeI,jalview.datamodel.SequenceI,~N");
50 Clazz.defineMethod (c$, "findSequenceColour", 
51 function (seq, i) {
52 this.allGroups = this.av.getAlignment ().findAllGroups (seq);
53 this.drawBoxes (seq, i, i, 0);
54 return this.resBoxColour;
55 }, "jalview.datamodel.SequenceI,~N");
56 Clazz.defineMethod (c$, "drawSequence", 
57 function (seq, sg, start, end, y1) {
58 if (seq == null) {
59 return;
60 }this.allGroups = sg;
61 this.drawBoxes (seq, start, end, y1);
62 if (this.av.validCharWidth) {
63 this.drawText (seq, start, end, y1);
64 }}, "jalview.datamodel.SequenceI,~A,~N,~N,~N");
65 Clazz.defineMethod (c$, "drawBoxes", 
66 function (seq, start, end, y1) {
67 var i = start;
68 var length = seq.getLength ();
69 var curStart = -1;
70 var curWidth = this.av.getCharWidth ();
71 var avCharWidth = this.av.getCharWidth ();
72 var avCharHeight = this.av.getCharHeight ();
73 var tempColour = null;
74 while (i <= end) {
75 this.resBoxColour = java.awt.Color.white;
76 if (i < length) {
77 if (this.inCurrentSequenceGroup (i)) {
78 if (this.currentSequenceGroup.getDisplayBoxes ()) {
79 this.getBoxColour (this.currentSequenceGroup.cs, seq, i);
80 }} else if (this.av.getShowBoxes ()) {
81 this.getBoxColour (this.av.getGlobalColourScheme (), seq, i);
82 }}if (this.resBoxColour !== tempColour) {
83 if (tempColour != null) {
84 this.graphics.fillRect (avCharWidth * (curStart - start), y1, curWidth, avCharHeight);
85 }this.graphics.setColor (this.resBoxColour);
86 curStart = i;
87 curWidth = avCharWidth;
88 tempColour = this.resBoxColour;
89 } else {
90 curWidth += avCharWidth;
91 }i++;
92 }
93 this.graphics.fillRect (avCharWidth * (curStart - start), y1, curWidth, avCharHeight);
94 }, "jalview.datamodel.SequenceI,~N,~N,~N");
95 Clazz.defineMethod (c$, "drawText", 
96 function (seq, start, end, y1) {
97 var avCharWidth = this.av.getCharWidth ();
98 var avCharHeight = this.av.getCharHeight ();
99 var boldFont = null;
100 var bold = false;
101 if (this.av.isUpperCasebold ()) {
102 boldFont =  new java.awt.Font (this.av.getFont ().getName (), 1, avCharHeight);
103 this.graphics.setFont (this.av.getFont ());
104 }y1 += avCharHeight - Clazz.doubleToInt (avCharHeight / 5);
105 var charOffset = 0;
106 if (end + 1 >= seq.getLength ()) {
107 end = seq.getLength () - 1;
108 }var s = ' ';
109 var srep = this.av.isDisplayReferenceSeq ();
110 for (var i = start; i <= end; i++) {
111 this.graphics.setColor (java.awt.Color.black);
112 s = seq.getCharAt (i);
113 if (!this.renderGaps && jalview.util.Comparison.isGap (s)) {
114 continue;
115 }if (this.inCurrentSequenceGroup (i)) {
116 if (!this.currentSequenceGroup.getDisplayText ()) {
117 continue;
118 }if (this.currentSequenceGroup.getColourText ()) {
119 this.getBoxColour (this.currentSequenceGroup.cs, seq, i);
120 this.graphics.setColor (this.resBoxColour.darker ());
121 }if (this.currentSequenceGroup.getShowNonconserved ()) {
122 s = this.getDisplayChar (srep, i, s, '.');
123 }} else {
124 if (!this.av.getShowText ()) {
125 continue;
126 }if (this.av.getColourText ()) {
127 this.getBoxColour (this.av.getGlobalColourScheme (), seq, i);
128 if (this.av.getShowBoxes ()) {
129 this.graphics.setColor (this.resBoxColour.darker ());
130 } else {
131 this.graphics.setColor (this.resBoxColour);
132 }}if (this.av.getShowUnconserved ()) {
133 s = this.getDisplayChar (srep, i, s, '.');
134 }}if (this.av.isUpperCasebold ()) {
135 this.fm = this.graphics.getFontMetrics ();
136 if ('A' <= s && s <= 'Z') {
137 if (!bold) {
138 this.graphics.setFont (boldFont);
139 }bold = true;
140 } else if (bold) {
141 this.graphics.setFont (this.av.font);
142 bold = false;
143 }}charOffset = Clazz.doubleToInt ((avCharWidth - this.fm.charWidth (s)) / 2);
144 this.graphics.drawString (String.valueOf (s), charOffset + avCharWidth * (i - start), y1);
145 }
146 }, "jalview.datamodel.SequenceI,~N,~N,~N");
147 Clazz.defineMethod (c$, "getDisplayChar", 
148  function (usesrep, position, s, c) {
149 var conschar = (usesrep) ? this.av.getAlignment ().getSeqrep ().getCharAt (position) : this.av.getAlignmentConsensusAnnotation ().annotations[position].displayCharacter.charAt (0);
150 if (conschar != '-' && s == conschar) {
151 s = c;
152 }return s;
153 }, "~B,~N,~S,~S");
154 Clazz.defineMethod (c$, "inCurrentSequenceGroup", 
155 function (res) {
156 if (this.allGroups == null) {
157 return false;
158 }for (var i = 0; i < this.allGroups.length; i++) {
159 if (this.allGroups[i].getStartRes () <= res && this.allGroups[i].getEndRes () >= res) {
160 this.currentSequenceGroup = this.allGroups[i];
161 return true;
162 }}
163 return false;
164 }, "~N");
165 Clazz.defineMethod (c$, "drawHighlightedText", 
166 function (seq, start, end, x1, y1) {
167 var avCharWidth = this.av.getCharWidth ();
168 var avCharHeight = this.av.getCharHeight ();
169 var pady = Clazz.doubleToInt (avCharHeight / 5);
170 var charOffset = 0;
171 this.graphics.setColor (java.awt.Color.black);
172 this.graphics.fillRect (x1, y1, avCharWidth * (end - start + 1), avCharHeight);
173 this.graphics.setColor (java.awt.Color.white);
174 var s = '~';
175 if (this.av.validCharWidth) {
176 for (var i = start; i <= end; i++) {
177 if (i < seq.getLength ()) {
178 s = seq.getCharAt (i);
179 }charOffset = Clazz.doubleToInt ((avCharWidth - this.fm.charWidth (s)) / 2);
180 this.graphics.drawString (String.valueOf (s), charOffset + x1 + avCharWidth * (i - start), y1 + avCharHeight - pady);
181 }
182 }}, "jalview.datamodel.SequenceI,~N,~N,~N,~N");
183 Clazz.defineMethod (c$, "drawCursor", 
184 function (seq, res, x1, y1) {
185 var pady = Clazz.doubleToInt (this.av.getCharHeight () / 5);
186 var charOffset = 0;
187 this.graphics.setColor (java.awt.Color.black);
188 this.graphics.fillRect (x1, y1, this.av.getCharWidth (), this.av.getCharHeight ());
189 this.graphics.setColor (java.awt.Color.white);
190 this.graphics.setColor (java.awt.Color.white);
191 var s = seq.getCharAt (res);
192 if (this.av.validCharWidth) {
193 charOffset = Clazz.doubleToInt ((this.av.getCharWidth () - this.fm.charWidth (s)) / 2);
194 this.graphics.drawString (String.valueOf (s), charOffset + x1, (y1 + this.av.getCharHeight ()) - pady);
195 }}, "jalview.datamodel.SequenceI,~N,~N,~N");
196 });