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