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