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.analysis.Profile;
25 import jalview.datamodel.AnnotatedCollectionI;
26 import jalview.datamodel.SequenceCollectionI;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.MessageManager;
30 import java.awt.Color;
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;
52 /** Consenus as a hashtable array */
55 /** Conservation string as a char array */
58 int conservationLength = 0;
64 * Creates a new ResidueColourScheme object.
66 * @param final int[] index table into colors (ResidueProperties.naIndex or
67 * ResidueProperties.aaIndex)
69 * colours for symbols in sequences
71 * threshold for conservation shading
73 public ResidueColourScheme(int[] aaOrnaIndex, Color[] colours,
76 symbolIndex = aaOrnaIndex;
77 this.colors = colours;
78 this.threshold = threshold;
82 * Creates a new ResidueColourScheme object with a lookup table for indexing
85 public ResidueColourScheme(int[] aaOrNaIndex)
87 symbolIndex = aaOrNaIndex;
91 * Creates a new ResidueColourScheme object - default constructor for
92 * non-sequence dependent colourschemes
94 public ResidueColourScheme()
100 * Find a colour without an index in a sequence
103 public Color findColour(char c)
105 return colors == null ? Color.white : colors[symbolIndex[c]];
109 public Color findColour(char c, int j, SequenceI seq)
113 if (colors != null && symbolIndex != null && (threshold == 0)
114 || aboveThreshold(c, j))
116 currentColour = colors[symbolIndex[c]];
120 currentColour = Color.white;
123 if (conservationColouring)
125 currentColour = applyConservation(currentColour, j);
128 return currentColour;
132 * Get the percentage threshold for this colour scheme
134 * @return Returns the percentage threshold
137 public int getThreshold()
149 public void setThreshold(int ct, boolean ignoreGaps)
152 this.ignoreGaps = ignoreGaps;
163 * @return DOCUMENT ME!
165 public boolean aboveThreshold(char c, int j)
167 if ('a' <= c && c <= 'z')
170 // Faster than toUpperCase
174 if (consensus == null || consensus.length < j || consensus[j] == null)
179 if (consensus[j].getMaxCount() > 0) // != -1))
181 if (consensus[j].getPercentageIdentity(ignoreGaps) >= threshold)
191 public boolean conservationApplied()
193 return conservationColouring;
197 public void setConservationApplied(boolean conservationApplied)
199 conservationColouring = conservationApplied;
203 public void setConservationInc(int i)
209 public int getConservationInc()
221 public void setConsensus(Profile[] consensus)
223 if (consensus == null)
228 this.consensus = consensus;
232 public void setConservation(Conservation cons)
236 conservationColouring = false;
241 conservationColouring = true;
242 int i, iSize = cons.getConsSequence().getLength();
243 conservation = new char[iSize];
244 for (i = 0; i < iSize; i++)
246 conservation[i] = cons.getConsSequence().getCharAt(i);
248 conservationLength = conservation.length;
261 * @return DOCUMENT ME!
264 Color applyConservation(Color currentColour, int i)
267 if ((conservationLength > i) && (conservation[i] != '*')
268 && (conservation[i] != '+'))
270 if (jalview.util.Comparison.isGap(conservation[i]))
272 currentColour = Color.white;
276 float t = 11 - (conservation[i] - '0');
282 int red = currentColour.getRed();
283 int green = currentColour.getGreen();
284 int blue = currentColour.getBlue();
287 int dg = 255 - green;
294 red += (inc / 20f) * dr;
295 green += (inc / 20f) * dg;
296 blue += (inc / 20f) * db;
298 if (red > 255 || green > 255 || blue > 255)
300 currentColour = Color.white;
304 currentColour = new Color(red, green, blue);
308 return currentColour;
312 public void alignmentChanged(AnnotatedCollectionI alignment,
313 Map<SequenceI, SequenceCollectionI> hiddenReps)
318 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
319 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
323 return getClass().newInstance();
324 } catch (Exception q)
326 throw new Error(MessageManager.formatMessage(
327 "error.implementation_error_cannot_duplicate_colour_scheme",
328 new String[] { getClass().getName() }), q);