JAL-1370 allow show unconserved to reflect differences from the current reference...
[jalview.git] / src / jalview / gui / SequenceRenderer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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.gui;
19
20 import jalview.datamodel.AlignmentAnnotation;
21 import jalview.datamodel.SequenceGroup;
22 import jalview.datamodel.SequenceI;
23 import jalview.schemes.ColourSchemeI;
24
25 import java.awt.Color;
26 import java.awt.FontMetrics;
27 import java.awt.Graphics;
28
29 /**
30  * DOCUMENT ME!
31  * 
32  * @author $author$
33  * @version $Revision$
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 monospacedFont;
52
53   boolean forOverview = false;
54
55   /**
56    * Creates a new SequenceRenderer object.
57    * 
58    * @param av
59    *          DOCUMENT ME!
60    */
61   public SequenceRenderer(AlignViewport av)
62   {
63     this.av = av;
64   }
65
66   /**
67    * DOCUMENT ME!
68    * 
69    * @param b
70    *          DOCUMENT ME!
71    */
72   public void prepare(Graphics g, boolean renderGaps)
73   {
74     graphics = g;
75     fm = g.getFontMetrics();
76
77     // If EPS graphics, stringWidth will be a double, not an int
78     double dwidth = fm.getStringBounds("M", g).getWidth();
79
80     monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && (float) av.charWidth == dwidth);
81
82     this.renderGaps = renderGaps;
83   }
84
85   public Color getResidueBoxColour(SequenceI seq, int i)
86   {
87     allGroups = av.getAlignment().findAllGroups(seq);
88
89     if (inCurrentSequenceGroup(i))
90     {
91       if (currentSequenceGroup.getDisplayBoxes())
92       {
93         getBoxColour(currentSequenceGroup.cs, seq, i);
94       }
95     }
96     else if (av.getShowBoxes())
97     {
98       getBoxColour(av.getGlobalColourScheme(), seq, i);
99     }
100
101     return resBoxColour;
102   }
103
104   /**
105    * DOCUMENT ME!
106    * 
107    * @param cs
108    *          DOCUMENT ME!
109    * @param seq
110    *          DOCUMENT ME!
111    * @param i
112    *          DOCUMENT ME!
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    * DOCUMENT ME!
133    * 
134    * @param g
135    *          DOCUMENT ME!
136    * @param seq
137    *          DOCUMENT ME!
138    * @param sg
139    *          DOCUMENT ME!
140    * @param start
141    *          DOCUMENT ME!
142    * @param end
143    *          DOCUMENT ME!
144    * @param x1
145    *          DOCUMENT ME!
146    * @param y1
147    *          DOCUMENT ME!
148    * @param width
149    *          DOCUMENT ME!
150    * @param height
151    *          DOCUMENT ME!
152    */
153   public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
154           int end, int y1)
155   {
156     allGroups = sg;
157
158     drawBoxes(seq, start, end, y1);
159
160     if (av.validCharWidth)
161     {
162       drawText(seq, start, end, y1);
163     }
164   }
165
166   /**
167    * DOCUMENT ME!
168    * 
169    * @param seq
170    *          DOCUMENT ME!
171    * @param start
172    *          DOCUMENT ME!
173    * @param end
174    *          DOCUMENT ME!
175    * @param x1
176    *          DOCUMENT ME!
177    * @param y1
178    *          DOCUMENT ME!
179    * @param width
180    *          DOCUMENT ME!
181    * @param height
182    *          DOCUMENT ME!
183    */
184   public synchronized void drawBoxes(SequenceI seq, int start, int end,
185           int y1)
186   {
187     if (seq == null)
188       return; // fix for racecondition
189     int i = start;
190     int length = seq.getLength();
191
192     int curStart = -1;
193     int curWidth = av.charWidth;
194
195     Color tempColour = null;
196
197     while (i <= end)
198     {
199       resBoxColour = Color.white;
200
201       if (i < length)
202       {
203         if (inCurrentSequenceGroup(i))
204         {
205           if (currentSequenceGroup.getDisplayBoxes())
206           {
207             getBoxColour(currentSequenceGroup.cs, seq, i);
208           }
209         }
210         else if (av.getShowBoxes())
211         {
212           getBoxColour(av.getGlobalColourScheme(), seq, i);
213         }
214
215       }
216
217       if (resBoxColour != tempColour)
218       {
219         if (tempColour != null)
220         {
221           graphics.fillRect(av.charWidth * (curStart - start), y1,
222                   curWidth, av.charHeight);
223         }
224
225         graphics.setColor(resBoxColour);
226
227         curStart = i;
228         curWidth = av.charWidth;
229         tempColour = resBoxColour;
230       }
231       else
232       {
233         curWidth += av.charWidth;
234       }
235
236       i++;
237     }
238
239     graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
240             av.charHeight);
241
242   }
243
244   /**
245    * DOCUMENT ME!
246    * 
247    * @param seq
248    *          DOCUMENT ME!
249    * @param start
250    *          DOCUMENT ME!
251    * @param end
252    *          DOCUMENT ME!
253    * @param x1
254    *          DOCUMENT ME!
255    * @param y1
256    *          DOCUMENT ME!
257    * @param width
258    *          DOCUMENT ME!
259    * @param height
260    *          DOCUMENT ME!
261    */
262   public void drawText(SequenceI seq, int start, int end, int y1)
263   {
264     y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
265     int charOffset = 0;
266     char s;
267
268     if (end + 1 >= seq.getLength())
269     {
270       end = seq.getLength() - 1;
271     }
272     graphics.setColor(av.textColour);
273
274     if (monospacedFont && av.showText && allGroups.length == 0
275             && !av.getColourText() && av.thresholdTextColour == 0)
276     {
277       if (av.renderGaps)
278       {
279         graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
280       }
281       else
282       {
283         char gap = av.getGapCharacter();
284         graphics.drawString(seq.getSequenceAsString(start, end + 1)
285                 .replace(gap, ' '), 0, y1);
286       }
287     }
288     else
289     {
290       boolean srep = av.isDisplayReferenceSeq();
291       boolean getboxColour = false;
292       for (int i = start; i <= end; i++)
293       {
294         graphics.setColor(av.textColour);
295         getboxColour = false;
296         s = seq.getCharAt(i);
297         if (!renderGaps && jalview.util.Comparison.isGap(s))
298         {
299           continue;
300         }
301
302         if (inCurrentSequenceGroup(i))
303         {
304           if (!currentSequenceGroup.getDisplayText())
305           {
306             continue;
307           }
308
309           if (currentSequenceGroup.thresholdTextColour > 0
310                   || currentSequenceGroup.getColourText())
311           {
312             getboxColour = true;
313             getBoxColour(currentSequenceGroup.cs, seq, i);
314
315             if (currentSequenceGroup.getColourText())
316             {
317               graphics.setColor(resBoxColour.darker());
318             }
319
320             if (currentSequenceGroup.thresholdTextColour > 0)
321             {
322               if (resBoxColour.getRed() + resBoxColour.getBlue()
323                       + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
324               {
325                 graphics.setColor(currentSequenceGroup.textColour2);
326               }
327             }
328           }
329           else
330           {
331             graphics.setColor(currentSequenceGroup.textColour);
332           }
333           if (currentSequenceGroup.getShowNonconserved()) // todo optimize
334           {
335             // todo - use sequence group consensus
336             s = getDisplayChar(srep, i, s,
337                     '.');
338
339           }
340
341         }
342         else
343         {
344           if (!av.getShowText())
345           {
346             continue;
347           }
348
349           if (av.getColourText())
350           {
351             getboxColour = true;
352             getBoxColour(av.getGlobalColourScheme(), seq, i);
353
354             if (av.getShowBoxes())
355             {
356               graphics.setColor(resBoxColour.darker());
357             }
358             else
359             {
360               graphics.setColor(resBoxColour);
361             }
362           }
363
364           if (av.thresholdTextColour > 0)
365           {
366             if (!getboxColour)
367             {
368               getBoxColour(av.getGlobalColourScheme(), seq, i);
369             }
370
371             if (resBoxColour.getRed() + resBoxColour.getBlue()
372                     + resBoxColour.getGreen() < av.thresholdTextColour)
373             {
374               graphics.setColor(av.textColour2);
375             }
376           }
377           if (av.getShowUnconserved())
378           {
379             s = getDisplayChar(srep, i, s,
380                     '.');
381
382           }
383
384         }
385
386         charOffset = (av.charWidth - fm.charWidth(s)) / 2;
387         graphics.drawString(String.valueOf(s), charOffset + av.charWidth
388                 * (i - start), y1);
389
390       }
391     }
392   }
393
394   private char getDisplayChar(final boolean usesrep, int position,
395           char s, char c)
396   {
397     // TODO - use currentSequenceGroup rather than alignemnt 
398     // currentSequenceGroup.getConsensus()
399     char conschar = (usesrep) ? av.getAlignment().getSeqrep().getCharAt(position) : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
400             .charAt(0);
401     if (conschar != '-' && s == conschar)
402     {
403       s = c;
404     }
405     return s;
406   }
407
408   /**
409    * DOCUMENT ME!
410    * 
411    * @param res
412    *          DOCUMENT ME!
413    * 
414    * @return DOCUMENT ME!
415    */
416   boolean inCurrentSequenceGroup(int res)
417   {
418     if (allGroups == null)
419     {
420       return false;
421     }
422
423     for (int i = 0; i < allGroups.length; i++)
424     {
425       if ((allGroups[i].getStartRes() <= res)
426               && (allGroups[i].getEndRes() >= res))
427       {
428         currentSequenceGroup = allGroups[i];
429
430         return true;
431       }
432     }
433
434     return false;
435   }
436
437   /**
438    * DOCUMENT ME!
439    * 
440    * @param seq
441    *          DOCUMENT ME!
442    * @param start
443    *          DOCUMENT ME!
444    * @param end
445    *          DOCUMENT ME!
446    * @param x1
447    *          DOCUMENT ME!
448    * @param y1
449    *          DOCUMENT ME!
450    * @param width
451    *          DOCUMENT ME!
452    * @param height
453    *          DOCUMENT ME!
454    */
455   public void drawHighlightedText(SequenceI seq, int start, int end,
456           int x1, int y1)
457   {
458     int pady = av.charHeight / 5;
459     int charOffset = 0;
460     graphics.setColor(Color.BLACK);
461     graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
462             av.charHeight);
463     graphics.setColor(Color.white);
464
465     char s = '~';
466
467     // Need to find the sequence position here.
468     if (av.validCharWidth)
469     {
470       for (int i = start; i <= end; i++)
471       {
472         if (i < seq.getLength())
473         {
474           s = seq.getCharAt(i);
475         }
476
477         charOffset = (av.charWidth - fm.charWidth(s)) / 2;
478         graphics.drawString(String.valueOf(s), charOffset + x1
479                 + (av.charWidth * (i - start)), (y1 + av.charHeight) - pady);
480       }
481     }
482   }
483
484   public void drawCursor(SequenceI seq, int res, int x1, int y1)
485   {
486     int pady = av.charHeight / 5;
487     int charOffset = 0;
488     graphics.setColor(Color.black);
489     graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
490
491     if (av.validCharWidth)
492     {
493       graphics.setColor(Color.white);
494
495       char s = seq.getCharAt(res);
496
497       charOffset = (av.charWidth - fm.charWidth(s)) / 2;
498       graphics.drawString(String.valueOf(s), charOffset + x1,
499               (y1 + av.charHeight) - pady);
500     }
501
502   }
503 }