Merge remote-tracking branch 'origin/releases/Release_2_10_2_Branch' into features...
[jalview.git] / src / jalview / schemes / HMMERColourScheme.java
1 package jalview.schemes;
2
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;
9
10 import java.awt.Color;
11 import java.util.List;
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
35   @Override
36   public Color findColour(char symbol, int position, SequenceI seq,
37           String consensusResidue, float pid)
38   {
39     if (hmm ==null)
40     {
41       return Color.white;
42     }
43     return findColour(symbol, position);
44   }
45
46   /**
47    * Returns the colour at a particular symbol at a column in the alignment.
48    * 
49    * @param symbol
50    * @param position
51    * @return Red for an insertion, white for a gap, orange for a negative
52    *         information content, white to blue for increasing information
53    *         content.
54    */
55   private Color findColour(char symbol, int position)
56   {
57
58     if (Comparison.isGap(symbol))
59     {
60       return Color.white;
61     }
62     if (Character.isLowerCase(symbol))
63     {
64       return new Color(230, 0, 0);
65     }
66     Double prob;
67     prob = hmm.getMatchEmissionProbability(position, symbol);
68     double freq = 0;
69     String alpha = hmm.getAlphabetType();
70     if (!ResidueProperties.backgroundFrequencies.get(alpha).containsKey(symbol))
71     {
72       return Color.white;
73     }
74     else
75     {
76       freq = ResidueProperties.backgroundFrequencies.get(alpha).get(symbol);
77     }
78     if (prob == 0)
79     {
80       return new Color(230, 0, 0);
81     }
82     Double value = Math.log(prob / freq);
83     Color colour = null;
84     if (value > 0)
85     {
86
87       colour = ColorUtils.getGraduatedColour(value.floatValue(), 0,
88               Color.WHITE, 4.52f, Color.blue);
89     }
90     else if (value < 0)
91     {
92       return Color.ORANGE;
93
94     }
95     return colour;
96
97   }
98     
99
100
101
102
103
104   @Override
105   public void alignmentChanged(AnnotatedCollectionI collection,
106           Map<SequenceI, SequenceCollectionI> hiddenReps)
107   {
108           List<SequenceI> seqs = collection.getSequences();
109             for (SequenceI seq : seqs)
110             {
111               if (seq.getHMM() != null)
112               {
113                 hmm = seq.getHMM();
114                 break;
115               }
116             }
117
118   }
119
120
121
122   @Override
123   public ColourSchemeI getInstance(AnnotatedCollectionI sg,
124           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
125   {
126           HiddenMarkovModel markov = null;
127             List<SequenceI> seqs = sg.getSequences();
128             for (SequenceI seq : seqs)
129             {
130               if (seq.getHMM() != null)
131               {
132                 markov = seq.getHMM();
133                 break;
134               }
135             }
136             HMMERColourScheme colour = new HMMERColourScheme(markov);
137             return colour;
138
139   }
140
141   @Override
142   public boolean isApplicableTo(AnnotatedCollectionI ac)
143   {
144       return true;
145
146   }
147
148   @Override
149   public String getSchemeName()
150   {
151
152     return JalviewColourScheme.HMMER.name();
153   }
154
155   @Override
156   public boolean isSimple()
157   {
158     return false;
159   }
160
161 }