1 package jalview.renderer;
3 import jalview.analysis.Conservation;
4 import jalview.api.ViewStyleI;
5 import jalview.datamodel.AnnotatedCollectionI;
6 import jalview.datamodel.ProfileI;
7 import jalview.datamodel.ProfilesI;
8 import jalview.datamodel.SequenceCollectionI;
9 import jalview.datamodel.SequenceI;
10 import jalview.schemes.ColourSchemeI;
11 import jalview.util.ColorUtils;
12 import jalview.util.Comparison;
14 import java.awt.Color;
18 * A class that computes the colouring of an alignment (or subgroup). Currently
19 * the factors that may influence residue colouring are
21 * <li>the colour scheme that provides a colour for each aligned residue</li>
22 * <li>any threshold for colour, based on percentage identity with consensus</li>
23 * <li>any graduation based on conservation of physico-chemical properties</li>
29 public class ResidueShader implements ResidueShaderI
31 private static final int INITIAL_CONSERVATION = 30;
34 * the colour scheme that gives the colour of each residue
35 * before applying any conservation or PID shading
37 private ColourSchemeI colourScheme;
40 * the consensus data for each column
42 private ProfilesI consensus;
45 * if true, apply shading of colour by conservation
47 private boolean conservationColouring;
50 * the physico-chemical property conservation scores for columns, with values
51 * 0-9, '+' (all properties conserved), '*' (residue fully conserved) or '-' (gap)
52 * (may be null if colour by conservation is not selected)
54 private char[] conservation;
57 * minimum percentage identity for colour to be applied;
58 * if above zero, residue must match consensus (or joint consensus)
59 * and column have >= pidThreshold identity with the residue
61 private int pidThreshold;
64 * if true, ignore gaps in percentage identity calculation
66 private boolean ignoreGaps;
69 * setting of the By Conservation slider
71 private int conservationIncrement = INITIAL_CONSERVATION;
73 public ResidueShader(ColourSchemeI cs)
81 public ResidueShader()
86 * Constructor given view style settings
90 public ResidueShader(ViewStyleI viewStyle)
92 // TODO remove duplicated storing of conservation / pid thresholds?
94 setConservationApplied(viewStyle.isConservationColourSelected());
95 // setThreshold(viewStyle.getThreshold());
101 public ResidueShader(ResidueShader rs)
103 this.colourScheme = rs.colourScheme;
104 this.consensus = rs.consensus;
105 this.conservation = rs.conservation;
106 this.conservationColouring = rs.conservationColouring;
107 this.conservationIncrement = rs.conservationIncrement;
108 this.ignoreGaps = rs.ignoreGaps;
109 this.pidThreshold = rs.pidThreshold;
113 * @see jalview.renderer.ResidueShaderI#setConsensus(jalview.datamodel.ProfilesI)
116 public void setConsensus(ProfilesI cons)
122 * @see jalview.renderer.ResidueShaderI#conservationApplied()
125 public boolean conservationApplied()
127 return conservationColouring;
131 * @see jalview.renderer.ResidueShaderI#setConservationApplied(boolean)
134 public void setConservationApplied(boolean conservationApplied)
136 conservationColouring = conservationApplied;
140 * @see jalview.renderer.ResidueShaderI#setConservation(jalview.analysis.Conservation)
143 public void setConservation(Conservation cons)
147 conservationColouring = false;
152 conservationColouring = true;
153 conservation = cons.getConsSequence().getSequenceAsString()
160 * @see jalview.renderer.ResidueShaderI#alignmentChanged(jalview.datamodel.AnnotatedCollectionI,
164 public void alignmentChanged(AnnotatedCollectionI alignment,
165 Map<SequenceI, SequenceCollectionI> hiddenReps)
167 if (colourScheme != null)
169 colourScheme.alignmentChanged(alignment, hiddenReps);
174 * @see jalview.renderer.ResidueShaderI#setThreshold(int, boolean)
177 public void setThreshold(int consensusThreshold, boolean ignoreGaps)
179 pidThreshold = consensusThreshold;
180 this.ignoreGaps = ignoreGaps;
184 * @see jalview.renderer.ResidueShaderI#setConservationInc(int)
187 public void setConservationInc(int i)
189 conservationIncrement = i;
193 * @see jalview.renderer.ResidueShaderI#getConservationInc()
196 public int getConservationInc()
198 return conservationIncrement;
202 * @see jalview.renderer.ResidueShaderI#getThreshold()
205 public int getThreshold()
211 * @see jalview.renderer.ResidueShaderI#findColour(char, int,
212 * jalview.datamodel.SequenceI)
215 public Color findColour(char symbol, int position, SequenceI seq)
220 ProfileI profile = consensus == null ? null : consensus.get(position);
221 String modalResidue = profile == null ? null : profile
223 float pid = profile == null ? 0f : profile
224 .getPercentageIdentity(ignoreGaps);
225 Color colour = colourScheme == null ? Color.white : colourScheme
226 .findColour(symbol, position, seq, modalResidue, pid);
229 * apply PID threshold and consensus fading if in force
231 colour = adjustColour(symbol, position, colour);
237 * Adjusts colour by applying thresholding or conservation shading, if in
240 * <li>if there is a threshold set for colouring, and the residue doesn't
241 * match the consensus (or a joint consensus) residue, or the consensus score
242 * is not above the threshold, then the colour is set to white</li>
243 * <li>if conservation colouring is selected, the colour is faded by an amount
244 * depending on the conservation score for the column, and the conservation
245 * colour threshold</li>
253 protected Color adjustColour(char symbol, int column, Color colour)
255 if (!aboveThreshold(symbol, column))
257 colour = Color.white;
260 if (conservationColouring)
262 colour = applyConservation(colour, column);
268 * Answers true if there is a consensus profile for the specified column, and
269 * the given residue matches the consensus (or joint consensus) residue for
270 * the column, and the percentage identity for the profile is equal to or
271 * greater than the current threshold; else answers false. The percentage
272 * calculation depends on whether or not we are ignoring gapped sequences.
276 * (index into consensus profiles)
279 * @see #setThreshold(int, boolean)
281 protected boolean aboveThreshold(char residue, int column)
283 if (pidThreshold == 0)
287 if ('a' <= residue && residue <= 'z')
290 // Faster than toUpperCase
291 residue -= ('a' - 'A');
294 if (consensus == null)
299 ProfileI profile = consensus.get(column);
302 * test whether this is the consensus (or joint consensus) residue
305 && profile.getModalResidue().contains(String.valueOf(residue)))
307 if (profile.getPercentageIdentity(ignoreGaps) >= pidThreshold)
317 * Applies a combination of column conservation score, and conservation
318 * percentage slider, to 'bleach' out the residue colours towards white.
320 * If a column is fully conserved (identical residues, conservation score 11,
321 * shown as *), or all 10 physico-chemical properties are conserved
322 * (conservation score 10, shown as +), then the colour is left unchanged.
324 * Otherwise a 'bleaching' factor is computed and applied to the colour. This
325 * is designed to fade colours for scores of 0-9 completely to white at slider
326 * positions ranging from 18% - 100% respectively.
328 * @param currentColour
331 * @return bleached (or unmodified) colour
333 protected Color applyConservation(Color currentColour, int column)
335 if (conservation == null || conservation.length <= column)
337 return currentColour;
339 char conservationScore = conservation[column];
342 * if residues are fully conserved (* or 11), or all properties
343 * are conserved (+ or 10), leave colour unchanged
345 if (conservationScore == '*' || conservationScore == '+'
346 || conservationScore == (char) 10
347 || conservationScore == (char) 11)
349 return currentColour;
352 if (Comparison.isGap(conservationScore))
358 * convert score 0-9 to a bleaching factor 1.1 - 0.2
360 float bleachFactor = (11 - (conservationScore - '0')) / 10f;
363 * scale this up by 0-5 (percentage slider / 20)
364 * as a result, scores of: 0 1 2 3 4 5 6 7 8 9
365 * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100%
367 bleachFactor *= (conservationIncrement / 20f);
369 return ColorUtils.bleachColour(currentColour, bleachFactor);
373 * @see jalview.renderer.ResidueShaderI#getColourScheme()
376 public ColourSchemeI getColourScheme()
378 return this.colourScheme;
382 * @see jalview.renderer.ResidueShaderI#setColourScheme(jalview.schemes.ColourSchemeI)
385 public void setColourScheme(ColourSchemeI cs)