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