JAL-2089 patch broken merge to master for Release 2.10.0b1
[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   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   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 Color 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 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 Color 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     Color 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, '.', currentSequenceGroup);
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, '.', null);
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   /**
319    * Returns 'conservedChar' to represent the given position if the sequence
320    * character at that position is equal to the consensus (ignoring case), else
321    * returns the sequence character
322    * 
323    * @param usesrep
324    * @param position
325    * @param sequenceChar
326    * @param conservedChar
327    * @return
328    */
329   private char getDisplayChar(final boolean usesrep, int position,
330           char sequenceChar, char conservedChar, SequenceGroup currentGroup)
331   {
332     // TODO - use currentSequenceGroup rather than alignment
333     // currentSequenceGroup.getConsensus()
334     char conschar = (usesrep) ? (currentGroup == null
335             || position < currentGroup.getStartRes()
336             || position > currentGroup.getEndRes() ? av.getAlignment()
337             .getSeqrep().getCharAt(position)
338             : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
339                     .getCharAt(position) : av.getAlignment().getSeqrep()
340                     .getCharAt(position)))
341             : (currentGroup != null && currentGroup.getConsensus() != null
342                     && position >= currentGroup.getStartRes()
343                     && position <= currentGroup.getEndRes() && currentGroup
344                     .getConsensus().annotations.length > position) ? currentGroup
345                     .getConsensus().annotations[position].displayCharacter
346                     .charAt(0)
347                     : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
348                             .charAt(0);
349     if (!jalview.util.Comparison.isGap(conschar)
350             && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
351     {
352       sequenceChar = conservedChar;
353     }
354     return sequenceChar;
355   }
356
357   boolean inCurrentSequenceGroup(int res)
358   {
359     if (allGroups == null)
360     {
361       return false;
362     }
363
364     for (int i = 0; i < allGroups.length; i++)
365     {
366       if (allGroups[i].getStartRes() <= res
367               && allGroups[i].getEndRes() >= res)
368       {
369         currentSequenceGroup = allGroups[i];
370         return true;
371       }
372     }
373
374     return false;
375   }
376
377   public void drawHighlightedText(SequenceI seq, int start, int end,
378           int x1, int y1)
379   {
380     int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
381     int pady = avCharHeight / 5;
382     int charOffset = 0;
383     graphics.setColor(Color.black);
384     graphics.fillRect(x1, y1, avCharWidth * (end - start + 1), avCharHeight);
385     graphics.setColor(Color.white);
386
387     char s = '~';
388     // Need to find the sequence position here.
389     if (av.validCharWidth)
390     {
391       for (int i = start; i <= end; i++)
392       {
393         if (i < seq.getLength())
394         {
395           s = seq.getCharAt(i);
396         }
397
398         charOffset = (avCharWidth - fm.charWidth(s)) / 2;
399         graphics.drawString(String.valueOf(s), charOffset + x1
400                 + avCharWidth * (i - start), y1 + avCharHeight - pady);
401       }
402     }
403   }
404
405   public void drawCursor(SequenceI seq, int res, int x1, int y1)
406   {
407     int pady = av.getCharHeight() / 5;
408     int charOffset = 0;
409     graphics.setColor(Color.black);
410     graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
411     graphics.setColor(Color.white);
412
413     graphics.setColor(Color.white);
414
415     char s = seq.getCharAt(res);
416     if (av.validCharWidth)
417     {
418
419       charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
420       graphics.drawString(String.valueOf(s), charOffset + x1,
421               (y1 + av.getCharHeight()) - pady);
422     }
423   }
424
425 }