2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
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;
29 import java.awt.Color;
30 import java.util.Hashtable;
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 String ignoreGaps = AAFrequency.PID_GAPS;
52 /** Consenus as a hashtable array */
53 Hashtable[] consensus;
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
102 public Color findColour(char c)
104 return colors == null ? Color.white : colors[symbolIndex[c]];
108 public Color findColour(char c, int j, SequenceI seq)
112 if (colors != null && symbolIndex != null && (threshold == 0)
113 || aboveThreshold(c, j))
115 currentColour = colors[symbolIndex[c]];
119 currentColour = Color.white;
122 if (conservationColouring)
124 currentColour = applyConservation(currentColour, j);
127 return currentColour;
131 * Get the percentage threshold for this colour scheme
133 * @return Returns the percentage threshold
135 public int getThreshold()
146 public void setThreshold(int ct, boolean ignoreGaps)
151 this.ignoreGaps = AAFrequency.PID_NOGAPS;
155 this.ignoreGaps = AAFrequency.PID_GAPS;
167 * @return DOCUMENT ME!
169 public boolean aboveThreshold(char c, int j)
171 if ('a' <= c && c <= 'z')
174 // Faster than toUpperCase
178 if (consensus == null || consensus.length < j || consensus[j] == null)
183 if ((((Integer) consensus[j].get(AAFrequency.MAXCOUNT)).intValue() != -1)
184 && consensus[j].contains(String.valueOf(c)))
186 if (((Float) consensus[j].get(ignoreGaps)).floatValue() >= threshold)
195 public boolean conservationApplied()
197 return conservationColouring;
200 public void setConservationApplied(boolean conservationApplied)
202 conservationColouring = conservationApplied;
205 public void setConservationInc(int i)
210 public int getConservationInc()
221 public void setConsensus(Hashtable[] consensus)
223 if (consensus == null)
228 this.consensus = consensus;
231 public void setConservation(Conservation cons)
235 conservationColouring = false;
240 conservationColouring = true;
241 int i, iSize = cons.getConsSequence().getLength();
242 conservation = new char[iSize];
243 for (i = 0; i < iSize; i++)
245 conservation[i] = cons.getConsSequence().getCharAt(i);
247 conservationLength = conservation.length;
260 * @return DOCUMENT ME!
263 Color applyConservation(Color currentColour, int i)
266 if ((conservationLength > i) && (conservation[i] != '*')
267 && (conservation[i] != '+'))
269 if (jalview.util.Comparison.isGap(conservation[i]))
271 currentColour = Color.white;
275 float t = 11 - (conservation[i] - '0');
281 int red = currentColour.getRed();
282 int green = currentColour.getGreen();
283 int blue = currentColour.getBlue();
286 int dg = 255 - green;
293 red += (inc / 20f) * dr;
294 green += (inc / 20f) * dg;
295 blue += (inc / 20f) * db;
297 if (red > 255 || green > 255 || blue > 255)
299 currentColour = Color.white;
303 currentColour = new Color(red, green, blue);
307 return currentColour;
311 public void alignmentChanged(AnnotatedCollectionI alignment,
312 Map<SequenceI, SequenceCollectionI> hiddenReps)
317 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
318 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
321 return getClass().newInstance();
325 throw new Error("Serious implementation error: cannot duplicate colourscheme "+getClass().getName(), q);