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