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