2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.schemes;
23 import jalview.analysis.AAFrequency;
24 import jalview.analysis.Conservation;
25 import jalview.datamodel.AnnotatedCollectionI;
26 import jalview.datamodel.SequenceCollectionI;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.MessageManager;
30 import java.awt.Color;
31 import java.util.Hashtable;
40 public class ResidueColourScheme implements ColourSchemeI
42 final int[] symbolIndex;
44 boolean conservationColouring = false;
46 Color[] colors = null;
50 /* Set when threshold colouring to either pid_gaps or pid_nogaps */
51 protected String ignoreGaps = AAFrequency.PID_GAPS;
53 /** Consenus as a hashtable array */
54 Hashtable[] consensus;
56 /** Conservation string as a char array */
59 int conservationLength = 0;
65 * Creates a new ResidueColourScheme object.
67 * @param final int[] index table into colors (ResidueProperties.naIndex or
68 * ResidueProperties.aaIndex)
70 * colours for symbols in sequences
72 * threshold for conservation shading
74 public ResidueColourScheme(int[] aaOrnaIndex, Color[] colours,
77 symbolIndex = aaOrnaIndex;
78 this.colors = colours;
79 this.threshold = threshold;
83 * Creates a new ResidueColourScheme object with a lookup table for indexing
86 public ResidueColourScheme(int[] aaOrNaIndex)
88 symbolIndex = aaOrNaIndex;
92 * Creates a new ResidueColourScheme object - default constructor for
93 * non-sequence dependent colourschemes
95 public ResidueColourScheme()
101 * Find a colour without an index in a sequence
103 public Color findColour(char c)
105 return colors == null ? Color.white : colors[symbolIndex[c]];
109 public Color findColour(char c, int j, SequenceI seq)
113 if (colors != null && symbolIndex != null && (threshold == 0)
114 || aboveThreshold(c, j))
116 currentColour = colors[symbolIndex[c]];
120 currentColour = Color.white;
123 if (conservationColouring)
125 currentColour = applyConservation(currentColour, j);
128 return currentColour;
132 * Get the percentage threshold for this colour scheme
134 * @return Returns the percentage threshold
136 public int getThreshold()
147 public void setThreshold(int ct, boolean ignoreGaps)
152 this.ignoreGaps = AAFrequency.PID_NOGAPS;
156 this.ignoreGaps = AAFrequency.PID_GAPS;
168 * @return DOCUMENT ME!
170 public boolean aboveThreshold(char c, int j)
172 if ('a' <= c && c <= 'z')
175 // Faster than toUpperCase
179 if (consensus == null || consensus.length < j || consensus[j] == null)
184 if ((((Integer) consensus[j].get(AAFrequency.MAXCOUNT)).intValue() != -1)
185 && consensus[j].contains(String.valueOf(c)))
187 if (((Float) consensus[j].get(ignoreGaps)).floatValue() >= threshold)
196 public boolean conservationApplied()
198 return conservationColouring;
202 public void setConservationApplied(boolean conservationApplied)
204 conservationColouring = conservationApplied;
207 public void setConservationInc(int i)
212 public int getConservationInc()
223 public void setConsensus(Hashtable[] consensus)
225 if (consensus == null)
230 this.consensus = consensus;
233 public void setConservation(Conservation cons)
237 conservationColouring = false;
242 conservationColouring = true;
243 int i, iSize = cons.getConsSequence().getLength();
244 conservation = new char[iSize];
245 for (i = 0; i < iSize; i++)
247 conservation[i] = cons.getConsSequence().getCharAt(i);
249 conservationLength = conservation.length;
262 * @return DOCUMENT ME!
265 Color applyConservation(Color currentColour, int i)
268 if ((conservationLength > i) && (conservation[i] != '*')
269 && (conservation[i] != '+'))
271 if (jalview.util.Comparison.isGap(conservation[i]))
273 currentColour = Color.white;
277 float t = 11 - (conservation[i] - '0');
283 int red = currentColour.getRed();
284 int green = currentColour.getGreen();
285 int blue = currentColour.getBlue();
288 int dg = 255 - green;
295 red += (inc / 20f) * dr;
296 green += (inc / 20f) * dg;
297 blue += (inc / 20f) * db;
299 if (red > 255 || green > 255 || blue > 255)
301 currentColour = Color.white;
305 currentColour = new Color(red, green, blue);
309 return currentColour;
313 public void alignmentChanged(AnnotatedCollectionI alignment,
314 Map<SequenceI, SequenceCollectionI> hiddenReps)
319 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
320 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
324 return getClass().newInstance();
325 } catch (Exception q)
327 throw new Error(MessageManager.formatMessage(
328 "error.implementation_error_cannot_duplicate_colour_scheme",
329 new String[] { getClass().getName() }), q);