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.schemes;
23 import jalview.analysis.Conservation;
24 import jalview.datamodel.AlignmentI;
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.util.ColorUtils;
31 import jalview.util.Comparison;
33 import java.awt.Color;
37 * Base class for residue-based colour schemes
39 public abstract class ResidueColourScheme implements ColourSchemeI
41 public static final String NONE = "None";
43 public static final String USER_DEFINED = "User Defined";
46 * lookup up by character value e.g. 'G' to the colors array index
47 * e.g. if symbolIndex['K'] = 11 then colors[11] is the colour for K
49 final int[] symbolIndex;
51 boolean conservationColouring = false;
54 * colour for residue characters as indexed by symbolIndex
56 Color[] colors = null;
60 /* Set when threshold colouring to either pid_gaps or pid_nogaps */
61 protected boolean ignoreGaps = false;
64 * Consensus data indexed by column
69 * Conservation string as a char array
74 * The conservation slider percentage setting
79 * Creates a new ResidueColourScheme object.
81 * @param final int[] index table into colors (ResidueProperties.naIndex or
82 * ResidueProperties.aaIndex)
84 * colours for symbols in sequences
86 * threshold for conservation shading
88 public ResidueColourScheme(int[] aaOrnaIndex, Color[] colours,
91 symbolIndex = aaOrnaIndex;
92 this.colors = colours;
93 this.threshold = threshold;
97 * Creates a new ResidueColourScheme object with a lookup table for indexing
100 public ResidueColourScheme(int[] aaOrNaIndex)
102 symbolIndex = aaOrNaIndex;
106 * Creates a new ResidueColourScheme object - default constructor for
107 * non-sequence dependent colourschemes
109 public ResidueColourScheme()
115 * Find a colour without an index in a sequence
118 public Color findColour(char c)
120 return colors == null ? Color.white : colors[symbolIndex[c]];
124 public Color findColour(char c, int j, SequenceI seq)
126 Color colour = Color.white;
128 if (colors != null && symbolIndex != null)
130 colour = colors[symbolIndex[c]];
132 colour = adjustColour(c, j, colour);
138 * Adjusts colour by applying thresholding or conservation shading, if in
141 * <li>if there is a threshold set for colouring, and the residue doesn't
142 * match the consensus (or a joint consensus) residue, or the consensus score
143 * is not above the threshold, then the colour is set to white</li>
144 * <li>if conservation colouring is selected, the colour is faded by an amount
145 * depending on the conservation score for the column, and the conservation
146 * colour threshold</li>
154 protected Color adjustColour(char symbol, int column, Color colour)
156 if (!aboveThreshold(symbol, column))
158 colour = Color.white;
161 if (conservationColouring)
163 colour = applyConservation(colour, column);
169 * Get the percentage threshold for this colour scheme
171 * @return Returns the percentage threshold
174 public int getThreshold()
180 * Sets the percentage consensus threshold value, and whether gaps are ignored
181 * in percentage identity calculation
183 * @param consensusThreshold
187 public void setThreshold(int consensusThreshold, boolean ignoreGaps)
189 threshold = consensusThreshold;
190 this.ignoreGaps = ignoreGaps;
194 * Answers true if there is a consensus profile for the specified column, and
195 * the given residue matches the consensus (or joint consensus) residue for
196 * the column, and the percentage identity for the profile is equal to or
197 * greater than the current threshold; else answers false. The percentage
198 * calculation depends on whether or not we are ignoring gapped sequences.
202 * (index into consensus profiles)
205 * @see #setThreshold(int, boolean)
207 public boolean aboveThreshold(char residue, int column)
213 if ('a' <= residue && residue <= 'z')
216 // Faster than toUpperCase
217 residue -= ('a' - 'A');
220 if (consensus == null)
225 ProfileI profile = consensus.get(column);
228 * test whether this is the consensus (or joint consensus) residue
231 && profile.getModalResidue().contains(String.valueOf(residue)))
233 if (profile.getPercentageIdentity(ignoreGaps) >= threshold)
243 public boolean conservationApplied()
245 return conservationColouring;
249 public void setConservationApplied(boolean conservationApplied)
251 conservationColouring = conservationApplied;
255 public void setConservationInc(int i)
261 public int getConservationInc()
273 public void setConsensus(ProfilesI consensus)
275 if (consensus == null)
280 this.consensus = consensus;
284 public void setConservation(Conservation cons)
288 conservationColouring = false;
293 conservationColouring = true;
294 int iSize = cons.getConsSequence().getLength();
295 conservation = new char[iSize];
296 for (int i = 0; i < iSize; i++)
298 conservation[i] = cons.getConsSequence().getCharAt(i);
305 * Applies a combination of column conservation score, and conservation
306 * percentage slider, to 'bleach' out the residue colours towards white.
308 * If a column is fully conserved (identical residues, conservation score 11,
309 * shown as *), or all 10 physico-chemical properties are conserved
310 * (conservation score 10, shown as +), then the colour is left unchanged.
312 * Otherwise a 'bleaching' factor is computed and applied to the colour. This
313 * is designed to fade colours for scores of 0-9 completely to white at slider
314 * positions ranging from 18% - 100% respectively.
316 * @param currentColour
319 * @return bleached (or unmodified) colour
321 Color applyConservation(Color currentColour, int column)
323 if (conservation == null || conservation.length <= column)
325 return currentColour;
327 char conservationScore = conservation[column];
330 * if residues are fully conserved (* or 11), or all properties
331 * are conserved (+ or 10), leave colour unchanged
333 if (conservationScore == '*' || conservationScore == '+'
334 || conservationScore == (char) 10
335 || conservationScore == (char) 11)
337 return currentColour;
340 if (Comparison.isGap(conservationScore))
346 * convert score 0-9 to a bleaching factor 1.1 - 0.2
348 float bleachFactor = (11 - (conservationScore - '0')) / 10f;
351 * scale this up by 0-5 (percentage slider / 20)
352 * as a result, scores of: 0 1 2 3 4 5 6 7 8 9
353 * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100%
355 bleachFactor *= (inc / 20f);
357 return ColorUtils.bleachColour(currentColour, bleachFactor);
361 public void alignmentChanged(AnnotatedCollectionI alignment,
362 Map<SequenceI, SequenceCollectionI> hiddenReps)
367 * Answers false if the colour scheme is nucleotide or peptide specific, and
368 * the data does not match, else true. Override to modify or extend this test
372 public boolean isApplicableTo(AnnotatedCollectionI ac)
374 if (!isPeptideSpecific() && !isNucleotideSpecific())
380 * inspect the data context (alignment) for residue type
382 boolean nucleotide = false;
383 if (ac instanceof AlignmentI)
385 nucleotide = ((AlignmentI) ac).isNucleotide();
389 AnnotatedCollectionI context = ac.getContext();
390 if (context instanceof AlignmentI)
392 nucleotide = ((AlignmentI) context).isNucleotide();
396 // not sure what's going on, play safe
402 * does data type match colour scheme type?
404 return (nucleotide && isNucleotideSpecific())
405 || (!nucleotide && isPeptideSpecific());
409 * Answers true if the colour scheme is normally only for peptide data
413 public boolean isPeptideSpecific()
419 * Answers true if the colour scheme is normally only for nucleotide data
423 public boolean isNucleotideSpecific()
429 * Default method returns true. Override this to return false in colour
430 * schemes that are not determined solely by the sequence symbol.
433 public boolean isSimple()