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.AAFrequency;
24 import jalview.analysis.Conservation;
25 import jalview.datamodel.AnnotatedCollectionI;
26 import jalview.datamodel.SequenceCollectionI;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.Comparison;
29 import jalview.util.MessageManager;
31 import java.awt.Color;
32 import java.util.Hashtable;
41 public class ResidueColourScheme implements ColourSchemeI
43 final int[] symbolIndex;
45 boolean conservationColouring = false;
47 Color[] colors = null;
51 /* Set when threshold colouring to either pid_gaps or pid_nogaps */
52 protected String ignoreGaps = AAFrequency.PID_GAPS;
54 /** Consenus as a hashtable array */
55 Hashtable[] consensus;
57 /** Conservation string as a char array */
60 int conservationLength = 0;
66 * Creates a new ResidueColourScheme object.
68 * @param final int[] index table into colors (ResidueProperties.naIndex or
69 * ResidueProperties.aaIndex)
71 * colours for symbols in sequences
73 * threshold for conservation shading
75 public ResidueColourScheme(int[] aaOrnaIndex, Color[] colours,
78 symbolIndex = aaOrnaIndex;
79 this.colors = colours;
80 this.threshold = threshold;
84 * Creates a new ResidueColourScheme object with a lookup table for indexing
87 public ResidueColourScheme(int[] aaOrNaIndex)
89 symbolIndex = aaOrNaIndex;
93 * Creates a new ResidueColourScheme object - default constructor for
94 * non-sequence dependent colourschemes
96 public ResidueColourScheme()
102 * Find a colour without an index in a sequence
104 public Color findColour(char c)
106 return colors == null ? Color.white : colors[symbolIndex[c]];
110 public Color findColour(char c, int j, SequenceI seq)
114 if (colors != null && symbolIndex != null && (threshold == 0)
115 || aboveThreshold(c, j))
117 currentColour = colors[symbolIndex[c]];
121 currentColour = Color.white;
124 if (conservationColouring)
126 currentColour = applyConservation(currentColour, j);
129 return currentColour;
133 * Get the percentage threshold for this colour scheme
135 * @return Returns the percentage threshold
137 public int getThreshold()
148 public void setThreshold(int ct, boolean ignoreGaps)
153 this.ignoreGaps = AAFrequency.PID_NOGAPS;
157 this.ignoreGaps = AAFrequency.PID_GAPS;
169 * @return DOCUMENT ME!
171 public boolean aboveThreshold(char c, int j)
173 if ('a' <= c && c <= 'z')
176 // Faster than toUpperCase
180 if (consensus == null || consensus.length < j || consensus[j] == null)
185 if ((((Integer) consensus[j].get(AAFrequency.MAXCOUNT)).intValue() != -1)
186 && consensus[j].contains(String.valueOf(c)))
188 if (((Float) consensus[j].get(ignoreGaps)).floatValue() >= threshold)
197 public boolean conservationApplied()
199 return conservationColouring;
203 public void setConservationApplied(boolean conservationApplied)
205 conservationColouring = conservationApplied;
208 public void setConservationInc(int i)
213 public int getConservationInc()
224 public void setConsensus(Hashtable[] consensus)
226 if (consensus == null)
231 this.consensus = consensus;
234 public void setConservation(Conservation cons)
238 conservationColouring = false;
243 conservationColouring = true;
244 int i, iSize = cons.getConsSequence().getLength();
245 conservation = new char[iSize];
246 for (i = 0; i < iSize; i++)
248 conservation[i] = cons.getConsSequence().getCharAt(i);
250 conservationLength = conservation.length;
263 * @return DOCUMENT ME!
266 Color applyConservation(Color currentColour, int i)
269 if ((conservationLength > i) && (conservation[i] != '*')
270 && (conservation[i] != '+'))
272 if (Comparison.isGap(conservation[i]))
274 currentColour = Color.white;
278 float t = 11 - (conservation[i] - '0');
284 int red = currentColour.getRed();
285 int green = currentColour.getGreen();
286 int blue = currentColour.getBlue();
289 int dg = 255 - green;
296 red += (inc / 20f) * dr;
297 green += (inc / 20f) * dg;
298 blue += (inc / 20f) * db;
300 if (red > 255 || green > 255 || blue > 255)
302 currentColour = Color.white;
306 currentColour = new Color(red, green, blue);
310 return currentColour;
314 public void alignmentChanged(AnnotatedCollectionI alignment,
315 Map<SequenceI, SequenceCollectionI> hiddenReps)
320 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
321 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
325 return getClass().newInstance();
326 } catch (Exception q)
328 throw new Error(MessageManager.formatMessage("error.implementation_error_cannot_duplicate_colour_scheme", new String[]{getClass().getName()}), q);