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