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.renderer;
23 import jalview.analysis.Conservation;
24 import jalview.api.ViewStyleI;
25 import jalview.datamodel.AnnotatedCollectionI;
26 import jalview.datamodel.ProfileI;
27 import jalview.datamodel.ProfilesI;
28 import jalview.datamodel.SequenceCollectionI;
29 import jalview.datamodel.SequenceI;
30 import jalview.schemes.ColourSchemeI;
31 import jalview.util.ColorUtils;
32 import jalview.util.Comparison;
34 import java.awt.Color;
38 * A class that computes the colouring of an alignment (or subgroup). Currently
39 * the factors that may influence residue colouring are
41 * <li>the colour scheme that provides a colour for each aligned residue</li>
42 * <li>any threshold for colour, based on percentage identity with
44 * <li>any graduation based on conservation of physico-chemical properties</li>
50 public class ResidueShader implements ResidueShaderI
52 private static final int INITIAL_CONSERVATION = 30;
55 * the colour scheme that gives the colour of each residue
56 * before applying any conservation or PID shading
58 private ColourSchemeI colourScheme;
61 * the consensus data for each column
63 private ProfilesI consensus;
66 * if true, apply shading of colour by conservation
68 private boolean conservationColouring;
71 * the physico-chemical property conservation scores for columns, with values
72 * 0-9, '+' (all properties conserved), '*' (residue fully conserved) or '-' (gap)
73 * (may be null if colour by conservation is not selected)
75 private char[] conservation;
78 * minimum percentage identity for colour to be applied;
79 * if above zero, residue must match consensus (or joint consensus)
80 * and column have >= pidThreshold identity with the residue
82 private int pidThreshold;
85 * if true, ignore gaps in percentage identity calculation
87 private boolean ignoreGaps;
90 * setting of the By Conservation slider
92 private int conservationIncrement = INITIAL_CONSERVATION;
94 public ResidueShader(ColourSchemeI cs)
100 * Default constructor
102 public ResidueShader()
107 * Constructor given view style settings
111 public ResidueShader(ViewStyleI viewStyle)
113 // TODO remove duplicated storing of conservation / pid thresholds?
115 setConservationApplied(viewStyle.isConservationColourSelected());
116 // setThreshold(viewStyle.getThreshold());
122 public ResidueShader(ResidueShader rs)
124 this.colourScheme = rs.colourScheme;
125 this.consensus = rs.consensus;
126 this.conservation = rs.conservation;
127 this.conservationColouring = rs.conservationColouring;
128 this.conservationIncrement = rs.conservationIncrement;
129 this.ignoreGaps = rs.ignoreGaps;
130 this.pidThreshold = rs.pidThreshold;
134 * @see jalview.renderer.ResidueShaderI#setConsensus(jalview.datamodel.ProfilesI)
137 public void setConsensus(ProfilesI cons)
143 * @see jalview.renderer.ResidueShaderI#conservationApplied()
146 public boolean conservationApplied()
148 return conservationColouring;
152 * @see jalview.renderer.ResidueShaderI#setConservationApplied(boolean)
155 public void setConservationApplied(boolean conservationApplied)
157 conservationColouring = conservationApplied;
161 * @see jalview.renderer.ResidueShaderI#setConservation(jalview.analysis.Conservation)
164 public void setConservation(Conservation cons)
168 conservationColouring = false;
173 conservationColouring = true;
174 conservation = cons.getConsSequence().getSequenceAsString()
181 * @see jalview.renderer.ResidueShaderI#alignmentChanged(jalview.datamodel.AnnotatedCollectionI,
185 public void alignmentChanged(AnnotatedCollectionI alignment,
186 Map<SequenceI, SequenceCollectionI> hiddenReps)
188 if (colourScheme != null)
190 colourScheme.alignmentChanged(alignment, hiddenReps);
195 * @see jalview.renderer.ResidueShaderI#setThreshold(int, boolean)
198 public void setThreshold(int consensusThreshold, boolean ignoreGaps)
200 pidThreshold = consensusThreshold;
201 this.ignoreGaps = ignoreGaps;
205 * @see jalview.renderer.ResidueShaderI#setConservationInc(int)
208 public void setConservationInc(int i)
210 conservationIncrement = i;
214 * @see jalview.renderer.ResidueShaderI#getConservationInc()
217 public int getConservationInc()
219 return conservationIncrement;
223 * @see jalview.renderer.ResidueShaderI#getThreshold()
226 public int getThreshold()
232 * @see jalview.renderer.ResidueShaderI#findColour(char, int,
233 * jalview.datamodel.SequenceI)
236 public Color findColour(char symbol, int position, SequenceI seq)
238 if (colourScheme == null)
240 return Color.white; // Colour is 'None'
246 ProfileI profile = consensus == null ? null : consensus.get(position);
247 String modalResidue = profile == null ? null
248 : profile.getModalResidue();
249 float pid = profile == null ? 0f
250 : profile.getPercentageIdentity(ignoreGaps);
251 Color colour = colourScheme.findColour(symbol, position, seq,
255 * apply PID threshold and consensus fading if in force
257 if (!Comparison.isGap(symbol))
259 colour = adjustColour(symbol, position, colour);
266 * Adjusts colour by applying thresholding or conservation shading, if in
269 * <li>if there is a threshold set for colouring, and the residue doesn't
270 * match the consensus (or a joint consensus) residue, or the consensus score
271 * is not above the threshold, then the colour is set to white</li>
272 * <li>if conservation colouring is selected, the colour is faded by an amount
273 * depending on the conservation score for the column, and the conservation
274 * colour threshold</li>
282 protected Color adjustColour(char symbol, int column, Color colour)
284 if (!aboveThreshold(symbol, column))
286 colour = Color.white;
289 if (conservationColouring)
291 colour = applyConservation(colour, column);
297 * Answers true if there is a consensus profile for the specified column, and
298 * the given residue matches the consensus (or joint consensus) residue for
299 * the column, and the percentage identity for the profile is equal to or
300 * greater than the current threshold; else answers false. The percentage
301 * calculation depends on whether or not we are ignoring gapped sequences.
305 * (index into consensus profiles)
308 * @see #setThreshold(int, boolean)
310 protected boolean aboveThreshold(char residue, int column)
312 if (pidThreshold == 0)
316 if ('a' <= residue && residue <= 'z')
319 // Faster than toUpperCase
320 residue -= ('a' - 'A');
323 if (consensus == null)
328 ProfileI profile = consensus.get(column);
331 * test whether this is the consensus (or joint consensus) residue
334 && profile.getModalResidue().contains(String.valueOf(residue)))
336 if (profile.getPercentageIdentity(ignoreGaps) >= pidThreshold)
346 * Applies a combination of column conservation score, and conservation
347 * percentage slider, to 'bleach' out the residue colours towards white.
349 * If a column is fully conserved (identical residues, conservation score 11,
350 * shown as *), or all 10 physico-chemical properties are conserved
351 * (conservation score 10, shown as +), then the colour is left unchanged.
353 * Otherwise a 'bleaching' factor is computed and applied to the colour. This
354 * is designed to fade colours for scores of 0-9 completely to white at slider
355 * positions ranging from 18% - 100% respectively.
357 * @param currentColour
360 * @return bleached (or unmodified) colour
362 protected Color applyConservation(Color currentColour, int column)
364 if (conservation == null || conservation.length <= column)
366 return currentColour;
368 char conservationScore = conservation[column];
371 * if residues are fully conserved (* or 11), or all properties
372 * are conserved (+ or 10), leave colour unchanged
374 if (conservationScore == '*' || conservationScore == '+'
375 || conservationScore == (char) 10
376 || conservationScore == (char) 11)
378 return currentColour;
381 if (Comparison.isGap(conservationScore))
387 * convert score 0-9 to a bleaching factor 1.1 - 0.2
389 float bleachFactor = (11 - (conservationScore - '0')) / 10f;
392 * scale this up by 0-5 (percentage slider / 20)
393 * as a result, scores of: 0 1 2 3 4 5 6 7 8 9
394 * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100%
396 bleachFactor *= (conservationIncrement / 20f);
398 return ColorUtils.bleachColour(currentColour, bleachFactor);
402 * @see jalview.renderer.ResidueShaderI#getColourScheme()
405 public ColourSchemeI getColourScheme()
407 return this.colourScheme;
411 * @see jalview.renderer.ResidueShaderI#setColourScheme(jalview.schemes.ColourSchemeI)
414 public void setColourScheme(ColourSchemeI cs)