patch to fix occasional arrayoutofbounds exception when working with hidden columns...
[jalview.git] / src / jalview / schemes / ClustalxColourScheme.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.schemes;
20
21 import java.util.*;
22
23 import java.awt.*;
24
25 import jalview.datamodel.*;
26
27 public class ClustalxColourScheme extends ResidueColourScheme // implements
28                                                               // IParameterizable
29 {
30   public static Hashtable colhash = new Hashtable();
31
32   Hashtable[] cons;
33
34   int[][] cons2;
35
36   ConsensusColour[] colours;
37
38   ConsensusColour[] ResidueColour;
39
40   int size;
41
42   Consensus[] conses = new Consensus[32];
43
44   Vector colourTable = new Vector();
45
46   private boolean includeGaps = true;
47
48   {
49     colhash.put("RED", new Color((float) 0.9, (float) 0.2, (float) 0.1));
50     colhash.put("BLUE", new Color((float) 0.5, (float) 0.7, (float) 0.9));
51     colhash.put("GREEN", new Color((float) 0.1, (float) 0.8, (float) 0.1));
52     colhash.put("ORANGE", new Color((float) 0.9, (float) 0.6, (float) 0.3));
53     colhash.put("CYAN", new Color((float) 0.1, (float) 0.7, (float) 0.7));
54     colhash.put("PINK", new Color((float) 0.9, (float) 0.5, (float) 0.5));
55     colhash
56             .put("MAGENTA",
57                     new Color((float) 0.8, (float) 0.3, (float) 0.8));
58     colhash.put("YELLOW", new Color((float) 0.8, (float) 0.8, (float) 0.0));
59   }
60
61   public ClustalxColourScheme(Vector seqs, int maxWidth)
62   {
63     resetClustalX(seqs, maxWidth);
64   }
65
66   public void resetClustalX(Vector seqs, int maxWidth)
67   {
68     cons2 = new int[maxWidth][24];
69     includeGaps = isIncludeGaps(); // does nothing - TODO replace with call to
70                                     // get the current setting of the
71                                     // includeGaps param.
72     int start = 0;
73
74     // Initialize the array
75     for (int j = 0; j < 24; j++)
76     {
77       for (int i = 0; i < maxWidth; i++)
78       {
79         cons2[i][j] = 0;
80       }
81     }
82
83     int res;
84     int i;
85     int j = 0;
86     char[] seq;
87
88     while (j < seqs.size())
89     {
90       seq = ((SequenceI) seqs.elementAt(j)).getSequence();
91
92       int end_j = seq.length - 1;
93
94       for (i = start; i <= end_j; i++)
95       {
96         if ((seq.length - 1) < i)
97         {
98           res = 23;
99         }
100         else
101         {
102           res = ResidueProperties.aaIndex[seq[i]];
103         }
104
105         cons2[i][res]++;
106       }
107
108       j++;
109     }
110
111     this.size = seqs.size();
112     makeColours();
113   }
114
115   public void makeColours()
116   {
117     conses[0] = new Consensus("WLVIMAFCYHP", 60);
118     conses[1] = new Consensus("WLVIMAFCYHP", 80);
119     conses[2] = new Consensus("ED", 50);
120     conses[3] = new Consensus("KR", 60);
121     conses[4] = new Consensus("G", 50);
122     conses[5] = new Consensus("N", 50);
123     conses[6] = new Consensus("QE", 50);
124     conses[7] = new Consensus("P", 50);
125     conses[8] = new Consensus("TS", 50);
126
127     conses[26] = new Consensus("A", 85);
128     conses[27] = new Consensus("C", 85);
129     conses[10] = new Consensus("E", 85);
130     conses[11] = new Consensus("F", 85);
131     conses[12] = new Consensus("G", 85);
132     conses[13] = new Consensus("H", 85);
133     conses[14] = new Consensus("I", 85);
134     conses[15] = new Consensus("L", 85);
135     conses[16] = new Consensus("M", 85);
136     conses[17] = new Consensus("N", 85);
137     conses[18] = new Consensus("P", 85);
138     conses[19] = new Consensus("Q", 85);
139     conses[20] = new Consensus("R", 85);
140     conses[21] = new Consensus("S", 85);
141     conses[22] = new Consensus("T", 85);
142     conses[23] = new Consensus("V", 85);
143     conses[24] = new Consensus("W", 85);
144     conses[25] = new Consensus("Y", 85);
145     conses[28] = new Consensus("K", 85);
146     conses[29] = new Consensus("D", 85);
147
148     conses[30] = new Consensus("G", 0);
149     conses[31] = new Consensus("P", 0);
150
151     // We now construct the colours
152     colours = new ConsensusColour[11];
153
154     Consensus[] tmp8 = new Consensus[1];
155     tmp8[0] = conses[30]; // G
156     colours[7] = new ConsensusColour((Color) colhash.get("ORANGE"), tmp8);
157
158     Consensus[] tmp9 = new Consensus[1];
159     tmp9[0] = conses[31]; // P
160     colours[8] = new ConsensusColour((Color) colhash.get("YELLOW"), tmp9);
161
162     Consensus[] tmp10 = new Consensus[1];
163     tmp10[0] = conses[27]; // C
164     colours[9] = new ConsensusColour((Color) colhash.get("PINK"), tmp8);
165
166     Consensus[] tmp1 = new Consensus[14];
167     tmp1[0] = conses[0]; // %
168     tmp1[1] = conses[1]; // #
169     tmp1[2] = conses[26]; // A
170     tmp1[3] = conses[27]; // C
171     tmp1[4] = conses[11]; // F
172     tmp1[5] = conses[13]; // H
173     tmp1[6] = conses[14]; // I
174     tmp1[7] = conses[15]; // L
175     tmp1[8] = conses[16]; // M
176     tmp1[9] = conses[23]; // V
177     tmp1[10] = conses[24]; // W
178     tmp1[11] = conses[25]; // Y
179     tmp1[12] = conses[18]; // P
180     tmp1[13] = conses[19]; // p
181     colours[0] = new ConsensusColour((Color) colhash.get("BLUE"), tmp1);
182
183     colours[10] = new ConsensusColour((Color) colhash.get("CYAN"), tmp1);
184
185     Consensus[] tmp2 = new Consensus[5];
186     tmp2[0] = conses[8]; // t
187     tmp2[1] = conses[21]; // S
188     tmp2[2] = conses[22]; // T
189     tmp2[3] = conses[0]; // %
190     tmp2[4] = conses[1]; // #
191     colours[1] = new ConsensusColour((Color) colhash.get("GREEN"), tmp2);
192
193     Consensus[] tmp3 = new Consensus[3];
194
195     tmp3[0] = conses[17]; // N
196     tmp3[1] = conses[29]; // D
197     tmp3[2] = conses[5]; // n
198     colours[2] = new ConsensusColour((Color) colhash.get("GREEN"), tmp3);
199
200     Consensus[] tmp4 = new Consensus[6];
201     tmp4[0] = conses[6]; // q = QE
202     tmp4[1] = conses[19]; // Q
203     tmp4[2] = conses[22]; // E
204     tmp4[3] = conses[3]; // +
205     tmp4[4] = conses[28]; // K
206     tmp4[5] = conses[20]; // R
207     colours[3] = new ConsensusColour((Color) colhash.get("GREEN"), tmp4);
208
209     Consensus[] tmp5 = new Consensus[4];
210     tmp5[0] = conses[3]; // +
211     tmp5[1] = conses[28]; // K
212     tmp5[2] = conses[20]; // R
213     tmp5[3] = conses[19]; // Q
214     colours[4] = new ConsensusColour((Color) colhash.get("RED"), tmp5);
215
216     Consensus[] tmp6 = new Consensus[5];
217     tmp6[0] = conses[3]; // -
218     tmp6[1] = conses[29]; // D
219     tmp6[2] = conses[10]; // E
220     tmp6[3] = conses[6]; // q
221     tmp6[4] = conses[19]; // Q
222     colours[5] = new ConsensusColour((Color) colhash.get("MAGENTA"), tmp6);
223
224     Consensus[] tmp7 = new Consensus[5];
225     tmp7[0] = conses[3]; // -
226     tmp7[1] = conses[29]; // D
227     tmp7[2] = conses[10]; // E
228     tmp7[3] = conses[17]; // N
229     tmp7[4] = conses[2]; // DE
230     colours[6] = new ConsensusColour((Color) colhash.get("MAGENTA"), tmp7);
231
232     // Now attach the ConsensusColours to the residue letters
233     ResidueColour = new ConsensusColour[20];
234     ResidueColour[0] = colours[0]; // A
235     ResidueColour[1] = colours[4]; // R
236     ResidueColour[2] = colours[2]; // N
237     ResidueColour[3] = colours[6]; // D
238     ResidueColour[4] = colours[0]; // C
239     ResidueColour[5] = colours[3]; // Q
240     ResidueColour[6] = colours[5]; // E
241     ResidueColour[7] = colours[7]; // G
242     ResidueColour[8] = colours[10]; // H
243     ResidueColour[9] = colours[0]; // I
244     ResidueColour[10] = colours[0]; // L
245     ResidueColour[11] = colours[4]; // K
246     ResidueColour[12] = colours[0]; // M
247     ResidueColour[13] = colours[0]; // F
248     ResidueColour[14] = colours[8]; // P
249     ResidueColour[15] = colours[1]; // S
250     ResidueColour[16] = colours[1]; // T
251     ResidueColour[17] = colours[0]; // W
252     ResidueColour[18] = colours[10]; // Y
253     ResidueColour[19] = colours[0]; // V
254   }
255
256   public Color findColour(char c)
257   {
258     return Color.pink;
259   }
260
261   public Color findColour(char c, int j)
262   {
263     Color currentColour;
264
265     if (cons2.length <= j
266             || (includeGaps && threshold != 0 && !aboveThreshold(c, j)))
267     {
268       return Color.white;
269     }
270
271     int i = ResidueProperties.aaIndex[c];
272
273     currentColour = Color.white;
274
275     if (i > 19)
276     {
277       return currentColour;
278     }
279
280     for (int k = 0; k < ResidueColour[i].conses.length; k++)
281     {
282       if (ResidueColour[i].conses[k].isConserved(cons2, j, size,
283               includeGaps))
284       {
285         currentColour = ResidueColour[i].c;
286       }
287     }
288
289     if (i == 4)
290     {
291       if (conses[27].isConserved(cons2, j, size, includeGaps))
292       {
293         currentColour = (Color) colhash.get("PINK");
294       }
295     }
296
297     if (conservationColouring)
298     {
299       currentColour = applyConservation(currentColour, j);
300     }
301
302     return currentColour;
303   }
304
305   /**
306    * @return the includeGaps
307    */
308   protected boolean isIncludeGaps()
309   {
310     return includeGaps;
311   }
312
313   /**
314    * @param includeGaps
315    *                the includeGaps to set
316    */
317   protected void setIncludeGaps(boolean includeGaps)
318   {
319     this.includeGaps = includeGaps;
320   }
321 }
322
323 class ConsensusColour
324 {
325   Consensus[] conses;
326
327   Color c;
328
329   public ConsensusColour(Color c, Consensus[] conses)
330   {
331     this.conses = conses;
332
333     // this.list = list;
334     this.c = c;
335   }
336 }