d9a0d2aed1ad1bf665e092ce87a1ee394d35843e
[jalview.git] / src / jalview / appletgui / SequenceRenderer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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.FeatureRenderer;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.schemes.ColourSchemeI;
28
29 import java.awt.Color;
30 import java.awt.Font;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
33
34 public class SequenceRenderer implements jalview.api.SequenceRenderer
35 {
36   AlignViewport av;
37
38   FontMetrics fm;
39
40   boolean renderGaps = true;
41
42   SequenceGroup currentSequenceGroup = null;
43
44   SequenceGroup[] allGroups = null;
45
46   Color resBoxColour;
47
48   Graphics graphics;
49
50   boolean forOverview = false;
51
52   public SequenceRenderer(AlignViewport av)
53   {
54     this.av = av;
55   }
56
57   /**
58    * DOCUMENT ME!
59    * 
60    * @param b
61    *          DOCUMENT ME!
62    */
63   public void prepare(Graphics g, boolean renderGaps)
64   {
65     graphics = g;
66     fm = g.getFontMetrics();
67
68     this.renderGaps = renderGaps;
69   }
70
71   public Color getResidueBoxColour(SequenceI seq, int i)
72   {
73     allGroups = av.getAlignment().findAllGroups(seq);
74
75     if (inCurrentSequenceGroup(i))
76     {
77       if (currentSequenceGroup.getDisplayBoxes())
78       {
79         getBoxColour(currentSequenceGroup.cs, seq, i);
80       }
81     }
82     else if (av.getShowBoxes())
83     {
84       getBoxColour(av.getGlobalColourScheme(), seq, i);
85     }
86
87     return resBoxColour;
88   }
89
90   /**
91    * Get the residue colour at the given sequence position - as determined by
92    * the sequence group colour (if any), else the colour scheme, possibly
93    * overridden by a feature colour.
94    * 
95    * @param seq
96    * @param position
97    * @param fr
98    * @return
99    */
100   @Override
101   public Color getResidueColour(final SequenceI seq, int position,
102           FeatureRenderer fr)
103   {
104     // TODO replace 8 or so code duplications with calls to this method
105     // (refactored as needed)
106     Color col = getResidueBoxColour(seq, position);
107
108     if (fr != null)
109     {
110       col = fr.findFeatureColour(col, seq, position);
111     }
112     return col;
113   }
114
115   void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
116   {
117     if (cs != null)
118     {
119       resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
120     }
121     else if (forOverview
122             && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
123     {
124       resBoxColour = Color.lightGray;
125     }
126     else
127     {
128       resBoxColour = Color.white;
129     }
130
131   }
132
133   public Color findSequenceColour(SequenceI seq, int i)
134   {
135     allGroups = av.getAlignment().findAllGroups(seq);
136     drawBoxes(seq, i, i, 0);
137     return resBoxColour;
138   }
139
140   public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
141           int end, int y1)
142   {
143     if (seq == null)
144     {
145       return;
146     }
147
148     allGroups = sg;
149
150     drawBoxes(seq, start, end, y1);
151
152     if (av.validCharWidth)
153     {
154       drawText(seq, start, end, y1);
155     }
156   }
157
158   public void drawBoxes(SequenceI seq, int start, int end, int y1)
159   {
160     int i = start;
161     int length = seq.getLength();
162
163     int curStart = -1;
164     int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(), avCharHeight = av
165             .getCharHeight();
166
167     Color tempColour = null;
168     while (i <= end)
169     {
170       resBoxColour = Color.white;
171       if (i < length)
172       {
173         if (inCurrentSequenceGroup(i))
174         {
175           if (currentSequenceGroup.getDisplayBoxes())
176           {
177             getBoxColour(currentSequenceGroup.cs, seq, i);
178           }
179         }
180         else if (av.getShowBoxes())
181         {
182           getBoxColour(av.getGlobalColourScheme(), seq, i);
183         }
184       }
185
186       if (resBoxColour != tempColour)
187       {
188         if (tempColour != null)
189         {
190           graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
191                   avCharHeight);
192         }
193         graphics.setColor(resBoxColour);
194
195         curStart = i;
196         curWidth = avCharWidth;
197         tempColour = resBoxColour;
198
199       }
200       else
201       {
202         curWidth += avCharWidth;
203       }
204
205       i++;
206     }
207
208     graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
209             avCharHeight);
210   }
211
212   public void drawText(SequenceI seq, int start, int end, int y1)
213   {
214     int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
215     Font boldFont = null;
216     boolean bold = false;
217     if (av.isUpperCasebold())
218     {
219       boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);
220
221       graphics.setFont(av.getFont());
222     }
223
224     y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady
225
226     int charOffset = 0;
227
228     // Need to find the sequence position here.
229     if (end + 1 >= seq.getLength())
230     {
231       end = seq.getLength() - 1;
232     }
233
234     char s = ' ';
235     boolean srep = av.isDisplayReferenceSeq();
236     for (int i = start; i <= end; i++)
237     {
238       graphics.setColor(Color.black);
239
240       s = seq.getCharAt(i);
241       if (!renderGaps && jalview.util.Comparison.isGap(s))
242       {
243         continue;
244       }
245
246       if (inCurrentSequenceGroup(i))
247       {
248         if (!currentSequenceGroup.getDisplayText())
249         {
250           continue;
251         }
252
253         if (currentSequenceGroup.getColourText())
254         {
255           getBoxColour(currentSequenceGroup.cs, seq, i);
256           graphics.setColor(resBoxColour.darker());
257         }
258         if (currentSequenceGroup.getShowNonconserved())
259         {
260           s = getDisplayChar(srep, i, s,
261                   '.');
262         }
263       }
264       else
265       {
266         if (!av.getShowText())
267         {
268           continue;
269         }
270
271         if (av.getColourText())
272         {
273           getBoxColour(av.getGlobalColourScheme(), seq, i);
274           if (av.getShowBoxes())
275           {
276             graphics.setColor(resBoxColour.darker());
277           }
278           else
279           {
280             graphics.setColor(resBoxColour);
281           }
282         }
283         if (av.getShowUnconserved())
284         {
285           s = getDisplayChar(srep, i, s,
286                   '.');
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,
319           char s, char c)
320   {
321     // TODO - use currentSequenceGroup rather than alignemnt 
322     // currentSequenceGroup.getConsensus()
323     char conschar = (usesrep) ? av.getAlignment().getSeqrep().getCharAt(position) : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
324             .charAt(0);
325     if (conschar != '-' && s == conschar)
326     {
327       s = c;
328     }
329     return s;
330   }
331
332   boolean inCurrentSequenceGroup(int res)
333   {
334     if (allGroups == null)
335     {
336       return false;
337     }
338
339     for (int i = 0; i < allGroups.length; i++)
340     {
341       if (allGroups[i].getStartRes() <= res
342               && allGroups[i].getEndRes() >= res)
343       {
344         currentSequenceGroup = allGroups[i];
345         return true;
346       }
347     }
348
349     return false;
350   }
351
352   public void drawHighlightedText(SequenceI seq, int start, int end,
353           int x1, int y1)
354   {
355     int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
356     int pady = avCharHeight / 5;
357     int charOffset = 0;
358     graphics.setColor(Color.black);
359     graphics.fillRect(x1, y1, avCharWidth * (end - start + 1), avCharHeight);
360     graphics.setColor(Color.white);
361
362     char s = '~';
363     // Need to find the sequence position here.
364     if (av.validCharWidth)
365     {
366       for (int i = start; i <= end; i++)
367       {
368         if (i < seq.getLength())
369         {
370           s = seq.getCharAt(i);
371         }
372
373         charOffset = (avCharWidth - fm.charWidth(s)) / 2;
374         graphics.drawString(String.valueOf(s), charOffset + x1
375                 + avCharWidth * (i - start), y1 + avCharHeight - pady);
376       }
377     }
378   }
379
380   public void drawCursor(SequenceI seq, int res, int x1, int y1)
381   {
382     int pady = av.getCharHeight() / 5;
383     int charOffset = 0;
384     graphics.setColor(Color.black);
385     graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
386     graphics.setColor(Color.white);
387
388     graphics.setColor(Color.white);
389
390     char s = seq.getCharAt(res);
391     if (av.validCharWidth)
392     {
393
394       charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
395       graphics.drawString(String.valueOf(s), charOffset + x1,
396               (y1 + av.getCharHeight()) - pady);
397     }
398   }
399
400 }