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.Conservation;
24 import jalview.analysis.Profile;
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;
39 public class ResidueColourScheme implements ColourSchemeI
41 final int[] symbolIndex;
43 boolean conservationColouring = false;
45 Color[] colors = null;
49 /* Set when threshold colouring to either pid_gaps or pid_nogaps */
50 protected boolean ignoreGaps = false;
53 * Consensus data indexed by column
58 * Conservation string as a char array
62 int conservationLength = 0;
68 * Creates a new ResidueColourScheme object.
70 * @param final int[] index table into colors (ResidueProperties.naIndex or
71 * ResidueProperties.aaIndex)
73 * colours for symbols in sequences
75 * threshold for conservation shading
77 public ResidueColourScheme(int[] aaOrnaIndex, Color[] colours,
80 symbolIndex = aaOrnaIndex;
81 this.colors = colours;
82 this.threshold = threshold;
86 * Creates a new ResidueColourScheme object with a lookup table for indexing
89 public ResidueColourScheme(int[] aaOrNaIndex)
91 symbolIndex = aaOrNaIndex;
95 * Creates a new ResidueColourScheme object - default constructor for
96 * non-sequence dependent colourschemes
98 public ResidueColourScheme()
104 * Find a colour without an index in a sequence
107 public Color findColour(char c)
109 return colors == null ? Color.white : colors[symbolIndex[c]];
113 public Color findColour(char c, int j, SequenceI seq)
117 if (colors != null && symbolIndex != null && (threshold == 0)
118 || aboveThreshold(c, j))
120 currentColour = colors[symbolIndex[c]];
124 currentColour = Color.white;
127 if (conservationColouring)
129 currentColour = applyConservation(currentColour, j);
132 return currentColour;
136 * Get the percentage threshold for this colour scheme
138 * @return Returns the percentage threshold
141 public int getThreshold()
153 public void setThreshold(int ct, boolean ignoreGaps)
156 this.ignoreGaps = ignoreGaps;
167 * @return DOCUMENT ME!
169 public boolean aboveThreshold(char c, int j)
171 if ('a' <= c && c <= 'z')
174 // Faster than toUpperCase
178 if (consensus == null || consensus.length < j || consensus[j] == null)
183 if (consensus[j].getMaxCount() > 0) // != -1))
185 if (consensus[j].getPercentageIdentity(ignoreGaps) >= threshold)
195 public boolean conservationApplied()
197 return conservationColouring;
201 public void setConservationApplied(boolean conservationApplied)
203 conservationColouring = conservationApplied;
207 public void setConservationInc(int i)
213 public int getConservationInc()
225 public void setConsensus(Profile[] consensus)
227 if (consensus == null)
232 this.consensus = consensus;
236 public void setConservation(Conservation cons)
240 conservationColouring = false;
245 conservationColouring = true;
246 int i, iSize = cons.getConsSequence().getLength();
247 conservation = new char[iSize];
248 for (i = 0; i < iSize; i++)
250 conservation[i] = cons.getConsSequence().getCharAt(i);
252 conservationLength = conservation.length;
265 * @return DOCUMENT ME!
268 Color applyConservation(Color currentColour, int i)
271 if ((conservationLength > i) && (conservation[i] != '*')
272 && (conservation[i] != '+'))
274 if (jalview.util.Comparison.isGap(conservation[i]))
276 currentColour = Color.white;
280 float t = 11 - (conservation[i] - '0');
286 int red = currentColour.getRed();
287 int green = currentColour.getGreen();
288 int blue = currentColour.getBlue();
291 int dg = 255 - green;
298 red += (inc / 20f) * dr;
299 green += (inc / 20f) * dg;
300 blue += (inc / 20f) * db;
302 if (red > 255 || green > 255 || blue > 255)
304 currentColour = Color.white;
308 currentColour = new Color(red, green, blue);
312 return currentColour;
316 public void alignmentChanged(AnnotatedCollectionI alignment,
317 Map<SequenceI, SequenceCollectionI> hiddenReps)
322 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
323 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
327 return getClass().newInstance();
328 } catch (Exception q)
330 throw new Error(MessageManager.formatMessage(
331 "error.implementation_error_cannot_duplicate_colour_scheme",
332 new String[] { getClass().getName() }), q);