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.AnnotatedCollectionI;
25 import jalview.datamodel.ProfileI;
26 import jalview.datamodel.SequenceCollectionI;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.ColorUtils;
29 import jalview.util.Comparison;
30 import jalview.util.MessageManager;
32 import java.awt.Color;
41 public class ResidueColourScheme implements ColourSchemeI
43 final int[] symbolIndex;
45 boolean conservationColouring = false;
47 Color[] colors = null;
51 /* Set when threshold colouring to either pid_gaps or pid_nogaps */
52 protected boolean ignoreGaps = false;
55 * Consensus data indexed by column
60 * Conservation string as a char array
65 * The conservation slider percentage setting
70 * Creates a new ResidueColourScheme object.
72 * @param final int[] index table into colors (ResidueProperties.naIndex or
73 * ResidueProperties.aaIndex)
75 * colours for symbols in sequences
77 * threshold for conservation shading
79 public ResidueColourScheme(int[] aaOrnaIndex, Color[] colours,
82 symbolIndex = aaOrnaIndex;
83 this.colors = colours;
84 this.threshold = threshold;
88 * Creates a new ResidueColourScheme object with a lookup table for indexing
91 public ResidueColourScheme(int[] aaOrNaIndex)
93 symbolIndex = aaOrNaIndex;
97 * Creates a new ResidueColourScheme object - default constructor for
98 * non-sequence dependent colourschemes
100 public ResidueColourScheme()
106 * Find a colour without an index in a sequence
109 public Color findColour(char c)
111 return colors == null ? Color.white : colors[symbolIndex[c]];
115 public Color findColour(char c, int j, SequenceI seq)
119 if (colors != null && symbolIndex != null && (threshold == 0)
120 || aboveThreshold(c, j))
122 currentColour = colors[symbolIndex[c]];
126 currentColour = Color.white;
129 if (conservationColouring)
131 currentColour = applyConservation(currentColour, j);
134 return currentColour;
138 * Get the percentage threshold for this colour scheme
140 * @return Returns the percentage threshold
143 public int getThreshold()
149 * Sets the percentage consensus threshold value, and whether gaps are ignored
150 * in percentage identity calculation
152 * @param consensusThreshold
156 public void setThreshold(int consensusThreshold, boolean ignoreGaps)
158 threshold = consensusThreshold;
159 this.ignoreGaps = ignoreGaps;
163 * Answers true if there is a consensus profile for the specified column, and
164 * the given residue matches the consensus (or joint consensus) residue for
165 * the column, and the percentage identity for the profile is equal to or
166 * greater than the current threshold; else answers false. The percentage
167 * calculation depends on whether or not we are ignoring gapped sequences.
171 * (index into consensus profiles)
174 * @see #setThreshold(int, boolean)
176 public boolean aboveThreshold(char residue, int column)
178 if ('a' <= residue && residue <= 'z')
181 // Faster than toUpperCase
182 residue -= ('a' - 'A');
185 if (consensus == null || consensus.length < column
186 || consensus[column] == null)
192 * test whether this is the consensus (or joint consensus) residue
194 if (consensus[column].getModalResidue().contains(
195 String.valueOf(residue)))
197 if (consensus[column].getPercentageIdentity(ignoreGaps) >= threshold)
207 public boolean conservationApplied()
209 return conservationColouring;
213 public void setConservationApplied(boolean conservationApplied)
215 conservationColouring = conservationApplied;
219 public void setConservationInc(int i)
225 public int getConservationInc()
237 public void setConsensus(ProfileI[] consensus)
239 if (consensus == null)
244 this.consensus = consensus;
248 public void setConservation(Conservation cons)
252 conservationColouring = false;
257 conservationColouring = true;
258 int iSize = cons.getConsSequence().getLength();
259 conservation = new char[iSize];
260 for (int i = 0; i < iSize; i++)
262 conservation[i] = cons.getConsSequence().getCharAt(i);
269 * Applies a combination of column conservation score, and conservation
270 * percentage slider, to 'bleach' out the residue colours towards white.
272 * If a column is fully conserved (identical residues, conservation score 11,
273 * shown as *), or all 10 physico-chemical properties are conserved
274 * (conservation score 10, shown as +), then the colour is left unchanged.
276 * Otherwise a 'bleaching' factor is computed and applied to the colour. This
277 * is designed to fade colours for scores of 0-9 completely to white at slider
278 * positions ranging from 18% - 100% respectively.
280 * @param currentColour
283 * @return bleached (or unmodified) colour
285 Color applyConservation(Color currentColour, int column)
287 if (conservation == null || conservation.length <= column)
289 return currentColour;
291 char conservationScore = conservation[column];
294 * if residues are fully conserved (* or 11), or all properties
295 * are conserved (+ or 10), leave colour unchanged
297 if (conservationScore == '*' || conservationScore == '+'
298 || conservationScore == (char) 10
299 || conservationScore == (char) 11)
301 return currentColour;
304 if (Comparison.isGap(conservationScore))
310 * convert score 0-9 to a bleaching factor 1.1 - 0.2
312 float bleachFactor = (11 - (conservationScore - '0')) / 10f;
315 * scale this up by 0-5 (percentage slider / 20)
316 * as a result, scores of: 0 1 2 3 4 5 6 7 8 9
317 * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100%
319 bleachFactor *= (inc / 20f);
321 return ColorUtils.bleachColour(currentColour, bleachFactor);
325 public void alignmentChanged(AnnotatedCollectionI alignment,
326 Map<SequenceI, SequenceCollectionI> hiddenReps)
331 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
332 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
336 return getClass().newInstance();
337 } catch (Exception q)
339 throw new Error(MessageManager.formatMessage(
340 "error.implementation_error_cannot_duplicate_colour_scheme",
341 new String[] { getClass().getName() }), q);