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