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