featuresAdded method introduced for consistency with application for notifying gui...
[jalview.git] / src / jalview / appletgui / FeatureRenderer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.appletgui;
20
21 import java.util.*;
22
23 import java.awt.*;
24
25 import jalview.datamodel.*;
26
27 /**
28  * DOCUMENT ME!
29  *
30  * @author $author$
31  * @version $Revision$
32  */
33 public class FeatureRenderer
34 {
35   AlignViewport av;
36
37   Hashtable featureColours = new Hashtable();
38
39   // A higher level for grouping features of a
40   // particular type
41   Hashtable featureGroups = null;
42
43   // Holds web links for feature groups and feature types
44   // in the form label|link
45   Hashtable featureLinks = null;
46
47   // This is actually an Integer held in the hashtable,
48   // Retrieved using the key feature type
49   Object currentColour;
50
51   String[] renderOrder;
52
53   FontMetrics fm;
54   int charOffset;
55
56   float transparency = 1f;
57
58   TransparencySetter transparencySetter = null;
59
60   /**
61    * Creates a new FeatureRenderer object.
62    *
63    * @param av DOCUMENT ME!
64    */
65   public FeatureRenderer(AlignViewport av)
66   {
67     this.av = av;
68
69     if (!System.getProperty("java.version").startsWith("1.1"))
70     {
71       transparencySetter = new TransparencySetter();
72     }
73   }
74
75   public void transferSettings(FeatureRenderer fr)
76   {
77     renderOrder = fr.renderOrder;
78     featureGroups = fr.featureGroups;
79     featureColours = fr.featureColours;
80     transparency = fr.transparency;
81   }
82
83   public Color findFeatureColour(Color initialCol, SequenceI seq, int i)
84   {
85     overview = true;
86     if (!av.showSequenceFeatures)
87     {
88       return initialCol;
89     }
90
91     lastSequence = seq;
92     sequenceFeatures = lastSequence.getSequenceFeatures();
93     if (sequenceFeatures == null)
94     {
95       return initialCol;
96     }
97
98     sfSize = sequenceFeatures.length;
99
100     if (jalview.util.Comparison.isGap(lastSequence.getCharAt(i)))
101     {
102       return Color.white;
103     }
104
105     currentColour = null;
106
107     drawSequence(null, lastSequence, lastSequence.findPosition(i), -1, -1);
108
109     if (currentColour == null)
110     {
111       return initialCol;
112     }
113
114     return new Color( ( (Integer) currentColour).intValue());
115   }
116
117   /**
118    * This is used by the Molecule Viewer to get the accurate colour
119    * of the rendered sequence
120    */
121   boolean overview = false;
122
123   int white = Color.white.getRGB();
124   public int findFeatureColour(int initialCol, int seqIndex, int column)
125   {
126     if (!av.showSequenceFeatures)
127     {
128       return initialCol;
129     }
130
131     if (seqIndex != lastSequenceIndex)
132     {
133       lastSequence = av.alignment.getSequenceAt(seqIndex);
134       lastSequenceIndex = seqIndex;
135       sequenceFeatures = lastSequence.getSequenceFeatures();
136       if (sequenceFeatures == null)
137       {
138         return initialCol;
139       }
140
141       sfSize = sequenceFeatures.length;
142     }
143
144     if (jalview.util.Comparison.isGap(lastSequence.getCharAt(column)))
145     {
146       return Color.white.getRGB();
147     }
148
149     currentColour = null;
150
151     drawSequence(null, lastSequence, lastSequence.findPosition(column), -1, -1);
152
153     if (currentColour == null)
154     {
155       return initialCol;
156     }
157
158     return ( (Integer) currentColour).intValue();
159   }
160
161   /**
162    * DOCUMENT ME!
163    *
164    * @param g DOCUMENT ME!
165    * @param seq DOCUMENT ME!
166    * @param sg DOCUMENT ME!
167    * @param start DOCUMENT ME!
168    * @param end DOCUMENT ME!
169    * @param x1 DOCUMENT ME!
170    * @param y1 DOCUMENT ME!
171    * @param width DOCUMENT ME!
172    * @param height DOCUMENT ME!
173    */
174   // String type;
175   // SequenceFeature sf;
176   int lastSequenceIndex = -1;
177   SequenceI lastSequence;
178   SequenceFeature[] sequenceFeatures;
179   int sfSize, sfindex, spos, epos;
180
181   public void drawSequence(Graphics g, SequenceI seq,
182                            int start, int end, int y1)
183   {
184     if (seq.getSequenceFeatures() == null
185         || seq.getSequenceFeatures().length == 0)
186     {
187       return;
188     }
189
190     if (transparencySetter != null && g != null)
191     {
192       transparencySetter.setTransparency(g, transparency);
193     }
194     
195     if (lastSequence == null || seq != lastSequence || sequenceFeatures!=seq.getSequenceFeatures())
196     {
197       lastSequence = seq;
198       sequenceFeatures = seq.getSequenceFeatures();
199       sfSize = sequenceFeatures.length;
200     }
201
202     if (av.featuresDisplayed == null || renderOrder == null)
203     {
204       findAllFeatures();
205       if (av.featuresDisplayed.size() < 1)
206       {
207         return;
208       }
209
210       sequenceFeatures = seq.getSequenceFeatures();
211       sfSize = sequenceFeatures.length;
212     }
213     if (!overview)
214     {
215       spos = lastSequence.findPosition(start);
216       epos = lastSequence.findPosition(end);
217       if (g != null)
218       {
219         fm = g.getFontMetrics();
220       }
221     }
222     String type;
223     for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
224     {
225       type = renderOrder[renderIndex];
226       if (!av.featuresDisplayed.containsKey(type))
227       {
228         continue;
229       }
230
231       // loop through all features in sequence to find
232       // current feature to render
233       for (sfindex = 0; sfindex < sfSize; sfindex++)
234       {
235         if (!sequenceFeatures[sfindex].type.equals(type))
236         {
237           continue;
238         }
239
240         if (featureGroups != null
241             && sequenceFeatures[sfindex].featureGroup != null
242             &&
243             featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)
244             &&
245             ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].
246                                             featureGroup)).
247             booleanValue())
248         {
249           continue;
250         }
251
252         if (!overview && (sequenceFeatures[sfindex].getBegin() > epos
253                           || sequenceFeatures[sfindex].getEnd() < spos))
254         {
255           continue;
256         }
257
258         if (overview)
259         {
260           if (sequenceFeatures[sfindex].begin <= start &&
261               sequenceFeatures[sfindex].end >= start)
262           {
263             currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex].
264                 type);
265           }
266
267         }
268         else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
269         {
270
271           renderFeature(g, seq,
272                         seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
273                         seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
274                         new Color( ( (Integer) av.featuresDisplayed.get(
275                             sequenceFeatures[sfindex].type)).intValue()),
276                         start, end, y1);
277           renderFeature(g, seq,
278                         seq.findIndex(sequenceFeatures[sfindex].end) - 1,
279                         seq.findIndex(sequenceFeatures[sfindex].end) - 1,
280                         new Color( ( (Integer) av.featuresDisplayed.get(
281                             sequenceFeatures[sfindex].type)).intValue()),
282                         start, end, y1);
283
284         }
285         else
286         {
287           renderFeature(g, seq,
288                         seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
289                         seq.findIndex(sequenceFeatures[sfindex].end) - 1,
290                         getColour(sequenceFeatures[sfindex].type),
291                         start, end, y1);
292         }
293
294       }
295     }
296
297     if (transparencySetter != null && g != null)
298     {
299       transparencySetter.setTransparency(g, 1.0f);
300     }
301   }
302
303   char s;
304   int i;
305   void renderFeature(Graphics g, SequenceI seq,
306                      int fstart, int fend, Color featureColour, int start,
307                      int end, int y1)
308   {
309
310     if ( ( (fstart <= end) && (fend >= start)))
311     {
312       if (fstart < start)
313       { // fix for if the feature we have starts before the sequence start,
314         fstart = start; // but the feature end is still valid!!
315       }
316
317       if (fend >= end)
318       {
319         fend = end;
320       }
321
322       for (i = fstart; i <= fend; i++)
323       {
324         s = seq.getCharAt(i);
325
326         if (jalview.util.Comparison.isGap(s))
327         {
328           continue;
329         }
330
331         g.setColor(featureColour);
332
333         g.fillRect( (i - start) * av.charWidth, y1, av.charWidth, av.charHeight);
334
335         if (!av.validCharWidth)
336         {
337           continue;
338         }
339
340         g.setColor(Color.white);
341         charOffset = (av.charWidth - fm.charWidth(s)) / 2;
342         g.drawString(String.valueOf(s),
343                      charOffset + (av.charWidth * (i - start)),
344                      (y1 + av.charHeight) - av.charHeight / 5); //pady = height / 5;
345
346       }
347     }
348   }
349
350   void findAllFeatures()
351   {
352     jalview.schemes.UserColourScheme ucs = new
353         jalview.schemes.UserColourScheme();
354     
355     av.featuresDisplayed = new Hashtable();
356     Vector allfeatures = new Vector();
357     for (int i = 0; i < av.alignment.getHeight(); i++)
358     {
359       SequenceFeature[] features = av.alignment.getSequenceAt(i).
360           getSequenceFeatures();
361
362       if (features == null)
363       {
364         continue;
365       }
366
367       int index = 0;
368       while (index < features.length)
369       {
370         if (!av.featuresDisplayed.containsKey(features[index].getType()))
371         {
372           if (getColour(features[index].getType()) == null)
373           {
374             featureColours.put(features[index].getType(),
375                                ucs.createColourFromName(features[index].
376                 getType()));
377           }
378
379           av.featuresDisplayed.put(features[index].getType(),
380                                    new Integer(getColour(features[index].
381               getType()).getRGB()));
382           allfeatures.addElement(features[index].getType());
383         }
384         index++;
385       }
386     }
387
388     renderOrder = new String[allfeatures.size()];
389     Enumeration en = allfeatures.elements();
390     int i = allfeatures.size() - 1;
391     while (en.hasMoreElements())
392     {
393       renderOrder[i] = en.nextElement().toString();
394       i--;
395     }
396   }
397
398   public Color getColour(String featureType)
399   {
400     return (Color) featureColours.get(featureType);
401   }
402
403   public void addNewFeature(String name, Color col)
404   {
405
406     setColour(name, col);
407     if (av.featuresDisplayed == null)
408     {
409       av.featuresDisplayed = new Hashtable();
410     }
411
412     av.featuresDisplayed.put(name, "NOGROUP");
413   }
414
415   public void setColour(String featureType, Color col)
416   {
417     featureColours.put(featureType, col);
418   }
419
420   public void setFeaturePriority(Object[][] data)
421   {
422     // The feature table will display high priority
423     // features at the top, but theses are the ones
424     // we need to render last, so invert the data
425     if (av.featuresDisplayed != null)
426     {
427       av.featuresDisplayed.clear();
428     }
429
430     renderOrder = new String[data.length];
431
432     if (data.length > 0)
433     {
434       for (int i = 0; i < data.length; i++)
435       {
436         String type = data[i][0].toString();
437         setColour(type, (Color) data[i][1]);
438         if ( ( (Boolean) data[i][2]).booleanValue())
439         {
440           av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));
441         }
442
443         renderOrder[data.length - i - 1] = type;
444       }
445     }
446   }
447
448   public void featuresAdded()
449   {
450     findAllFeatures();
451   }
452 }
453
454 class TransparencySetter
455 {
456   void setTransparency(Graphics g, float value)
457   {
458      Graphics2D g2 = (Graphics2D) g;
459      g2.setComposite(
460         AlphaComposite.getInstance(
461              AlphaComposite.SRC_OVER, value));
462   }
463 }