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