1 package jalview.schemes;
3 import jalview.datamodel.AnnotatedCollectionI;
4 import jalview.datamodel.HiddenMarkovModel;
5 import jalview.datamodel.SequenceCollectionI;
6 import jalview.datamodel.SequenceI;
7 import jalview.util.ColorUtils;
8 import jalview.util.Comparison;
10 import java.awt.Color;
14 * Base class for colour schemes based on a selected Hidden Markov Model. The
17 * <li>white for a gap</li>
18 * <li>red for an insertion</li>
19 * <li>orange for negative information content</li>
20 * <li>white to blue for increasing information content</li>
22 * where information content is the log ratio
25 * log(profile match emission probability / residue background probability)
28 * Sub-class implementations use either global ('Uniprot') or local
29 * ('alignment') background frequencies.
34 public abstract class HmmerColourScheme extends ResidueColourScheme
36 private static final Color INSERTION_COLOUR = new Color(230, 0, 0); // reddish
38 private HiddenMarkovModel hmm;
40 private Map<Character, Float> frequencies;
43 * Constructor given a Hidden Markov Model
47 public HmmerColourScheme(HiddenMarkovModel markov)
53 * Default constructor (required by ColourSchemes.loadColourSchemes)
55 public HmmerColourScheme()
60 public Color findColour(char symbol, int position, SequenceI seq,
61 String consensusResidue, float pid)
63 return findColour(symbol, position);
67 * Returns the colour at a particular symbol at a column in the alignment:
69 * <li>white for a gap</li>
70 * <li>red for an insertion</li>
71 * <li>orange for negative information content</li>
72 * <li>white to blue for increasing information content</li>
79 private Color findColour(char symbol, int column)
81 if (getHmm() == null || Comparison.isGap(symbol))
85 if (Character.isLowerCase(symbol))
87 symbol = Character.toUpperCase(symbol);
90 final double prob = getHmm().getMatchEmissionProbability(column,
95 if (!frequencies.containsKey(symbol))
101 freq = frequencies.get(symbol);
105 return INSERTION_COLOUR;
107 double value = Math.log(prob / freq.floatValue());
114 colour = ColorUtils.getGraduatedColour((float) value, 0, Color.WHITE,
115 getMaxInformationScore(), Color.blue);
120 * Answers the maximum possible value of information score (log ratio), for
121 * use in scaling a graduated colour range
125 abstract float getMaxInformationScore();
128 public void alignmentChanged(AnnotatedCollectionI collection,
129 Map<SequenceI, SequenceCollectionI> hiddenReps)
132 * ? no need to do anything if alignment is adjusted
133 * since findColour() handles everything
138 * Answers a new colour scheme instance based on the HMM of the first sequence
139 * in ac that has an HMM
142 public ColourSchemeI getInstance(AnnotatedCollectionI ac,
143 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
145 SequenceI hmmSeq = ac.getHmmConsensus();
146 HiddenMarkovModel model = hmmSeq == null ? null : hmmSeq.getHMM();
148 return newInstance(ac, model);
152 * Answers a new instance of the colour scheme for the given HMM
158 protected abstract HmmerColourScheme newInstance(AnnotatedCollectionI ac,
159 HiddenMarkovModel model);
162 public boolean isSimple()
168 * Answers true if the sequence collection has an HMM consensus sequence, else
172 public boolean isApplicableTo(AnnotatedCollectionI ac)
174 return ac.getHmmConsensus() != null;
177 protected Map<Character, Float> getFrequencies()
182 protected void setFrequencies(Map<Character, Float> frequencies)
184 this.frequencies = frequencies;
187 protected HiddenMarkovModel getHmm()