2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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;
40 boolean conservationColouring = false;
42 Color[] colors = null;
46 /* Set when threshold colouring to either pid_gaps or pid_nogaps */
47 protected String ignoreGaps = AAFrequency.PID_GAPS;
49 /** Consenus as a hashtable array */
50 Hashtable[] consensus;
52 /** Conservation string as a char array */
55 int conservationLength = 0;
61 * Creates a new ResidueColourScheme object.
63 * @param final int[] index table into colors (ResidueProperties.naIndex or
64 * ResidueProperties.aaIndex)
66 * colours for symbols in sequences
68 * threshold for conservation shading
70 public ResidueColourScheme(int[] aaOrnaIndex, Color[] colours,
73 symbolIndex = aaOrnaIndex;
74 this.colors = colours;
75 this.threshold = threshold;
79 * Creates a new ResidueColourScheme object with a lookup table for indexing
82 public ResidueColourScheme(int[] aaOrNaIndex)
84 symbolIndex = aaOrNaIndex;
88 * Creates a new ResidueColourScheme object - default constructor for
89 * non-sequence dependent colourschemes
91 public ResidueColourScheme()
97 * Find a colour without an index in a sequence
99 public Color findColour(char c)
101 return colors == null ? Color.white : colors[symbolIndex[c]];
105 public Color findColour(char c, int j, SequenceI seq)
109 if (colors != null && symbolIndex != null && (threshold == 0)
110 || aboveThreshold(c, j))
112 currentColour = colors[symbolIndex[c]];
116 currentColour = Color.white;
119 if (conservationColouring)
121 currentColour = applyConservation(currentColour, j);
124 return currentColour;
128 * Get the percentage threshold for this colour scheme
130 * @return Returns the percentage threshold
132 public int getThreshold()
143 public void setThreshold(int ct, boolean ignoreGaps)
148 this.ignoreGaps = AAFrequency.PID_NOGAPS;
152 this.ignoreGaps = AAFrequency.PID_GAPS;
164 * @return DOCUMENT ME!
166 public boolean aboveThreshold(char c, int j)
168 if ('a' <= c && c <= 'z')
171 // Faster than toUpperCase
175 if (consensus == null || consensus.length < j || consensus[j] == null)
180 if ((((Integer) consensus[j].get(AAFrequency.MAXCOUNT)).intValue() != -1)
181 && consensus[j].contains(String.valueOf(c)))
183 if (((Float) consensus[j].get(ignoreGaps)).floatValue() >= threshold)
192 public boolean conservationApplied()
194 return conservationColouring;
197 public void setConservationInc(int i)
202 public int getConservationInc()
213 public void setConsensus(Hashtable[] consensus)
215 if (consensus == null)
220 this.consensus = consensus;
223 public void setConservation(Conservation cons)
227 conservationColouring = false;
232 conservationColouring = true;
233 int i, iSize = cons.getConsSequence().getLength();
234 conservation = new char[iSize];
235 for (i = 0; i < iSize; i++)
237 conservation[i] = cons.getConsSequence().getCharAt(i);
239 conservationLength = conservation.length;
252 * @return DOCUMENT ME!
255 Color applyConservation(Color currentColour, int i)
258 if ((conservationLength > i) && (conservation[i] != '*')
259 && (conservation[i] != '+'))
261 if (jalview.util.Comparison.isGap(conservation[i]))
263 currentColour = Color.white;
267 float t = 11 - (conservation[i] - '0');
273 int red = currentColour.getRed();
274 int green = currentColour.getGreen();
275 int blue = currentColour.getBlue();
278 int dg = 255 - green;
285 red += (inc / 20f) * dr;
286 green += (inc / 20f) * dg;
287 blue += (inc / 20f) * db;
289 if (red > 255 || green > 255 || blue > 255)
291 currentColour = Color.white;
295 currentColour = new Color(red, green, blue);
299 return currentColour;
303 public void alignmentChanged(AnnotatedCollectionI alignment,
304 Map<SequenceI, SequenceCollectionI> hiddenReps)