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