857472ba8e0c29bc7c8d4f4165141bbdfcca4191
[jalview.git] / src / jalview / appletgui / SequenceRenderer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.appletgui;
19
20 import java.awt.*;
21
22 import jalview.datamodel.*;
23 import jalview.schemes.*;
24
25 public class SequenceRenderer implements jalview.api.SequenceRenderer
26 {
27   AlignViewport av;
28
29   FontMetrics fm;
30
31   boolean renderGaps = true;
32
33   SequenceGroup currentSequenceGroup = null;
34
35   SequenceGroup[] allGroups = null;
36
37   Color resBoxColour;
38
39   Graphics graphics;
40
41   boolean forOverview = false;
42
43   public SequenceRenderer(AlignViewport av)
44   {
45     this.av = av;
46   }
47
48   /**
49    * DOCUMENT ME!
50    * 
51    * @param b
52    *          DOCUMENT ME!
53    */
54   public void prepare(Graphics g, boolean renderGaps)
55   {
56     graphics = g;
57     fm = g.getFontMetrics();
58
59     this.renderGaps = renderGaps;
60   }
61
62   public Color getResidueBoxColour(SequenceI seq, int i)
63   {
64     allGroups = av.alignment.findAllGroups(seq);
65
66     if (inCurrentSequenceGroup(i))
67     {
68       if (currentSequenceGroup.getDisplayBoxes())
69       {
70         getBoxColour(currentSequenceGroup.cs, seq, i);
71       }
72     }
73     else if (av.getShowBoxes())
74     {
75       getBoxColour(av.globalColourScheme, seq, i);
76     }
77
78     return resBoxColour;
79   }
80
81   void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
82   {
83     if (cs != null)
84     {
85       resBoxColour = cs.findColour(seq.getCharAt(i), i);
86     }
87     else if (forOverview
88             && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
89     {
90       resBoxColour = Color.lightGray;
91     }
92     else
93     {
94       resBoxColour = Color.white;
95     }
96
97   }
98
99   public Color findSequenceColour(SequenceI seq, int i)
100   {
101     allGroups = av.alignment.findAllGroups(seq);
102     drawBoxes(seq, i, i, 0);
103     return resBoxColour;
104   }
105
106   public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
107           int end, int y1)
108   {
109     if (seq == null)
110     {
111       return;
112     }
113
114     allGroups = sg;
115
116     drawBoxes(seq, start, end, y1);
117
118     if (av.validCharWidth)
119     {
120       drawText(seq, start, end, y1);
121     }
122   }
123
124   public void drawBoxes(SequenceI seq, int start, int end, int y1)
125   {
126     int i = start;
127     int length = seq.getLength();
128
129     int curStart = -1;
130     int curWidth = av.charWidth;
131
132     Color tempColour = null;
133     while (i <= end)
134     {
135       resBoxColour = Color.white;
136       if (i < length)
137       {
138         if (inCurrentSequenceGroup(i))
139         {
140           if (currentSequenceGroup.getDisplayBoxes())
141           {
142             getBoxColour(currentSequenceGroup.cs, seq, i);
143           }
144         }
145         else if (av.getShowBoxes())
146         {
147           getBoxColour(av.getGlobalColourScheme(), seq, i);
148         }
149       }
150
151       if (resBoxColour != tempColour)
152       {
153         if (tempColour != null)
154         {
155           graphics.fillRect(av.charWidth * (curStart - start), y1,
156                   curWidth, av.charHeight);
157         }
158         graphics.setColor(resBoxColour);
159
160         curStart = i;
161         curWidth = av.charWidth;
162         tempColour = resBoxColour;
163
164       }
165       else
166       {
167         curWidth += av.charWidth;
168       }
169
170       i++;
171     }
172
173     graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
174             av.charHeight);
175   }
176
177   public void drawText(SequenceI seq, int start, int end, int y1)
178   {
179     Font boldFont = null;
180     boolean bold = false;
181     if (av.upperCasebold)
182     {
183       boldFont = new Font(av.getFont().getName(), Font.BOLD, av.charHeight);
184
185       graphics.setFont(av.getFont());
186     }
187
188     y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
189
190     int charOffset = 0;
191
192     // Need to find the sequence position here.
193     if (end + 1 >= seq.getLength())
194     {
195       end = seq.getLength() - 1;
196     }
197
198     char s = ' ';
199
200     for (int i = start; i <= end; i++)
201     {
202       graphics.setColor(Color.black);
203
204       s = seq.getCharAt(i);
205       if (!renderGaps && jalview.util.Comparison.isGap(s))
206       {
207         continue;
208       }
209
210       if (inCurrentSequenceGroup(i))
211       {
212         if (!currentSequenceGroup.getDisplayText())
213         {
214           continue;
215         }
216
217         if (currentSequenceGroup.getColourText())
218         {
219           getBoxColour(currentSequenceGroup.cs, seq, i);
220           graphics.setColor(resBoxColour.darker());
221         }
222         if (currentSequenceGroup.getShowNonconserved())
223         {
224           // cheat - use this if we have a consensus for each group: s =
225           // getDisplayChar(currentSequenceGroup.getConsensus(), i, s, '.');
226           s = getDisplayChar(av.consensus, i, s, '.');
227         }
228       }
229       else
230       {
231         if (!av.getShowText())
232         {
233           continue;
234         }
235
236         if (av.getColourText())
237         {
238           getBoxColour(av.getGlobalColourScheme(), seq, i);
239           if (av.getShowBoxes())
240           {
241             graphics.setColor(resBoxColour.darker());
242           }
243           else
244           {
245             graphics.setColor(resBoxColour);
246           }
247         }
248         if (av.getShowunconserved())
249         {
250           s = getDisplayChar(av.consensus, i, s, '.');
251
252         }
253       }
254
255       if (av.upperCasebold)
256       {
257         fm = graphics.getFontMetrics();
258         if ('A' <= s && s <= 'Z')
259         {
260           if (!bold)
261           {
262
263             graphics.setFont(boldFont);
264           }
265           bold = true;
266         }
267         else if (bold)
268         {
269           graphics.setFont(av.font);
270           bold = false;
271         }
272
273       }
274
275       charOffset = (av.charWidth - fm.charWidth(s)) / 2;
276       graphics.drawString(String.valueOf(s), charOffset + av.charWidth
277               * (i - start), y1);
278     }
279
280   }
281
282   private char getDisplayChar(AlignmentAnnotation consensus, int position,
283           char s, char c)
284   {
285     char conschar = consensus.annotations[position].displayCharacter
286             .charAt(0);
287     if (conschar != '-' && s == conschar)
288     {
289       s = c;
290     }
291     return s;
292   }
293
294   boolean inCurrentSequenceGroup(int res)
295   {
296     if (allGroups == null)
297     {
298       return false;
299     }
300
301     for (int i = 0; i < allGroups.length; i++)
302     {
303       if (allGroups[i].getStartRes() <= res
304               && allGroups[i].getEndRes() >= res)
305       {
306         currentSequenceGroup = allGroups[i];
307         return true;
308       }
309     }
310
311     return false;
312   }
313
314   public void drawHighlightedText(SequenceI seq, int start, int end,
315           int x1, int y1)
316   {
317     int pady = av.charHeight / 5;
318     int charOffset = 0;
319     graphics.setColor(Color.black);
320     graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
321             av.charHeight);
322     graphics.setColor(Color.white);
323
324     char s = '~';
325     // Need to find the sequence position here.
326     if (av.validCharWidth)
327     {
328       for (int i = start; i <= end; i++)
329       {
330         if (i < seq.getLength())
331         {
332           s = seq.getCharAt(i);
333         }
334
335         charOffset = (av.charWidth - fm.charWidth(s)) / 2;
336         graphics.drawString(String.valueOf(s), charOffset + x1
337                 + av.charWidth * (i - start), y1 + av.charHeight - pady);
338       }
339     }
340   }
341
342   public void drawCursor(SequenceI seq, int res, int x1, int y1)
343   {
344     int pady = av.charHeight / 5;
345     int charOffset = 0;
346     graphics.setColor(Color.black);
347     graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
348     graphics.setColor(Color.white);
349
350     graphics.setColor(Color.white);
351
352     char s = seq.getCharAt(res);
353     if (av.validCharWidth)
354     {
355
356       charOffset = (av.charWidth - fm.charWidth(s)) / 2;
357       graphics.drawString(String.valueOf(s), charOffset + x1,
358               (y1 + av.charHeight) - pady);
359     }
360   }
361
362 }