FeatureGroups added
[jalview.git] / src / jalview / gui / 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.gui;\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 import java.awt.image.*;\r
28 \r
29 \r
30 /**\r
31  * DOCUMENT ME!\r
32  *\r
33  * @author $author$\r
34  * @version $Revision$\r
35  */\r
36 public class FeatureRenderer\r
37 {\r
38     AlignViewport av;\r
39     Color resBoxColour;\r
40     float transparency = 1.0f;\r
41     FontMetrics fm;\r
42     int charOffset;\r
43     boolean drawText = true;\r
44 \r
45     // The following vector holds the features which are\r
46     // to be added, in the correct order or rendering\r
47     Vector featuresDisplayed = null;\r
48 \r
49     // A higher level for grouping features of a\r
50    // particular type\r
51     Hashtable featureGroups = null;\r
52 \r
53 \r
54     /**\r
55      * Creates a new FeatureRenderer object.\r
56      *\r
57      * @param av DOCUMENT ME!\r
58      */\r
59     public FeatureRenderer(AlignViewport av)\r
60     {\r
61         this.av = av;\r
62         initColours();\r
63     }\r
64 \r
65     /**\r
66      * This is used by the Molecule Viewer to get the accurate colour\r
67      * of the rendered sequence\r
68      */\r
69     BufferedImage bi;\r
70     public Color findFeatureColour(Color initialCol, SequenceI seq, int i)\r
71     {\r
72       if(!av.showSequenceFeatures)\r
73         return initialCol;\r
74 \r
75       if (bi == null)\r
76         bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);\r
77 \r
78       bi.setRGB(0,0, initialCol.getRGB());\r
79 \r
80       drawText = false;\r
81 \r
82       drawSequence(bi.getGraphics(), seq, i, i, 0, 0, 1, 1);\r
83       drawText = true;\r
84 \r
85       return new Color(bi.getRGB(0, 0));\r
86     }\r
87 \r
88 \r
89     /**\r
90      * DOCUMENT ME!\r
91      *\r
92      * @param g DOCUMENT ME!\r
93      * @param seq DOCUMENT ME!\r
94      * @param sg DOCUMENT ME!\r
95      * @param start DOCUMENT ME!\r
96      * @param end DOCUMENT ME!\r
97      * @param x1 DOCUMENT ME!\r
98      * @param y1 DOCUMENT ME!\r
99      * @param width DOCUMENT ME!\r
100      * @param height DOCUMENT ME!\r
101      */\r
102     public void drawSequence(Graphics g, SequenceI seq,\r
103                              int start, int end, int x1, int y1, int width, int height)\r
104     {\r
105 \r
106         //System.out.println(start+" "+end+" "+x1+" "+y1);\r
107       if (seq.getDatasetSequence().getSequenceFeatures() == null\r
108           || seq.getDatasetSequence().getSequenceFeatures().size()==0)\r
109         return;\r
110 \r
111       fm = g.getFontMetrics();\r
112 \r
113       if (transparency != 1)\r
114       {\r
115         Graphics2D g2 = (Graphics2D) g;\r
116         g2.setComposite(\r
117             AlphaComposite.getInstance(\r
118                 AlphaComposite.SRC_OVER, transparency));\r
119       }\r
120 \r
121       String type;\r
122       SequenceFeature sf;\r
123       if (featuresDisplayed == null)\r
124         findAllFeatures();\r
125 \r
126       Enumeration e = featuresDisplayed.elements(), e2;\r
127 \r
128       // Loop through each visible feature\r
129       while (e.hasMoreElements())\r
130       {\r
131 \r
132         type = e.nextElement().toString();\r
133         e2 = seq.getDatasetSequence().getSequenceFeatures().elements();\r
134         // loop through all features in sequence to find\r
135         // current feature to render\r
136           while (e2.hasMoreElements())\r
137         {\r
138 \r
139           sf = (SequenceFeature) e2.nextElement();\r
140           if (!type.equals(sf.getType()))\r
141             continue;\r
142 \r
143           if(featureGroups!=null\r
144              && sf.getFeatureGroup()!=null\r
145              && featureGroups.containsKey(sf.getFeatureGroup())\r
146              && !((Boolean)featureGroups.get(sf.getFeatureGroup())).booleanValue())\r
147           {\r
148             continue;\r
149           }\r
150 \r
151           if (sf.getBegin() > seq.getEnd())\r
152             continue;\r
153 \r
154           if (type.equals("disulfide bond"))\r
155           {\r
156 \r
157             renderFeature(g, seq,\r
158                           seq.findIndex(sf.getBegin()) - 1,\r
159                           seq.findIndex(sf.getBegin()) - 1,\r
160                           type, start, end, x1, y1, width, height);\r
161             renderFeature(g, seq,\r
162                           seq.findIndex(sf.getEnd()) - 1,\r
163                           seq.findIndex(sf.getEnd()) - 1,\r
164                           type, start, end, x1, y1, width, height);\r
165 \r
166           }\r
167           else\r
168             renderFeature(g, seq,\r
169                           seq.findIndex(sf.getBegin()) - 1,\r
170                           seq.findIndex(sf.getEnd()) - 1,\r
171                           type, start, end, x1, y1, width, height);\r
172         }\r
173       }\r
174 \r
175         if(transparency!=1.0f)\r
176         {\r
177           Graphics2D g2 = (Graphics2D) g;\r
178           g2.setComposite(\r
179               AlphaComposite.getInstance(\r
180                   AlphaComposite.SRC_OVER, 1.0f));\r
181         }\r
182     }\r
183 \r
184 \r
185     void renderFeature(Graphics g, SequenceI seq,\r
186                        int fstart, int fend, String type, int start, int end, int x1, int y1, int width, int height)\r
187     {\r
188 \r
189       if (((fstart <= end) && (fend >= start)))\r
190       {\r
191           if (fstart < start)\r
192           { // fix for if the feature we have starts before the sequence start,\r
193               fstart = start; // but the feature end is still valid!!\r
194           }\r
195 \r
196           if (fend >= end)\r
197           {\r
198             fend = end;\r
199           }\r
200           for (int i = fstart; i <= fend; i++)\r
201           {\r
202             char s = seq.getSequence().charAt(i);\r
203 \r
204             if (jalview.util.Comparison.isGap(s))\r
205             {\r
206               continue;\r
207             }\r
208 \r
209             g.setColor(getColour(type));\r
210 \r
211             g.fillRect( (i - start) * width, y1, width, height);\r
212 \r
213             if(drawText)\r
214            {\r
215              g.setColor(Color.white);\r
216              charOffset = (width - fm.charWidth(s)) / 2;\r
217              g.drawString(String.valueOf(s),\r
218                           charOffset + x1 + (width * (i - start)),\r
219                           (y1 + height) - height / 5); //pady = height / 5;\r
220            }\r
221           }\r
222         }\r
223     }\r
224 \r
225     void findAllFeatures()\r
226     {\r
227       Vector features = new Vector();\r
228       SequenceFeature sf;\r
229       featuresDisplayed = new Vector();\r
230       Enumeration e;\r
231       for (int i = 0; i < av.alignment.getHeight(); i++)\r
232       {\r
233         features = av.alignment.getSequenceAt(i).getDatasetSequence().\r
234             getSequenceFeatures();\r
235         if (features == null)\r
236           continue;\r
237 \r
238         e = features.elements();\r
239         while (e.hasMoreElements())\r
240         {\r
241           sf = (SequenceFeature) e.nextElement();\r
242           if (!featuresDisplayed.contains(sf.getType()))\r
243           {\r
244             featuresDisplayed.addElement(sf.getType());\r
245           }\r
246         }\r
247       }\r
248     }\r
249 \r
250     public Color getColour(String featureType)\r
251     {\r
252       return (Color)featureColours.get(featureType);\r
253     }\r
254 \r
255     public void addNewFeature(String name, Color col)\r
256     {\r
257       setColour(name, col);\r
258       if(featuresDisplayed==null)\r
259         featuresDisplayed = new Vector();\r
260       featuresDisplayed.insertElementAt(name, 0);\r
261     }\r
262 \r
263     public void setColour(String featureType, Color col)\r
264     {\r
265       featureColours.put(featureType, col);\r
266     }\r
267 \r
268     public void setTransparency(float value)\r
269     {\r
270       transparency = value;\r
271     }\r
272 \r
273     public float getTransparency()\r
274     {\r
275       return transparency;\r
276     }\r
277 \r
278     public void setFeaturePriority(Object [][] data)\r
279     {\r
280       // The feature table will display high priority\r
281       // features at the top, but theses are the ones\r
282       // we need to render last, so invert the data\r
283       featuresDisplayed.clear();\r
284       for(int i=data.length-1; i>-1; i--)\r
285       {\r
286        String type = data[i][0].toString();\r
287        setColour(type, (Color)data[i][1]);\r
288        if( ((Boolean)data[i][2]).booleanValue() )\r
289          featuresDisplayed.addElement(type);\r
290       }\r
291     }\r
292 \r
293     Hashtable featureColours = new Hashtable();\r
294     void initColours()\r
295     {\r
296       featureColours.put("active site", new Color(255, 75, 0));\r
297       featureColours.put("binding site", new Color(245, 85, 0));\r
298       featureColours.put("calcium-binding region", new Color(235, 95, 0));\r
299       featureColours.put("chain", new Color(225, 105, 0));\r
300       featureColours.put("coiled-coil region", new Color(215, 115, 0));\r
301       featureColours.put("compositionally biased region", new Color(205, 125, 0));\r
302       featureColours.put("cross-link", new Color(195, 135, 0));\r
303       featureColours.put("disulfide bond", new Color(230,230,0));\r
304       featureColours.put("DNA-binding region", new Color(175, 155, 0));\r
305       featureColours.put("domain", new Color(165, 165, 0));\r
306       featureColours.put("glycosylation site", new Color(155, 175, 0));\r
307       featureColours.put("helix", new Color(145, 185, 0));\r
308       featureColours.put("initiator methionine", new Color(135, 195, 5));\r
309       featureColours.put("lipid moiety-binding region", new Color(125, 205, 15));\r
310       featureColours.put("metal ion-binding site", new Color(115, 215, 25));\r
311       featureColours.put("modified residue", new Color(105, 225, 35));\r
312       featureColours.put("mutagenesis site", new Color(95, 235, 45));\r
313       featureColours.put("non-consecutive residues", new Color(85, 245, 55));\r
314       featureColours.put("non-terminal residue", new Color(75, 255, 65));\r
315       featureColours.put("nucleotide phosphate-binding region",new Color(65, 245, 75));\r
316       featureColours.put("peptide", new Color(55, 235, 85));\r
317       featureColours.put("propeptide", new Color(45, 225, 95));\r
318       featureColours.put("region of interest", new Color(35, 215, 105));\r
319       featureColours.put("repeat", new Color(25, 205, 115));\r
320       featureColours.put("selenocysteine", new Color(15, 195, 125));\r
321       featureColours.put("sequence conflict", new Color(5, 185, 135));\r
322       featureColours.put("sequence variant", new Color(0, 175, 145));\r
323       featureColours.put("short sequence motif", new Color(0, 165, 155));\r
324       featureColours.put("signal peptide", new Color(0, 155, 165));\r
325       featureColours.put("site", new Color(0, 145, 175));\r
326       featureColours.put("splice variant", new Color(0, 135, 185));\r
327       featureColours.put("strand", new Color(0, 125, 195));\r
328       featureColours.put("topological domain", new Color(0, 115, 205));\r
329       featureColours.put("transit peptide", new Color(0, 105, 215));\r
330       featureColours.put("transmembrane region", new Color(0, 95, 225));\r
331       featureColours.put("turn", new Color(0, 85, 235));\r
332       featureColours.put("unsure residue", new Color(0, 75, 245));\r
333       featureColours.put("zinc finger region", new Color(0, 65, 255));\r
334     }\r
335 \r
336 }\r