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