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