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;
37 * Base class for residue-based colour schemes
39 public class ResidueColourScheme implements ColourSchemeI
41 final int[] symbolIndex;
43 boolean conservationColouring = false;
45 Color[] colors = null;
49 /* Set when threshold colouring to either pid_gaps or pid_nogaps */
50 protected boolean ignoreGaps = false;
53 * Consensus data indexed by column
58 * Conservation string as a char array
63 * The conservation slider percentage setting
68 * Creates a new ResidueColourScheme object.
70 * @param final int[] index table into colors (ResidueProperties.naIndex or
71 * ResidueProperties.aaIndex)
73 * colours for symbols in sequences
75 * threshold for conservation shading
77 public ResidueColourScheme(int[] aaOrnaIndex, Color[] colours,
80 symbolIndex = aaOrnaIndex;
81 this.colors = colours;
82 this.threshold = threshold;
86 * Creates a new ResidueColourScheme object with a lookup table for indexing
89 public ResidueColourScheme(int[] aaOrNaIndex)
91 symbolIndex = aaOrNaIndex;
95 * Creates a new ResidueColourScheme object - default constructor for
96 * non-sequence dependent colourschemes
98 public ResidueColourScheme()
104 * Find a colour without an index in a sequence
107 public Color findColour(char c)
109 return colors == null ? Color.white : colors[symbolIndex[c]];
113 public Color findColour(char c, int j, SequenceI seq)
117 if (colors != null && symbolIndex != null && (threshold == 0)
118 || aboveThreshold(c, j))
120 currentColour = colors[symbolIndex[c]];
124 currentColour = Color.white;
127 if (conservationColouring)
129 currentColour = applyConservation(currentColour, j);
132 return currentColour;
136 * Get the percentage threshold for this colour scheme
138 * @return Returns the percentage threshold
141 public int getThreshold()
147 * Sets the percentage consensus threshold value, and whether gaps are ignored
148 * in percentage identity calculation
150 * @param consensusThreshold
154 public void setThreshold(int consensusThreshold, boolean ignoreGaps)
156 threshold = consensusThreshold;
157 this.ignoreGaps = ignoreGaps;
161 * Answers true if there is a consensus profile for the specified column, and
162 * the given residue matches the consensus (or joint consensus) residue for
163 * the column, and the percentage identity for the profile is equal to or
164 * greater than the current threshold; else answers false. The percentage
165 * calculation depends on whether or not we are ignoring gapped sequences.
169 * (index into consensus profiles)
172 * @see #setThreshold(int, boolean)
174 public boolean aboveThreshold(char residue, int column)
176 if ('a' <= residue && residue <= 'z')
179 // Faster than toUpperCase
180 residue -= ('a' - 'A');
183 if (consensus == null)
188 ProfileI profile = consensus.get(column);
191 * test whether this is the consensus (or joint consensus) residue
194 && profile.getModalResidue().contains(String.valueOf(residue)))
196 if (profile.getPercentageIdentity(ignoreGaps) >= threshold)
206 public boolean conservationApplied()
208 return conservationColouring;
212 public void setConservationApplied(boolean conservationApplied)
214 conservationColouring = conservationApplied;
218 public void setConservationInc(int i)
224 public int getConservationInc()
236 public void setConsensus(ProfilesI consensus)
238 if (consensus == null)
243 this.consensus = consensus;
247 public void setConservation(Conservation cons)
251 conservationColouring = false;
256 conservationColouring = true;
257 int iSize = cons.getConsSequence().getLength();
258 conservation = new char[iSize];
259 for (int i = 0; i < iSize; i++)
261 conservation[i] = cons.getConsSequence().getCharAt(i);
268 * Applies a combination of column conservation score, and conservation
269 * percentage slider, to 'bleach' out the residue colours towards white.
271 * If a column is fully conserved (identical residues, conservation score 11,
272 * shown as *), or all 10 physico-chemical properties are conserved
273 * (conservation score 10, shown as +), then the colour is left unchanged.
275 * Otherwise a 'bleaching' factor is computed and applied to the colour. This
276 * is designed to fade colours for scores of 0-9 completely to white at slider
277 * positions ranging from 18% - 100% respectively.
279 * @param currentColour
282 * @return bleached (or unmodified) colour
284 Color applyConservation(Color currentColour, int column)
286 if (conservation == null || conservation.length <= column)
288 return currentColour;
290 char conservationScore = conservation[column];
293 * if residues are fully conserved (* or 11), or all properties
294 * are conserved (+ or 10), leave colour unchanged
296 if (conservationScore == '*' || conservationScore == '+'
297 || conservationScore == (char) 10
298 || conservationScore == (char) 11)
300 return currentColour;
303 if (Comparison.isGap(conservationScore))
309 * convert score 0-9 to a bleaching factor 1.1 - 0.2
311 float bleachFactor = (11 - (conservationScore - '0')) / 10f;
314 * scale this up by 0-5 (percentage slider / 20)
315 * as a result, scores of: 0 1 2 3 4 5 6 7 8 9
316 * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100%
318 bleachFactor *= (inc / 20f);
320 return ColorUtils.bleachColour(currentColour, bleachFactor);
324 public void alignmentChanged(AnnotatedCollectionI alignment,
325 Map<SequenceI, SequenceCollectionI> hiddenReps)
330 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
331 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
335 return getClass().newInstance();
336 } catch (Exception q)
338 throw new Error(MessageManager.formatMessage(
339 "error.implementation_error_cannot_duplicate_colour_scheme",
340 new String[] { getClass().getName() }), q);