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.SequenceCollectionI;
24 import jalview.datamodel.SequenceI;
26 import java.awt.Color;
27 import java.util.Hashtable;
36 public class ResidueColourScheme implements ColourSchemeI
39 boolean conservationColouring = false;
41 Color[] colors = null;
45 /* Set when threshold colouring to either pid_gaps or pid_nogaps */
46 protected String ignoreGaps = AAFrequency.PID_GAPS;
48 /** Consenus as a hashtable array */
49 Hashtable[] consensus;
51 /** Conservation string as a char array */
54 int conservationLength = 0;
60 * Creates a new ResidueColourScheme object.
67 public ResidueColourScheme(Color[] colours, int threshold)
69 this.colors = colours;
70 this.threshold = threshold;
74 * Creates a new ResidueColourScheme object.
76 public ResidueColourScheme()
81 * Find a colour without an index in a sequence
83 public Color findColour(char c)
85 return colors == null ? Color.white
86 : colors[ResidueProperties.aaIndex[c]];
90 public Color findColour(char c, int j, SequenceI seq)
94 if ((threshold == 0) || aboveThreshold(c, j))
96 currentColour = colors[ResidueProperties.aaIndex[c]];
100 currentColour = Color.white;
103 if (conservationColouring)
105 currentColour = applyConservation(currentColour, j);
108 return currentColour;
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;
286 public void alignmentChanged(AnnotatedCollectionI alignment,
287 Map<SequenceI, SequenceCollectionI> hiddenReps)