2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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/>.
18 package jalview.schemes;
20 import jalview.analysis.AAFrequency;
21 import jalview.analysis.Conservation;
22 import jalview.datamodel.SequenceI;
24 import java.awt.Color;
25 import java.util.Hashtable;
33 public class ResidueColourScheme implements ColourSchemeI
36 boolean conservationColouring = false;
38 Color[] colors = null;
42 /* Set when threshold colouring to either pid_gaps or pid_nogaps */
43 protected String ignoreGaps = AAFrequency.PID_GAPS;
45 /** Consenus as a hashtable array */
46 Hashtable[] consensus;
48 /** Conservation string as a char array */
51 int conservationLength = 0;
57 * Creates a new ResidueColourScheme object.
64 public ResidueColourScheme(Color[] colours, int threshold)
66 this.colors = colours;
67 this.threshold = threshold;
71 * Creates a new ResidueColourScheme object.
73 public ResidueColourScheme()
78 * Find a colour without an index in a sequence
80 public Color findColour(char c)
82 return colors == null ? Color.white
83 : colors[ResidueProperties.aaIndex[c]];
87 public Color findColour(char c, int j, SequenceI seq)
91 if ((threshold == 0) || aboveThreshold(c, j))
93 currentColour = colors[ResidueProperties.aaIndex[c]];
97 currentColour = Color.white;
100 if (conservationColouring)
102 currentColour = applyConservation(currentColour, j);
105 return currentColour;
109 * Get the percentage threshold for this colour scheme
111 * @return Returns the percentage threshold
113 public int getThreshold()
124 public void setThreshold(int ct, boolean ignoreGaps)
129 this.ignoreGaps = AAFrequency.PID_NOGAPS;
133 this.ignoreGaps = AAFrequency.PID_GAPS;
145 * @return DOCUMENT ME!
147 public boolean aboveThreshold(char c, int j)
149 if ('a' <= c && c <= 'z')
152 // Faster than toUpperCase
156 if (consensus == null || consensus.length < j || consensus[j] == null)
161 if ((((Integer) consensus[j].get(AAFrequency.MAXCOUNT)).intValue() != -1)
162 && consensus[j].contains(String.valueOf(c)))
164 if (((Float) consensus[j].get(ignoreGaps)).floatValue() >= threshold)
173 public boolean conservationApplied()
175 return conservationColouring;
178 public void setConservationInc(int i)
183 public int getConservationInc()
194 public void setConsensus(Hashtable[] consensus)
196 if (consensus == null)
201 this.consensus = consensus;
204 public void setConservation(Conservation cons)
208 conservationColouring = false;
213 conservationColouring = true;
214 int i, iSize = cons.getConsSequence().getLength();
215 conservation = new char[iSize];
216 for (i = 0; i < iSize; i++)
218 conservation[i] = cons.getConsSequence().getCharAt(i);
220 conservationLength = conservation.length;
233 * @return DOCUMENT ME!
236 Color applyConservation(Color currentColour, int i)
239 if ((conservationLength > i) && (conservation[i] != '*')
240 && (conservation[i] != '+'))
242 if (jalview.util.Comparison.isGap(conservation[i]))
244 currentColour = Color.white;
248 float t = 11 - (conservation[i] - '0');
254 int red = currentColour.getRed();
255 int green = currentColour.getGreen();
256 int blue = currentColour.getBlue();
259 int dg = 255 - green;
266 red += (inc / 20f) * dr;
267 green += (inc / 20f) * dg;
268 blue += (inc / 20f) * db;
270 if (red > 255 || green > 255 || blue > 255)
272 currentColour = Color.white;
276 currentColour = new Color(red, green, blue);
280 return currentColour;