09092a6e7726225335126802827d1c3ffa8594c9
[jalview.git] / src / jalview / schemes / HMMERColourScheme.java
1 package jalview.schemes;
2
3 import jalview.datamodel.AlignmentAnnotation;
4 import jalview.datamodel.AnnotatedCollectionI;
5 import jalview.datamodel.HiddenMarkovModel;
6 import jalview.datamodel.SequenceCollectionI;
7 import jalview.datamodel.SequenceI;
8 import jalview.util.ColorUtils;
9 import jalview.util.Comparison;
10
11 import java.awt.Color;
12 import java.util.Map;
13
14 public class HMMERColourScheme extends ResidueColourScheme
15 {
16
17   AnnotatedCollectionI alignment;
18
19   HiddenMarkovModel hmm;
20   
21   boolean peptideSpecific;
22
23   boolean nucleotideSpecific;
24
25   public HMMERColourScheme(HiddenMarkovModel markov)
26   {
27     hmm = markov;
28   }
29   public HMMERColourScheme()
30   {
31
32   }
33
34   @Override
35   public Color findColour(char symbol, int position, SequenceI seq,
36           String consensusResidue, float pid)
37   {
38     if (hmm ==null)
39     {
40       return Color.white;
41     }
42     return findColour(symbol, position);
43   }
44
45   public Color findColour(char symbol, int position)
46   {
47
48     if (Comparison.isGap(symbol))
49     {
50       return Color.white;
51     }
52     Double prob;
53     prob = hmm.getMatchEmissionProbability(position, symbol);
54     double freq = 0;
55     if ("amino".equals(hmm.getAlphabetType()))
56     {
57       if (!ResidueProperties.aminoBackgroundFrequencies.containsKey(symbol))
58       {
59         return Color.white;
60       }
61       freq = ResidueProperties.aminoBackgroundFrequencies.get(symbol);
62     }
63     else if ("DNA".equals(hmm.getAlphabetType()))
64     {
65       if (!ResidueProperties.dnaBackgroundFrequencies.containsKey(symbol))
66       {
67         return Color.white;
68       }
69       freq = ResidueProperties.dnaBackgroundFrequencies.get(symbol);
70     }
71     else if ("RNA".equals(hmm.getAlphabetType()))
72     {
73       if (!ResidueProperties.rnaBackgroundFrequencies.containsKey(symbol))
74       {
75         return Color.white;
76       }
77       freq = ResidueProperties.rnaBackgroundFrequencies.get(symbol);
78     }
79     if (prob == 0)
80     {
81       return Color.red;
82     }
83     Double value = Math.log(prob / freq);
84     Color colour = null;
85     if (value > 0)
86     {
87
88       colour = ColorUtils.getGraduatedColour(value.floatValue(), 0,
89               Color.WHITE, 3f, Color.blue);
90     }
91     else if (value < 0)
92     {
93       return Color.ORANGE;
94
95     }
96     return colour;
97
98   }
99     
100
101
102
103
104
105   @Override
106   public void alignmentChanged(AnnotatedCollectionI collection,
107           Map<SequenceI, SequenceCollectionI> hiddenReps)
108   {
109     AlignmentAnnotation[] annArr = collection.getAlignmentAnnotation();
110     for (AlignmentAnnotation ann : annArr)
111     {
112       if (ann.label.indexOf("Information Content") > -1)
113       {
114         hmm = ann.getHMM();
115       }
116     }
117
118   }
119
120   @Override
121   public ColourSchemeI getInstance(AnnotatedCollectionI sg,
122           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
123   {
124     HiddenMarkovModel markov = null;
125     AlignmentAnnotation[] annArr = sg.getAlignmentAnnotation();
126     for (AlignmentAnnotation ann : annArr)
127     {
128       if (ann.label.indexOf("Information Content") > -1)
129       {
130         markov = ann.getHMM();
131       }
132     }
133     
134     
135     
136     
137
138     HMMERColourScheme colour = new HMMERColourScheme(markov);
139     return colour;
140
141   }
142
143   @Override
144   public boolean isApplicableTo(AnnotatedCollectionI ac)
145   {
146       return true;
147
148   }
149
150   @Override
151   public String getSchemeName()
152   {
153
154     return JalviewColourScheme.HMMER.name();
155   }
156
157   @Override
158   public boolean isSimple()
159   {
160     // TODO Auto-generated method stub
161     return false;
162   }
163
164 }