2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.schemes;
23 import jalview.analysis.Conservation;
24 import jalview.analysis.Profile;
25 import jalview.datamodel.AnnotatedCollectionI;
26 import jalview.datamodel.SequenceCollectionI;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.Comparison;
29 import jalview.util.MessageManager;
31 import java.awt.Color;
40 public class ResidueColourScheme implements ColourSchemeI
42 final int[] symbolIndex;
44 boolean conservationColouring = false;
46 Color[] colors = null;
50 /* Set when threshold colouring to either pid_gaps or pid_nogaps */
51 protected boolean ignoreGaps = false;
54 * Consensus data indexed by column
59 * Conservation string as a char array
64 * The conservation slider percentage setting
69 * Creates a new ResidueColourScheme object.
71 * @param final int[] index table into colors (ResidueProperties.naIndex or
72 * ResidueProperties.aaIndex)
74 * colours for symbols in sequences
76 * threshold for conservation shading
78 public ResidueColourScheme(int[] aaOrnaIndex, Color[] colours,
81 symbolIndex = aaOrnaIndex;
82 this.colors = colours;
83 this.threshold = threshold;
87 * Creates a new ResidueColourScheme object with a lookup table for indexing
90 public ResidueColourScheme(int[] aaOrNaIndex)
92 symbolIndex = aaOrNaIndex;
96 * Creates a new ResidueColourScheme object - default constructor for
97 * non-sequence dependent colourschemes
99 public ResidueColourScheme()
105 * Find a colour without an index in a sequence
108 public Color findColour(char c)
110 return colors == null ? Color.white : colors[symbolIndex[c]];
114 public Color findColour(char c, int j, SequenceI seq)
118 if (colors != null && symbolIndex != null && (threshold == 0)
119 || aboveThreshold(c, j))
121 currentColour = colors[symbolIndex[c]];
125 currentColour = Color.white;
128 if (conservationColouring)
130 currentColour = applyConservation(currentColour, j);
133 return currentColour;
137 * Get the percentage threshold for this colour scheme
139 * @return Returns the percentage threshold
142 public int getThreshold()
148 * Sets the percentage consensus threshold value, and whether gaps are ignored
149 * in percentage identity calculation
151 * @param consensusThreshold
155 public void setThreshold(int consensusThreshold, boolean ignoreGaps)
157 threshold = consensusThreshold;
158 this.ignoreGaps = ignoreGaps;
162 * Answers true if there is a consensus profile for the specified column, and
163 * the given residue matches the consensus (or joint consensus) residue for
164 * the column, and the percentage identity for the profile is equal to or
165 * greater than the current threshold; else answers false. The percentage
166 * calculation depends on whether or not we are ignoring gapped sequences.
170 * (index into consensus profiles)
173 * @see #setThreshold(int, boolean)
175 public boolean aboveThreshold(char residue, int column)
177 if ('a' <= residue && residue <= 'z')
180 // Faster than toUpperCase
181 residue -= ('a' - 'A');
184 if (consensus == null || consensus.length < column
185 || consensus[column] == null)
191 * test whether this is the consensus (or joint consensus) residue
193 if (consensus[column].getModalResidue().contains(
194 String.valueOf(residue)))
196 if (consensus[column].getPercentageIdentity(ignoreGaps) >= threshold)
206 public boolean conservationApplied()
208 return conservationColouring;
212 public void setConservationApplied(boolean conservationApplied)
214 conservationColouring = conservationApplied;
218 public void setConservationInc(int i)
224 public int getConservationInc()
236 public void setConsensus(Profile[] consensus)
238 if (consensus == null)
243 this.consensus = consensus;
247 public void setConservation(Conservation cons)
251 conservationColouring = false;
256 conservationColouring = true;
257 int iSize = cons.getConsSequence().getLength();
258 conservation = new char[iSize];
259 for (int i = 0; i < iSize; i++)
261 conservation[i] = cons.getConsSequence().getCharAt(i);
268 * Applies a combination of column conservation score, and conservation
269 * percentage slider, to 'bleach' out the residue colours towards white.
271 * If a column is fully conserved (identical residues, conservation score 11,
272 * shown as *), or all 10 physico-chemical properties are conserved
273 * (conservation score 10, shown as +), then the colour is left unchanged.
275 * Otherwise a 'bleaching' factor is computed and applied to the colour. This
276 * is designed to fade colours for scores of 0-9 completely to white at slider
277 * positions ranging from 18% - 100% respectively.
279 * @param currentColour
282 * @return bleached (or unmodified) colour
284 Color applyConservation(Color currentColour, int column)
286 if (conservation == null || conservation.length <= column)
288 return currentColour;
290 char conservationScore = conservation[column];
293 * if residues are fully conserved (* or 11), or all properties
294 * are conserved (+ or 10), leave colour unchanged
296 if (conservationScore == '*' || conservationScore == '+'
297 || conservationScore == (char) 10
298 || conservationScore == (char) 11)
300 return currentColour;
303 if (Comparison.isGap(conservationScore))
309 * convert score 0-9 to a bleaching factor 1.1 - 0.2
311 float bleachFactor = (11 - (conservationScore - '0')) / 10f;
314 * scale this by the percentage slider / 20
316 bleachFactor *= (inc / 20f);
318 int red = currentColour.getRed();
319 int green = currentColour.getGreen();
320 int blue = currentColour.getBlue();
323 * bleach colours towards white (255, 255, 255),
324 * depending on the consensus score and the conservation slider value
325 * scores of: 0 1 2 3 4 5 6 7 8 9
326 * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100%
328 red += (255 - red) * bleachFactor;
329 green += (255 - green) * bleachFactor;
330 blue += (255 - blue) * bleachFactor;
332 if (red > 255 || green > 255 || blue > 255)
334 currentColour = Color.white;
338 currentColour = new Color(red, green, blue);
340 return currentColour;
344 public void alignmentChanged(AnnotatedCollectionI alignment,
345 Map<SequenceI, SequenceCollectionI> hiddenReps)
350 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
351 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
355 return getClass().newInstance();
356 } catch (Exception q)
358 throw new Error(MessageManager.formatMessage(
359 "error.implementation_error_cannot_duplicate_colour_scheme",
360 new String[] { getClass().getName() }), q);