JAL-1807 still testing
[jalviewjs.git] / unused / appletgui / SequenceRenderer.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)\r
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors\r
4  * \r
5  * This file is part of Jalview.\r
6  * \r
7  * Jalview is free software: you can redistribute it and/or\r
8  * modify it under the terms of the GNU General Public License \r
9  * as published by the Free Software Foundation, either version 3\r
10  * of the License, or (at your option) any later version.\r
11  *  \r
12  * Jalview is distributed in the hope that it will be useful, but \r
13  * WITHOUT ANY WARRANTY; without even the implied warranty \r
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
15  * PURPOSE.  See the GNU General Public License for more details.\r
16  * \r
17  * You should have received a copy of the GNU General Public License\r
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.\r
19  * The Jalview Authors are detailed in the 'AUTHORS' file.\r
20  */\r
21 package jalview.appletgui;\r
22 \r
23 import jalview.api.FeatureRenderer;\r
24 import jalview.datamodel.SequenceGroup;\r
25 import jalview.datamodel.SequenceI;\r
26 import jalview.schemes.ColourSchemeI;\r
27 import jalview.util.Comparison;\r
28 \r
29 import java.awt.Color;\r
30 import java.awt.Font;\r
31 import java.awt.FontMetrics;\r
32 import java.awt.Graphics;\r
33 \r
34 public class SequenceRenderer implements jalview.api.SequenceRenderer\r
35 {\r
36   AlignViewport av;\r
37 \r
38   FontMetrics fm;\r
39 \r
40   boolean renderGaps = true;\r
41 \r
42   SequenceGroup currentSequenceGroup = null;\r
43 \r
44   SequenceGroup[] allGroups = null;\r
45 \r
46   Color resBoxColour;\r
47 \r
48   Graphics graphics;\r
49 \r
50   boolean forOverview = false;\r
51 \r
52   public SequenceRenderer(AlignViewport av)\r
53   {\r
54     this.av = av;\r
55   }\r
56 \r
57   /**\r
58    * DOCUMENT ME!\r
59    * \r
60    * @param b\r
61    *          DOCUMENT ME!\r
62    */\r
63   public void prepare(Graphics g, boolean renderGaps)\r
64   {\r
65     graphics = g;\r
66     fm = g.getFontMetrics();\r
67 \r
68     this.renderGaps = renderGaps;\r
69   }\r
70 \r
71   public Color getResidueBoxColour(SequenceI seq, int i)\r
72   {\r
73     allGroups = av.getAlignment().findAllGroups(seq);\r
74 \r
75     if (inCurrentSequenceGroup(i))\r
76     {\r
77       if (currentSequenceGroup.getDisplayBoxes())\r
78       {\r
79         getBoxColour(currentSequenceGroup.cs, seq, i);\r
80       }\r
81     }\r
82     else if (av.getShowBoxes())\r
83     {\r
84       getBoxColour(av.getGlobalColourScheme(), seq, i);\r
85     }\r
86 \r
87     return resBoxColour;\r
88   }\r
89 \r
90   /**\r
91    * Get the residue colour at the given sequence position - as determined by\r
92    * the sequence group colour (if any), else the colour scheme, possibly\r
93    * overridden by a feature colour.\r
94    * \r
95    * @param seq\r
96    * @param position\r
97    * @param fr\r
98    * @return\r
99    */\r
100   @Override\r
101   public Color getResidueColour(final SequenceI seq, int position,\r
102           FeatureRenderer fr)\r
103   {\r
104     // TODO replace 8 or so code duplications with calls to this method\r
105     // (refactored as needed)\r
106     Color col = getResidueBoxColour(seq, position);\r
107 \r
108     if (fr != null)\r
109     {\r
110       col = fr.findFeatureColour(col, seq, position);\r
111     }\r
112     return col;\r
113   }\r
114 \r
115   void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
116   {\r
117     if (cs != null)\r
118     {\r
119       resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);\r
120     }\r
121     else if (forOverview && !Comparison.isGap(seq.getCharAt(i)))\r
122     {\r
123       resBoxColour = Color.lightGray;\r
124     }\r
125     else\r
126     {\r
127       resBoxColour = Color.white;\r
128     }\r
129 \r
130   }\r
131 \r
132   public Color findSequenceColour(SequenceI seq, int i)\r
133   {\r
134     allGroups = av.getAlignment().findAllGroups(seq);\r
135     drawBoxes(seq, i, i, 0);\r
136     return resBoxColour;\r
137   }\r
138 \r
139   public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,\r
140           int end, int y1)\r
141   {\r
142     if (seq == null)\r
143     {\r
144       return;\r
145     }\r
146 \r
147     allGroups = sg;\r
148 \r
149     drawBoxes(seq, start, end, y1);\r
150 \r
151     if (av.validCharWidth)\r
152     {\r
153       drawText(seq, start, end, y1);\r
154     }\r
155   }\r
156 \r
157   public void drawBoxes(SequenceI seq, int start, int end, int y1)\r
158   {\r
159     int i = start;\r
160     int length = seq.getLength();\r
161 \r
162     int curStart = -1;\r
163     int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(), avCharHeight = av\r
164             .getCharHeight();\r
165 \r
166     Color tempColour = null;\r
167     while (i <= end)\r
168     {\r
169       resBoxColour = Color.white;\r
170       if (i < length)\r
171       {\r
172         if (inCurrentSequenceGroup(i))\r
173         {\r
174           if (currentSequenceGroup.getDisplayBoxes())\r
175           {\r
176             getBoxColour(currentSequenceGroup.cs, seq, i);\r
177           }\r
178         }\r
179         else if (av.getShowBoxes())\r
180         {\r
181           getBoxColour(av.getGlobalColourScheme(), seq, i);\r
182         }\r
183       }\r
184 \r
185       if (resBoxColour != tempColour)\r
186       {\r
187         if (tempColour != null)\r
188         {\r
189           graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,\r
190                   avCharHeight);\r
191         }\r
192         graphics.setColor(resBoxColour);\r
193 \r
194         curStart = i;\r
195         curWidth = avCharWidth;\r
196         tempColour = resBoxColour;\r
197 \r
198       }\r
199       else\r
200       {\r
201         curWidth += avCharWidth;\r
202       }\r
203 \r
204       i++;\r
205     }\r
206 \r
207     graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,\r
208             avCharHeight);\r
209   }\r
210 \r
211   public void drawText(SequenceI seq, int start, int end, int y1)\r
212   {\r
213     int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();\r
214     Font boldFont = null;\r
215     boolean bold = false;\r
216     if (av.isUpperCasebold())\r
217     {\r
218       boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);\r
219 \r
220       graphics.setFont(av.getFont());\r
221     }\r
222 \r
223     y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady\r
224 \r
225     int charOffset = 0;\r
226 \r
227     // Need to find the sequence position here.\r
228     if (end + 1 >= seq.getLength())\r
229     {\r
230       end = seq.getLength() - 1;\r
231     }\r
232 \r
233     char s = ' ';\r
234     boolean srep = av.isDisplayReferenceSeq();\r
235     for (int i = start; i <= end; i++)\r
236     {\r
237       graphics.setColor(Color.black);\r
238 \r
239       s = seq.getCharAt(i);\r
240       if (!renderGaps && Comparison.isGap(s))\r
241       {\r
242         continue;\r
243       }\r
244 \r
245       if (inCurrentSequenceGroup(i))\r
246       {\r
247         if (!currentSequenceGroup.getDisplayText())\r
248         {\r
249           continue;\r
250         }\r
251 \r
252         if (currentSequenceGroup.getColourText())\r
253         {\r
254           getBoxColour(currentSequenceGroup.cs, seq, i);\r
255           graphics.setColor(resBoxColour.darker());\r
256         }\r
257         if (currentSequenceGroup.getShowNonconserved())\r
258         {\r
259           s = getDisplayChar(srep, i, s,\r
260                   '.');\r
261         }\r
262       }\r
263       else\r
264       {\r
265         if (!av.getShowText())\r
266         {\r
267           continue;\r
268         }\r
269 \r
270         if (av.getColourText())\r
271         {\r
272           getBoxColour(av.getGlobalColourScheme(), seq, i);\r
273           if (av.getShowBoxes())\r
274           {\r
275             graphics.setColor(resBoxColour.darker());\r
276           }\r
277           else\r
278           {\r
279             graphics.setColor(resBoxColour);\r
280           }\r
281         }\r
282         if (av.getShowUnconserved())\r
283         {\r
284           s = getDisplayChar(srep, i, s,\r
285                   '.');\r
286 \r
287         }\r
288       }\r
289 \r
290       if (av.isUpperCasebold())\r
291       {\r
292         fm = graphics.getFontMetrics();\r
293         if ('A' <= s && s <= 'Z')\r
294         {\r
295           if (!bold)\r
296           {\r
297 \r
298             graphics.setFont(boldFont);\r
299           }\r
300           bold = true;\r
301         }\r
302         else if (bold)\r
303         {\r
304           graphics.setFont(av.font);\r
305           bold = false;\r
306         }\r
307 \r
308       }\r
309 \r
310       charOffset = (avCharWidth - fm.charWidth(s)) / 2;\r
311       graphics.drawString(String.valueOf(s), charOffset + avCharWidth\r
312               * (i - start), y1);\r
313     }\r
314 \r
315   }\r
316 \r
317   private char getDisplayChar(final boolean usesrep, int position,\r
318           char s, char c)\r
319   {\r
320     // TODO - use currentSequenceGroup rather than alignemnt \r
321     // currentSequenceGroup.getConsensus()\r
322     char conschar = (usesrep) ? av.getAlignment().getSeqrep().getCharAt(position) : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter\r
323             .charAt(0);\r
324     if (conschar != '-' && s == conschar)\r
325     {\r
326       s = c;\r
327     }\r
328     return s;\r
329   }\r
330 \r
331   boolean inCurrentSequenceGroup(int res)\r
332   {\r
333     if (allGroups == null)\r
334     {\r
335       return false;\r
336     }\r
337 \r
338     for (int i = 0; i < allGroups.length; i++)\r
339     {\r
340       if (allGroups[i].getStartRes() <= res\r
341               && allGroups[i].getEndRes() >= res)\r
342       {\r
343         currentSequenceGroup = allGroups[i];\r
344         return true;\r
345       }\r
346     }\r
347 \r
348     return false;\r
349   }\r
350 \r
351   public void drawHighlightedText(SequenceI seq, int start, int end,\r
352           int x1, int y1)\r
353   {\r
354     int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();\r
355     int pady = avCharHeight / 5;\r
356     int charOffset = 0;\r
357     graphics.setColor(Color.black);\r
358     graphics.fillRect(x1, y1, avCharWidth * (end - start + 1), avCharHeight);\r
359     graphics.setColor(Color.white);\r
360 \r
361     char s = '~';\r
362     // Need to find the sequence position here.\r
363     if (av.validCharWidth)\r
364     {\r
365       for (int i = start; i <= end; i++)\r
366       {\r
367         if (i < seq.getLength())\r
368         {\r
369           s = seq.getCharAt(i);\r
370         }\r
371 \r
372         charOffset = (avCharWidth - fm.charWidth(s)) / 2;\r
373         graphics.drawString(String.valueOf(s), charOffset + x1\r
374                 + avCharWidth * (i - start), y1 + avCharHeight - pady);\r
375       }\r
376     }\r
377   }\r
378 \r
379   public void drawCursor(SequenceI seq, int res, int x1, int y1)\r
380   {\r
381     int pady = av.getCharHeight() / 5;\r
382     int charOffset = 0;\r
383     graphics.setColor(Color.black);\r
384     graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());\r
385     graphics.setColor(Color.white);\r
386 \r
387     graphics.setColor(Color.white);\r
388 \r
389     char s = seq.getCharAt(res);\r
390     if (av.validCharWidth)\r
391     {\r
392 \r
393       charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;\r
394       graphics.drawString(String.valueOf(s), charOffset + x1,\r
395               (y1 + av.getCharHeight()) - pady);\r
396     }\r
397   }\r
398 \r
399 }\r