1 package jalview.schemes;
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.util.ColorUtils;
10 import jalview.util.Comparison;
12 import java.awt.Color;
16 * A class that computes the colouring of an alignment (or subgroup). Currently
17 * the factors that may influence residue colouring are
19 * <li>the colour scheme that provides a colour for each aligned residue</li>
20 * <li>any threshold for colour, based on percentage identity with consensus</li>
21 * <li>any graduation based on conservation of physico-chemical properties</li>
27 public class CollectionColourScheme implements CollectionColourSchemeI
29 private ColourSchemeI colourScheme;
31 private ProfilesI consensus;
33 private boolean conservationColouring;
35 private char[] conservation;
37 private int threshold;
39 private boolean ignoreGaps;
43 public CollectionColourScheme(ColourSchemeI cs)
51 public CollectionColourScheme()
56 * @see jalview.schemes.CollectionColourSchemeI#setConsensus(jalview.datamodel.ProfilesI)
59 public void setConsensus(ProfilesI cons)
65 * @see jalview.schemes.CollectionColourSchemeI#conservationApplied()
68 public boolean conservationApplied()
70 return conservationColouring;
74 * @see jalview.schemes.CollectionColourSchemeI#setConservationApplied(boolean)
77 public void setConservationApplied(boolean conservationApplied)
79 conservationColouring = conservationApplied;
83 * @see jalview.schemes.CollectionColourSchemeI#setConservation(jalview.analysis.Conservation)
86 public void setConservation(Conservation cons)
90 conservationColouring = false;
95 conservationColouring = true;
96 conservation = cons.getConsSequence().getSequenceAsString()
103 * @see jalview.schemes.CollectionColourSchemeI#alignmentChanged(jalview.datamodel.AnnotatedCollectionI,
107 public void alignmentChanged(AnnotatedCollectionI alignment,
108 Map<SequenceI, SequenceCollectionI> hiddenReps)
110 if (colourScheme != null)
112 colourScheme.alignmentChanged(alignment, hiddenReps);
117 * @see jalview.schemes.CollectionColourSchemeI#setThreshold(int, boolean)
120 public void setThreshold(int consensusThreshold, boolean ignoreGaps)
122 threshold = consensusThreshold;
123 this.ignoreGaps = ignoreGaps;
127 * @see jalview.schemes.CollectionColourSchemeI#setConservationInc(int)
130 public void setConservationInc(int i)
136 * @see jalview.schemes.CollectionColourSchemeI#getConservationInc()
139 public int getConservationInc()
145 * @see jalview.schemes.CollectionColourSchemeI#getThreshold()
148 public int getThreshold()
154 * @see jalview.schemes.CollectionColourSchemeI#findColour(char, int,
155 * jalview.datamodel.SequenceI)
158 public Color findColour(char symbol, int position, SequenceI seq)
163 ProfileI profile = consensus == null ? null : consensus.get(position);
164 String modalResidue = profile == null ? null : profile
166 float pid = profile == null ? 0f : profile
167 .getPercentageIdentity(ignoreGaps);
168 Color colour = colourScheme == null ? Color.white : colourScheme
169 .findColour(symbol, position, seq, modalResidue, pid);
172 * apply PID threshold and consensus fading if in force
174 colour = adjustColour(symbol, position, colour);
180 * Adjusts colour by applying thresholding or conservation shading, if in
183 * <li>if there is a threshold set for colouring, and the residue doesn't
184 * match the consensus (or a joint consensus) residue, or the consensus score
185 * is not above the threshold, then the colour is set to white</li>
186 * <li>if conservation colouring is selected, the colour is faded by an amount
187 * depending on the conservation score for the column, and the conservation
188 * colour threshold</li>
196 protected Color adjustColour(char symbol, int column, Color colour)
198 if (!aboveThreshold(symbol, column))
200 colour = Color.white;
203 if (conservationColouring)
205 colour = applyConservation(colour, column);
211 * Answers true if there is a consensus profile for the specified column, and
212 * the given residue matches the consensus (or joint consensus) residue for
213 * the column, and the percentage identity for the profile is equal to or
214 * greater than the current threshold; else answers false. The percentage
215 * calculation depends on whether or not we are ignoring gapped sequences.
219 * (index into consensus profiles)
222 * @see #setThreshold(int, boolean)
224 protected boolean aboveThreshold(char residue, int column)
230 if ('a' <= residue && residue <= 'z')
233 // Faster than toUpperCase
234 residue -= ('a' - 'A');
237 if (consensus == null)
242 ProfileI profile = consensus.get(column);
245 * test whether this is the consensus (or joint consensus) residue
248 && profile.getModalResidue().contains(String.valueOf(residue)))
250 if (profile.getPercentageIdentity(ignoreGaps) >= threshold)
260 * Applies a combination of column conservation score, and conservation
261 * percentage slider, to 'bleach' out the residue colours towards white.
263 * If a column is fully conserved (identical residues, conservation score 11,
264 * shown as *), or all 10 physico-chemical properties are conserved
265 * (conservation score 10, shown as +), then the colour is left unchanged.
267 * Otherwise a 'bleaching' factor is computed and applied to the colour. This
268 * is designed to fade colours for scores of 0-9 completely to white at slider
269 * positions ranging from 18% - 100% respectively.
271 * @param currentColour
274 * @return bleached (or unmodified) colour
276 protected Color applyConservation(Color currentColour, int column)
278 if (conservation == null || conservation.length <= column)
280 return currentColour;
282 char conservationScore = conservation[column];
285 * if residues are fully conserved (* or 11), or all properties
286 * are conserved (+ or 10), leave colour unchanged
288 if (conservationScore == '*' || conservationScore == '+'
289 || conservationScore == (char) 10
290 || conservationScore == (char) 11)
292 return currentColour;
295 if (Comparison.isGap(conservationScore))
301 * convert score 0-9 to a bleaching factor 1.1 - 0.2
303 float bleachFactor = (11 - (conservationScore - '0')) / 10f;
306 * scale this up by 0-5 (percentage slider / 20)
307 * as a result, scores of: 0 1 2 3 4 5 6 7 8 9
308 * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100%
310 bleachFactor *= (inc / 20f);
312 return ColorUtils.bleachColour(currentColour, bleachFactor);
316 * @see jalview.schemes.CollectionColourSchemeI#getColourScheme()
319 public ColourSchemeI getColourScheme()
321 return this.colourScheme;
325 * @see jalview.schemes.CollectionColourSchemeI#setColourScheme(jalview.schemes.ColourSchemeI)
328 public void setColourScheme(ColourSchemeI cs)