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