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.ProfilesI;
27 import jalview.datamodel.SequenceCollectionI;
28 import jalview.datamodel.SequenceI;
29 import jalview.util.ColorUtils;
30 import jalview.util.Comparison;
31 import jalview.util.MessageManager;
33 import java.awt.Color;
42 public class ResidueColourScheme implements ColourSchemeI
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 * Find a colour without an index in a sequence
110 public Color findColour(char c)
112 return colors == null ? Color.white : colors[symbolIndex[c]];
116 public Color findColour(char c, int j, SequenceI seq)
120 if (colors != null && symbolIndex != null && (threshold == 0)
121 || aboveThreshold(c, j))
123 currentColour = colors[symbolIndex[c]];
127 currentColour = Color.white;
130 if (conservationColouring)
132 currentColour = applyConservation(currentColour, j);
135 return currentColour;
139 * Get the percentage threshold for this colour scheme
141 * @return Returns the percentage threshold
144 public int getThreshold()
150 * Sets the percentage consensus threshold value, and whether gaps are ignored
151 * in percentage identity calculation
153 * @param consensusThreshold
157 public void setThreshold(int consensusThreshold, boolean ignoreGaps)
159 threshold = consensusThreshold;
160 this.ignoreGaps = ignoreGaps;
164 * Answers true if there is a consensus profile for the specified column, and
165 * the given residue matches the consensus (or joint consensus) residue for
166 * the column, and the percentage identity for the profile is equal to or
167 * greater than the current threshold; else answers false. The percentage
168 * calculation depends on whether or not we are ignoring gapped sequences.
172 * (index into consensus profiles)
175 * @see #setThreshold(int, boolean)
177 public boolean aboveThreshold(char residue, int column)
179 if ('a' <= residue && residue <= 'z')
182 // Faster than toUpperCase
183 residue -= ('a' - 'A');
186 if (consensus == null)
191 ProfileI profile = consensus.get(column);
194 * test whether this is the consensus (or joint consensus) residue
197 && profile.getModalResidue().contains(String.valueOf(residue)))
199 if (profile.getPercentageIdentity(ignoreGaps) >= threshold)
209 public boolean conservationApplied()
211 return conservationColouring;
215 public void setConservationApplied(boolean conservationApplied)
217 conservationColouring = conservationApplied;
221 public void setConservationInc(int i)
227 public int getConservationInc()
239 public void setConsensus(ProfilesI consensus)
241 if (consensus == null)
246 this.consensus = consensus;
250 public void setConservation(Conservation cons)
254 conservationColouring = false;
259 conservationColouring = true;
260 int iSize = cons.getConsSequence().getLength();
261 conservation = new char[iSize];
262 for (int i = 0; i < iSize; i++)
264 conservation[i] = cons.getConsSequence().getCharAt(i);
271 * Applies a combination of column conservation score, and conservation
272 * percentage slider, to 'bleach' out the residue colours towards white.
274 * If a column is fully conserved (identical residues, conservation score 11,
275 * shown as *), or all 10 physico-chemical properties are conserved
276 * (conservation score 10, shown as +), then the colour is left unchanged.
278 * Otherwise a 'bleaching' factor is computed and applied to the colour. This
279 * is designed to fade colours for scores of 0-9 completely to white at slider
280 * positions ranging from 18% - 100% respectively.
282 * @param currentColour
285 * @return bleached (or unmodified) colour
287 Color applyConservation(Color currentColour, int column)
289 if (conservation == null || conservation.length <= column)
291 return currentColour;
293 char conservationScore = conservation[column];
296 * if residues are fully conserved (* or 11), or all properties
297 * are conserved (+ or 10), leave colour unchanged
299 if (conservationScore == '*' || conservationScore == '+'
300 || conservationScore == (char) 10
301 || conservationScore == (char) 11)
303 return currentColour;
306 if (Comparison.isGap(conservationScore))
312 * convert score 0-9 to a bleaching factor 1.1 - 0.2
314 float bleachFactor = (11 - (conservationScore - '0')) / 10f;
317 * scale this up by 0-5 (percentage slider / 20)
318 * as a result, scores of: 0 1 2 3 4 5 6 7 8 9
319 * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100%
321 bleachFactor *= (inc / 20f);
323 return ColorUtils.bleachColour(currentColour, bleachFactor);
327 public void alignmentChanged(AnnotatedCollectionI alignment,
328 Map<SequenceI, SequenceCollectionI> hiddenReps)
333 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
334 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
338 return getClass().newInstance();
339 } catch (Exception q)
341 throw new Error(MessageManager.formatMessage(
342 "error.implementation_error_cannot_duplicate_colour_scheme",
343 new String[] { getClass().getName() }), q);