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;
16 * A colour scheme based on a selected Hidden Markov Model. The colour is
18 * <li>white for a gap</li>
19 * <li>red for an insertion</li>
20 * <li>orange for negative information content</li>
21 * <li>white to blue for increasing information content</li>
23 * where information content is the log ratio
26 * log(profile match emission probability / residue background probability>
29 * using the alignment's background frequencies for residues.
34 public class HMMERAlignmentColourScheme extends ResidueColourScheme
37 * the ratio, for each symbol, of its frequency to total symbol count
39 Map<Character, Double> frequency = new HashMap<>();
43 HiddenMarkovModel hmm;
46 * Constructor given a Hidden Markov Model
52 public HMMERAlignmentColourScheme(AnnotatedCollectionI sg,
53 HiddenMarkovModel markov)
60 * Default constructor (required by ColourSchemes.loadColourSchemes)
62 public HMMERAlignmentColourScheme()
67 public Color findColour(char symbol, int position, SequenceI seq,
68 String consensusResidue, float pid)
70 return findColour(symbol, position);
74 * Returns the colour at a particular symbol at a column in the alignment:
76 * <li>white for a gap</li>
77 * <li>red for an insertion</li>
78 * <li>orange for negative information content</li>
79 * <li>white to blue for increasing information content</li>
86 private Color findColour(char symbol, int column)
88 if (hmm == null || Comparison.isGap(symbol))
92 if (Character.isLowerCase(symbol))
94 symbol = Character.toUpperCase(symbol);
96 double prob = hmm.getMatchEmissionProbability(column, symbol);
97 Double freq = frequency.get(symbol);
104 return new Color(230, 0, 0);
106 double value = Math.log(prob / freq.doubleValue());
110 colour = ColorUtils.getGraduatedColour((float) value, 0,
111 Color.WHITE, logTotalCount, Color.blue);
121 public void alignmentChanged(AnnotatedCollectionI collection,
122 Map<SequenceI, SequenceCollectionI> hiddenReps)
125 * ? no need to do anything if alignment is adjusted
126 * since findColour() handles everything
131 public ColourSchemeI getInstance(AnnotatedCollectionI sg,
132 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
134 SequenceI hmmSeq = sg.getHmmConsensus();
135 HiddenMarkovModel model = hmmSeq == null ? null : hmmSeq.getHMM();
136 return new HMMERAlignmentColourScheme(sg, model);
140 * Answers true if the sequence collection has an HMM consensus sequence, else
144 public boolean isApplicableTo(AnnotatedCollectionI ac)
146 return ac.getHmmConsensus() != null;
150 public String getSchemeName()
152 return JalviewColourScheme.HMMERA.toString();
156 public boolean isSimple()
162 * Counts and stores the relatively frequency of every residue in the
167 public void countFrequencies(AnnotatedCollectionI sg)
169 ResidueCount counts = new ResidueCount(sg.getSequences());
170 int total = counts.getTotalResidueCount(); // excludes gaps
172 for (char symbol : counts.getSymbolCounts().symbols)
174 double freq = counts.getCount(symbol) / (double) total;
175 frequency.put(symbol, freq);
177 logTotalCount = (float) Math.log(total);