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;
32 import jalview.util.MessageManager;
34 import java.awt.Color;
38 * Base class for residue-based colour schemes
40 public class ResidueColourScheme implements ColourSchemeI
42 public static final String NONE = "None";
44 final int[] symbolIndex;
46 boolean conservationColouring = false;
48 Color[] colors = null;
52 /* Set when threshold colouring to either pid_gaps or pid_nogaps */
53 protected boolean ignoreGaps = false;
56 * Consensus data indexed by column
61 * Conservation string as a char array
66 * The conservation slider percentage setting
71 * Creates a new ResidueColourScheme object.
73 * @param final int[] index table into colors (ResidueProperties.naIndex or
74 * ResidueProperties.aaIndex)
76 * colours for symbols in sequences
78 * threshold for conservation shading
80 public ResidueColourScheme(int[] aaOrnaIndex, Color[] colours,
83 symbolIndex = aaOrnaIndex;
84 this.colors = colours;
85 this.threshold = threshold;
89 * Creates a new ResidueColourScheme object with a lookup table for indexing
92 public ResidueColourScheme(int[] aaOrNaIndex)
94 symbolIndex = aaOrNaIndex;
98 * Creates a new ResidueColourScheme object - default constructor for
99 * non-sequence dependent colourschemes
101 public ResidueColourScheme()
107 * Returns the colour for symbol 'A'. Intended for use in a 'fixed colour'
108 * colour scheme (for example a feature colour).
111 public Color findColour()
113 return findColour('A');
117 * Find a colour without an index in a sequence
120 public Color findColour(char c)
122 return colors == null ? Color.white : colors[symbolIndex[c]];
126 public Color findColour(char c, int j, SequenceI seq)
130 if (colors != null && symbolIndex != null && (threshold == 0)
131 || aboveThreshold(c, j))
133 currentColour = colors[symbolIndex[c]];
137 currentColour = Color.white;
140 if (conservationColouring)
142 currentColour = applyConservation(currentColour, j);
145 return currentColour;
149 * Get the percentage threshold for this colour scheme
151 * @return Returns the percentage threshold
154 public int getThreshold()
160 * Sets the percentage consensus threshold value, and whether gaps are ignored
161 * in percentage identity calculation
163 * @param consensusThreshold
167 public void setThreshold(int consensusThreshold, boolean ignoreGaps)
169 threshold = consensusThreshold;
170 this.ignoreGaps = ignoreGaps;
174 * Answers true if there is a consensus profile for the specified column, and
175 * the given residue matches the consensus (or joint consensus) residue for
176 * the column, and the percentage identity for the profile is equal to or
177 * greater than the current threshold; else answers false. The percentage
178 * calculation depends on whether or not we are ignoring gapped sequences.
182 * (index into consensus profiles)
185 * @see #setThreshold(int, boolean)
187 public boolean aboveThreshold(char residue, int column)
189 if ('a' <= residue && residue <= 'z')
192 // Faster than toUpperCase
193 residue -= ('a' - 'A');
196 if (consensus == null)
201 ProfileI profile = consensus.get(column);
204 * test whether this is the consensus (or joint consensus) residue
207 && profile.getModalResidue().contains(String.valueOf(residue)))
209 if (profile.getPercentageIdentity(ignoreGaps) >= threshold)
219 public boolean conservationApplied()
221 return conservationColouring;
225 public void setConservationApplied(boolean conservationApplied)
227 conservationColouring = conservationApplied;
231 public void setConservationInc(int i)
237 public int getConservationInc()
249 public void setConsensus(ProfilesI consensus)
251 if (consensus == null)
256 this.consensus = consensus;
260 public void setConservation(Conservation cons)
264 conservationColouring = false;
269 conservationColouring = true;
270 int iSize = cons.getConsSequence().getLength();
271 conservation = new char[iSize];
272 for (int i = 0; i < iSize; i++)
274 conservation[i] = cons.getConsSequence().getCharAt(i);
281 * Applies a combination of column conservation score, and conservation
282 * percentage slider, to 'bleach' out the residue colours towards white.
284 * If a column is fully conserved (identical residues, conservation score 11,
285 * shown as *), or all 10 physico-chemical properties are conserved
286 * (conservation score 10, shown as +), then the colour is left unchanged.
288 * Otherwise a 'bleaching' factor is computed and applied to the colour. This
289 * is designed to fade colours for scores of 0-9 completely to white at slider
290 * positions ranging from 18% - 100% respectively.
292 * @param currentColour
295 * @return bleached (or unmodified) colour
297 Color applyConservation(Color currentColour, int column)
299 if (conservation == null || conservation.length <= column)
301 return currentColour;
303 char conservationScore = conservation[column];
306 * if residues are fully conserved (* or 11), or all properties
307 * are conserved (+ or 10), leave colour unchanged
309 if (conservationScore == '*' || conservationScore == '+'
310 || conservationScore == (char) 10
311 || conservationScore == (char) 11)
313 return currentColour;
316 if (Comparison.isGap(conservationScore))
322 * convert score 0-9 to a bleaching factor 1.1 - 0.2
324 float bleachFactor = (11 - (conservationScore - '0')) / 10f;
327 * scale this up by 0-5 (percentage slider / 20)
328 * as a result, scores of: 0 1 2 3 4 5 6 7 8 9
329 * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100%
331 bleachFactor *= (inc / 20f);
333 return ColorUtils.bleachColour(currentColour, bleachFactor);
337 public void alignmentChanged(AnnotatedCollectionI alignment,
338 Map<SequenceI, SequenceCollectionI> hiddenReps)
343 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
344 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
348 return getClass().newInstance();
349 } catch (Exception q)
351 throw new Error(MessageManager.formatMessage(
352 "error.implementation_error_cannot_duplicate_colour_scheme",
353 new String[] { getClass().getName() }), q);
358 * Answers false if the colour scheme is nucleotide or peptide specific, and
359 * the data does not match, else false. Override to modify or extend this test
363 public boolean isApplicableTo(AnnotatedCollectionI ac)
365 if (!isPeptideSpecific() && !isNucleotideSpecific())
371 * inspect the data context (alignment dataset) for residue type
373 boolean nucleotide = false;
374 AnnotatedCollectionI context = ac.getContext();
377 if (context instanceof AlignmentI)
379 nucleotide = ((AlignmentI) context).isNucleotide();
383 // not sure what's going on, play safe
387 else if (ac instanceof AlignmentI)
389 nucleotide = ((AlignmentI) ac).isNucleotide();
397 * does data type match colour scheme type?
399 return (nucleotide && isNucleotideSpecific())
400 || (!nucleotide && isPeptideSpecific());
404 * Answers true if the colour scheme is normally only for peptide data
408 public boolean isPeptideSpecific()
414 * Answers true if the colour scheme is normally only for nucleotide data
418 public boolean isNucleotideSpecific()
424 public String getSchemeName()