Merge branch 'Jalview-JS/develop' into merge_js_develop
[jalview.git] / src / jalview / schemes / HmmerLocalBackground.java
1 package jalview.schemes;
2
3 import jalview.datamodel.AnnotatedCollectionI;
4 import jalview.datamodel.ResidueCount;
5 import jalview.datamodel.SequenceCollectionI;
6 import jalview.datamodel.SequenceI;
7
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11
12 /**
13  * An HMM colour scheme that uses local (alignment or sub-group) background
14  * frequencies for residues
15  * 
16  * @author tzvanaalten
17  */
18 public class HmmerLocalBackground extends HmmerColourScheme
19 {
20   float logTotalCount;
21
22   /**
23    * Constructor given a sequence collection
24    * 
25    * @param ac
26    */
27   public HmmerLocalBackground(AnnotatedCollectionI ac)
28   {
29     super(ac.getHmmSequences());
30     countFrequencies(ac);
31   }
32
33   /**
34    * Default constructor (required by ColourSchemes.loadColourSchemes)
35    */
36   public HmmerLocalBackground()
37   {
38   }
39
40   @Override
41   public String getSchemeName()
42   {
43     return JalviewColourScheme.HMMERA.toString();
44   }
45
46   /**
47    * Counts and stores the relative frequency of every residue in the alignment
48    * (apart from any HMM consensus sequences)
49    * 
50    * @param sc
51    */
52   public void countFrequencies(SequenceCollectionI sc)
53   {
54     // TODO or total counts in Consensus Profile (how do we get at it?)?
55     Map<Character, Float> freqs = new HashMap<>();
56
57     /*
58      * count symbols, excluding any HMM consensus sequences
59      */
60     ResidueCount counts = new ResidueCount();
61     List<SequenceI> seqs = sc.getSequences();
62     for (SequenceI seq : seqs)
63     {
64       if (!seq.hasHMMProfile())
65       {
66         for (char c : seq.getSequence())
67         {
68           counts.add(c);
69         }
70       }
71     }
72     int total = counts.getTotalResidueCount(); // excludes gaps
73
74     for (char symbol : counts.getSymbolCounts().symbols)
75     {
76       double freq = counts.getCount(symbol) / (double) total;
77       freqs.put(symbol, (float) freq);
78     }
79
80     setFrequencies(freqs);
81
82     logTotalCount = (float) Math.log(total);
83   }
84
85   @Override
86   float getMaxInformationScore()
87   {
88     return logTotalCount;
89   }
90
91   @Override
92   protected HmmerColourScheme newInstance(AnnotatedCollectionI ac)
93   {
94     return new HmmerLocalBackground(ac);
95   }
96 }