1 package jalview.renderer;
3 import jalview.analysis.Conservation;
4 import jalview.datamodel.AnnotatedCollectionI;
5 import jalview.datamodel.ProfileI;
6 import jalview.datamodel.ProfilesI;
7 import jalview.datamodel.SequenceCollectionI;
8 import jalview.datamodel.SequenceI;
9 import jalview.schemes.ColourSchemeI;
10 import jalview.util.ColorUtils;
11 import jalview.util.Comparison;
13 import java.awt.Color;
17 * A class that computes the colouring of an alignment (or subgroup). Currently
18 * the factors that may influence residue colouring are
20 * <li>the colour scheme that provides a colour for each aligned residue</li>
21 * <li>any threshold for colour, based on percentage identity with consensus</li>
22 * <li>any graduation based on conservation of physico-chemical properties</li>
28 public class ResidueShader implements ResidueShaderI
30 private ColourSchemeI colourScheme;
32 private ProfilesI consensus;
34 private boolean conservationColouring;
36 private char[] conservation;
38 private int threshold;
40 private boolean ignoreGaps;
44 public ResidueShader(ColourSchemeI cs)
52 public ResidueShader()
57 * @see jalview.renderer.ResidueShaderI#setConsensus(jalview.datamodel.ProfilesI)
60 public void setConsensus(ProfilesI cons)
66 * @see jalview.renderer.ResidueShaderI#conservationApplied()
69 public boolean conservationApplied()
71 return conservationColouring;
75 * @see jalview.renderer.ResidueShaderI#setConservationApplied(boolean)
78 public void setConservationApplied(boolean conservationApplied)
80 conservationColouring = conservationApplied;
84 * @see jalview.renderer.ResidueShaderI#setConservation(jalview.analysis.Conservation)
87 public void setConservation(Conservation cons)
91 conservationColouring = false;
96 conservationColouring = true;
97 conservation = cons.getConsSequence().getSequenceAsString()
104 * @see jalview.renderer.ResidueShaderI#alignmentChanged(jalview.datamodel.AnnotatedCollectionI,
108 public void alignmentChanged(AnnotatedCollectionI alignment,
109 Map<SequenceI, SequenceCollectionI> hiddenReps)
111 if (colourScheme != null)
113 colourScheme.alignmentChanged(alignment, hiddenReps);
118 * @see jalview.renderer.ResidueShaderI#setThreshold(int, boolean)
121 public void setThreshold(int consensusThreshold, boolean ignoreGaps)
123 threshold = consensusThreshold;
124 this.ignoreGaps = ignoreGaps;
128 * @see jalview.renderer.ResidueShaderI#setConservationInc(int)
131 public void setConservationInc(int i)
137 * @see jalview.renderer.ResidueShaderI#getConservationInc()
140 public int getConservationInc()
146 * @see jalview.renderer.ResidueShaderI#getThreshold()
149 public int getThreshold()
155 * @see jalview.renderer.ResidueShaderI#findColour(char, int,
156 * jalview.datamodel.SequenceI)
159 public Color findColour(char symbol, int position, SequenceI seq)
164 ProfileI profile = consensus == null ? null : consensus.get(position);
165 String modalResidue = profile == null ? null : profile
167 float pid = profile == null ? 0f : profile
168 .getPercentageIdentity(ignoreGaps);
169 Color colour = colourScheme == null ? Color.white : colourScheme
170 .findColour(symbol, position, seq, modalResidue, pid);
173 * apply PID threshold and consensus fading if in force
175 colour = adjustColour(symbol, position, colour);
181 * Adjusts colour by applying thresholding or conservation shading, if in
184 * <li>if there is a threshold set for colouring, and the residue doesn't
185 * match the consensus (or a joint consensus) residue, or the consensus score
186 * is not above the threshold, then the colour is set to white</li>
187 * <li>if conservation colouring is selected, the colour is faded by an amount
188 * depending on the conservation score for the column, and the conservation
189 * colour threshold</li>
197 protected Color adjustColour(char symbol, int column, Color colour)
199 if (!aboveThreshold(symbol, column))
201 colour = Color.white;
204 if (conservationColouring)
206 colour = applyConservation(colour, column);
212 * Answers true if there is a consensus profile for the specified column, and
213 * the given residue matches the consensus (or joint consensus) residue for
214 * the column, and the percentage identity for the profile is equal to or
215 * greater than the current threshold; else answers false. The percentage
216 * calculation depends on whether or not we are ignoring gapped sequences.
220 * (index into consensus profiles)
223 * @see #setThreshold(int, boolean)
225 protected boolean aboveThreshold(char residue, int column)
231 if ('a' <= residue && residue <= 'z')
234 // Faster than toUpperCase
235 residue -= ('a' - 'A');
238 if (consensus == null)
243 ProfileI profile = consensus.get(column);
246 * test whether this is the consensus (or joint consensus) residue
249 && profile.getModalResidue().contains(String.valueOf(residue)))
251 if (profile.getPercentageIdentity(ignoreGaps) >= threshold)
261 * Applies a combination of column conservation score, and conservation
262 * percentage slider, to 'bleach' out the residue colours towards white.
264 * If a column is fully conserved (identical residues, conservation score 11,
265 * shown as *), or all 10 physico-chemical properties are conserved
266 * (conservation score 10, shown as +), then the colour is left unchanged.
268 * Otherwise a 'bleaching' factor is computed and applied to the colour. This
269 * is designed to fade colours for scores of 0-9 completely to white at slider
270 * positions ranging from 18% - 100% respectively.
272 * @param currentColour
275 * @return bleached (or unmodified) colour
277 protected Color applyConservation(Color currentColour, int column)
279 if (conservation == null || conservation.length <= column)
281 return currentColour;
283 char conservationScore = conservation[column];
286 * if residues are fully conserved (* or 11), or all properties
287 * are conserved (+ or 10), leave colour unchanged
289 if (conservationScore == '*' || conservationScore == '+'
290 || conservationScore == (char) 10
291 || conservationScore == (char) 11)
293 return currentColour;
296 if (Comparison.isGap(conservationScore))
302 * convert score 0-9 to a bleaching factor 1.1 - 0.2
304 float bleachFactor = (11 - (conservationScore - '0')) / 10f;
307 * scale this up by 0-5 (percentage slider / 20)
308 * as a result, scores of: 0 1 2 3 4 5 6 7 8 9
309 * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100%
311 bleachFactor *= (inc / 20f);
313 return ColorUtils.bleachColour(currentColour, bleachFactor);
317 * @see jalview.renderer.ResidueShaderI#getColourScheme()
320 public ColourSchemeI getColourScheme()
322 return this.colourScheme;
326 * @see jalview.renderer.ResidueShaderI#setColourScheme(jalview.schemes.ColourSchemeI)
329 public void setColourScheme(ColourSchemeI cs)