2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.schemes;
21 import jalview.analysis.AAFrequency;
22 import jalview.analysis.Conservation;
23 import jalview.datamodel.AnnotatedCollectionI;
24 import jalview.datamodel.SequenceCollectionI;
25 import jalview.datamodel.SequenceI;
27 import java.awt.Color;
28 import java.util.Hashtable;
37 public class ResidueColourScheme implements ColourSchemeI
39 final int[] symbolIndex;
41 boolean conservationColouring = false;
43 Color[] colors = null;
47 /* Set when threshold colouring to either pid_gaps or pid_nogaps */
48 protected String ignoreGaps = AAFrequency.PID_GAPS;
50 /** Consenus as a hashtable array */
51 Hashtable[] consensus;
53 /** Conservation string as a char array */
56 int conservationLength = 0;
62 * Creates a new ResidueColourScheme object.
64 * @param final int[] index table into colors (ResidueProperties.naIndex or
65 * ResidueProperties.aaIndex)
67 * colours for symbols in sequences
69 * threshold for conservation shading
71 public ResidueColourScheme(int[] aaOrnaIndex, Color[] colours,
74 symbolIndex = aaOrnaIndex;
75 this.colors = colours;
76 this.threshold = threshold;
80 * Creates a new ResidueColourScheme object with a lookup table for indexing
83 public ResidueColourScheme(int[] aaOrNaIndex)
85 symbolIndex = aaOrNaIndex;
89 * Creates a new ResidueColourScheme object - default constructor for
90 * non-sequence dependent colourschemes
92 public ResidueColourScheme()
98 * Find a colour without an index in a sequence
100 public Color findColour(char c)
102 return colors == null ? Color.white : colors[symbolIndex[c]];
106 public Color findColour(char c, int j, SequenceI seq)
110 if (colors != null && symbolIndex != null && (threshold == 0)
111 || aboveThreshold(c, j))
113 currentColour = colors[symbolIndex[c]];
117 currentColour = Color.white;
120 if (conservationColouring)
122 currentColour = applyConservation(currentColour, j);
125 return currentColour;
129 * Get the percentage threshold for this colour scheme
131 * @return Returns the percentage threshold
133 public int getThreshold()
144 public void setThreshold(int ct, boolean ignoreGaps)
149 this.ignoreGaps = AAFrequency.PID_NOGAPS;
153 this.ignoreGaps = AAFrequency.PID_GAPS;
165 * @return DOCUMENT ME!
167 public boolean aboveThreshold(char c, int j)
169 if ('a' <= c && c <= 'z')
172 // Faster than toUpperCase
176 if (consensus == null || consensus.length < j || consensus[j] == null)
181 if ((((Integer) consensus[j].get(AAFrequency.MAXCOUNT)).intValue() != -1)
182 && consensus[j].contains(String.valueOf(c)))
184 if (((Float) consensus[j].get(ignoreGaps)).floatValue() >= threshold)
193 public boolean conservationApplied()
195 return conservationColouring;
198 public void setConservationApplied(boolean conservationApplied)
200 conservationColouring = conservationApplied;
203 public void setConservationInc(int i)
208 public int getConservationInc()
219 public void setConsensus(Hashtable[] consensus)
221 if (consensus == null)
226 this.consensus = consensus;
229 public void setConservation(Conservation cons)
233 conservationColouring = false;
238 conservationColouring = true;
239 int i, iSize = cons.getConsSequence().getLength();
240 conservation = new char[iSize];
241 for (i = 0; i < iSize; i++)
243 conservation[i] = cons.getConsSequence().getCharAt(i);
245 conservationLength = conservation.length;
258 * @return DOCUMENT ME!
261 Color applyConservation(Color currentColour, int i)
264 if ((conservationLength > i) && (conservation[i] != '*')
265 && (conservation[i] != '+'))
267 if (jalview.util.Comparison.isGap(conservation[i]))
269 currentColour = Color.white;
273 float t = 11 - (conservation[i] - '0');
279 int red = currentColour.getRed();
280 int green = currentColour.getGreen();
281 int blue = currentColour.getBlue();
284 int dg = 255 - green;
291 red += (inc / 20f) * dr;
292 green += (inc / 20f) * dg;
293 blue += (inc / 20f) * db;
295 if (red > 255 || green > 255 || blue > 255)
297 currentColour = Color.white;
301 currentColour = new Color(red, green, blue);
305 return currentColour;
309 public void alignmentChanged(AnnotatedCollectionI alignment,
310 Map<SequenceI, SequenceCollectionI> hiddenReps)
315 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
316 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
319 return getClass().newInstance();
323 throw new Error("Serious implementation error: cannot duplicate colourscheme "+getClass().getName(), q);