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";
45 * lookup up by character value e.g. 'G' to the colors array index
46 * e.g. if symbolIndex['K'] = 11 then colors[11] is the colour for K
48 final int[] symbolIndex;
50 boolean conservationColouring = false;
53 * colour for residue characters as indexed by symbolIndex
55 Color[] colors = null;
59 /* Set when threshold colouring to either pid_gaps or pid_nogaps */
60 protected boolean ignoreGaps = false;
63 * Consensus data indexed by column
68 * Conservation string as a char array
73 * The conservation slider percentage setting
78 * Creates a new ResidueColourScheme object.
80 * @param final int[] index table into colors (ResidueProperties.naIndex or
81 * ResidueProperties.aaIndex)
83 * colours for symbols in sequences
85 * threshold for conservation shading
87 public ResidueColourScheme(int[] aaOrnaIndex, Color[] colours,
90 symbolIndex = aaOrnaIndex;
91 this.colors = colours;
92 this.threshold = threshold;
96 * Creates a new ResidueColourScheme object with a lookup table for indexing
99 public ResidueColourScheme(int[] aaOrNaIndex)
101 symbolIndex = aaOrNaIndex;
105 * Creates a new ResidueColourScheme object - default constructor for
106 * non-sequence dependent colourschemes
108 public ResidueColourScheme()
114 * Returns the colour for symbol 'A'. Intended for use in a 'fixed colour'
115 * colour scheme (for example a feature colour).
118 public Color findColour()
120 return findColour('A');
124 * Find a colour without an index in a sequence
127 public Color findColour(char c)
129 return colors == null ? Color.white : colors[symbolIndex[c]];
133 public Color findColour(char c, int j, SequenceI seq)
137 if (colors != null && symbolIndex != null && (threshold == 0)
138 || aboveThreshold(c, j))
140 currentColour = colors[symbolIndex[c]];
144 currentColour = Color.white;
147 if (conservationColouring)
149 currentColour = applyConservation(currentColour, j);
152 return currentColour;
156 * Get the percentage threshold for this colour scheme
158 * @return Returns the percentage threshold
161 public int getThreshold()
167 * Sets the percentage consensus threshold value, and whether gaps are ignored
168 * in percentage identity calculation
170 * @param consensusThreshold
174 public void setThreshold(int consensusThreshold, boolean ignoreGaps)
176 threshold = consensusThreshold;
177 this.ignoreGaps = ignoreGaps;
181 * Answers true if there is a consensus profile for the specified column, and
182 * the given residue matches the consensus (or joint consensus) residue for
183 * the column, and the percentage identity for the profile is equal to or
184 * greater than the current threshold; else answers false. The percentage
185 * calculation depends on whether or not we are ignoring gapped sequences.
189 * (index into consensus profiles)
192 * @see #setThreshold(int, boolean)
194 public boolean aboveThreshold(char residue, int column)
196 if ('a' <= residue && residue <= 'z')
199 // Faster than toUpperCase
200 residue -= ('a' - 'A');
203 if (consensus == null)
208 ProfileI profile = consensus.get(column);
211 * test whether this is the consensus (or joint consensus) residue
214 && profile.getModalResidue().contains(String.valueOf(residue)))
216 if (profile.getPercentageIdentity(ignoreGaps) >= threshold)
226 public boolean conservationApplied()
228 return conservationColouring;
232 public void setConservationApplied(boolean conservationApplied)
234 conservationColouring = conservationApplied;
238 public void setConservationInc(int i)
244 public int getConservationInc()
256 public void setConsensus(ProfilesI consensus)
258 if (consensus == null)
263 this.consensus = consensus;
267 public void setConservation(Conservation cons)
271 conservationColouring = false;
276 conservationColouring = true;
277 int iSize = cons.getConsSequence().getLength();
278 conservation = new char[iSize];
279 for (int i = 0; i < iSize; i++)
281 conservation[i] = cons.getConsSequence().getCharAt(i);
288 * Applies a combination of column conservation score, and conservation
289 * percentage slider, to 'bleach' out the residue colours towards white.
291 * If a column is fully conserved (identical residues, conservation score 11,
292 * shown as *), or all 10 physico-chemical properties are conserved
293 * (conservation score 10, shown as +), then the colour is left unchanged.
295 * Otherwise a 'bleaching' factor is computed and applied to the colour. This
296 * is designed to fade colours for scores of 0-9 completely to white at slider
297 * positions ranging from 18% - 100% respectively.
299 * @param currentColour
302 * @return bleached (or unmodified) colour
304 Color applyConservation(Color currentColour, int column)
306 if (conservation == null || conservation.length <= column)
308 return currentColour;
310 char conservationScore = conservation[column];
313 * if residues are fully conserved (* or 11), or all properties
314 * are conserved (+ or 10), leave colour unchanged
316 if (conservationScore == '*' || conservationScore == '+'
317 || conservationScore == (char) 10
318 || conservationScore == (char) 11)
320 return currentColour;
323 if (Comparison.isGap(conservationScore))
329 * convert score 0-9 to a bleaching factor 1.1 - 0.2
331 float bleachFactor = (11 - (conservationScore - '0')) / 10f;
334 * scale this up by 0-5 (percentage slider / 20)
335 * as a result, scores of: 0 1 2 3 4 5 6 7 8 9
336 * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100%
338 bleachFactor *= (inc / 20f);
340 return ColorUtils.bleachColour(currentColour, bleachFactor);
344 public void alignmentChanged(AnnotatedCollectionI alignment,
345 Map<SequenceI, SequenceCollectionI> hiddenReps)
350 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
351 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
355 return getClass().newInstance();
356 } catch (Exception q)
358 throw new Error(MessageManager.formatMessage(
359 "error.implementation_error_cannot_duplicate_colour_scheme",
360 new String[] { getClass().getName() }), q);
365 * Answers false if the colour scheme is nucleotide or peptide specific, and
366 * the data does not match, else false. Override to modify or extend this test
370 public boolean isApplicableTo(AnnotatedCollectionI ac)
372 if (!isPeptideSpecific() && !isNucleotideSpecific())
378 * inspect the data context (alignment dataset) for residue type
380 boolean nucleotide = false;
381 AnnotatedCollectionI context = ac.getContext();
384 if (context instanceof AlignmentI)
386 nucleotide = ((AlignmentI) context).isNucleotide();
390 // not sure what's going on, play safe
394 else if (ac instanceof AlignmentI)
396 nucleotide = ((AlignmentI) ac).isNucleotide();
404 * does data type match colour scheme type?
406 return (nucleotide && isNucleotideSpecific())
407 || (!nucleotide && isPeptideSpecific());
411 * Answers true if the colour scheme is normally only for peptide data
415 public boolean isPeptideSpecific()
421 * Answers true if the colour scheme is normally only for nucleotide data
425 public boolean isNucleotideSpecific()
431 public String getSchemeName()