JAL-2588 Gap colour scheme now shown in overview
[jalview.git] / src / jalview / gui / 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.gui;
22
23 import jalview.api.AlignViewportI;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.renderer.ResidueShaderI;
27 import jalview.renderer.seqfeatures.FeatureColourFinder;
28 import jalview.util.Comparison;
29
30 import java.awt.Color;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
33
34 public class SequenceRenderer implements jalview.api.SequenceRenderer
35 {
36   final static int CHAR_TO_UPPER = 'A' - 'a';
37
38   AlignViewportI av;
39
40   FontMetrics fm;
41
42   boolean renderGaps = true;
43
44   SequenceGroup currentSequenceGroup = null;
45
46   SequenceGroup[] allGroups = null;
47
48   Color resBoxColour;
49
50   Graphics graphics;
51
52   boolean monospacedFont;
53
54   boolean forOverview = false;
55
56   /**
57    * Creates a new SequenceRenderer object
58    * 
59    * @param viewport
60    */
61   public SequenceRenderer(AlignViewportI viewport)
62   {
63     this.av = viewport;
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() && av
81             .getCharWidth() == dwidth);
82
83     this.renderGaps = renderGaps;
84   }
85
86   protected Color getResidueBoxColour(SequenceI seq, int i)
87   {
88     // rate limiting step when rendering overview for lots of groups
89     allGroups = av.getAlignment().findAllGroups(seq);
90
91     if (inCurrentSequenceGroup(i))
92     {
93       if (currentSequenceGroup.getDisplayBoxes())
94       {
95         getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
96       }
97     }
98     else if (av.getShowBoxes())
99     {
100       getBoxColour(av.getResidueShading(), seq, i);
101     }
102
103     return resBoxColour;
104   }
105
106   /**
107    * Get the residue colour at the given sequence position - as determined by
108    * the sequence group colour (if any), else the colour scheme, possibly
109    * overridden by a feature colour.
110    * 
111    * @param seq
112    * @param position
113    * @param finder
114    * @return
115    */
116   @Override
117   public Color getResidueColour(final SequenceI seq, int position,
118           FeatureColourFinder finder)
119   {
120     Color col = getResidueBoxColour(seq, position);
121
122     if (finder != null)
123     {
124       col = finder.findFeatureColour(col, seq, position);
125     }
126     return col;
127   }
128
129   /**
130    * DOCUMENT ME!
131    * 
132    * @param shader
133    *          DOCUMENT ME!
134    * @param seq
135    *          DOCUMENT ME!
136    * @param i
137    *          DOCUMENT ME!
138    */
139   void getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
140   {
141     if (shader.getColourScheme() != null)
142     {
143       if (forOverview && Comparison.isGap(seq.getCharAt(i))
144               && !shader.getColourScheme().hasGapColour())
145       {
146         resBoxColour = Color.lightGray;
147       }
148       else
149       {
150         resBoxColour = shader.findColour(seq.getCharAt(i),
151               i, seq);
152       }
153     }
154     else if (forOverview && Comparison.isGap(seq.getCharAt(i)))
155     {
156       resBoxColour = Color.lightGray;
157     }
158     else
159     {
160       resBoxColour = Color.white;
161     }
162   }
163
164   /**
165    * DOCUMENT ME!
166    * 
167    * @param g
168    *          DOCUMENT ME!
169    * @param seq
170    *          DOCUMENT ME!
171    * @param sg
172    *          DOCUMENT ME!
173    * @param start
174    *          DOCUMENT ME!
175    * @param end
176    *          DOCUMENT ME!
177    * @param x1
178    *          DOCUMENT ME!
179    * @param y1
180    *          DOCUMENT ME!
181    * @param width
182    *          DOCUMENT ME!
183    * @param height
184    *          DOCUMENT ME!
185    */
186   public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
187           int end, int y1)
188   {
189     allGroups = sg;
190
191     drawBoxes(seq, start, end, y1);
192
193     if (av.isValidCharWidth())
194     {
195       drawText(seq, start, end, y1);
196     }
197   }
198
199   /**
200    * DOCUMENT ME!
201    * 
202    * @param seq
203    *          DOCUMENT ME!
204    * @param start
205    *          DOCUMENT ME!
206    * @param end
207    *          DOCUMENT ME!
208    * @param x1
209    *          DOCUMENT ME!
210    * @param y1
211    *          DOCUMENT ME!
212    * @param width
213    *          DOCUMENT ME!
214    * @param height
215    *          DOCUMENT ME!
216    */
217   public synchronized void drawBoxes(SequenceI seq, int start, int end,
218           int y1)
219   {
220     if (seq == null)
221     {
222       return; // fix for racecondition
223     }
224     int i = start;
225     int length = seq.getLength();
226
227     int curStart = -1;
228     int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(), avHeight = av
229             .getCharHeight();
230
231     Color tempColour = null;
232
233     while (i <= end)
234     {
235       resBoxColour = Color.white;
236
237       if (i < length)
238       {
239         if (inCurrentSequenceGroup(i))
240         {
241           if (currentSequenceGroup.getDisplayBoxes())
242           {
243             getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
244                     i);
245           }
246         }
247         else if (av.getShowBoxes())
248         {
249           getBoxColour(av.getResidueShading(), seq, i);
250         }
251       }
252
253       if (resBoxColour != tempColour)
254       {
255         if (tempColour != null)
256         {
257           graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
258                   avHeight);
259         }
260
261         graphics.setColor(resBoxColour);
262
263         curStart = i;
264         curWidth = avWidth;
265         tempColour = resBoxColour;
266       }
267       else
268       {
269         curWidth += avWidth;
270       }
271
272       i++;
273     }
274
275     graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
276
277   }
278
279   /**
280    * DOCUMENT ME!
281    * 
282    * @param seq
283    *          DOCUMENT ME!
284    * @param start
285    *          DOCUMENT ME!
286    * @param end
287    *          DOCUMENT ME!
288    * @param x1
289    *          DOCUMENT ME!
290    * @param y1
291    *          DOCUMENT ME!
292    * @param width
293    *          DOCUMENT ME!
294    * @param height
295    *          DOCUMENT ME!
296    */
297   public void drawText(SequenceI seq, int start, int end, int y1)
298   {
299     y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
300     int charOffset = 0;
301     char s;
302
303     if (end + 1 >= seq.getLength())
304     {
305       end = seq.getLength() - 1;
306     }
307     graphics.setColor(av.getTextColour());
308
309     if (monospacedFont && av.getShowText() && allGroups.length == 0
310             && !av.getColourText() && av.getThresholdTextColour() == 0)
311     {
312       if (av.isRenderGaps())
313       {
314         graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
315       }
316       else
317       {
318         char gap = av.getGapCharacter();
319         graphics.drawString(seq.getSequenceAsString(start, end + 1)
320                 .replace(gap, ' '), 0, y1);
321       }
322     }
323     else
324     {
325       boolean srep = av.isDisplayReferenceSeq();
326       boolean getboxColour = false;
327       boolean isarep = av.getAlignment().getSeqrep() == seq;
328       boolean isgrep = currentSequenceGroup != null ? currentSequenceGroup
329               .getSeqrep() == seq : false;
330       char sr_c;
331       for (int i = start; i <= end; i++)
332       {
333
334         graphics.setColor(av.getTextColour());
335         getboxColour = false;
336         s = seq.getCharAt(i);
337
338         if (!renderGaps && jalview.util.Comparison.isGap(s))
339         {
340           continue;
341         }
342
343         if (inCurrentSequenceGroup(i))
344         {
345           if (!currentSequenceGroup.getDisplayText())
346           {
347             continue;
348           }
349
350           if (currentSequenceGroup.thresholdTextColour > 0
351                   || currentSequenceGroup.getColourText())
352           {
353             getboxColour = true;
354             getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
355                     i);
356
357             if (currentSequenceGroup.getColourText())
358             {
359               graphics.setColor(resBoxColour.darker());
360             }
361
362             if (currentSequenceGroup.thresholdTextColour > 0)
363             {
364               if (resBoxColour.getRed() + resBoxColour.getBlue()
365                       + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
366               {
367                 graphics.setColor(currentSequenceGroup.textColour2);
368               }
369             }
370           }
371           else
372           {
373             graphics.setColor(currentSequenceGroup.textColour);
374           }
375           if (!isarep && !isgrep
376                   && currentSequenceGroup.getShowNonconserved()) // todo
377                                                                  // optimize
378           {
379             // todo - use sequence group consensus
380             s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
381
382           }
383
384         }
385         else
386         {
387           if (!av.getShowText())
388           {
389             continue;
390           }
391
392           if (av.getColourText())
393           {
394             getboxColour = true;
395             getBoxColour(av.getResidueShading(), seq, i);
396
397             if (av.getShowBoxes())
398             {
399               graphics.setColor(resBoxColour.darker());
400             }
401             else
402             {
403               graphics.setColor(resBoxColour);
404             }
405           }
406
407           if (av.getThresholdTextColour() > 0)
408           {
409             if (!getboxColour)
410             {
411               getBoxColour(av.getResidueShading(), seq, i);
412             }
413
414             if (resBoxColour.getRed() + resBoxColour.getBlue()
415                     + resBoxColour.getGreen() < av.getThresholdTextColour())
416             {
417               graphics.setColor(av.getTextColour2());
418             }
419           }
420           if (!isarep && av.getShowUnconserved())
421           {
422             s = getDisplayChar(srep, i, s, '.', null);
423
424           }
425
426         }
427
428         charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
429         graphics.drawString(String.valueOf(s),
430                 charOffset + av.getCharWidth() * (i - start), y1);
431
432       }
433     }
434   }
435
436   /**
437    * Returns 'conservedChar' to represent the given position if the sequence
438    * character at that position is equal to the consensus (ignoring case), else
439    * returns the sequence character
440    * 
441    * @param usesrep
442    * @param position
443    * @param sequenceChar
444    * @param conservedChar
445    * @return
446    */
447   private char getDisplayChar(final boolean usesrep, int position,
448           char sequenceChar, char conservedChar, SequenceGroup currentGroup)
449   {
450     // TODO - use currentSequenceGroup rather than alignment
451     // currentSequenceGroup.getConsensus()
452     char conschar = (usesrep) ? (currentGroup == null
453             || position < currentGroup.getStartRes()
454             || position > currentGroup.getEndRes() ? av.getAlignment()
455             .getSeqrep().getCharAt(position)
456             : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
457                     .getCharAt(position) : av.getAlignment().getSeqrep()
458                     .getCharAt(position)))
459             : (currentGroup != null && currentGroup.getConsensus() != null
460                     && position >= currentGroup.getStartRes()
461                     && position <= currentGroup.getEndRes() && currentGroup
462                     .getConsensus().annotations.length > position) ? currentGroup
463                     .getConsensus().annotations[position].displayCharacter
464                     .charAt(0)
465                     : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
466                             .charAt(0);
467     if (!jalview.util.Comparison.isGap(conschar)
468             && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
469     {
470       sequenceChar = conservedChar;
471     }
472     return sequenceChar;
473   }
474
475   /**
476    * DOCUMENT ME!
477    * 
478    * @param res
479    *          DOCUMENT ME!
480    * 
481    * @return DOCUMENT ME!
482    */
483   boolean inCurrentSequenceGroup(int res)
484   {
485     if (allGroups == null)
486     {
487       return false;
488     }
489
490     for (int i = 0; i < allGroups.length; i++)
491     {
492       if ((allGroups[i].getStartRes() <= res)
493               && (allGroups[i].getEndRes() >= res))
494       {
495         currentSequenceGroup = allGroups[i];
496
497         return true;
498       }
499     }
500
501     return false;
502   }
503
504   /**
505    * DOCUMENT ME!
506    * 
507    * @param seq
508    *          DOCUMENT ME!
509    * @param start
510    *          DOCUMENT ME!
511    * @param end
512    *          DOCUMENT ME!
513    * @param x1
514    *          DOCUMENT ME!
515    * @param y1
516    *          DOCUMENT ME!
517    * @param width
518    *          DOCUMENT ME!
519    * @param height
520    *          DOCUMENT ME!
521    */
522   public void drawHighlightedText(SequenceI seq, int start, int end,
523           int x1, int y1)
524   {
525     int pady = av.getCharHeight() / 5;
526     int charOffset = 0;
527     graphics.setColor(Color.BLACK);
528     graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
529             av.getCharHeight());
530     graphics.setColor(Color.white);
531
532     char s = '~';
533
534     // Need to find the sequence position here.
535     if (av.isValidCharWidth())
536     {
537       for (int i = start; i <= end; i++)
538       {
539         if (i < seq.getLength())
540         {
541           s = seq.getCharAt(i);
542         }
543
544         charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
545         graphics.drawString(String.valueOf(s),
546                 charOffset + x1 + (av.getCharWidth() * (i - start)),
547                 (y1 + av.getCharHeight()) - pady);
548       }
549     }
550   }
551
552   public void drawCursor(SequenceI seq, int res, int x1, int y1)
553   {
554     int pady = av.getCharHeight() / 5;
555     int charOffset = 0;
556     graphics.setColor(Color.black);
557     graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
558
559     if (av.isValidCharWidth())
560     {
561       graphics.setColor(Color.white);
562
563       char s = seq.getCharAt(res);
564
565       charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
566       graphics.drawString(String.valueOf(s), charOffset + x1,
567               (y1 + av.getCharHeight()) - pady);
568     }
569
570   }
571 }