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());
99 * @see jalview.renderer.ResidueShaderI#setConsensus(jalview.datamodel.ProfilesI)
102 public void setConsensus(ProfilesI cons)
108 * @see jalview.renderer.ResidueShaderI#conservationApplied()
111 public boolean conservationApplied()
113 return conservationColouring;
117 * @see jalview.renderer.ResidueShaderI#setConservationApplied(boolean)
120 public void setConservationApplied(boolean conservationApplied)
122 conservationColouring = conservationApplied;
126 * @see jalview.renderer.ResidueShaderI#setConservation(jalview.analysis.Conservation)
129 public void setConservation(Conservation cons)
133 conservationColouring = false;
138 conservationColouring = true;
139 conservation = cons.getConsSequence().getSequenceAsString()
146 * @see jalview.renderer.ResidueShaderI#alignmentChanged(jalview.datamodel.AnnotatedCollectionI,
150 public void alignmentChanged(AnnotatedCollectionI alignment,
151 Map<SequenceI, SequenceCollectionI> hiddenReps)
153 if (colourScheme != null)
155 colourScheme.alignmentChanged(alignment, hiddenReps);
160 * @see jalview.renderer.ResidueShaderI#setThreshold(int, boolean)
163 public void setThreshold(int consensusThreshold, boolean ignoreGaps)
165 pidThreshold = consensusThreshold;
166 this.ignoreGaps = ignoreGaps;
170 * @see jalview.renderer.ResidueShaderI#setConservationInc(int)
173 public void setConservationInc(int i)
175 conservationIncrement = i;
179 * @see jalview.renderer.ResidueShaderI#getConservationInc()
182 public int getConservationInc()
184 return conservationIncrement;
188 * @see jalview.renderer.ResidueShaderI#getThreshold()
191 public int getThreshold()
197 * @see jalview.renderer.ResidueShaderI#findColour(char, int,
198 * jalview.datamodel.SequenceI)
201 public Color findColour(char symbol, int position, SequenceI seq)
206 ProfileI profile = consensus == null ? null : consensus.get(position);
207 String modalResidue = profile == null ? null : profile
209 float pid = profile == null ? 0f : profile
210 .getPercentageIdentity(ignoreGaps);
211 Color colour = colourScheme == null ? Color.white : colourScheme
212 .findColour(symbol, position, seq, modalResidue, pid);
215 * apply PID threshold and consensus fading if in force
217 colour = adjustColour(symbol, position, colour);
223 * Adjusts colour by applying thresholding or conservation shading, if in
226 * <li>if there is a threshold set for colouring, and the residue doesn't
227 * match the consensus (or a joint consensus) residue, or the consensus score
228 * is not above the threshold, then the colour is set to white</li>
229 * <li>if conservation colouring is selected, the colour is faded by an amount
230 * depending on the conservation score for the column, and the conservation
231 * colour threshold</li>
239 protected Color adjustColour(char symbol, int column, Color colour)
241 if (!aboveThreshold(symbol, column))
243 colour = Color.white;
246 if (conservationColouring)
248 colour = applyConservation(colour, column);
254 * Answers true if there is a consensus profile for the specified column, and
255 * the given residue matches the consensus (or joint consensus) residue for
256 * the column, and the percentage identity for the profile is equal to or
257 * greater than the current threshold; else answers false. The percentage
258 * calculation depends on whether or not we are ignoring gapped sequences.
262 * (index into consensus profiles)
265 * @see #setThreshold(int, boolean)
267 protected boolean aboveThreshold(char residue, int column)
269 if (pidThreshold == 0)
273 if ('a' <= residue && residue <= 'z')
276 // Faster than toUpperCase
277 residue -= ('a' - 'A');
280 if (consensus == null)
285 ProfileI profile = consensus.get(column);
288 * test whether this is the consensus (or joint consensus) residue
291 && profile.getModalResidue().contains(String.valueOf(residue)))
293 if (profile.getPercentageIdentity(ignoreGaps) >= pidThreshold)
303 * Applies a combination of column conservation score, and conservation
304 * percentage slider, to 'bleach' out the residue colours towards white.
306 * If a column is fully conserved (identical residues, conservation score 11,
307 * shown as *), or all 10 physico-chemical properties are conserved
308 * (conservation score 10, shown as +), then the colour is left unchanged.
310 * Otherwise a 'bleaching' factor is computed and applied to the colour. This
311 * is designed to fade colours for scores of 0-9 completely to white at slider
312 * positions ranging from 18% - 100% respectively.
314 * @param currentColour
317 * @return bleached (or unmodified) colour
319 protected Color applyConservation(Color currentColour, int column)
321 if (conservation == null || conservation.length <= column)
323 return currentColour;
325 char conservationScore = conservation[column];
328 * if residues are fully conserved (* or 11), or all properties
329 * are conserved (+ or 10), leave colour unchanged
331 if (conservationScore == '*' || conservationScore == '+'
332 || conservationScore == (char) 10
333 || conservationScore == (char) 11)
335 return currentColour;
338 if (Comparison.isGap(conservationScore))
344 * convert score 0-9 to a bleaching factor 1.1 - 0.2
346 float bleachFactor = (11 - (conservationScore - '0')) / 10f;
349 * scale this up by 0-5 (percentage slider / 20)
350 * as a result, scores of: 0 1 2 3 4 5 6 7 8 9
351 * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100%
353 bleachFactor *= (conservationIncrement / 20f);
355 return ColorUtils.bleachColour(currentColour, bleachFactor);
359 * @see jalview.renderer.ResidueShaderI#getColourScheme()
362 public ColourSchemeI getColourScheme()
364 return this.colourScheme;
368 * @see jalview.renderer.ResidueShaderI#setColourScheme(jalview.schemes.ColourSchemeI)
371 public void setColourScheme(ColourSchemeI cs)
377 public void setInformation(ProfilesI info)
379 // TODO Auto-generated method stub