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