JAL-2588 (and JAL-2610, JAL-2603) mid refactor
[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   public SequenceRenderer(AlignViewport av)
52   {
53     this.av = av;
54   }
55
56   /**
57    * DOCUMENT ME!
58    * 
59    * @param b
60    *          DOCUMENT ME!
61    */
62   public void prepare(Graphics g, boolean renderGaps)
63   {
64     graphics = g;
65     fm = g.getFontMetrics();
66
67     this.renderGaps = renderGaps;
68   }
69
70   protected Color getResidueBoxColour(SequenceI seq, int i)
71   {
72     allGroups = av.getAlignment().findAllGroups(seq);
73
74     if (inCurrentSequenceGroup(i))
75     {
76       if (currentSequenceGroup.getDisplayBoxes())
77       {
78         getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
79       }
80     }
81     else if (av.getShowBoxes())
82     {
83       getBoxColour(av.getResidueShading(), seq, i);
84     }
85
86     return resBoxColour;
87   }
88
89   /**
90    * Get the residue colour at the given sequence position - as determined by
91    * the sequence group colour (if any), else the colour scheme, possibly
92    * overridden by a feature colour.
93    * 
94    * @param seq
95    * @param position
96    * @param finder
97    * @return
98    */
99   @Override
100   public Color getResidueColour(final SequenceI seq, int position,
101           FeatureColourFinder finder)
102   {
103     // TODO replace 8 or so code duplications with calls to this method
104     // (refactored as needed)
105     Color col = getResidueBoxColour(seq, position);
106
107     if (finder != null)
108     {
109       col = finder.findFeatureColour(col, seq, position);
110     }
111     return col;
112   }
113
114   void getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
115   {
116     if (shader.getColourScheme() != null)
117     {
118       resBoxColour = shader.findColour(seq.getCharAt(i), i, seq);
119     }
120     else
121     {
122       resBoxColour = Color.white;
123     }
124
125   }
126
127   public Color findSequenceColour(SequenceI seq, int i)
128   {
129     allGroups = av.getAlignment().findAllGroups(seq);
130     drawBoxes(seq, i, i, 0);
131     return resBoxColour;
132   }
133
134   public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
135           int end, int y1)
136   {
137     if (seq == null)
138     {
139       return;
140     }
141
142     allGroups = sg;
143
144     drawBoxes(seq, start, end, y1);
145
146     if (av.validCharWidth)
147     {
148       drawText(seq, start, end, y1);
149     }
150   }
151
152   public void drawBoxes(SequenceI seq, int start, int end, int y1)
153   {
154     int i = start;
155     int length = seq.getLength();
156
157     int curStart = -1;
158     int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(), avCharHeight = av
159             .getCharHeight();
160
161     Color tempColour = null;
162     while (i <= end)
163     {
164       resBoxColour = Color.white;
165       if (i < length)
166       {
167         if (inCurrentSequenceGroup(i))
168         {
169           if (currentSequenceGroup.getDisplayBoxes())
170           {
171             getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
172                     i);
173           }
174         }
175         else if (av.getShowBoxes())
176         {
177           getBoxColour(av.getResidueShading(), seq, i);
178         }
179       }
180
181       if (resBoxColour != tempColour)
182       {
183         if (tempColour != null)
184         {
185           graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
186                   avCharHeight);
187         }
188         graphics.setColor(resBoxColour);
189
190         curStart = i;
191         curWidth = avCharWidth;
192         tempColour = resBoxColour;
193
194       }
195       else
196       {
197         curWidth += avCharWidth;
198       }
199
200       i++;
201     }
202
203     graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
204             avCharHeight);
205   }
206
207   public void drawText(SequenceI seq, int start, int end, int y1)
208   {
209     int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
210     Font boldFont = null;
211     boolean bold = false;
212     if (av.isUpperCasebold())
213     {
214       boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);
215
216       graphics.setFont(av.getFont());
217     }
218
219     y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady
220
221     int charOffset = 0;
222
223     // Need to find the sequence position here.
224     if (end + 1 >= seq.getLength())
225     {
226       end = seq.getLength() - 1;
227     }
228
229     char s = ' ';
230     boolean srep = av.isDisplayReferenceSeq();
231     for (int i = start; i <= end; i++)
232     {
233       graphics.setColor(Color.black);
234
235       s = seq.getCharAt(i);
236       if (!renderGaps && jalview.util.Comparison.isGap(s))
237       {
238         continue;
239       }
240
241       if (inCurrentSequenceGroup(i))
242       {
243         if (!currentSequenceGroup.getDisplayText())
244         {
245           continue;
246         }
247
248         if (currentSequenceGroup.getColourText())
249         {
250           getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
251           graphics.setColor(resBoxColour.darker());
252         }
253         if (currentSequenceGroup.getShowNonconserved())
254         {
255           s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
256         }
257       }
258       else
259       {
260         if (!av.getShowText())
261         {
262           continue;
263         }
264
265         if (av.getColourText())
266         {
267           getBoxColour(av.getResidueShading(), seq, i);
268           if (av.getShowBoxes())
269           {
270             graphics.setColor(resBoxColour.darker());
271           }
272           else
273           {
274             graphics.setColor(resBoxColour);
275           }
276         }
277         if (av.getShowUnconserved())
278         {
279           s = getDisplayChar(srep, i, s, '.', null);
280
281         }
282       }
283
284       if (av.isUpperCasebold())
285       {
286         fm = graphics.getFontMetrics();
287         if ('A' <= s && s <= 'Z')
288         {
289           if (!bold)
290           {
291
292             graphics.setFont(boldFont);
293           }
294           bold = true;
295         }
296         else if (bold)
297         {
298           graphics.setFont(av.font);
299           bold = false;
300         }
301
302       }
303
304       charOffset = (avCharWidth - fm.charWidth(s)) / 2;
305       graphics.drawString(String.valueOf(s), charOffset + avCharWidth
306               * (i - start), y1);
307     }
308
309   }
310
311   /**
312    * Returns 'conservedChar' to represent the given position if the sequence
313    * character at that position is equal to the consensus (ignoring case), else
314    * returns the sequence character
315    * 
316    * @param usesrep
317    * @param position
318    * @param sequenceChar
319    * @param conservedChar
320    * @return
321    */
322   private char getDisplayChar(final boolean usesrep, int position,
323           char sequenceChar, char conservedChar, SequenceGroup currentGroup)
324   {
325     // TODO - use currentSequenceGroup rather than alignment
326     // currentSequenceGroup.getConsensus()
327     char conschar = (usesrep) ? (currentGroup == null
328             || position < currentGroup.getStartRes()
329             || position > currentGroup.getEndRes() ? av.getAlignment()
330             .getSeqrep().getCharAt(position)
331             : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
332                     .getCharAt(position) : av.getAlignment().getSeqrep()
333                     .getCharAt(position)))
334             : (currentGroup != null && currentGroup.getConsensus() != null
335                     && position >= currentGroup.getStartRes()
336                     && position <= currentGroup.getEndRes() && currentGroup
337                     .getConsensus().annotations.length > position) ? currentGroup
338                     .getConsensus().annotations[position].displayCharacter
339                     .charAt(0)
340                     : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
341                             .charAt(0);
342     if (!jalview.util.Comparison.isGap(conschar)
343             && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
344     {
345       sequenceChar = conservedChar;
346     }
347     return sequenceChar;
348   }
349
350   boolean inCurrentSequenceGroup(int res)
351   {
352     if (allGroups == null)
353     {
354       return false;
355     }
356
357     for (int i = 0; i < allGroups.length; i++)
358     {
359       if (allGroups[i].getStartRes() <= res
360               && allGroups[i].getEndRes() >= res)
361       {
362         currentSequenceGroup = allGroups[i];
363         return true;
364       }
365     }
366
367     return false;
368   }
369
370   public void drawHighlightedText(SequenceI seq, int start, int end,
371           int x1, int y1)
372   {
373     int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
374     int pady = avCharHeight / 5;
375     int charOffset = 0;
376     graphics.setColor(Color.black);
377     graphics.fillRect(x1, y1, avCharWidth * (end - start + 1), avCharHeight);
378     graphics.setColor(Color.white);
379
380     char s = '~';
381     // Need to find the sequence position here.
382     if (av.validCharWidth)
383     {
384       for (int i = start; i <= end; i++)
385       {
386         if (i < seq.getLength())
387         {
388           s = seq.getCharAt(i);
389         }
390
391         charOffset = (avCharWidth - fm.charWidth(s)) / 2;
392         graphics.drawString(String.valueOf(s), charOffset + x1
393                 + avCharWidth * (i - start), y1 + avCharHeight - pady);
394       }
395     }
396   }
397
398   public void drawCursor(SequenceI seq, int res, int x1, int y1)
399   {
400     int pady = av.getCharHeight() / 5;
401     int charOffset = 0;
402     graphics.setColor(Color.black);
403     graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
404     graphics.setColor(Color.white);
405
406     graphics.setColor(Color.white);
407
408     char s = seq.getCharAt(res);
409     if (av.validCharWidth)
410     {
411
412       charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
413       graphics.drawString(String.valueOf(s), charOffset + x1,
414               (y1 + av.getCharHeight()) - pady);
415     }
416   }
417
418 }