Formatted source
[jalview.git] / src / jalview / schemes / ClustalxColourScheme.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.schemes;\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 public class ClustalxColourScheme extends ResidueColourScheme {\r
29     public static Hashtable colhash = new Hashtable();\r
30     Hashtable[] cons;\r
31     int[][] cons2;\r
32     ConsensusColour[] colours;\r
33     ConsensusColour[] ResidueColour;\r
34     int size;\r
35     Consensus[] conses = new Consensus[32];\r
36     Vector colourTable = new Vector();\r
37 \r
38     {\r
39         colhash.put("RED", new Color((float) 0.9, (float) 0.2, (float) 0.1));\r
40         colhash.put("BLUE", new Color((float) 0.5, (float) 0.7, (float) 0.9));\r
41         colhash.put("GREEN", new Color((float) 0.1, (float) 0.8, (float) 0.1));\r
42         colhash.put("ORANGE", new Color((float) 0.9, (float) 0.6, (float) 0.3));\r
43         colhash.put("CYAN", new Color((float) 0.1, (float) 0.7, (float) 0.7));\r
44         colhash.put("PINK", new Color((float) 0.9, (float) 0.5, (float) 0.5));\r
45         colhash.put("MAGENTA", new Color((float) 0.8, (float) 0.3, (float) 0.8));\r
46         colhash.put("YELLOW", new Color((float) 0.8, (float) 0.8, (float) 0.0));\r
47     }\r
48 \r
49     public ClustalxColourScheme(Vector seqs, int maxWidth) {\r
50         resetClustalX(seqs, maxWidth);\r
51     }\r
52 \r
53     public void resetClustalX(Vector seqs, int maxWidth) {\r
54         cons2 = new int[maxWidth][24];\r
55 \r
56         int start = 0;\r
57 \r
58         // Initialize the array\r
59         for (int j = 0; j < 24; j++)\r
60             for (int i = 0; i < maxWidth; i++)\r
61                 cons2[i][j] = 0;\r
62 \r
63         int res;\r
64         int i;\r
65         int j = 0;\r
66         String seq;\r
67 \r
68         while (j < seqs.size()) {\r
69             seq = ((SequenceI) seqs.elementAt(j)).getSequence();\r
70 \r
71             int end_j = seq.length() - 1;\r
72 \r
73             for (i = start; i <= end_j; i++) {\r
74                 if ((seq.length() - 1) < i) {\r
75                     res = 23;\r
76                 } else {\r
77                     res = ((Integer) ResidueProperties.aaHash.get(seq.charAt(i) +\r
78                             "")).intValue();\r
79                 }\r
80 \r
81                 cons2[i][res]++;\r
82             }\r
83 \r
84             j++;\r
85         }\r
86 \r
87         this.size = seqs.size();\r
88         makeColours();\r
89     }\r
90 \r
91     public void makeColours() {\r
92         conses[0] = new Consensus("WLVIMAFCYHP", 60);\r
93         conses[1] = new Consensus("WLVIMAFCYHP", 80);\r
94         conses[2] = new Consensus("ED", 50);\r
95         conses[3] = new Consensus("KR", 60);\r
96         conses[4] = new Consensus("G", 50);\r
97         conses[5] = new Consensus("N", 50);\r
98         conses[6] = new Consensus("QE", 50);\r
99         conses[7] = new Consensus("P", 50);\r
100         conses[8] = new Consensus("TS", 50);\r
101 \r
102         conses[26] = new Consensus("A", 85);\r
103         conses[27] = new Consensus("C", 85);\r
104         conses[10] = new Consensus("E", 85);\r
105         conses[11] = new Consensus("F", 85);\r
106         conses[12] = new Consensus("G", 85);\r
107         conses[13] = new Consensus("H", 85);\r
108         conses[14] = new Consensus("I", 85);\r
109         conses[15] = new Consensus("L", 85);\r
110         conses[16] = new Consensus("M", 85);\r
111         conses[17] = new Consensus("N", 85);\r
112         conses[18] = new Consensus("P", 85);\r
113         conses[19] = new Consensus("Q", 85);\r
114         conses[20] = new Consensus("R", 85);\r
115         conses[21] = new Consensus("S", 85);\r
116         conses[22] = new Consensus("T", 85);\r
117         conses[23] = new Consensus("V", 85);\r
118         conses[24] = new Consensus("W", 85);\r
119         conses[25] = new Consensus("Y", 85);\r
120         conses[28] = new Consensus("K", 85);\r
121         conses[29] = new Consensus("D", 85);\r
122 \r
123         conses[30] = new Consensus("G", 0);\r
124         conses[31] = new Consensus("P", 0);\r
125 \r
126         // We now construct the colours\r
127         colours = new ConsensusColour[11];\r
128 \r
129         Consensus[] tmp8 = new Consensus[1];\r
130         tmp8[0] = conses[30]; //G\r
131         colours[7] = new ConsensusColour((Color) colhash.get("ORANGE"), tmp8);\r
132 \r
133         Consensus[] tmp9 = new Consensus[1];\r
134         tmp9[0] = conses[31]; //P\r
135         colours[8] = new ConsensusColour((Color) colhash.get("YELLOW"), tmp9);\r
136 \r
137         Consensus[] tmp10 = new Consensus[1];\r
138         tmp10[0] = conses[27]; //C\r
139         colours[9] = new ConsensusColour((Color) colhash.get("PINK"), tmp8);\r
140 \r
141         Consensus[] tmp1 = new Consensus[14];\r
142         tmp1[0] = conses[0]; //%\r
143         tmp1[1] = conses[1]; //#\r
144         tmp1[2] = conses[26]; //A\r
145         tmp1[3] = conses[27]; //C\r
146         tmp1[4] = conses[11]; //F\r
147         tmp1[5] = conses[13]; //H\r
148         tmp1[6] = conses[14]; //I\r
149         tmp1[7] = conses[15]; //L\r
150         tmp1[8] = conses[16]; //M\r
151         tmp1[9] = conses[23]; //V\r
152         tmp1[10] = conses[24]; //W\r
153         tmp1[11] = conses[25]; //Y\r
154         tmp1[12] = conses[18]; //P\r
155         tmp1[13] = conses[19]; //p\r
156         colours[0] = new ConsensusColour((Color) colhash.get("BLUE"), tmp1);\r
157 \r
158         colours[10] = new ConsensusColour((Color) colhash.get("CYAN"), tmp1);\r
159 \r
160         Consensus[] tmp2 = new Consensus[5];\r
161         tmp2[0] = conses[8]; //t\r
162         tmp2[1] = conses[21]; //S\r
163         tmp2[2] = conses[22]; //T\r
164         tmp2[3] = conses[0]; //%\r
165         tmp2[4] = conses[1]; //#\r
166         colours[1] = new ConsensusColour((Color) colhash.get("GREEN"), tmp2);\r
167 \r
168         Consensus[] tmp3 = new Consensus[3];\r
169 \r
170         tmp3[0] = conses[17]; //N\r
171         tmp3[1] = conses[29]; //D\r
172         tmp3[2] = conses[5]; //n\r
173         colours[2] = new ConsensusColour((Color) colhash.get("GREEN"), tmp3);\r
174 \r
175         Consensus[] tmp4 = new Consensus[6];\r
176         tmp4[0] = conses[6]; // q = QE\r
177         tmp4[1] = conses[19]; //Q\r
178         tmp4[2] = conses[22]; //E\r
179         tmp4[3] = conses[3]; //+\r
180         tmp4[4] = conses[28]; //K\r
181         tmp4[5] = conses[20]; //R\r
182         colours[3] = new ConsensusColour((Color) colhash.get("GREEN"), tmp4);\r
183 \r
184         Consensus[] tmp5 = new Consensus[4];\r
185         tmp5[0] = conses[3]; //+\r
186         tmp5[1] = conses[28]; //K\r
187         tmp5[2] = conses[20]; //R\r
188         tmp5[3] = conses[19]; //Q\r
189         colours[4] = new ConsensusColour((Color) colhash.get("RED"), tmp5);\r
190 \r
191         Consensus[] tmp6 = new Consensus[5];\r
192         tmp6[0] = conses[3]; //-\r
193         tmp6[1] = conses[29]; //D\r
194         tmp6[2] = conses[10]; //E\r
195         tmp6[3] = conses[6]; //q\r
196         tmp6[4] = conses[19]; //Q\r
197         colours[5] = new ConsensusColour((Color) colhash.get("MAGENTA"), tmp6);\r
198 \r
199         Consensus[] tmp7 = new Consensus[5];\r
200         tmp7[0] = conses[3]; //-\r
201         tmp7[1] = conses[29]; //D\r
202         tmp7[2] = conses[10]; //E\r
203         tmp7[3] = conses[17]; //N\r
204         tmp7[4] = conses[2]; //DE\r
205         colours[6] = new ConsensusColour((Color) colhash.get("MAGENTA"), tmp7);\r
206 \r
207         // Now attach the ConsensusColours to the residue letters\r
208         ResidueColour = new ConsensusColour[20];\r
209         ResidueColour[0] = colours[0]; // A\r
210         ResidueColour[1] = colours[4]; // R\r
211         ResidueColour[2] = colours[2]; // N\r
212         ResidueColour[3] = colours[6]; // D\r
213         ResidueColour[4] = colours[0]; // C\r
214         ResidueColour[5] = colours[3]; // Q\r
215         ResidueColour[6] = colours[5]; // E\r
216         ResidueColour[7] = colours[7]; // G\r
217         ResidueColour[8] = colours[10]; // H\r
218         ResidueColour[9] = colours[0]; // I\r
219         ResidueColour[10] = colours[0]; // L\r
220         ResidueColour[11] = colours[4]; // K\r
221         ResidueColour[12] = colours[0]; // M\r
222         ResidueColour[13] = colours[0]; // F\r
223         ResidueColour[14] = colours[8]; // P\r
224         ResidueColour[15] = colours[1]; // S\r
225         ResidueColour[16] = colours[1]; // T\r
226         ResidueColour[17] = colours[0]; // W\r
227         ResidueColour[18] = colours[10]; // Y\r
228         ResidueColour[19] = colours[0]; // V\r
229     }\r
230 \r
231     public Color findColour(String s) {\r
232         return Color.pink;\r
233     }\r
234 \r
235     public Color findColour(String s, int j) {\r
236         if ((threshold != 0) && !aboveThreshold(s, j)) {\r
237             return Color.white;\r
238         }\r
239 \r
240         int i = ((Integer) ResidueProperties.aaHash.get(s)).intValue();\r
241 \r
242         Color c = Color.white;\r
243 \r
244         if (i > 19) {\r
245             return c;\r
246         }\r
247 \r
248         for (int k = 0; k < ResidueColour[i].conses.length; k++)\r
249             if (ResidueColour[i].conses[k].isConserved(cons2, j, size)) {\r
250                 c = ResidueColour[i].c;\r
251             }\r
252 \r
253         if (i == 4) {\r
254             if (conses[27].isConserved(cons2, j, size)) {\r
255                 c = (Color) colhash.get("PINK");\r
256             }\r
257         }\r
258 \r
259         return c;\r
260     }\r
261 }\r
262 \r
263 \r
264 class ConsensusColour {\r
265     Consensus[] conses;\r
266     Color c;\r
267 \r
268     public ConsensusColour(Color c, Consensus[] conses) {\r
269         this.conses = conses;\r
270 \r
271         //    this.list = list;\r
272         this.c = c;\r
273     }\r
274 }\r