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