Show hide cols and rows
[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     }\r
82 \r
83 \r
84     public Color findFeatureColour(Color initialCol, SequenceI seq, int i)\r
85     {\r
86       overview = true;\r
87       if(!av.showSequenceFeatures)\r
88         return initialCol;\r
89 \r
90         lastSequence = seq;\r
91         sequenceFeatures = lastSequence.getSequenceFeatures();\r
92         if(sequenceFeatures==null)\r
93           return initialCol;\r
94 \r
95         sfSize = sequenceFeatures.length;\r
96 \r
97       if(jalview.util.Comparison.isGap(lastSequence.getCharAt(i)))\r
98         return Color.white;\r
99 \r
100       currentColour = null;\r
101 \r
102       drawSequence(null, lastSequence, lastSequence.findPosition(i), -1,-1, -1, -1);\r
103 \r
104       if(currentColour==null)\r
105         return initialCol;\r
106 \r
107       return new Color( ((Integer)currentColour).intValue() );\r
108     }\r
109 \r
110     /**\r
111      * This is used by the Molecule Viewer to get the accurate colour\r
112      * of the rendered sequence\r
113      */\r
114     boolean overview = false;\r
115 \r
116     int white = Color.white.getRGB();\r
117     public int findFeatureColour(int initialCol, int seqIndex, int column)\r
118     {\r
119       if(!av.showSequenceFeatures)\r
120         return initialCol;\r
121 \r
122       if(seqIndex!=lastSequenceIndex)\r
123       {\r
124         lastSequence = av.alignment.getSequenceAt(seqIndex);\r
125         lastSequenceIndex = seqIndex;\r
126         sequenceFeatures = lastSequence.getSequenceFeatures();\r
127         if(sequenceFeatures==null)\r
128           return initialCol;\r
129 \r
130         sfSize = sequenceFeatures.length;\r
131       }\r
132 \r
133 \r
134       if(jalview.util.Comparison.isGap(lastSequence.getCharAt(column)))\r
135         return Color.white.getRGB();\r
136 \r
137       currentColour = null;\r
138 \r
139       drawSequence(null, lastSequence, lastSequence.findPosition(column), -1,-1, -1, -1);\r
140 \r
141       if(currentColour==null)\r
142         return initialCol;\r
143 \r
144       return  ((Integer)currentColour).intValue();\r
145     }\r
146 \r
147 \r
148     /**\r
149      * DOCUMENT ME!\r
150      *\r
151      * @param g DOCUMENT ME!\r
152      * @param seq DOCUMENT ME!\r
153      * @param sg DOCUMENT ME!\r
154      * @param start DOCUMENT ME!\r
155      * @param end DOCUMENT ME!\r
156      * @param x1 DOCUMENT ME!\r
157      * @param y1 DOCUMENT ME!\r
158      * @param width DOCUMENT ME!\r
159      * @param height DOCUMENT ME!\r
160      */\r
161    // String type;\r
162    // SequenceFeature sf;\r
163     int lastSequenceIndex=-1;\r
164     SequenceI lastSequence;\r
165     SequenceFeature [] sequenceFeatures;\r
166     int sfSize, sfindex, spos, epos;\r
167 \r
168     public void drawSequence(Graphics g, SequenceI seq,\r
169                              int start, int end, int y1, int width, int height)\r
170     {\r
171       if (   seq.getSequenceFeatures() == null\r
172           || seq.getSequenceFeatures().length==0)\r
173         return;\r
174 \r
175       if(transparencySetter!=null && g!=null)\r
176       {\r
177         transparencySetter.setTransparency(g, transparency);\r
178       }\r
179 \r
180       if (av.featuresDisplayed == null || renderOrder==null)\r
181        {\r
182          findAllFeatures();\r
183          if(av.featuresDisplayed.size()<1)\r
184            return;\r
185 \r
186          sequenceFeatures = seq.getSequenceFeatures();\r
187          sfSize = sequenceFeatures.length;\r
188        }\r
189        if(lastSequence==null || seq!=lastSequence)\r
190       {\r
191         lastSequence = seq;\r
192         sequenceFeatures = seq.getSequenceFeatures();\r
193         sfSize = sequenceFeatures.length;\r
194       }\r
195       if(!overview)\r
196       {\r
197         spos = lastSequence.findPosition(start);\r
198         epos = lastSequence.findPosition(end);\r
199         if(g!=null)\r
200           fm = g.getFontMetrics();\r
201       }\r
202       String type;\r
203       for(int renderIndex=0; renderIndex<renderOrder.length; renderIndex++)\r
204        {\r
205         type =  renderOrder[renderIndex];\r
206         if(!av.featuresDisplayed.containsKey(type))\r
207           continue;\r
208 \r
209         // loop through all features in sequence to find\r
210         // current feature to render\r
211         for (sfindex = 0; sfindex < sfSize; sfindex++)\r
212         {\r
213           if (!sequenceFeatures[sfindex].type.equals(type))\r
214             continue;\r
215 \r
216           if (featureGroups != null\r
217               && sequenceFeatures[sfindex].featureGroup != null\r
218               &&\r
219               featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)\r
220               &&\r
221               ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].featureGroup)).\r
222               booleanValue())\r
223           {\r
224             continue;\r
225           }\r
226 \r
227           if (!overview && (sequenceFeatures[sfindex].getBegin() > epos\r
228                             || sequenceFeatures[sfindex].getEnd() < spos))\r
229             continue;\r
230 \r
231           if (overview)\r
232           {\r
233             if (sequenceFeatures[sfindex].begin <= start &&\r
234                 sequenceFeatures[sfindex].end >= start)\r
235             {\r
236               currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex].\r
237                   type);\r
238             }\r
239 \r
240           }\r
241           else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))\r
242           {\r
243 \r
244             renderFeature(g, seq,\r
245                           seq.findIndex(sequenceFeatures[sfindex].begin) - 1,\r
246                           seq.findIndex(sequenceFeatures[sfindex].begin) - 1,\r
247                           new Color( ( (Integer) av.featuresDisplayed.get(\r
248                 sequenceFeatures[sfindex].type)).intValue()),\r
249                           start, end, y1, width, height);\r
250             renderFeature(g, seq,\r
251                           seq.findIndex(sequenceFeatures[sfindex].end) - 1,\r
252                           seq.findIndex(sequenceFeatures[sfindex].end) - 1,\r
253                           new Color( ( (Integer) av.featuresDisplayed.get(\r
254                 sequenceFeatures[sfindex].type)).intValue()),\r
255                           start, end, y1, width, height);\r
256 \r
257           }\r
258           else\r
259             renderFeature(g, seq,\r
260                           seq.findIndex(sequenceFeatures[sfindex].begin) - 1,\r
261                           seq.findIndex(sequenceFeatures[sfindex].end) - 1,\r
262                           getColour(sequenceFeatures[sfindex].type),\r
263                           start, end, y1, width, height);\r
264 \r
265         }\r
266       }\r
267 \r
268       if(transparencySetter!=null && g!=null)\r
269       {\r
270         transparencySetter.setTransparency(g, 1.0f);\r
271       }\r
272     }\r
273 \r
274 \r
275     char s;\r
276     int i;\r
277     void renderFeature(Graphics g, SequenceI seq,\r
278                        int fstart, int fend, Color featureColour, int start, int end,  int y1, int width, int height)\r
279     {\r
280 \r
281       if (((fstart <= end) && (fend >= start)))\r
282       {\r
283         if (fstart < start)\r
284         { // fix for if the feature we have starts before the sequence start,\r
285           fstart = start; // but the feature end is still valid!!\r
286         }\r
287 \r
288         if (fend >= end)\r
289         {\r
290           fend = end;\r
291         }\r
292 \r
293           for (i = fstart; i <= fend; i++)\r
294           {\r
295             s = seq.getSequence().charAt(i);\r
296 \r
297             if (jalview.util.Comparison.isGap(s))\r
298             {\r
299               continue;\r
300             }\r
301 \r
302             g.setColor(featureColour);\r
303 \r
304             g.fillRect( (i - start) * width, y1, width, height);\r
305 \r
306             if(!av.validCharWidth)\r
307               continue;\r
308 \r
309             g.setColor(Color.white);\r
310             charOffset = (width - fm.charWidth(s)) / 2;\r
311             g.drawString(String.valueOf(s),\r
312                          charOffset + (width * (i - start)),\r
313                          (y1 + height) - height / 5); //pady = height / 5;\r
314 \r
315           }\r
316       }\r
317     }\r
318 \r
319     void findAllFeatures()\r
320     {\r
321       av.featuresDisplayed = new Hashtable();\r
322       Vector allfeatures = new Vector();\r
323       for (int i = 0; i < av.alignment.getHeight(); i++)\r
324       {\r
325         SequenceFeature [] features = av.alignment.getSequenceAt(i).getSequenceFeatures();\r
326 \r
327         if (features == null)\r
328           continue;\r
329 \r
330         int index = 0;\r
331         while (index < features.length)\r
332         {\r
333           if (!av.featuresDisplayed.containsKey(features[index].getType()))\r
334           {\r
335             av.featuresDisplayed.put(features[index].getType(),\r
336                                   new Integer( getColour(features[index].getType()).getRGB()) );\r
337             allfeatures.addElement(features[index].getType());\r
338           }\r
339           index++;\r
340         }\r
341       }\r
342 \r
343       renderOrder = new String[allfeatures.size()];\r
344       Enumeration en = allfeatures.elements();\r
345       int i = allfeatures.size()-1;\r
346       while(en.hasMoreElements())\r
347       {\r
348         renderOrder[i] = en.nextElement().toString();\r
349         i--;\r
350       }\r
351     }\r
352 \r
353     public Color getColour(String featureType)\r
354     {\r
355       return (Color)featureColours.get(featureType);\r
356     }\r
357 \r
358     public void addNewFeature(String name, Color col)\r
359     {\r
360 \r
361       setColour(name, col);\r
362       if(av.featuresDisplayed==null)\r
363         av.featuresDisplayed = new Hashtable();\r
364 \r
365 \r
366       av.featuresDisplayed.put(name, "NOGROUP");\r
367     }\r
368 \r
369     public void setColour(String featureType, Color col)\r
370     {\r
371       featureColours.put(featureType, col);\r
372     }\r
373 \r
374     public void setFeaturePriority(Object [][] data)\r
375     {\r
376         // The feature table will display high priority\r
377         // features at the top, but theses are the ones\r
378         // we need to render last, so invert the data\r
379         if(av.featuresDisplayed!=null)\r
380           av.featuresDisplayed.clear();\r
381 \r
382         renderOrder = new String[data.length];\r
383 \r
384         if (data.length > 0)\r
385           for (int i = 0; i < data.length; i++)\r
386           {\r
387             String type = data[i][0].toString();\r
388             setColour(type, (Color) data[i][1]);\r
389             if ( ( (Boolean) data[i][2]).booleanValue())\r
390             {\r
391               av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));\r
392             }\r
393 \r
394             renderOrder[data.length - i - 1] = type;\r
395           }\r
396     }\r
397 }\r
398 \r
399   class TransparencySetter\r
400   {\r
401     void setTransparency(Graphics g, float value)\r
402     {\r
403   //    Graphics2D g2 = (Graphics2D) g;\r
404    //   g2.setComposite(\r
405     //      AlphaComposite.getInstance(\r
406     //          AlphaComposite.SRC_OVER, value));\r
407     }\r
408   }\r
409 \r
410 \r
411 \r
412 \r