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