1 package jalview.schemes;
3 import jalview.api.AlignViewportI;
4 import jalview.datamodel.AnnotatedCollectionI;
5 import jalview.datamodel.HiddenMarkovModel;
6 import jalview.datamodel.SequenceI;
7 import jalview.util.ColorUtils;
8 import jalview.util.Comparison;
10 import java.awt.Color;
11 import java.io.BufferedReader;
12 import java.io.FileNotFoundException;
13 import java.io.FileReader;
14 import java.io.IOException;
15 import java.util.HashMap;
16 import java.util.List;
20 public class HMMMatchScoreColourScheme extends ResidueColourScheme
23 private Map<Character, Map<String, Double>> probabilities;
25 public class MatchProbReader
27 private BufferedReader reader;
29 MatchProbReader() throws FileNotFoundException
31 reader = new BufferedReader(
32 new FileReader("resources/ProbabilityOfMatch"));
35 public Map<Character, Map<String, Double>> getProbabilities() throws IOException
37 Map<Character, Map<String, Double>> probabilities = new HashMap<>();
39 String[] alphabet = reader.readLine().split("\\,");
40 for (int i = 1; i < alphabet.length - 1; i++)
42 probabilities.put(alphabet[i].replaceAll("\\ ", "").charAt(0),
46 String line = reader.readLine();
49 String[] contents = line.split("\\,");
51 for(int i = 1; i < contents.length; i++)
53 probabilities.get(alphabet[i].replaceAll("\\ ", "").charAt(0))
54 .put(contents[0], Double
55 .valueOf(contents[i].replaceAll("\\ ", "")));
57 line = reader.readLine();
67 private static final Color INSERTION_COLOUR = Color.white;
70 * the aligned HMM consensus sequence to use as reference for colouring
72 private SequenceI hmmSeq;
74 private HiddenMarkovModel hmm;
76 private Map<Character, Float> frequencies;
79 * Constructor given a list of Hidden Markov Model consensus sequences. The
80 * first sequence provides the HMM profile from which we can read the emission
81 * probabilities that determine the colour.
86 public HMMMatchScoreColourScheme(List<SequenceI> hmmSeqs)
88 hmmSeq = hmmSeqs.isEmpty() ? null : hmmSeqs.get(0);
89 hmm = hmmSeq == null ? null : hmmSeq.getHMM();
93 MatchProbReader probabilityReader = new MatchProbReader();
94 probabilities = probabilityReader.getProbabilities();
95 } catch (IOException e)
97 System.out.println(e.getStackTrace());
102 * Default constructor (required by ColourSchemes.loadColourSchemes)
104 public HMMMatchScoreColourScheme()
109 public Color findColour(char symbol, int column, SequenceI seq,
110 String consensusResidue, float pid)
112 return findColour(symbol, column);
117 * Returns the colour at a particular symbol at a column in the alignment:
119 * <li>white for a gap</li>
120 * <li>red for an insertion</li>
121 * <li>orange for negative information content</li>
122 * <li>white to blue for increasing information content</li>
129 private Color findColour(char symbol, int column)
131 if (getHmm() == null || Comparison.isGap(symbol))
135 if (Comparison.isGap(hmmSeq.getCharAt(column)))
137 return INSERTION_COLOUR;
139 if (Character.isLowerCase(symbol))
141 symbol = Character.toUpperCase(symbol);
145 if (hmm.getBackgroundFrequencies().containsKey(symbol))
148 .log(getHmm().getMatchEmissionProbability(column, symbol)
149 / hmm.getBackgroundFrequencies().get(symbol));
151 if (!probabilities.get(symbol).containsKey(format(llr)))
156 prob = probabilities.get(symbol).get(format(llr));
160 return new Color(140, 140, 140);
163 Color colour = Color.ORANGE;
167 colour = ColorUtils.getGraduatedColour((float) prob, 0.5f,
173 colour = ColorUtils.getGraduatedColour((float) prob, 0f, Color.red,
180 public static String format(Double d)
182 String formatted = String.format("%.1f", d);
183 if (Double.valueOf(formatted) == 0)
191 * Answers the maximum possible value of information score (log ratio), for use
192 * in scaling a graduated colour range
196 protected float getMaxInformationScore()
202 * Answers a new colour scheme instance based on the HMM of the first sequence
203 * in ac that has an HMM
206 public ColourSchemeI getInstance(AlignViewportI viewport,
207 AnnotatedCollectionI ac)
209 return newInstance(ac);
213 * Constructor given a sequence collection
217 public HMMMatchScoreColourScheme(AnnotatedCollectionI ac)
219 this(ac.getHmmSequences());
223 * Answers a new instance of the colour scheme for the given HMM
228 protected HMMMatchScoreColourScheme newInstance(AnnotatedCollectionI ac)
230 return new HMMMatchScoreColourScheme(ac);
234 public boolean isSimple()
240 * Answers true if the sequence collection has an HMM consensus sequence and
241 * that the first HMM sequence contains background frequencies, else false
244 public boolean isApplicableTo(AnnotatedCollectionI ac)
246 return !ac.getHmmSequences().isEmpty() && ac.getHmmSequences().get(0)
247 .getHMM().getBackgroundFrequencies() != null;
250 protected Map<Character, Float> getFrequencies()
255 protected void setFrequencies(Map<Character, Float> frequencies)
257 this.frequencies = frequencies;
260 protected HiddenMarkovModel getHmm()
265 protected SequenceI getHmmSequence()
271 public String getSchemeName()
273 return JalviewColourScheme.HMMMatchScore.toString();