a4325fe0305d610ce8960ba9da8cbf8b52390a50
[jalview.git] / src / jalview / gui / SequenceRenderer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.gui;
19
20 import jalview.datamodel.AlignmentAnnotation;
21 import jalview.datamodel.SequenceGroup;
22 import jalview.datamodel.SequenceI;
23 import jalview.schemes.ColourSchemeI;
24
25 import java.awt.Color;
26 import java.awt.FontMetrics;
27 import java.awt.Graphics;
28
29 /**
30  * DOCUMENT ME!
31  * 
32  * @author $author$
33  * @version $Revision$
34  */
35 public class SequenceRenderer implements jalview.api.SequenceRenderer
36 {
37   AlignViewport av;
38
39   FontMetrics fm;
40
41   boolean renderGaps = true;
42
43   SequenceGroup currentSequenceGroup = null;
44
45   SequenceGroup[] allGroups = null;
46
47   Color resBoxColour;
48
49   Graphics graphics;
50
51   boolean monospacedFont;
52
53   boolean forOverview = false;
54
55   /**
56    * Creates a new SequenceRenderer object.
57    * 
58    * @param av
59    *          DOCUMENT ME!
60    */
61   public SequenceRenderer(AlignViewport av)
62   {
63     this.av = av;
64   }
65
66   /**
67    * DOCUMENT ME!
68    * 
69    * @param b
70    *          DOCUMENT ME!
71    */
72   public void prepare(Graphics g, boolean renderGaps)
73   {
74     graphics = g;
75     fm = g.getFontMetrics();
76
77     // If EPS graphics, stringWidth will be a double, not an int
78     double dwidth = fm.getStringBounds("M", g).getWidth();
79
80     monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && (float) av.charWidth == dwidth);
81
82     this.renderGaps = renderGaps;
83   }
84
85   public Color getResidueBoxColour(SequenceI seq, int i)
86   {
87     allGroups = av.getAlignment().findAllGroups(seq);
88
89     if (inCurrentSequenceGroup(i))
90     {
91       if (currentSequenceGroup.getDisplayBoxes())
92       {
93         getBoxColour(currentSequenceGroup.cs, seq, i);
94       }
95     }
96     else if (av.getShowBoxes())
97     {
98       getBoxColour(av.getGlobalColourScheme(), seq, i);
99     }
100
101     return resBoxColour;
102   }
103
104   /**
105    * DOCUMENT ME!
106    * 
107    * @param cs
108    *          DOCUMENT ME!
109    * @param seq
110    *          DOCUMENT ME!
111    * @param i
112    *          DOCUMENT ME!
113    */
114   void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
115   {
116     if (cs != null)
117     {
118       resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
119     }
120     else if (forOverview
121             && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
122     {
123       resBoxColour = Color.lightGray;
124     }
125     else
126     {
127       resBoxColour = Color.white;
128     }
129   }
130
131   /**
132    * DOCUMENT ME!
133    * 
134    * @param g
135    *          DOCUMENT ME!
136    * @param seq
137    *          DOCUMENT ME!
138    * @param sg
139    *          DOCUMENT ME!
140    * @param start
141    *          DOCUMENT ME!
142    * @param end
143    *          DOCUMENT ME!
144    * @param x1
145    *          DOCUMENT ME!
146    * @param y1
147    *          DOCUMENT ME!
148    * @param width
149    *          DOCUMENT ME!
150    * @param height
151    *          DOCUMENT ME!
152    */
153   public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
154           int end, int y1)
155   {
156     allGroups = sg;
157
158     drawBoxes(seq, start, end, y1);
159
160     if (av.validCharWidth)
161     {
162       drawText(seq, start, end, y1);
163     }
164   }
165
166   /**
167    * DOCUMENT ME!
168    * 
169    * @param seq
170    *          DOCUMENT ME!
171    * @param start
172    *          DOCUMENT ME!
173    * @param end
174    *          DOCUMENT ME!
175    * @param x1
176    *          DOCUMENT ME!
177    * @param y1
178    *          DOCUMENT ME!
179    * @param width
180    *          DOCUMENT ME!
181    * @param height
182    *          DOCUMENT ME!
183    */
184   public synchronized void drawBoxes(SequenceI seq, int start, int end,
185           int y1)
186   {
187     if (seq == null)
188       return; // fix for racecondition
189     int i = start;
190     int length = seq.getLength();
191
192     int curStart = -1;
193     int curWidth = av.charWidth;
194
195     Color tempColour = null;
196
197     while (i <= end)
198     {
199       resBoxColour = Color.white;
200
201       if (i < length)
202       {
203         if (inCurrentSequenceGroup(i))
204         {
205           if (currentSequenceGroup.getDisplayBoxes())
206           {
207             getBoxColour(currentSequenceGroup.cs, seq, i);
208           }
209         }
210         else if (av.getShowBoxes())
211         {
212           getBoxColour(av.getGlobalColourScheme(), seq, i);
213         }
214
215       }
216
217       if (resBoxColour != tempColour)
218       {
219         if (tempColour != null)
220         {
221           graphics.fillRect(av.charWidth * (curStart - start), y1,
222                   curWidth, av.charHeight);
223         }
224
225         graphics.setColor(resBoxColour);
226
227         curStart = i;
228         curWidth = av.charWidth;
229         tempColour = resBoxColour;
230       }
231       else
232       {
233         curWidth += av.charWidth;
234       }
235
236       i++;
237     }
238
239     graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
240             av.charHeight);
241
242   }
243
244   /**
245    * DOCUMENT ME!
246    * 
247    * @param seq
248    *          DOCUMENT ME!
249    * @param start
250    *          DOCUMENT ME!
251    * @param end
252    *          DOCUMENT ME!
253    * @param x1
254    *          DOCUMENT ME!
255    * @param y1
256    *          DOCUMENT ME!
257    * @param width
258    *          DOCUMENT ME!
259    * @param height
260    *          DOCUMENT ME!
261    */
262   public void drawText(SequenceI seq, int start, int end, int y1)
263   {
264     y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
265     int charOffset = 0;
266     char s;
267
268     if (end + 1 >= seq.getLength())
269     {
270       end = seq.getLength() - 1;
271     }
272     graphics.setColor(av.textColour);
273
274     if (monospacedFont && av.showText && allGroups.length == 0
275             && !av.getColourText() && av.thresholdTextColour == 0)
276     {
277       if (av.renderGaps)
278       {
279         graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
280       }
281       else
282       {
283         char gap = av.getGapCharacter();
284         graphics.drawString(seq.getSequenceAsString(start, end + 1)
285                 .replace(gap, ' '), 0, y1);
286       }
287     }
288     else
289     {
290       boolean getboxColour = false;
291       for (int i = start; i <= end; i++)
292       {
293         graphics.setColor(av.textColour);
294         getboxColour = false;
295         s = seq.getCharAt(i);
296         if (!renderGaps && jalview.util.Comparison.isGap(s))
297         {
298           continue;
299         }
300
301         if (inCurrentSequenceGroup(i))
302         {
303           if (!currentSequenceGroup.getDisplayText())
304           {
305             continue;
306           }
307
308           if (currentSequenceGroup.thresholdTextColour > 0
309                   || currentSequenceGroup.getColourText())
310           {
311             getboxColour = true;
312             getBoxColour(currentSequenceGroup.cs, seq, i);
313
314             if (currentSequenceGroup.getColourText())
315             {
316               graphics.setColor(resBoxColour.darker());
317             }
318
319             if (currentSequenceGroup.thresholdTextColour > 0)
320             {
321               if (resBoxColour.getRed() + resBoxColour.getBlue()
322                       + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
323               {
324                 graphics.setColor(currentSequenceGroup.textColour2);
325               }
326             }
327           }
328           else
329           {
330             graphics.setColor(currentSequenceGroup.textColour);
331           }
332           if (currentSequenceGroup.getShowNonconserved()) // todo optimize
333           {
334             // todo - use sequence group consensus
335             s = getDisplayChar(av.getAlignmentConsensusAnnotation(), i, s,
336                     '.');
337
338           }
339
340         }
341         else
342         {
343           if (!av.getShowText())
344           {
345             continue;
346           }
347
348           if (av.getColourText())
349           {
350             getboxColour = true;
351             getBoxColour(av.getGlobalColourScheme(), seq, i);
352
353             if (av.getShowBoxes())
354             {
355               graphics.setColor(resBoxColour.darker());
356             }
357             else
358             {
359               graphics.setColor(resBoxColour);
360             }
361           }
362
363           if (av.thresholdTextColour > 0)
364           {
365             if (!getboxColour)
366             {
367               getBoxColour(av.getGlobalColourScheme(), seq, i);
368             }
369
370             if (resBoxColour.getRed() + resBoxColour.getBlue()
371                     + resBoxColour.getGreen() < av.thresholdTextColour)
372             {
373               graphics.setColor(av.textColour2);
374             }
375           }
376           if (av.getShowUnconserved())
377           {
378             s = getDisplayChar(av.getAlignmentConsensusAnnotation(), i, s,
379                     '.');
380
381           }
382
383         }
384
385         charOffset = (av.charWidth - fm.charWidth(s)) / 2;
386         graphics.drawString(String.valueOf(s), charOffset + av.charWidth
387                 * (i - start), y1);
388
389       }
390     }
391   }
392
393   private char getDisplayChar(AlignmentAnnotation consensus, int position,
394           char s, char c)
395   {
396     char conschar = consensus.annotations[position].displayCharacter
397             .charAt(0);
398     if (conschar != '-' && s == conschar)
399     {
400       s = c;
401     }
402     return s;
403   }
404
405   /**
406    * DOCUMENT ME!
407    * 
408    * @param res
409    *          DOCUMENT ME!
410    * 
411    * @return DOCUMENT ME!
412    */
413   boolean inCurrentSequenceGroup(int res)
414   {
415     if (allGroups == null)
416     {
417       return false;
418     }
419
420     for (int i = 0; i < allGroups.length; i++)
421     {
422       if ((allGroups[i].getStartRes() <= res)
423               && (allGroups[i].getEndRes() >= res))
424       {
425         currentSequenceGroup = allGroups[i];
426
427         return true;
428       }
429     }
430
431     return false;
432   }
433
434   /**
435    * DOCUMENT ME!
436    * 
437    * @param seq
438    *          DOCUMENT ME!
439    * @param start
440    *          DOCUMENT ME!
441    * @param end
442    *          DOCUMENT ME!
443    * @param x1
444    *          DOCUMENT ME!
445    * @param y1
446    *          DOCUMENT ME!
447    * @param width
448    *          DOCUMENT ME!
449    * @param height
450    *          DOCUMENT ME!
451    */
452   public void drawHighlightedText(SequenceI seq, int start, int end,
453           int x1, int y1)
454   {
455     int pady = av.charHeight / 5;
456     int charOffset = 0;
457     graphics.setColor(Color.BLACK);
458     graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
459             av.charHeight);
460     graphics.setColor(Color.white);
461
462     char s = '~';
463
464     // Need to find the sequence position here.
465     if (av.validCharWidth)
466     {
467       for (int i = start; i <= end; i++)
468       {
469         if (i < seq.getLength())
470         {
471           s = seq.getCharAt(i);
472         }
473
474         charOffset = (av.charWidth - fm.charWidth(s)) / 2;
475         graphics.drawString(String.valueOf(s), charOffset + x1
476                 + (av.charWidth * (i - start)), (y1 + av.charHeight) - pady);
477       }
478     }
479   }
480
481   public void drawCursor(SequenceI seq, int res, int x1, int y1)
482   {
483     int pady = av.charHeight / 5;
484     int charOffset = 0;
485     graphics.setColor(Color.black);
486     graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
487
488     if (av.validCharWidth)
489     {
490       graphics.setColor(Color.white);
491
492       char s = seq.getCharAt(res);
493
494       charOffset = (av.charWidth - fm.charWidth(s)) / 2;
495       graphics.drawString(String.valueOf(s), charOffset + x1,
496               (y1 + av.charHeight) - pady);
497     }
498
499   }
500 }