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