MyGraphics added to enhance applet GUI
[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 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     SequenceGroup currentSequenceGroup = null;\r
40     SequenceGroup[] allGroups = null;\r
41     Color resBoxColour;\r
42     Graphics graphics;\r
43     float transparency = .4f;\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;\r
48 \r
49     /**\r
50      * Creates a new FeatureRenderer object.\r
51      *\r
52      * @param av DOCUMENT ME!\r
53      */\r
54     public FeatureRenderer(AlignViewport av)\r
55     {\r
56         this.av = av;\r
57         initColours();\r
58         String version = System.getProperty("java.version");\r
59         if (version.indexOf("1.1") == 0)\r
60           highlightTransparent = false;\r
61 \r
62     }\r
63 \r
64 boolean highlightTransparent = true;\r
65     /**\r
66      * DOCUMENT ME!\r
67      *\r
68      * @param g DOCUMENT ME!\r
69      * @param seq DOCUMENT ME!\r
70      * @param sg DOCUMENT ME!\r
71      * @param start DOCUMENT ME!\r
72      * @param end DOCUMENT ME!\r
73      * @param x1 DOCUMENT ME!\r
74      * @param y1 DOCUMENT ME!\r
75      * @param width DOCUMENT ME!\r
76      * @param height DOCUMENT ME!\r
77      */\r
78     public void drawSequence(Graphics g, SequenceI seq, SequenceGroup[] sg,\r
79         int start, int end, int x1, int y1, int width, int height)\r
80     {\r
81 \r
82         if(seq.getSequenceFeatures()==null)\r
83           return;\r
84 \r
85         Enumeration e = null, e2;\r
86         String type;\r
87         if(featuresDisplayed!=null)\r
88           e = featuresDisplayed.elements();\r
89         else\r
90          e = seq.getSequenceFeatures().elements();\r
91 \r
92        if(highlightTransparent)\r
93         {\r
94         //  MyGraphics.SetTransparency(g, transparency);\r
95         }\r
96 \r
97 \r
98         while (e.hasMoreElements())\r
99         {\r
100             SequenceFeature sf=null;\r
101             if(featuresDisplayed!=null)\r
102             {\r
103               e2 = seq.getSequenceFeatures().elements();\r
104               type = e.nextElement().toString();\r
105               while(e2.hasMoreElements())\r
106               {\r
107                 sf = (SequenceFeature) e2.nextElement();\r
108                 if(sf.getType().equals(type))\r
109                   break;\r
110                 else\r
111                   sf = null;\r
112               }\r
113             }\r
114             else\r
115             {\r
116               sf = (SequenceFeature) e.nextElement();\r
117               type = sf.getType();\r
118             }\r
119 \r
120             if(sf==null)\r
121               continue;\r
122 \r
123 \r
124             if (sf.getBegin() > seq.getEnd())\r
125             {\r
126                 continue;\r
127             }\r
128 \r
129             int fstart = seq.findIndex(sf.getBegin()) - 1;\r
130             int fend = seq.findIndex(sf.getEnd()) - 1;\r
131 \r
132             if (((fstart <= end) && (fend >= start)))\r
133             {\r
134                 if (fstart < start)\r
135                 { // fix for if the feature we have starts before the sequence start,\r
136                     fstart = start; // but the feature end is still valid!!\r
137                 }\r
138 \r
139                 if (fend >= end)\r
140                 {\r
141                     fend = end;\r
142                 }\r
143 \r
144                 if (fstart == fend)\r
145                 {\r
146                     g.setColor(getColour(type));\r
147                     g.fillRoundRect((fstart - start) * width, y1, width,\r
148                         height, 4, 4);\r
149                     g.setColor(Color.white);\r
150 \r
151                     char s = seq.getSequence().charAt(fstart);\r
152                     FontMetrics fm = g.getFontMetrics();\r
153                     int charOffset = (width - fm.charWidth(s)) / 2;\r
154                     int pady = height / 5;\r
155                     g.drawString(String.valueOf(s),\r
156                         charOffset + x1 + (width * (fstart - start)),\r
157                         (y1 + height) - pady);\r
158                 }\r
159                 else\r
160                 {\r
161                     for (int i = fstart; i <= fend; i++)\r
162                     {\r
163                         char s = seq.getSequence().charAt(i);\r
164 \r
165                         if (jalview.util.Comparison.isGap(s))\r
166                         {\r
167                             continue;\r
168                         }\r
169 \r
170                         g.setColor(getColour(type));\r
171                         g.fillRect((i - start) * width, y1, width, height);\r
172 \r
173                         g.setColor(Color.white);\r
174 \r
175                         FontMetrics fm = g.getFontMetrics();\r
176                         int charOffset = (width - fm.charWidth(s)) / 2;\r
177                         int pady = height / 5;\r
178                         g.drawString(String.valueOf(s),\r
179                             charOffset + x1 + (width * (i - start)),\r
180                             (y1 + height) - pady);\r
181                     }\r
182                 }\r
183             }\r
184           }\r
185 \r
186           if (highlightTransparent)\r
187           {\r
188         //    MyGraphics.SetTransparency(g, 1.0f);\r
189           }\r
190     }\r
191 \r
192     public Color getColour(String featureType)\r
193     {\r
194       return (Color)featureColours.get(featureType);\r
195     }\r
196 \r
197     public void setColour(String featureType, Color col)\r
198     {\r
199       featureColours.put(featureType, col);\r
200     }\r
201 \r
202     public void setTransparency(float value)\r
203     {\r
204       transparency = value;\r
205     }\r
206 \r
207     public float getTransparency()\r
208     {\r
209       return transparency;\r
210     }\r
211 \r
212     public void setFeaturePriority(Object [][] data)\r
213     {\r
214       // The feature table will display high priority\r
215       // features at the top, but theses are the ones\r
216       // we need to render last, so invert the data\r
217       featuresDisplayed = new Vector();\r
218       for(int i=data.length-1; i>-1; i--)\r
219       {\r
220        String type = data[i][0].toString();\r
221        setColour(type, (Color)data[i][1]);\r
222        if( ((Boolean)data[i][2]).booleanValue() )\r
223          featuresDisplayed.addElement(type);\r
224       }\r
225     }\r
226 \r
227     Hashtable featureColours = new Hashtable();\r
228     void initColours()\r
229     {\r
230       featureColours.put("active site", new Color(255, 75, 0));\r
231       featureColours.put("binding site", new Color(245, 85, 0));\r
232       featureColours.put("calcium-binding region", new Color(235, 95, 0));\r
233       featureColours.put("chain", new Color(225, 105, 0));\r
234       featureColours.put("coiled-coil region", new Color(215, 115, 0));\r
235       featureColours.put("compositionally biased region", new Color(205, 125, 0));\r
236       featureColours.put("cross-link", new Color(195, 135, 0));\r
237       featureColours.put("disulfide bond", new Color(185, 145, 0));\r
238       featureColours.put("DNA-binding region", new Color(175, 155, 0));\r
239       featureColours.put("domain", new Color(165, 165, 0));\r
240       featureColours.put("glycosylation site", new Color(155, 175, 0));\r
241       featureColours.put("helix", new Color(145, 185, 0));\r
242       featureColours.put("initiator methionine", new Color(135, 195, 5));\r
243       featureColours.put("lipid moiety-binding region", new Color(125, 205, 15));\r
244       featureColours.put("metal ion-binding site", new Color(115, 215, 25));\r
245       featureColours.put("modified residue", new Color(105, 225, 35));\r
246       featureColours.put("mutagenesis site", new Color(95, 235, 45));\r
247       featureColours.put("non-consecutive residues", new Color(85, 245, 55));\r
248       featureColours.put("non-terminal residue", new Color(75, 255, 65));\r
249       featureColours.put("nucleotide phosphate-binding region",\r
250                          new Color(65, 245, 75));\r
251       featureColours.put("peptide", new Color(55, 235, 85));\r
252       featureColours.put("propeptide", new Color(45, 225, 95));\r
253       featureColours.put("region of interest", new Color(35, 215, 105));\r
254       featureColours.put("repeat", new Color(25, 205, 115));\r
255       featureColours.put("selenocysteine", new Color(15, 195, 125));\r
256       featureColours.put("sequence conflict", new Color(5, 185, 135));\r
257       featureColours.put("sequence variant", new Color(0, 175, 145));\r
258       featureColours.put("short sequence motif", new Color(0, 165, 155));\r
259       featureColours.put("signal peptide", new Color(0, 155, 165));\r
260       featureColours.put("site", new Color(0, 145, 175));\r
261       featureColours.put("splice variant", new Color(0, 135, 185));\r
262       featureColours.put("strand", new Color(0, 125, 195));\r
263       featureColours.put("topological domain", new Color(0, 115, 205));\r
264       featureColours.put("transit peptide", new Color(0, 105, 215));\r
265       featureColours.put("transmembrane region", new Color(0, 95, 225));\r
266       featureColours.put("turn", new Color(0, 85, 235));\r
267       featureColours.put("unsure residue", new Color(0, 75, 245));\r
268       featureColours.put("zinc finger region", new Color(0, 65, 255));\r
269     }\r
270 \r
271 }\r
272 \r
273 \r
274 \r