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