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 * Returns the colour for symbol 'A'. Intended for use in a 'fixed colour'
116 * colour scheme (for example a feature colour).
119 public Color findColour()
121 // TODO delete this method in favour of ColorUtils.parseColourString()?
122 return findColour('A');
126 * Find a colour without an index in a sequence
129 public Color findColour(char c)
131 return colors == null ? Color.white : colors[symbolIndex[c]];
135 public Color findColour(char c, int j, SequenceI seq)
137 Color colour = Color.white;
139 if (colors != null && symbolIndex != null)
141 colour = colors[symbolIndex[c]];
143 colour = adjustColour(c, j, colour);
149 * Adjusts colour by applying thresholding or conservation shading, if in
152 * <li>if there is a threshold set for colouring, and the residue doesn't
153 * match the consensus (or a joint consensus) residue, or the consensus score
154 * is not above the threshold, then the colour is set to white</li>
155 * <li>if conservation colouring is selected, the colour is faded by an amount
156 * depending on the conservation score for the column, and the conservation
157 * colour threshold</li>
165 protected Color adjustColour(char symbol, int column, Color colour)
167 if (!aboveThreshold(symbol, column))
169 colour = Color.white;
172 if (conservationColouring)
174 colour = applyConservation(colour, column);
180 * Get the percentage threshold for this colour scheme
182 * @return Returns the percentage threshold
185 public int getThreshold()
191 * Sets the percentage consensus threshold value, and whether gaps are ignored
192 * in percentage identity calculation
194 * @param consensusThreshold
198 public void setThreshold(int consensusThreshold, boolean ignoreGaps)
200 threshold = consensusThreshold;
201 this.ignoreGaps = ignoreGaps;
205 * Answers true if there is a consensus profile for the specified column, and
206 * the given residue matches the consensus (or joint consensus) residue for
207 * the column, and the percentage identity for the profile is equal to or
208 * greater than the current threshold; else answers false. The percentage
209 * calculation depends on whether or not we are ignoring gapped sequences.
213 * (index into consensus profiles)
216 * @see #setThreshold(int, boolean)
218 public boolean aboveThreshold(char residue, int column)
224 if ('a' <= residue && residue <= 'z')
227 // Faster than toUpperCase
228 residue -= ('a' - 'A');
231 if (consensus == null)
236 ProfileI profile = consensus.get(column);
239 * test whether this is the consensus (or joint consensus) residue
242 && profile.getModalResidue().contains(String.valueOf(residue)))
244 if (profile.getPercentageIdentity(ignoreGaps) >= threshold)
254 public boolean conservationApplied()
256 return conservationColouring;
260 public void setConservationApplied(boolean conservationApplied)
262 conservationColouring = conservationApplied;
266 public void setConservationInc(int i)
272 public int getConservationInc()
284 public void setConsensus(ProfilesI consensus)
286 if (consensus == null)
291 this.consensus = consensus;
295 public void setConservation(Conservation cons)
299 conservationColouring = false;
304 conservationColouring = true;
305 int iSize = cons.getConsSequence().getLength();
306 conservation = new char[iSize];
307 for (int i = 0; i < iSize; i++)
309 conservation[i] = cons.getConsSequence().getCharAt(i);
316 * Applies a combination of column conservation score, and conservation
317 * percentage slider, to 'bleach' out the residue colours towards white.
319 * If a column is fully conserved (identical residues, conservation score 11,
320 * shown as *), or all 10 physico-chemical properties are conserved
321 * (conservation score 10, shown as +), then the colour is left unchanged.
323 * Otherwise a 'bleaching' factor is computed and applied to the colour. This
324 * is designed to fade colours for scores of 0-9 completely to white at slider
325 * positions ranging from 18% - 100% respectively.
327 * @param currentColour
330 * @return bleached (or unmodified) colour
332 Color applyConservation(Color currentColour, int column)
334 if (conservation == null || conservation.length <= column)
336 return currentColour;
338 char conservationScore = conservation[column];
341 * if residues are fully conserved (* or 11), or all properties
342 * are conserved (+ or 10), leave colour unchanged
344 if (conservationScore == '*' || conservationScore == '+'
345 || conservationScore == (char) 10
346 || conservationScore == (char) 11)
348 return currentColour;
351 if (Comparison.isGap(conservationScore))
357 * convert score 0-9 to a bleaching factor 1.1 - 0.2
359 float bleachFactor = (11 - (conservationScore - '0')) / 10f;
362 * scale this up by 0-5 (percentage slider / 20)
363 * as a result, scores of: 0 1 2 3 4 5 6 7 8 9
364 * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100%
366 bleachFactor *= (inc / 20f);
368 return ColorUtils.bleachColour(currentColour, bleachFactor);
372 public void alignmentChanged(AnnotatedCollectionI alignment,
373 Map<SequenceI, SequenceCollectionI> hiddenReps)
378 * Answers false if the colour scheme is nucleotide or peptide specific, and
379 * the data does not match, else true. Override to modify or extend this test
383 public boolean isApplicableTo(AnnotatedCollectionI ac)
385 if (!isPeptideSpecific() && !isNucleotideSpecific())
391 * inspect the data context (alignment) for residue type
393 boolean nucleotide = false;
394 if (ac instanceof AlignmentI)
396 nucleotide = ((AlignmentI) ac).isNucleotide();
400 AnnotatedCollectionI context = ac.getContext();
401 if (context instanceof AlignmentI)
403 nucleotide = ((AlignmentI) context).isNucleotide();
407 // not sure what's going on, play safe
413 * does data type match colour scheme type?
415 return (nucleotide && isNucleotideSpecific())
416 || (!nucleotide && isPeptideSpecific());
420 * Answers true if the colour scheme is normally only for peptide data
424 public boolean isPeptideSpecific()
430 * Answers true if the colour scheme is normally only for nucleotide data
434 public boolean isNucleotideSpecific()
440 * Default method returns true. Override this to return false in colour
441 * schemes that are not determined solely by the sequence symbol.
444 public boolean isSimple()