09b50c470275b4a5ea29315e38b41eb182978b6f
[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.FeatureRenderer;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.schemes.ColourSchemeI;
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   AlignViewport av;
36
37   FontMetrics fm;
38
39   boolean renderGaps = true;
40
41   SequenceGroup currentSequenceGroup = null;
42
43   SequenceGroup[] allGroups = null;
44
45   Color resBoxColour;
46
47   Graphics graphics;
48
49   boolean forOverview = false;
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   public 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.cs, seq, i);
79       }
80     }
81     else if (av.getShowBoxes())
82     {
83       getBoxColour(av.getGlobalColourScheme(), 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 fr
97    * @return
98    */
99   @Override
100   public Color getResidueColour(final SequenceI seq, int position,
101           FeatureRenderer fr)
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 (fr != null)
108     {
109       col = fr.findFeatureColour(col, seq, position);
110     }
111     return col;
112   }
113
114   void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
115   {
116     if (cs != null)
117     {
118       resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
119     }
120     else if (forOverview
121             && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
122     {
123       resBoxColour = Color.lightGray;
124     }
125     else
126     {
127       resBoxColour = Color.white;
128     }
129
130   }
131
132   public Color findSequenceColour(SequenceI seq, int i)
133   {
134     allGroups = av.getAlignment().findAllGroups(seq);
135     drawBoxes(seq, i, i, 0);
136     return resBoxColour;
137   }
138
139   public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
140           int end, int y1)
141   {
142     if (seq == null)
143     {
144       return;
145     }
146
147     allGroups = sg;
148
149     drawBoxes(seq, start, end, y1);
150
151     if (av.validCharWidth)
152     {
153       drawText(seq, start, end, y1);
154     }
155   }
156
157   public void drawBoxes(SequenceI seq, int start, int end, int y1)
158   {
159     int i = start;
160     int length = seq.getLength();
161
162     int curStart = -1;
163     int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(), avCharHeight = av
164             .getCharHeight();
165
166     Color tempColour = null;
167     while (i <= end)
168     {
169       resBoxColour = Color.white;
170       if (i < length)
171       {
172         if (inCurrentSequenceGroup(i))
173         {
174           if (currentSequenceGroup.getDisplayBoxes())
175           {
176             getBoxColour(currentSequenceGroup.cs, seq, i);
177           }
178         }
179         else if (av.getShowBoxes())
180         {
181           getBoxColour(av.getGlobalColourScheme(), seq, i);
182         }
183       }
184
185       if (resBoxColour != tempColour)
186       {
187         if (tempColour != null)
188         {
189           graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
190                   avCharHeight);
191         }
192         graphics.setColor(resBoxColour);
193
194         curStart = i;
195         curWidth = avCharWidth;
196         tempColour = resBoxColour;
197
198       }
199       else
200       {
201         curWidth += avCharWidth;
202       }
203
204       i++;
205     }
206
207     graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
208             avCharHeight);
209   }
210
211   public void drawText(SequenceI seq, int start, int end, int y1)
212   {
213     int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
214     Font boldFont = null;
215     boolean bold = false;
216     if (av.isUpperCasebold())
217     {
218       boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);
219
220       graphics.setFont(av.getFont());
221     }
222
223     y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady
224
225     int charOffset = 0;
226
227     // Need to find the sequence position here.
228     if (end + 1 >= seq.getLength())
229     {
230       end = seq.getLength() - 1;
231     }
232
233     char s = ' ';
234     boolean srep = av.isDisplayReferenceSeq();
235     for (int i = start; i <= end; i++)
236     {
237       graphics.setColor(Color.black);
238
239       s = seq.getCharAt(i);
240       if (!renderGaps && jalview.util.Comparison.isGap(s))
241       {
242         continue;
243       }
244
245       if (inCurrentSequenceGroup(i))
246       {
247         if (!currentSequenceGroup.getDisplayText())
248         {
249           continue;
250         }
251
252         if (currentSequenceGroup.getColourText())
253         {
254           getBoxColour(currentSequenceGroup.cs, seq, i);
255           graphics.setColor(resBoxColour.darker());
256         }
257         if (currentSequenceGroup.getShowNonconserved())
258         {
259           s = getDisplayChar(srep, i, s, '.');
260         }
261       }
262       else
263       {
264         if (!av.getShowText())
265         {
266           continue;
267         }
268
269         if (av.getColourText())
270         {
271           getBoxColour(av.getGlobalColourScheme(), seq, i);
272           if (av.getShowBoxes())
273           {
274             graphics.setColor(resBoxColour.darker());
275           }
276           else
277           {
278             graphics.setColor(resBoxColour);
279           }
280         }
281         if (av.getShowUnconserved())
282         {
283           s = getDisplayChar(srep, i, s, '.');
284
285         }
286       }
287
288       if (av.isUpperCasebold())
289       {
290         fm = graphics.getFontMetrics();
291         if ('A' <= s && s <= 'Z')
292         {
293           if (!bold)
294           {
295
296             graphics.setFont(boldFont);
297           }
298           bold = true;
299         }
300         else if (bold)
301         {
302           graphics.setFont(av.font);
303           bold = false;
304         }
305
306       }
307
308       charOffset = (avCharWidth - fm.charWidth(s)) / 2;
309       graphics.drawString(String.valueOf(s), charOffset + avCharWidth
310               * (i - start), y1);
311     }
312
313   }
314
315   private char getDisplayChar(final boolean usesrep, int position, char s,
316           char c)
317   {
318     // TODO - use currentSequenceGroup rather than alignment
319     // currentSequenceGroup.getConsensus()
320     char conschar = (usesrep) ? av.getAlignment().getSeqrep()
321             .getCharAt(position)
322             : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
323                     .charAt(0);
324     if (!jalview.util.Comparison.isGap(conschar) && s == conschar)
325     {
326       s = c;
327     }
328     return s;
329   }
330
331   boolean inCurrentSequenceGroup(int res)
332   {
333     if (allGroups == null)
334     {
335       return false;
336     }
337
338     for (int i = 0; i < allGroups.length; i++)
339     {
340       if (allGroups[i].getStartRes() <= res
341               && allGroups[i].getEndRes() >= res)
342       {
343         currentSequenceGroup = allGroups[i];
344         return true;
345       }
346     }
347
348     return false;
349   }
350
351   public void drawHighlightedText(SequenceI seq, int start, int end,
352           int x1, int y1)
353   {
354     int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
355     int pady = avCharHeight / 5;
356     int charOffset = 0;
357     graphics.setColor(Color.black);
358     graphics.fillRect(x1, y1, avCharWidth * (end - start + 1), avCharHeight);
359     graphics.setColor(Color.white);
360
361     char s = '~';
362     // Need to find the sequence position here.
363     if (av.validCharWidth)
364     {
365       for (int i = start; i <= end; i++)
366       {
367         if (i < seq.getLength())
368         {
369           s = seq.getCharAt(i);
370         }
371
372         charOffset = (avCharWidth - fm.charWidth(s)) / 2;
373         graphics.drawString(String.valueOf(s), charOffset + x1
374                 + avCharWidth * (i - start), y1 + avCharHeight - pady);
375       }
376     }
377   }
378
379   public void drawCursor(SequenceI seq, int res, int x1, int y1)
380   {
381     int pady = av.getCharHeight() / 5;
382     int charOffset = 0;
383     graphics.setColor(Color.black);
384     graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
385     graphics.setColor(Color.white);
386
387     graphics.setColor(Color.white);
388
389     char s = seq.getCharAt(res);
390     if (av.validCharWidth)
391     {
392
393       charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
394       graphics.drawString(String.valueOf(s), charOffset + x1,
395               (y1 + av.getCharHeight()) - pady);
396     }
397   }
398
399 }