JAL-2094 first pass with jalview.api.ColorI interface
[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.api.ColorI;
24 import jalview.api.FeatureRenderer;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.schemes.Colour;
28 import jalview.schemes.ColourSchemeI;
29
30 import java.awt.Color;
31 import java.awt.Font;
32 import java.awt.FontMetrics;
33 import java.awt.Graphics;
34
35 public class SequenceRenderer implements jalview.api.SequenceRenderer
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   @Override
73   public ColorI getResidueBoxColour(SequenceI seq, int i)
74   {
75     allGroups = av.getAlignment().findAllGroups(seq);
76
77     if (inCurrentSequenceGroup(i))
78     {
79       if (currentSequenceGroup.getDisplayBoxes())
80       {
81         getBoxColour(currentSequenceGroup.cs, seq, i);
82       }
83     }
84     else if (av.getShowBoxes())
85     {
86       getBoxColour(av.getGlobalColourScheme(), seq, i);
87     }
88
89     return new Colour(resBoxColour);
90   }
91
92   /**
93    * Get the residue colour at the given sequence position - as determined by
94    * the sequence group colour (if any), else the colour scheme, possibly
95    * overridden by a feature colour.
96    * 
97    * @param seq
98    * @param position
99    * @param fr
100    * @return
101    */
102   @Override
103   public ColorI getResidueColour(final SequenceI seq, int position,
104           FeatureRenderer fr)
105   {
106     // TODO replace 8 or so code duplications with calls to this method
107     // (refactored as needed)
108     ColorI col = getResidueBoxColour(seq, position);
109
110     if (fr != null)
111     {
112       col = fr.findFeatureColour(col, seq, position);
113     }
114     return col;
115   }
116
117   void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
118   {
119     if (cs != null)
120     {
121       resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
122     }
123     else if (forOverview
124             && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
125     {
126       resBoxColour = Color.lightGray;
127     }
128     else
129     {
130       resBoxColour = Color.white;
131     }
132
133   }
134
135   public Color findSequenceColour(SequenceI seq, int i)
136   {
137     allGroups = av.getAlignment().findAllGroups(seq);
138     drawBoxes(seq, i, i, 0);
139     return resBoxColour;
140   }
141
142   public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
143           int end, int y1)
144   {
145     if (seq == null)
146     {
147       return;
148     }
149
150     allGroups = sg;
151
152     drawBoxes(seq, start, end, y1);
153
154     if (av.validCharWidth)
155     {
156       drawText(seq, start, end, y1);
157     }
158   }
159
160   public void drawBoxes(SequenceI seq, int start, int end, int y1)
161   {
162     int i = start;
163     int length = seq.getLength();
164
165     int curStart = -1;
166     int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(), avCharHeight = av
167             .getCharHeight();
168
169     Color tempColour = null;
170     while (i <= end)
171     {
172       resBoxColour = Color.white;
173       if (i < length)
174       {
175         if (inCurrentSequenceGroup(i))
176         {
177           if (currentSequenceGroup.getDisplayBoxes())
178           {
179             getBoxColour(currentSequenceGroup.cs, seq, i);
180           }
181         }
182         else if (av.getShowBoxes())
183         {
184           getBoxColour(av.getGlobalColourScheme(), 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.cs, seq, i);
258           graphics.setColor(resBoxColour.darker());
259         }
260         if (currentSequenceGroup.getShowNonconserved())
261         {
262           s = getDisplayChar(srep, i, s, '.');
263         }
264       }
265       else
266       {
267         if (!av.getShowText())
268         {
269           continue;
270         }
271
272         if (av.getColourText())
273         {
274           getBoxColour(av.getGlobalColourScheme(), 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, '.');
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   private char getDisplayChar(final boolean usesrep, int position, char s,
319           char c)
320   {
321     // TODO - use currentSequenceGroup rather than alignment
322     // currentSequenceGroup.getConsensus()
323     char conschar = (usesrep) ? av.getAlignment().getSeqrep()
324             .getCharAt(position)
325             : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
326                     .charAt(0);
327     if (!jalview.util.Comparison.isGap(conschar) && s == conschar)
328     {
329       s = c;
330     }
331     return s;
332   }
333
334   boolean inCurrentSequenceGroup(int res)
335   {
336     if (allGroups == null)
337     {
338       return false;
339     }
340
341     for (int i = 0; i < allGroups.length; i++)
342     {
343       if (allGroups[i].getStartRes() <= res
344               && allGroups[i].getEndRes() >= res)
345       {
346         currentSequenceGroup = allGroups[i];
347         return true;
348       }
349     }
350
351     return false;
352   }
353
354   public void drawHighlightedText(SequenceI seq, int start, int end,
355           int x1, int y1)
356   {
357     int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
358     int pady = avCharHeight / 5;
359     int charOffset = 0;
360     graphics.setColor(Color.black);
361     graphics.fillRect(x1, y1, avCharWidth * (end - start + 1), avCharHeight);
362     graphics.setColor(Color.white);
363
364     char s = '~';
365     // Need to find the sequence position here.
366     if (av.validCharWidth)
367     {
368       for (int i = start; i <= end; i++)
369       {
370         if (i < seq.getLength())
371         {
372           s = seq.getCharAt(i);
373         }
374
375         charOffset = (avCharWidth - fm.charWidth(s)) / 2;
376         graphics.drawString(String.valueOf(s), charOffset + x1
377                 + avCharWidth * (i - start), y1 + avCharHeight - pady);
378       }
379     }
380   }
381
382   public void drawCursor(SequenceI seq, int res, int x1, int y1)
383   {
384     int pady = av.getCharHeight() / 5;
385     int charOffset = 0;
386     graphics.setColor(Color.black);
387     graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
388     graphics.setColor(Color.white);
389
390     graphics.setColor(Color.white);
391
392     char s = seq.getCharAt(res);
393     if (av.validCharWidth)
394     {
395
396       charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
397       graphics.drawString(String.valueOf(s), charOffset + x1,
398               (y1 + av.getCharHeight()) - pady);
399     }
400   }
401
402 }