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