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
24 * <li>any graduation based on conservation of physico-chemical properties</li>
30 public class ResidueShader implements ResidueShaderI
32 private static final int INITIAL_CONSERVATION = 30;
35 * the colour scheme that gives the colour of each residue
36 * before applying any conservation or PID shading
38 private ColourSchemeI colourScheme;
41 * the consensus data for each column
43 private ProfilesI consensus;
46 * if true, apply shading of colour by conservation
48 private boolean conservationColouring;
51 * the physico-chemical property conservation scores for columns, with values
52 * 0-9, '+' (all properties conserved), '*' (residue fully conserved) or '-' (gap)
53 * (may be null if colour by conservation is not selected)
55 private char[] conservation;
58 * minimum percentage identity for colour to be applied;
59 * if above zero, residue must match consensus (or joint consensus)
60 * and column have >= pidThreshold identity with the residue
62 private int pidThreshold;
65 * if true, ignore gaps in percentage identity calculation
67 private boolean ignoreGaps;
70 * setting of the By Conservation slider
72 private int conservationIncrement = INITIAL_CONSERVATION;
74 public ResidueShader(ColourSchemeI cs)
82 public ResidueShader()
87 * Constructor given view style settings
91 public ResidueShader(ViewStyleI viewStyle)
93 // TODO remove duplicated storing of conservation / pid thresholds?
95 setConservationApplied(viewStyle.isConservationColourSelected());
96 // setThreshold(viewStyle.getThreshold());
102 public ResidueShader(ResidueShader rs)
104 this.colourScheme = rs.colourScheme;
105 this.consensus = rs.consensus;
106 this.conservation = rs.conservation;
107 this.conservationColouring = rs.conservationColouring;
108 this.conservationIncrement = rs.conservationIncrement;
109 this.ignoreGaps = rs.ignoreGaps;
110 this.pidThreshold = rs.pidThreshold;
114 * @see jalview.renderer.ResidueShaderI#setConsensus(jalview.datamodel.ProfilesI)
117 public void setConsensus(ProfilesI cons)
123 * @see jalview.renderer.ResidueShaderI#conservationApplied()
126 public boolean conservationApplied()
128 return conservationColouring;
132 * @see jalview.renderer.ResidueShaderI#setConservationApplied(boolean)
135 public void setConservationApplied(boolean conservationApplied)
137 conservationColouring = conservationApplied;
141 * @see jalview.renderer.ResidueShaderI#setConservation(jalview.analysis.Conservation)
144 public void setConservation(Conservation cons)
148 conservationColouring = false;
153 conservationColouring = true;
154 conservation = cons.getConsSequence().getSequenceAsString()
161 * @see jalview.renderer.ResidueShaderI#alignmentChanged(jalview.datamodel.AnnotatedCollectionI,
165 public void alignmentChanged(AnnotatedCollectionI alignment,
166 Map<SequenceI, SequenceCollectionI> hiddenReps)
168 if (colourScheme != null)
170 colourScheme.alignmentChanged(alignment, hiddenReps);
175 * @see jalview.renderer.ResidueShaderI#setThreshold(int, boolean)
178 public void setThreshold(int consensusThreshold, boolean ignoreGaps)
180 pidThreshold = consensusThreshold;
181 this.ignoreGaps = ignoreGaps;
185 * @see jalview.renderer.ResidueShaderI#setConservationInc(int)
188 public void setConservationInc(int i)
190 conservationIncrement = i;
194 * @see jalview.renderer.ResidueShaderI#getConservationInc()
197 public int getConservationInc()
199 return conservationIncrement;
203 * @see jalview.renderer.ResidueShaderI#getThreshold()
206 public int getThreshold()
212 * @see jalview.renderer.ResidueShaderI#findColour(char, int,
213 * jalview.datamodel.SequenceI)
216 public Color findColour(char symbol, int position, SequenceI seq)
221 ProfileI profile = consensus == null ? null : consensus.get(position);
222 String modalResidue = profile == null ? null
223 : profile.getModalResidue();
224 float pid = profile == null ? 0f
225 : profile.getPercentageIdentity(ignoreGaps);
226 Color colour = colourScheme == null ? Color.white
227 : colourScheme.findColour(symbol, position, seq, modalResidue,
231 * apply PID threshold and consensus fading if in force
233 colour = adjustColour(symbol, position, colour);
239 * Adjusts colour by applying thresholding or conservation shading, if in
242 * <li>if there is a threshold set for colouring, and the residue doesn't
243 * match the consensus (or a joint consensus) residue, or the consensus score
244 * is not above the threshold, then the colour is set to white</li>
245 * <li>if conservation colouring is selected, the colour is faded by an amount
246 * depending on the conservation score for the column, and the conservation
247 * colour threshold</li>
255 protected Color adjustColour(char symbol, int column, Color colour)
257 if (!aboveThreshold(symbol, column))
259 colour = Color.white;
262 if (conservationColouring)
264 colour = applyConservation(colour, column);
270 * Answers true if there is a consensus profile for the specified column, and
271 * the given residue matches the consensus (or joint consensus) residue for
272 * the column, and the percentage identity for the profile is equal to or
273 * greater than the current threshold; else answers false. The percentage
274 * calculation depends on whether or not we are ignoring gapped sequences.
278 * (index into consensus profiles)
281 * @see #setThreshold(int, boolean)
283 protected boolean aboveThreshold(char residue, int column)
285 if (pidThreshold == 0)
289 if ('a' <= residue && residue <= 'z')
292 // Faster than toUpperCase
293 residue -= ('a' - 'A');
296 if (consensus == null)
301 ProfileI profile = consensus.get(column);
304 * test whether this is the consensus (or joint consensus) residue
307 && profile.getModalResidue().contains(String.valueOf(residue)))
309 if (profile.getPercentageIdentity(ignoreGaps) >= pidThreshold)
319 * Applies a combination of column conservation score, and conservation
320 * percentage slider, to 'bleach' out the residue colours towards white.
322 * If a column is fully conserved (identical residues, conservation score 11,
323 * shown as *), or all 10 physico-chemical properties are conserved
324 * (conservation score 10, shown as +), then the colour is left unchanged.
326 * Otherwise a 'bleaching' factor is computed and applied to the colour. This
327 * is designed to fade colours for scores of 0-9 completely to white at slider
328 * positions ranging from 18% - 100% respectively.
330 * @param currentColour
333 * @return bleached (or unmodified) colour
335 protected Color applyConservation(Color currentColour, int column)
337 if (conservation == null || conservation.length <= column)
339 return currentColour;
341 char conservationScore = conservation[column];
344 * if residues are fully conserved (* or 11), or all properties
345 * are conserved (+ or 10), leave colour unchanged
347 if (conservationScore == '*' || conservationScore == '+'
348 || conservationScore == (char) 10
349 || conservationScore == (char) 11)
351 return currentColour;
354 if (Comparison.isGap(conservationScore))
360 * convert score 0-9 to a bleaching factor 1.1 - 0.2
362 float bleachFactor = (11 - (conservationScore - '0')) / 10f;
365 * scale this up by 0-5 (percentage slider / 20)
366 * as a result, scores of: 0 1 2 3 4 5 6 7 8 9
367 * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100%
369 bleachFactor *= (conservationIncrement / 20f);
371 return ColorUtils.bleachColour(currentColour, bleachFactor);
375 * @see jalview.renderer.ResidueShaderI#getColourScheme()
378 public ColourSchemeI getColourScheme()
380 return this.colourScheme;
384 * @see jalview.renderer.ResidueShaderI#setColourScheme(jalview.schemes.ColourSchemeI)
387 public void setColourScheme(ColourSchemeI cs)