JAL-2629 tidy unit tests, constants etc
[jalview.git] / src / jalview / analysis / AAFrequency.java
index 85a9bb0..f77517c 100755 (executable)
@@ -50,31 +50,12 @@ import java.util.List;
  * This class is used extensively in calculating alignment colourschemes that
  * depend on the amount of conservation in each alignment column.
  * 
- * @author $author$
- * @version $Revision$
  */
 public class AAFrequency
 {
-  public static final String PROFILE = "P";
-
-  private static final String AMINO = "amino";
-
-  private static final String DNA = "DNA";
-
-  private static final String RNA = "RNA";
+  private static final double LOG2 = Math.log(2);
 
-  /*
-   * Quick look-up of String value of char 'A' to 'Z'
-   */
-  private static final String[] CHARS = new String['Z' - 'A' + 1];
-
-  static
-  {
-    for (char c = 'A'; c <= 'Z'; c++)
-    {
-      CHARS[c - 'A'] = String.valueOf(c);
-    }
-  }
+  public static final String PROFILE = "P";
 
   public static final ProfilesI calculate(List<SequenceI> list, int start,
           int end)
@@ -109,8 +90,6 @@ public class AAFrequency
     }
   }
 
-
-
   /**
    * Calculate the consensus symbol(s) for each column in the given range.
    * 
@@ -400,14 +379,12 @@ public class AAFrequency
         information.annotations[i] = null;
         return 0;
       }
-
-      HiddenMarkovModel hmm;
       
       SequenceI hmmSeq = information.sequenceRef;
       
-      hmm = hmmSeq.getHMM();
+      HiddenMarkovModel hmm = hmmSeq.getHMM();
       
-      Float value = getInformationContent(i, hmm);
+      float value = hmm.getInformationContent(i);
 
       if (value > max)
       {
@@ -865,121 +842,104 @@ public class AAFrequency
   }
 
   /**
-   * Returns the information content at a specified column.
+   * Returns the sorted HMM profile for the given column of the alignment. The
+   * returned array contains
    * 
-   * @param column
-   *          Index of the column, starting from 0.
-   * @return
-   */
-  public static float getInformationContent(int column,
-          HiddenMarkovModel hmm)
-  {
-    float informationContent = 0f;
-
-    for (char symbol : hmm.getSymbols())
-    {
-      float freq = 0f;
-      freq = ResidueProperties.backgroundFrequencies
-              .get(hmm.getAlphabetType()).get(symbol);
-      Double hmmProb = hmm.getMatchEmissionProbability(column, symbol);
-      float prob = hmmProb.floatValue();
-      informationContent += prob * (Math.log(prob / freq) / Math.log(2));
-
-    }
-
-    return informationContent;
-  }
-
-  /**
-   * Produces a HMM profile for a column in an alignment
+   * <pre>
+   *    [profileType=0, numberOfValues, 100, charValue1, percentage1, charValue2, percentage2, ...]
+   * in descending order of percentage value
+   * </pre>
    * 
-   * @param aa
-   *          Alignment annotation for which the profile is being calculated.
+   * @param hmm
    * @param column
-   *          Column in the alignment the profile is being made for.
    * @param removeBelowBackground
-   *          Boolean indicating whether to ignore residues with probabilities
-   *          less than their background frequencies.
+   *          if true, ignores residues with probability less than their
+   *          background frequency
+   * @param infoHeight
+   *          if true, uses the log ratio 'information' measure to scale the
+   *          value
    * @return
    */
   public static int[] extractHMMProfile(HiddenMarkovModel hmm, int column,
           boolean removeBelowBackground, boolean infoHeight)
   {
-
-    if (hmm != null)
+    if (hmm == null)
     {
-      int size = hmm.getNumberOfSymbols();
-      char symbols[] = new char[size];
-      int values[] = new int[size];
-      List<Character> charList = hmm.getSymbols();
-      Integer totalCount = 0;
+      return null;
+    }
+    int size = hmm.getNumberOfSymbols();
+    char symbols[] = new char[size];
+    int values[] = new int[size];
+    List<Character> charList = hmm.getSymbols();
+    int totalCount = 0;
 
-      for (int i = 0; i < size; i++)
-      {
-        char symbol = charList.get(i);
-        symbols[i] = symbol;
-        int value = getAnalogueCount(hmm, column, symbol,
-                removeBelowBackground, infoHeight);
-        values[i] = value;
-        totalCount += value;
-      }
+    for (int i = 0; i < size; i++)
+    {
+      char symbol = charList.get(i);
+      symbols[i] = symbol;
+      int value = getAnalogueCount(hmm, column, symbol,
+              removeBelowBackground, infoHeight);
+      values[i] = value;
+      totalCount += value;
+    }
 
-      QuickSort.sort(values, symbols);
+    /*
+     * sort symbols by increasing emission probability
+     */
+    QuickSort.sort(values, symbols);
 
-      int[] profile = new int[3 + size * 2];
+    int[] profile = new int[3 + size * 2];
 
-      profile[0] = AlignmentAnnotation.SEQUENCE_PROFILE;
-      profile[1] = size;
-      profile[2] = 100;
+    profile[0] = AlignmentAnnotation.SEQUENCE_PROFILE;
+    profile[1] = size;
+    profile[2] = 100;
 
-      if (totalCount != 0)
+    /*
+     * order symbol/count profile by decreasing emission probability
+     */
+    if (totalCount != 0)
+    {
+      int arrayPos = 3;
+      for (int k = size - 1; k >= 0; k--)
       {
-        int arrayPos = 3;
-        for (int k = size - 1; k >= 0; k--)
+        Float percentage;
+        int value = values[k];
+        if (removeBelowBackground)
         {
-          Float percentage;
-          Integer value = values[k];
-          if (removeBelowBackground)
-          {
-            percentage = (value.floatValue() / totalCount.floatValue())
-                    * 100;
-          }
-          else
-          {
-            percentage = value.floatValue() / 100f;
-          }
-          int intPercent = Math.round(percentage);
-          profile[arrayPos] = symbols[k];
-          profile[arrayPos + 1] = intPercent;
-          arrayPos += 2;
+          percentage = ((float) value) / totalCount * 100f;
         }
+        else
+        {
+          percentage = value / 100f;
+        }
+        int intPercent = Math.round(percentage);
+        profile[arrayPos] = symbols[k];
+        profile[arrayPos + 1] = intPercent;
+        arrayPos += 2;
       }
-      return profile;
     }
-    return null;
+    return profile;
   }
 
   /**
    * Converts the emission probability of a residue at a column in the alignment
-   * to a 'count' to allow for processing by the annotation renderer.
+   * to a 'count', suitable for rendering as an annotation value
    * 
    * @param hmm
    * @param column
-   * @param removeBelowBackground
-   *          When true, this method returns 0 for any symbols with a match
-   *          emission probability less than the background frequency.
    * @param symbol
+   * @param removeBelowBackground
+   *          if true, returns 0 for any symbol with a match emission
+   *          probability less than the background frequency
+   * @infoHeight if true, uses the log ratio 'information content' to scale the
+   *             value
    * @return
    */
   static int getAnalogueCount(HiddenMarkovModel hmm, int column,
           char symbol, boolean removeBelowBackground, boolean infoHeight)
   {
-    Double value;
-
-    value = hmm.getMatchEmissionProbability(column, symbol);
-    double freq;
-
-    freq = ResidueProperties.backgroundFrequencies
+    double value = hmm.getMatchEmissionProbability(column, symbol);
+    double freq = ResidueProperties.backgroundFrequencies
             .get(hmm.getAlphabetType()).get(symbol);
     if (value < freq && removeBelowBackground)
     {
@@ -988,10 +948,10 @@ public class AAFrequency
 
     if (infoHeight)
     {
-      value = value * (Math.log(value / freq) / Math.log(2));
+      value = value * (Math.log(value / freq) / LOG2);
     }
 
-    value = value * 10000;
-    return Math.round(value.floatValue());
+    value = value * 10000d;
+    return Math.round((float) value);
   }
 }