JAL-2094 first pass with jalview.api.ColorI interface
[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.ColorI;
24 import jalview.api.FeatureRenderer;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.schemes.Colour;
28 import jalview.schemes.ColourSchemeI;
29 import jalview.util.ColorUtils;
30
31 import java.awt.Color;
32 import java.awt.FontMetrics;
33 import java.awt.Graphics;
34
35 /**
36  * DOCUMENT ME!
37  * 
38  * @author $author$
39  * @version $Revision$
40  */
41 public class SequenceRenderer implements jalview.api.SequenceRenderer
42 {
43   final static int CHAR_TO_UPPER = 'A' - 'a';
44
45   AlignViewport av;
46
47   FontMetrics fm;
48
49   boolean renderGaps = true;
50
51   SequenceGroup currentSequenceGroup = null;
52
53   SequenceGroup[] allGroups = null;
54
55   Color resBoxColour;
56
57   Graphics graphics;
58
59   boolean monospacedFont;
60
61   boolean forOverview = false;
62
63   /**
64    * Creates a new SequenceRenderer object.
65    * 
66    * @param av
67    *          DOCUMENT ME!
68    */
69   public SequenceRenderer(AlignViewport av)
70   {
71     this.av = av;
72   }
73
74   /**
75    * DOCUMENT ME!
76    * 
77    * @param b
78    *          DOCUMENT ME!
79    */
80   public void prepare(Graphics g, boolean renderGaps)
81   {
82     graphics = g;
83     fm = g.getFontMetrics();
84
85     // If EPS graphics, stringWidth will be a double, not an int
86     double dwidth = fm.getStringBounds("M", g).getWidth();
87
88     monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && av
89             .getCharWidth() == dwidth);
90
91     this.renderGaps = renderGaps;
92   }
93
94   @Override
95   public ColorI getResidueBoxColour(SequenceI seq, int i)
96   {
97     allGroups = av.getAlignment().findAllGroups(seq);
98
99     if (inCurrentSequenceGroup(i))
100     {
101       if (currentSequenceGroup.getDisplayBoxes())
102       {
103         getBoxColour(currentSequenceGroup.cs, seq, i);
104       }
105     }
106     else if (av.getShowBoxes())
107     {
108       getBoxColour(av.getGlobalColourScheme(), seq, i);
109     }
110
111     return new Colour(resBoxColour);
112   }
113
114   /**
115    * Get the residue colour at the given sequence position - as determined by
116    * the sequence group colour (if any), else the colour scheme, possibly
117    * overridden by a feature colour.
118    * 
119    * @param seq
120    * @param position
121    * @param fr
122    * @return
123    */
124   @Override
125   public ColorI getResidueColour(final SequenceI seq, int position,
126           FeatureRenderer fr)
127   {
128     // TODO replace 8 or so code duplications with calls to this method
129     // (refactored as needed)
130     ColorI col = getResidueBoxColour(seq, position);
131
132     if (fr != null)
133     {
134       col = fr.findFeatureColour(col, seq, position);
135     }
136     return col;
137   }
138
139   /**
140    * DOCUMENT ME!
141    * 
142    * @param cs
143    *          DOCUMENT ME!
144    * @param seq
145    *          DOCUMENT ME!
146    * @param i
147    *          DOCUMENT ME!
148    */
149   void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
150   {
151     if (cs != null)
152     {
153       resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
154     }
155     else if (forOverview
156             && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
157     {
158       resBoxColour = Color.lightGray;
159     }
160     else
161     {
162       resBoxColour = Color.white;
163     }
164   }
165
166   /**
167    * DOCUMENT ME!
168    * 
169    * @param g
170    *          DOCUMENT ME!
171    * @param seq
172    *          DOCUMENT ME!
173    * @param sg
174    *          DOCUMENT ME!
175    * @param start
176    *          DOCUMENT ME!
177    * @param end
178    *          DOCUMENT ME!
179    * @param x1
180    *          DOCUMENT ME!
181    * @param y1
182    *          DOCUMENT ME!
183    * @param width
184    *          DOCUMENT ME!
185    * @param height
186    *          DOCUMENT ME!
187    */
188   public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
189           int end, int y1)
190   {
191     allGroups = sg;
192
193     drawBoxes(seq, start, end, y1);
194
195     if (av.validCharWidth)
196     {
197       drawText(seq, start, end, y1);
198     }
199   }
200
201   /**
202    * DOCUMENT ME!
203    * 
204    * @param seq
205    *          DOCUMENT ME!
206    * @param start
207    *          DOCUMENT ME!
208    * @param end
209    *          DOCUMENT ME!
210    * @param x1
211    *          DOCUMENT ME!
212    * @param y1
213    *          DOCUMENT ME!
214    * @param width
215    *          DOCUMENT ME!
216    * @param height
217    *          DOCUMENT ME!
218    */
219   public synchronized void drawBoxes(SequenceI seq, int start, int end,
220           int y1)
221   {
222     if (seq == null)
223     {
224       return; // fix for racecondition
225     }
226     int i = start;
227     int length = seq.getLength();
228
229     int curStart = -1;
230     int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(), avHeight = av
231             .getCharHeight();
232
233     Color tempColour = null;
234
235     while (i <= end)
236     {
237       resBoxColour = Color.white;
238
239       if (i < length)
240       {
241         if (inCurrentSequenceGroup(i))
242         {
243           if (currentSequenceGroup.getDisplayBoxes())
244           {
245             getBoxColour(currentSequenceGroup.cs, seq, i);
246           }
247         }
248         else if (av.getShowBoxes())
249         {
250           getBoxColour(av.getGlobalColourScheme(), seq, i);
251         }
252
253       }
254
255       if (resBoxColour != tempColour)
256       {
257         if (tempColour != null)
258         {
259           graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
260                   avHeight);
261         }
262
263         graphics.setColor(resBoxColour);
264
265         curStart = i;
266         curWidth = avWidth;
267         tempColour = resBoxColour;
268       }
269       else
270       {
271         curWidth += avWidth;
272       }
273
274       i++;
275     }
276
277     graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
278
279   }
280
281   /**
282    * DOCUMENT ME!
283    * 
284    * @param seq
285    *          DOCUMENT ME!
286    * @param start
287    *          DOCUMENT ME!
288    * @param end
289    *          DOCUMENT ME!
290    * @param x1
291    *          DOCUMENT ME!
292    * @param y1
293    *          DOCUMENT ME!
294    * @param width
295    *          DOCUMENT ME!
296    * @param height
297    *          DOCUMENT ME!
298    */
299   public void drawText(SequenceI seq, int start, int end, int y1)
300   {
301     y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
302     int charOffset = 0;
303     char s;
304
305     if (end + 1 >= seq.getLength())
306     {
307       end = seq.getLength() - 1;
308     }
309     graphics.setColor(ColorUtils.getColor(av.getTextColour()));
310
311     if (monospacedFont && av.getShowText() && allGroups.length == 0
312             && !av.getColourText() && av.getThresholdTextColour() == 0)
313     {
314       if (av.isRenderGaps())
315       {
316         graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
317       }
318       else
319       {
320         char gap = av.getGapCharacter();
321         graphics.drawString(seq.getSequenceAsString(start, end + 1)
322                 .replace(gap, ' '), 0, y1);
323       }
324     }
325     else
326     {
327       boolean srep = av.isDisplayReferenceSeq();
328       boolean getboxColour = false;
329       boolean isarep = av.getAlignment().getSeqrep() == seq;
330       boolean isgrep = currentSequenceGroup != null ? currentSequenceGroup
331               .getSeqrep() == seq : false;
332       char sr_c;
333       for (int i = start; i <= end; i++)
334       {
335
336         graphics.setColor(ColorUtils.getColor(av.getTextColour()));
337         getboxColour = false;
338         s = seq.getCharAt(i);
339
340         if (!renderGaps && jalview.util.Comparison.isGap(s))
341         {
342           continue;
343         }
344
345         if (inCurrentSequenceGroup(i))
346         {
347           if (!currentSequenceGroup.getDisplayText())
348           {
349             continue;
350           }
351
352           if (currentSequenceGroup.thresholdTextColour > 0
353                   || currentSequenceGroup.getColourText())
354           {
355             getboxColour = true;
356             getBoxColour(currentSequenceGroup.cs, seq, i);
357
358             if (currentSequenceGroup.getColourText())
359             {
360               graphics.setColor(resBoxColour.darker());
361             }
362
363             if (currentSequenceGroup.thresholdTextColour > 0)
364             {
365               if (resBoxColour.getRed() + resBoxColour.getBlue()
366                       + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
367               {
368                 graphics.setColor(ColorUtils.getColor(currentSequenceGroup.textColour2));
369               }
370             }
371           }
372           else
373           {
374             graphics.setColor(ColorUtils.getColor(currentSequenceGroup.textColour));
375           }
376           if (!isarep && !isgrep
377                   && currentSequenceGroup.getShowNonconserved()) // todo
378                                                                  // optimize
379           {
380             // todo - use sequence group consensus
381             s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
382
383           }
384
385         }
386         else
387         {
388           if (!av.getShowText())
389           {
390             continue;
391           }
392
393           if (av.getColourText())
394           {
395             getboxColour = true;
396             getBoxColour(av.getGlobalColourScheme(), seq, i);
397
398             if (av.getShowBoxes())
399             {
400               graphics.setColor(resBoxColour.darker());
401             }
402             else
403             {
404               graphics.setColor(resBoxColour);
405             }
406           }
407
408           if (av.getThresholdTextColour() > 0)
409           {
410             if (!getboxColour)
411             {
412               getBoxColour(av.getGlobalColourScheme(), seq, i);
413             }
414
415             if (resBoxColour.getRed() + resBoxColour.getBlue()
416                     + resBoxColour.getGreen() < av.getThresholdTextColour())
417             {
418               graphics.setColor(ColorUtils.getColor(av.getTextColour2()));
419             }
420           }
421           if (!isarep && av.getShowUnconserved())
422           {
423             s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
424
425           }
426
427         }
428
429         charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
430         graphics.drawString(String.valueOf(s),
431                 charOffset + av.getCharWidth() * (i - start), y1);
432
433       }
434     }
435   }
436
437   /**
438    * Returns 'conservedChar' to represent the given position if the sequence
439    * character at that position is equal to the consensus (ignoring case), else
440    * returns the sequence character
441    * 
442    * @param usesrep
443    * @param position
444    * @param sequenceChar
445    * @param conservedChar
446    * @return
447    */
448   private char getDisplayChar(final boolean usesrep, int position,
449           char sequenceChar, char conservedChar, SequenceGroup currentGroup)
450   {
451     // TODO - use currentSequenceGroup rather than alignment
452     // currentSequenceGroup.getConsensus()
453     char conschar = (usesrep) ? (currentGroup == null ? av.getAlignment()
454             .getSeqrep().getCharAt(position)
455             : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
456                     .getCharAt(position) : av.getAlignment().getSeqrep()
457                     .getCharAt(position)))
458             : (currentGroup != null && currentGroup.getConsensus() != null) ? 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 }