Merge branch 'feature/2588' into merge/develop_feature/2588_JAL-2588
[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()
79             && av.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    * DOCUMENT ME!
106    * 
107    * @param g
108    *          DOCUMENT ME!
109    * @param seq
110    *          DOCUMENT ME!
111    * @param sg
112    *          DOCUMENT ME!
113    * @param start
114    *          DOCUMENT ME!
115    * @param end
116    *          DOCUMENT ME!
117    * @param x1
118    *          DOCUMENT ME!
119    * @param y1
120    *          DOCUMENT ME!
121    * @param width
122    *          DOCUMENT ME!
123    * @param height
124    *          DOCUMENT ME!
125    */
126   public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
127           int end, int y1)
128   {
129     allGroups = sg;
130
131     drawBoxes(seq, start, end, y1);
132
133     if (av.isValidCharWidth())
134     {
135       drawText(seq, start, end, y1);
136     }
137   }
138
139   /**
140    * DOCUMENT ME!
141    * 
142    * @param seq
143    *          DOCUMENT ME!
144    * @param start
145    *          DOCUMENT ME!
146    * @param end
147    *          DOCUMENT ME!
148    * @param x1
149    *          DOCUMENT ME!
150    * @param y1
151    *          DOCUMENT ME!
152    * @param width
153    *          DOCUMENT ME!
154    * @param height
155    *          DOCUMENT ME!
156    */
157   public synchronized void drawBoxes(SequenceI seq, int start, int end,
158           int y1)
159   {
160     Color resBoxColour = Color.white;
161
162     if (seq == null)
163     {
164       return; // fix for racecondition
165     }
166     int i = start;
167     int length = seq.getLength();
168
169     int curStart = -1;
170     int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(),
171             avHeight = av.getCharHeight();
172
173     Color tempColour = null;
174
175     while (i <= end)
176     {
177       resBoxColour = Color.white;
178
179       if (i < length)
180       {
181         SequenceGroup currentSequenceGroup = resColourFinder
182                 .getCurrentSequenceGroup(
183                 allGroups, i);
184         if (currentSequenceGroup != null)
185         {
186           if (currentSequenceGroup.getDisplayBoxes())
187           {
188             resBoxColour = resColourFinder.getBoxColour(
189                     currentSequenceGroup.getGroupColourScheme(), seq,
190                     i);
191           }
192         }
193         else if (av.getShowBoxes())
194         {
195           resBoxColour = resColourFinder
196                   .getBoxColour(av.getResidueShading(), seq, i);
197         }
198       }
199
200       if (resBoxColour != tempColour)
201       {
202         if (tempColour != null)
203         {
204           graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
205                   avHeight);
206         }
207
208         graphics.setColor(resBoxColour);
209
210         curStart = i;
211         curWidth = avWidth;
212         tempColour = resBoxColour;
213       }
214       else
215       {
216         curWidth += avWidth;
217       }
218
219       i++;
220     }
221
222     graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
223
224   }
225
226   /**
227    * DOCUMENT ME!
228    * 
229    * @param seq
230    *          DOCUMENT ME!
231    * @param start
232    *          DOCUMENT ME!
233    * @param end
234    *          DOCUMENT ME!
235    * @param x1
236    *          DOCUMENT ME!
237    * @param y1
238    *          DOCUMENT ME!
239    * @param width
240    *          DOCUMENT ME!
241    * @param height
242    *          DOCUMENT ME!
243    */
244   public void drawText(SequenceI seq, int start, int end, int y1)
245   {
246     y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
247     int charOffset = 0;
248     char s;
249
250     if (end + 1 >= seq.getLength())
251     {
252       end = seq.getLength() - 1;
253     }
254     graphics.setColor(av.getTextColour());
255
256     if (monospacedFont && av.getShowText() && allGroups.length == 0
257             && !av.getColourText() && av.getThresholdTextColour() == 0)
258     {
259       if (av.isRenderGaps())
260       {
261         graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
262       }
263       else
264       {
265         char gap = av.getGapCharacter();
266         graphics.drawString(
267                 seq.getSequenceAsString(start, end + 1).replace(gap, ' '),
268                 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
317                               .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()
411                     ? av.getAlignment().getSeqrep().getCharAt(position)
412                     : (currentGroup.getSeqrep() != null
413                             ? currentGroup.getSeqrep().getCharAt(position)
414                             : av.getAlignment().getSeqrep()
415                                     .getCharAt(position)))
416             : (currentGroup != null && currentGroup.getConsensus() != null
417                     && position >= currentGroup.getStartRes()
418                     && position <= currentGroup.getEndRes()
419                     && currentGroup
420                             .getConsensus().annotations.length > position)
421                                     ? currentGroup
422                                             .getConsensus().annotations[position].displayCharacter
423                                                     .charAt(0)
424                                     : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
425                                             .charAt(0);
426     if (!jalview.util.Comparison.isGap(conschar)
427             && (sequenceChar == conschar
428                     || sequenceChar + CHAR_TO_UPPER == conschar))
429     {
430       sequenceChar = conservedChar;
431     }
432     return sequenceChar;
433   }
434
435   /**
436    * DOCUMENT ME!
437    * 
438    * @param seq
439    *          DOCUMENT ME!
440    * @param start
441    *          DOCUMENT ME!
442    * @param end
443    *          DOCUMENT ME!
444    * @param x1
445    *          DOCUMENT ME!
446    * @param y1
447    *          DOCUMENT ME!
448    * @param width
449    *          DOCUMENT ME!
450    * @param height
451    *          DOCUMENT ME!
452    */
453   public void drawHighlightedText(SequenceI seq, int start, int end, int x1,
454           int y1)
455   {
456     int pady = av.getCharHeight() / 5;
457     int charOffset = 0;
458     graphics.setColor(Color.BLACK);
459     graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
460             av.getCharHeight());
461     graphics.setColor(Color.white);
462
463     char s = '~';
464
465     // Need to find the sequence position here.
466     if (av.isValidCharWidth())
467     {
468       for (int i = start; i <= end; i++)
469       {
470         if (i < seq.getLength())
471         {
472           s = seq.getCharAt(i);
473         }
474
475         charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
476         graphics.drawString(String.valueOf(s),
477                 charOffset + x1 + (av.getCharWidth() * (i - start)),
478                 (y1 + av.getCharHeight()) - pady);
479       }
480     }
481   }
482
483   public void drawCursor(SequenceI seq, int res, int x1, int y1)
484   {
485     int pady = av.getCharHeight() / 5;
486     int charOffset = 0;
487     graphics.setColor(Color.black);
488     graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
489
490     if (av.isValidCharWidth())
491     {
492       graphics.setColor(Color.white);
493
494       char s = seq.getCharAt(res);
495
496       charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
497       graphics.drawString(String.valueOf(s), charOffset + x1,
498               (y1 + av.getCharHeight()) - pady);
499     }
500
501   }
502 }