JAL-1807 - Bob's last(?) before leaving Dundee -- adds fast file loading
[jalviewjs.git] / src / jalview / appletgui / 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.appletgui;
22
23 import jalview.api.FeatureRenderer;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.schemes.ColourSchemeI;
27 import jalview.util.Comparison;
28
29 import java.awt.Color;
30 import java.awt.Font;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
33
34 public class SequenceRenderer implements jalview.api.SequenceRenderer
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 forOverview = false;
51
52   public SequenceRenderer(AlignViewport av)
53   {
54     this.av = av;
55   }
56
57   /**
58    * DOCUMENT ME!
59    * 
60    * @param b
61    *          DOCUMENT ME!
62    */
63   public void prepare(Graphics g, boolean renderGaps)
64   {
65     graphics = g;
66     fm = g.getFontMetrics();
67
68     this.renderGaps = renderGaps;
69   }
70
71   public Color getResidueBoxColour(SequenceI seq, int i)
72   {
73     allGroups = av.getAlignment().findAllGroups(seq);
74
75     if (inCurrentSequenceGroup(i))
76     {
77       if (currentSequenceGroup.getDisplayBoxes())
78       {
79         getBoxColour(currentSequenceGroup.cs, seq, i);
80       }
81     }
82     else if (av.getShowBoxes())
83     {
84       getBoxColour(av.getGlobalColourScheme(), seq, i);
85     }
86
87     return resBoxColour;
88   }
89
90   /**
91    * Get the residue colour at the given sequence position - as determined by
92    * the sequence group colour (if any), else the colour scheme, possibly
93    * overridden by a feature colour.
94    * 
95    * @param seq
96    * @param position
97    * @param fr
98    * @return
99    */
100   @Override
101   public Color getResidueColour(final SequenceI seq, int position,
102           FeatureRenderer fr)
103   {
104     // TODO replace 8 or so code duplications with calls to this method
105     // (refactored as needed)
106     Color col = getResidueBoxColour(seq, position);
107
108     if (fr != null)
109     {
110       col = fr.findFeatureColour(col, seq, position);
111     }
112     return col;
113   }
114
115   void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
116   {
117     if (cs != null)
118     {
119       resBoxColour = cs.findColourSeq(seq.getCharAt(i), i, seq);
120     }
121     else if (forOverview && !Comparison.isGap(seq.getCharAt(i)))
122     {
123       resBoxColour = Color.lightGray;
124     }
125     else
126     {
127       resBoxColour = Color.white;
128     }
129
130   }
131
132   public Color findSequenceColour(SequenceI seq, int i)
133   {
134     allGroups = av.getAlignment().findAllGroups(seq);
135     drawBoxes(seq, i, i, 0);
136     return resBoxColour;
137   }
138
139   public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
140           int end, int y1)
141   {
142     if (seq == null)
143     {
144       return;
145     }
146
147     allGroups = sg;
148
149     drawBoxes(seq, start, end, y1);
150
151     if (av.validCharWidth)
152     {
153       drawText(seq, start, end, y1);
154     }
155   }
156
157   public void drawBoxes(SequenceI seq, int start, int end, int y1)
158   {
159     int i = start;
160     int length = seq.getLength();
161
162     int curStart = -1;
163     int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(), avCharHeight = av
164             .getCharHeight();
165
166     Color tempColour = null;
167     while (i <= end)
168     {
169       resBoxColour = Color.white;
170       if (i < length)
171       {
172         if (inCurrentSequenceGroup(i))
173         {
174           if (currentSequenceGroup.getDisplayBoxes())
175           {
176             getBoxColour(currentSequenceGroup.cs, seq, i);
177           }
178         }
179         else if (av.getShowBoxes())
180         {
181           getBoxColour(av.getGlobalColourScheme(), seq, i);
182         }
183       }
184
185       if (resBoxColour != tempColour)
186       {
187         if (tempColour != null)
188         {
189           graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
190                   avCharHeight);
191         }
192         graphics.setColor(resBoxColour);
193
194         curStart = i;
195         curWidth = avCharWidth;
196         tempColour = resBoxColour;
197
198       }
199       else
200       {
201         curWidth += avCharWidth;
202       }
203
204       i++;
205     }
206
207     graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
208             avCharHeight);
209   }
210
211   public void drawText(SequenceI seq, int start, int end, int y1)
212   {
213     int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
214     Font boldFont = null;
215     boolean bold = false;
216     if (av.isUpperCasebold())
217     {
218       boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);
219
220       graphics.setFont(av.getFont());
221     }
222
223     y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady
224
225     int charOffset = 0;
226
227     // Need to find the sequence position here.
228     if (end + 1 >= seq.getLength())
229     {
230       end = seq.getLength() - 1;
231     }
232
233     char s = ' ';
234     boolean srep = av.isDisplayReferenceSeq();
235     for (int i = start; i <= end; i++)
236     {
237       graphics.setColor(Color.black);
238
239       s = seq.getCharAt(i);
240       if (!renderGaps && Comparison.isGap(s))
241       {
242         continue;
243       }
244
245       if (inCurrentSequenceGroup(i))
246       {
247         if (!currentSequenceGroup.getDisplayText())
248         {
249           continue;
250         }
251
252         if (currentSequenceGroup.getColourText())
253         {
254           getBoxColour(currentSequenceGroup.cs, seq, i);
255           graphics.setColor(resBoxColour.darker());
256         }
257         if (currentSequenceGroup.getShowNonconserved())
258         {
259           s = getDisplayChar(srep, i, s,
260                   '.');
261         }
262       }
263       else
264       {
265         if (!av.getShowText())
266         {
267           continue;
268         }
269
270         if (av.getColourText())
271         {
272           getBoxColour(av.getGlobalColourScheme(), seq, i);
273           if (av.getShowBoxes())
274           {
275             graphics.setColor(resBoxColour.darker());
276           }
277           else
278           {
279             graphics.setColor(resBoxColour);
280           }
281         }
282         if (av.getShowUnconserved())
283         {
284           s = getDisplayChar(srep, i, s,
285                   '.');
286
287         }
288       }
289
290       if (av.isUpperCasebold())
291       {
292         fm = graphics.getFontMetrics();
293         if ('A' <= s && s <= 'Z')
294         {
295           if (!bold)
296           {
297
298             graphics.setFont(boldFont);
299           }
300           bold = true;
301         }
302         else if (bold)
303         {
304           graphics.setFont(av.font);
305           bold = false;
306         }
307
308       }
309
310       charOffset = (avCharWidth - fm.charWidth(s)) / 2;
311       awt2swing.Util.drawString(graphics, String.valueOf(s), charOffset + avCharWidth
312               * (i - start), y1);
313     }
314
315   }
316
317   private char getDisplayChar(final boolean usesrep, int position,
318           char s, char c)
319   {
320     // TODO - use currentSequenceGroup rather than alignemnt 
321     // currentSequenceGroup.getConsensus()
322     char conschar = (usesrep) ? av.getAlignment().getSeqrep().getCharAt(position) : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
323             .charAt(0);
324     if (conschar != '-' && s == conschar)
325     {
326       s = c;
327     }
328     return s;
329   }
330
331   boolean inCurrentSequenceGroup(int res)
332   {
333     if (allGroups == null)
334     {
335       return false;
336     }
337
338     for (int i = 0; i < allGroups.length; i++)
339     {
340       if (allGroups[i].getStartRes() <= res
341               && allGroups[i].getEndRes() >= res)
342       {
343         currentSequenceGroup = allGroups[i];
344         return true;
345       }
346     }
347
348     return false;
349   }
350
351   public void drawHighlightedText(SequenceI seq, int start, int end,
352           int x1, int y1)
353   {
354     int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
355     int pady = avCharHeight / 5;
356     int charOffset = 0;
357     graphics.setColor(Color.black);
358     graphics.fillRect(x1, y1, avCharWidth * (end - start + 1), avCharHeight);
359     graphics.setColor(Color.white);
360
361     char s = '~';
362     // Need to find the sequence position here.
363     if (av.validCharWidth)
364     {
365       for (int i = start; i <= end; i++)
366       {
367         if (i < seq.getLength())
368         {
369           s = seq.getCharAt(i);
370         }
371
372         charOffset = (avCharWidth - fm.charWidth(s)) / 2;
373         awt2swing.Util.drawString(graphics, String.valueOf(s), charOffset + x1
374                 + avCharWidth * (i - start), y1 + avCharHeight - pady);
375       }
376     }
377   }
378
379   public void drawCursor(SequenceI seq, int res, int x1, int y1)
380   {
381     int pady = av.getCharHeight() / 5;
382     int charOffset = 0;
383     graphics.setColor(Color.black);
384     graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
385     graphics.setColor(Color.white);
386
387     graphics.setColor(Color.white);
388
389     char s = seq.getCharAt(res);
390     if (av.validCharWidth)
391     {
392
393       charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
394       awt2swing.Util.drawString(graphics, String.valueOf(s), charOffset + x1,
395               (y1 + av.getCharHeight()) - pady);
396     }
397   }
398
399 }