2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.AnnotatedCollectionI;
23 import jalview.datamodel.SequenceI;
25 import java.awt.Color;
26 import java.util.Hashtable;
34 public class ResidueColourScheme implements ColourSchemeI
37 boolean conservationColouring = false;
39 Color[] colors = null;
43 /* Set when threshold colouring to either pid_gaps or pid_nogaps */
44 protected String ignoreGaps = AAFrequency.PID_GAPS;
46 /** Consenus as a hashtable array */
47 Hashtable[] consensus;
49 /** Conservation string as a char array */
52 int conservationLength = 0;
58 * Creates a new ResidueColourScheme object.
65 public ResidueColourScheme(Color[] colours, int threshold)
67 this.colors = colours;
68 this.threshold = threshold;
72 * Creates a new ResidueColourScheme object.
74 public ResidueColourScheme()
79 * Find a colour without an index in a sequence
81 public Color findColour(char c)
83 return colors == null ? Color.white
84 : colors[ResidueProperties.aaIndex[c]];
88 public Color findColour(char c, int j, SequenceI seq)
92 if ((threshold == 0) || aboveThreshold(c, j))
94 currentColour = colors[ResidueProperties.aaIndex[c]];
98 currentColour = Color.white;
101 if (conservationColouring)
103 currentColour = applyConservation(currentColour, j);
106 return currentColour;
108 @Override public void alignmentChanged(AnnotatedCollectionI alignment) {};
111 * Get the percentage threshold for this colour scheme
113 * @return Returns the percentage threshold
115 public int getThreshold()
126 public void setThreshold(int ct, boolean ignoreGaps)
131 this.ignoreGaps = AAFrequency.PID_NOGAPS;
135 this.ignoreGaps = AAFrequency.PID_GAPS;
147 * @return DOCUMENT ME!
149 public boolean aboveThreshold(char c, int j)
151 if ('a' <= c && c <= 'z')
154 // Faster than toUpperCase
158 if (consensus == null || consensus.length < j || consensus[j] == null)
163 if ((((Integer) consensus[j].get(AAFrequency.MAXCOUNT)).intValue() != -1)
164 && consensus[j].contains(String.valueOf(c)))
166 if (((Float) consensus[j].get(ignoreGaps)).floatValue() >= threshold)
175 public boolean conservationApplied()
177 return conservationColouring;
180 public void setConservationInc(int i)
185 public int getConservationInc()
196 public void setConsensus(Hashtable[] consensus)
198 if (consensus == null)
203 this.consensus = consensus;
206 public void setConservation(Conservation cons)
210 conservationColouring = false;
215 conservationColouring = true;
216 int i, iSize = cons.getConsSequence().getLength();
217 conservation = new char[iSize];
218 for (i = 0; i < iSize; i++)
220 conservation[i] = cons.getConsSequence().getCharAt(i);
222 conservationLength = conservation.length;
235 * @return DOCUMENT ME!
238 Color applyConservation(Color currentColour, int i)
241 if ((conservationLength > i) && (conservation[i] != '*')
242 && (conservation[i] != '+'))
244 if (jalview.util.Comparison.isGap(conservation[i]))
246 currentColour = Color.white;
250 float t = 11 - (conservation[i] - '0');
256 int red = currentColour.getRed();
257 int green = currentColour.getGreen();
258 int blue = currentColour.getBlue();
261 int dg = 255 - green;
268 red += (inc / 20f) * dr;
269 green += (inc / 20f) * dg;
270 blue += (inc / 20f) * db;
272 if (red > 255 || green > 255 || blue > 255)
274 currentColour = Color.white;
278 currentColour = new Color(red, green, blue);
282 return currentColour;