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;
23 import java.awt.Color;
24 import java.util.Hashtable;
32 public class ResidueColourScheme implements ColourSchemeI
35 boolean conservationColouring = false;
37 Color[] colors = null;
41 /* Set when threshold colouring to either pid_gaps or pid_nogaps */
42 protected String ignoreGaps = AAFrequency.PID_GAPS;
44 /** Consenus as a hashtable array */
45 Hashtable[] consensus;
47 /** Conservation string as a char array */
50 int conservationLength = 0;
56 * Creates a new ResidueColourScheme object.
63 public ResidueColourScheme(Color[] colours, int threshold)
65 this.colors = colours;
66 this.threshold = threshold;
70 * Creates a new ResidueColourScheme object.
72 public ResidueColourScheme()
77 * Find a colour without an index in a sequence
79 public Color findColour(char c)
81 return colors == null ? Color.white
82 : colors[ResidueProperties.aaIndex[c]];
86 public Color findColour(char c, int j, int sequenceIndex)
90 if ((threshold == 0) || aboveThreshold(c, j))
92 currentColour = colors[ResidueProperties.aaIndex[c]];
96 currentColour = Color.white;
99 if (conservationColouring)
101 currentColour = applyConservation(currentColour, j);
104 return currentColour;
108 * Get the percentage threshold for this colour scheme
110 * @return Returns the percentage threshold
112 public int getThreshold()
123 public void setThreshold(int ct, boolean ignoreGaps)
128 this.ignoreGaps = AAFrequency.PID_NOGAPS;
132 this.ignoreGaps = AAFrequency.PID_GAPS;
144 * @return DOCUMENT ME!
146 public boolean aboveThreshold(char c, int j)
148 if ('a' <= c && c <= 'z')
151 // Faster than toUpperCase
155 if (consensus == null || consensus.length < j || consensus[j] == null)
160 if ((((Integer) consensus[j].get(AAFrequency.MAXCOUNT)).intValue() != -1)
161 && consensus[j].contains(String.valueOf(c)))
163 if (((Float) consensus[j].get(ignoreGaps)).floatValue() >= threshold)
172 public boolean conservationApplied()
174 return conservationColouring;
177 public void setConservationInc(int i)
182 public int getConservationInc()
193 public void setConsensus(Hashtable[] consensus)
195 if (consensus == null)
200 this.consensus = consensus;
203 public void setConservation(Conservation cons)
207 conservationColouring = false;
212 conservationColouring = true;
213 int i, iSize = cons.getConsSequence().getLength();
214 conservation = new char[iSize];
215 for (i = 0; i < iSize; i++)
217 conservation[i] = cons.getConsSequence().getCharAt(i);
219 conservationLength = conservation.length;
232 * @return DOCUMENT ME!
235 Color applyConservation(Color currentColour, int i)
238 if ((conservationLength > i) && (conservation[i] != '*')
239 && (conservation[i] != '+'))
241 if (jalview.util.Comparison.isGap(conservation[i]))
243 currentColour = Color.white;
247 float t = 11 - (conservation[i] - '0');
253 int red = currentColour.getRed();
254 int green = currentColour.getGreen();
255 int blue = currentColour.getBlue();
258 int dg = 255 - green;
265 red += (inc / 20f) * dr;
266 green += (inc / 20f) * dg;
267 blue += (inc / 20f) * db;
269 if (red > 255 || green > 255 || blue > 255)
271 currentColour = Color.white;
275 currentColour = new Color(red, green, blue);
279 return currentColour;