1 package jalview.schemes;
3 import jalview.datamodel.AnnotatedCollectionI;
4 import jalview.datamodel.HiddenMarkovModel;
5 import jalview.datamodel.ResidueCount;
6 import jalview.datamodel.SequenceCollectionI;
7 import jalview.datamodel.SequenceI;
8 import jalview.util.ColorUtils;
9 import jalview.util.Comparison;
11 import java.awt.Color;
12 import java.util.HashMap;
13 import java.util.List;
17 * A colour scheme based on a selected Hidden Markov Model. The colour is
19 * <li>white for a gap</li>
20 * <li>red for an insertion</li>
21 * <li>orange for negative information content</li>
22 * <li>white to blue for increasing information content</li>
24 * where information content is the log ratio
27 * log(profile match emission probability / residue background probability>
30 * using the alignment's background frequencies for residues.
35 public class HMMERAlignmentColourScheme extends ResidueColourScheme
38 * the ratio, for each symbol, of its frequency to total symbol count
40 Map<Character, Double> frequency = new HashMap<>();
44 HiddenMarkovModel hmm;
47 * Constructor given a Hidden Markov Model
53 public HMMERAlignmentColourScheme(AnnotatedCollectionI sg,
54 HiddenMarkovModel markov)
61 * Default constructor (required by ColourSchemes.loadColourSchemes)
63 public HMMERAlignmentColourScheme()
68 public Color findColour(char symbol, int position, SequenceI seq,
69 String consensusResidue, float pid)
71 return findColour(symbol, position);
75 * Returns the colour at a particular symbol at a column in the alignment:
77 * <li>white for a gap</li>
78 * <li>red for an insertion</li>
79 * <li>orange for negative information content</li>
80 * <li>white to blue for increasing information content</li>
87 private Color findColour(char symbol, int column)
89 if (hmm == null || Comparison.isGap(symbol))
93 if (Character.isLowerCase(symbol))
95 symbol = Character.toUpperCase(symbol);
97 double prob = hmm.getMatchEmissionProbability(column, symbol);
98 Double freq = frequency.get(symbol);
105 return new Color(230, 0, 0);
107 double value = Math.log(prob / freq.doubleValue());
111 colour = ColorUtils.getGraduatedColour((float) value, 0,
112 Color.WHITE, logTotalCount, Color.blue);
122 public void alignmentChanged(AnnotatedCollectionI collection,
123 Map<SequenceI, SequenceCollectionI> hiddenReps)
126 * ? no need to do anything if alignment is adjusted
127 * since findColour() handles everything
132 public ColourSchemeI getInstance(AnnotatedCollectionI sg,
133 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
135 List<SequenceI> hmms = sg.getHMMConsensusSequences();
136 HiddenMarkovModel model = hmms.isEmpty() ? null : hmms.get(0).getHMM();
137 return new HMMERAlignmentColourScheme(sg, model);
141 public boolean isApplicableTo(AnnotatedCollectionI ac)
143 return !ac.getHMMConsensusSequences().isEmpty();
147 public String getSchemeName()
149 return JalviewColourScheme.HMMERA.toString();
153 public boolean isSimple()
159 * Counts and stores the relatively frequency of every residue in the
164 public void countFrequencies(AnnotatedCollectionI sg)
166 ResidueCount counts = new ResidueCount(sg.getSequences());
167 int total = counts.getTotalCount(); // excludes gaps
169 for (char symbol : counts.getSymbolCounts().symbols)
171 double freq = counts.getCount(symbol) / (double) total;
172 frequency.put(symbol, freq);
174 logTotalCount = (float) Math.log(total);