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
38 final int[] symbolIndex;
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.
61 * @param final int[] index table into colors (ResidueProperties.naIndex or ResidueProperties.aaIndex)
63 * colours for symbols in sequences
65 * threshold for conservation shading
67 public ResidueColourScheme(int[] aaOrnaIndex, Color[] colours, int threshold)
69 symbolIndex = aaOrnaIndex;
70 this.colors = colours;
71 this.threshold = threshold;
75 * Creates a new ResidueColourScheme object with a lookup table for indexing the colour map
77 public ResidueColourScheme(int[] aaOrNaIndex)
79 symbolIndex = aaOrNaIndex;
82 * Creates a new ResidueColourScheme object - default constructor for non-sequence dependent colourschemes
84 public ResidueColourScheme()
90 * Find a colour without an index in a sequence
92 public Color findColour(char c)
94 return colors == null ? Color.white
95 : colors[symbolIndex[c]];
99 public Color findColour(char c, int j, SequenceI seq)
103 if (colors!=null && symbolIndex!=null && (threshold == 0) || aboveThreshold(c, j))
105 currentColour = colors[symbolIndex[c]];
109 currentColour = Color.white;
112 if (conservationColouring)
114 currentColour = applyConservation(currentColour, j);
117 return currentColour;
120 * Get the percentage threshold for this colour scheme
122 * @return Returns the percentage threshold
124 public int getThreshold()
135 public void setThreshold(int ct, boolean ignoreGaps)
140 this.ignoreGaps = AAFrequency.PID_NOGAPS;
144 this.ignoreGaps = AAFrequency.PID_GAPS;
156 * @return DOCUMENT ME!
158 public boolean aboveThreshold(char c, int j)
160 if ('a' <= c && c <= 'z')
163 // Faster than toUpperCase
167 if (consensus == null || consensus.length < j || consensus[j] == null)
172 if ((((Integer) consensus[j].get(AAFrequency.MAXCOUNT)).intValue() != -1)
173 && consensus[j].contains(String.valueOf(c)))
175 if (((Float) consensus[j].get(ignoreGaps)).floatValue() >= threshold)
184 public boolean conservationApplied()
186 return conservationColouring;
189 public void setConservationInc(int i)
194 public int getConservationInc()
205 public void setConsensus(Hashtable[] consensus)
207 if (consensus == null)
212 this.consensus = consensus;
215 public void setConservation(Conservation cons)
219 conservationColouring = false;
224 conservationColouring = true;
225 int i, iSize = cons.getConsSequence().getLength();
226 conservation = new char[iSize];
227 for (i = 0; i < iSize; i++)
229 conservation[i] = cons.getConsSequence().getCharAt(i);
231 conservationLength = conservation.length;
244 * @return DOCUMENT ME!
247 Color applyConservation(Color currentColour, int i)
250 if ((conservationLength > i) && (conservation[i] != '*')
251 && (conservation[i] != '+'))
253 if (jalview.util.Comparison.isGap(conservation[i]))
255 currentColour = Color.white;
259 float t = 11 - (conservation[i] - '0');
265 int red = currentColour.getRed();
266 int green = currentColour.getGreen();
267 int blue = currentColour.getBlue();
270 int dg = 255 - green;
277 red += (inc / 20f) * dr;
278 green += (inc / 20f) * dg;
279 blue += (inc / 20f) * db;
281 if (red > 255 || green > 255 || blue > 255)
283 currentColour = Color.white;
287 currentColour = new Color(red, green, blue);
291 return currentColour;
295 public void alignmentChanged(AnnotatedCollectionI alignment,
296 Map<SequenceI, SequenceCollectionI> hiddenReps)