JAL-1978 quick hack to prototype msa grid by shrinking coloured box background for...
[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.ColourSchemeI;
27
28 import java.awt.Color;
29 import java.awt.FontMetrics;
30 import java.awt.Graphics;
31
32 /**
33  * DOCUMENT ME!
34  * 
35  * @author $author$
36  * @version $Revision$
37  */
38 public class SequenceRenderer implements jalview.api.SequenceRenderer
39 {
40   final static int CHAR_TO_UPPER = 'A' - 'a';
41
42   AlignViewport av;
43
44   FontMetrics fm;
45
46   boolean renderGaps = true;
47
48   SequenceGroup currentSequenceGroup = null;
49
50   SequenceGroup[] allGroups = null;
51
52   Color resBoxColour;
53
54   Graphics graphics;
55
56   boolean monospacedFont;
57
58   boolean forOverview = false;
59
60   boolean v_inset = true, h_inset = true;
61
62   /**
63    * Creates a new SequenceRenderer object.
64    * 
65    * @param av
66    *          DOCUMENT ME!
67    */
68   public SequenceRenderer(AlignViewport av)
69   {
70     this.av = av;
71   }
72
73   /**
74    * DOCUMENT ME!
75    * 
76    * @param b
77    *          DOCUMENT ME!
78    */
79   public void prepare(Graphics g, boolean renderGaps)
80   {
81     graphics = g;
82     fm = g.getFontMetrics();
83
84     // If EPS graphics, stringWidth will be a double, not an int
85     double dwidth = fm.getStringBounds("M", g).getWidth();
86
87     monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && av
88             .getCharWidth() == dwidth);
89
90     this.renderGaps = renderGaps;
91   }
92
93   @Override
94   public Color getResidueBoxColour(SequenceI seq, int i)
95   {
96     allGroups = av.getAlignment().findAllGroups(seq);
97
98     if (inCurrentSequenceGroup(i))
99     {
100       if (currentSequenceGroup.getDisplayBoxes())
101       {
102         getBoxColour(currentSequenceGroup.cs, seq, i);
103       }
104     }
105     else if (av.getShowBoxes())
106     {
107       getBoxColour(av.getGlobalColourScheme(), seq, i);
108     }
109
110     return resBoxColour;
111   }
112
113   /**
114    * Get the residue colour at the given sequence position - as determined by
115    * the sequence group colour (if any), else the colour scheme, possibly
116    * overridden by a feature colour.
117    * 
118    * @param seq
119    * @param position
120    * @param fr
121    * @return
122    */
123   @Override
124   public Color getResidueColour(final SequenceI seq, int position,
125           FeatureRenderer fr)
126   {
127     // TODO replace 8 or so code duplications with calls to this method
128     // (refactored as needed)
129     Color col = getResidueBoxColour(seq, position);
130
131     if (fr != null)
132     {
133       col = fr.findFeatureColour(col, seq, position);
134     }
135     return col;
136   }
137
138   /**
139    * DOCUMENT ME!
140    * 
141    * @param cs
142    *          DOCUMENT ME!
143    * @param seq
144    *          DOCUMENT ME!
145    * @param i
146    *          DOCUMENT ME!
147    */
148   void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
149   {
150     if (cs != null)
151     {
152       resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
153     }
154     else if (forOverview
155             && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
156     {
157       resBoxColour = Color.lightGray;
158     }
159     else
160     {
161       resBoxColour = Color.white;
162     }
163   }
164
165   /**
166    * DOCUMENT ME!
167    * 
168    * @param g
169    *          DOCUMENT ME!
170    * @param seq
171    *          DOCUMENT ME!
172    * @param sg
173    *          DOCUMENT ME!
174    * @param start
175    *          DOCUMENT ME!
176    * @param end
177    *          DOCUMENT ME!
178    * @param x1
179    *          DOCUMENT ME!
180    * @param y1
181    *          DOCUMENT ME!
182    * @param width
183    *          DOCUMENT ME!
184    * @param height
185    *          DOCUMENT ME!
186    */
187   public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
188           int end, int y1)
189   {
190     allGroups = sg;
191
192     drawBoxes(seq, start, end, y1);
193
194     if (av.validCharWidth)
195     {
196       drawText(seq, start, end, y1);
197     }
198   }
199
200   /**
201    * DOCUMENT ME!
202    * 
203    * @param seq
204    *          DOCUMENT ME!
205    * @param start
206    *          DOCUMENT ME!
207    * @param end
208    *          DOCUMENT ME!
209    * @param x1
210    *          DOCUMENT ME!
211    * @param y1
212    *          DOCUMENT ME!
213    * @param width
214    *          DOCUMENT ME!
215    * @param height
216    *          DOCUMENT ME!
217    */
218   public synchronized void drawBoxes(SequenceI seq, int start, int end,
219           int y1)
220   {
221     if (seq == null)
222     {
223       return; // fix for racecondition
224     }
225     int i = start;
226     int length = seq.getLength();
227
228     int curStart = -1;
229     int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(), avHeight = av
230             .getCharHeight();
231
232     Color tempColour = null;
233     int linset = (h_inset ? 1 : 0), vinset = (v_inset ? 1 : 0);
234     while (i <= end)
235     {
236       resBoxColour = Color.white;
237
238       if (i < length)
239       {
240         if (inCurrentSequenceGroup(i))
241         {
242           if (currentSequenceGroup.getDisplayBoxes())
243           {
244             getBoxColour(currentSequenceGroup.cs, seq, i);
245           }
246         }
247         else if (av.getShowBoxes())
248         {
249           getBoxColour(av.getGlobalColourScheme(), seq, i);
250         }
251
252       }
253
254       if (true) // resBoxColour != tempColour)
255       {
256         if (tempColour != null)
257         {
258           graphics.fillRect(avWidth * (curStart - start), y1, curWidth
259                   - linset, avHeight - vinset);
260         }
261
262         graphics.setColor(resBoxColour);
263
264         curStart = i;
265         curWidth = avWidth;
266         tempColour = resBoxColour;
267       }
268       else
269       {
270         curWidth += avWidth;
271       }
272
273       i++;
274     }
275
276     graphics.fillRect(avWidth * (curStart - start), y1, curWidth - linset,
277             avHeight - vinset);
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(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(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(currentSequenceGroup.textColour2);
369               }
370             }
371           }
372           else
373           {
374             graphics.setColor(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(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 }