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)
139 if (colors != null && symbolIndex != null && (threshold == 0)
140 || aboveThreshold(c, j))
142 currentColour = colors[symbolIndex[c]];
146 currentColour = Color.white;
149 if (conservationColouring)
151 currentColour = applyConservation(currentColour, j);
154 return currentColour;
158 * Get the percentage threshold for this colour scheme
160 * @return Returns the percentage threshold
163 public int getThreshold()
169 * Sets the percentage consensus threshold value, and whether gaps are ignored
170 * in percentage identity calculation
172 * @param consensusThreshold
176 public void setThreshold(int consensusThreshold, boolean ignoreGaps)
178 threshold = consensusThreshold;
179 this.ignoreGaps = ignoreGaps;
183 * Answers true if there is a consensus profile for the specified column, and
184 * the given residue matches the consensus (or joint consensus) residue for
185 * the column, and the percentage identity for the profile is equal to or
186 * greater than the current threshold; else answers false. The percentage
187 * calculation depends on whether or not we are ignoring gapped sequences.
191 * (index into consensus profiles)
194 * @see #setThreshold(int, boolean)
196 public boolean aboveThreshold(char residue, int column)
198 if ('a' <= residue && residue <= 'z')
201 // Faster than toUpperCase
202 residue -= ('a' - 'A');
205 if (consensus == null)
210 ProfileI profile = consensus.get(column);
213 * test whether this is the consensus (or joint consensus) residue
216 && profile.getModalResidue().contains(String.valueOf(residue)))
218 if (profile.getPercentageIdentity(ignoreGaps) >= threshold)
228 public boolean conservationApplied()
230 return conservationColouring;
234 public void setConservationApplied(boolean conservationApplied)
236 conservationColouring = conservationApplied;
240 public void setConservationInc(int i)
246 public int getConservationInc()
258 public void setConsensus(ProfilesI consensus)
260 if (consensus == null)
265 this.consensus = consensus;
269 public void setConservation(Conservation cons)
273 conservationColouring = false;
278 conservationColouring = true;
279 int iSize = cons.getConsSequence().getLength();
280 conservation = new char[iSize];
281 for (int i = 0; i < iSize; i++)
283 conservation[i] = cons.getConsSequence().getCharAt(i);
290 * Applies a combination of column conservation score, and conservation
291 * percentage slider, to 'bleach' out the residue colours towards white.
293 * If a column is fully conserved (identical residues, conservation score 11,
294 * shown as *), or all 10 physico-chemical properties are conserved
295 * (conservation score 10, shown as +), then the colour is left unchanged.
297 * Otherwise a 'bleaching' factor is computed and applied to the colour. This
298 * is designed to fade colours for scores of 0-9 completely to white at slider
299 * positions ranging from 18% - 100% respectively.
301 * @param currentColour
304 * @return bleached (or unmodified) colour
306 Color applyConservation(Color currentColour, int column)
308 if (conservation == null || conservation.length <= column)
310 return currentColour;
312 char conservationScore = conservation[column];
315 * if residues are fully conserved (* or 11), or all properties
316 * are conserved (+ or 10), leave colour unchanged
318 if (conservationScore == '*' || conservationScore == '+'
319 || conservationScore == (char) 10
320 || conservationScore == (char) 11)
322 return currentColour;
325 if (Comparison.isGap(conservationScore))
331 * convert score 0-9 to a bleaching factor 1.1 - 0.2
333 float bleachFactor = (11 - (conservationScore - '0')) / 10f;
336 * scale this up by 0-5 (percentage slider / 20)
337 * as a result, scores of: 0 1 2 3 4 5 6 7 8 9
338 * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100%
340 bleachFactor *= (inc / 20f);
342 return ColorUtils.bleachColour(currentColour, bleachFactor);
346 public void alignmentChanged(AnnotatedCollectionI alignment,
347 Map<SequenceI, SequenceCollectionI> hiddenReps)
352 * Answers false if the colour scheme is nucleotide or peptide specific, and
353 * the data does not match, else true. Override to modify or extend this test
357 public boolean isApplicableTo(AnnotatedCollectionI ac)
359 if (!isPeptideSpecific() && !isNucleotideSpecific())
365 * inspect the data context (alignment) for residue type
367 boolean nucleotide = false;
368 if (ac instanceof AlignmentI)
370 nucleotide = ((AlignmentI) ac).isNucleotide();
374 AnnotatedCollectionI context = ac.getContext();
375 if (context instanceof AlignmentI)
377 nucleotide = ((AlignmentI) context).isNucleotide();
381 // not sure what's going on, play safe
387 * does data type match colour scheme type?
389 return (nucleotide && isNucleotideSpecific())
390 || (!nucleotide && isPeptideSpecific());
394 * Answers true if the colour scheme is normally only for peptide data
398 public boolean isPeptideSpecific()
404 * Answers true if the colour scheme is normally only for nucleotide data
408 public boolean isNucleotideSpecific()