add partial button fix to annotation and statistics output
[jalview.git] / src / jalview / analysis / AAFrequency.java
index b5dab6f..57e00fe 100755 (executable)
@@ -24,6 +24,7 @@ import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Annotation;
+import jalview.datamodel.HiddenMarkovModel;
 import jalview.datamodel.Profile;
 import jalview.datamodel.ProfileI;
 import jalview.datamodel.Profiles;
@@ -32,11 +33,13 @@ import jalview.datamodel.ResidueCount;
 import jalview.datamodel.ResidueCount.SymbolCounts;
 import jalview.datamodel.SequenceI;
 import jalview.ext.android.SparseIntArray;
+import jalview.schemes.ResidueProperties;
 import jalview.util.Comparison;
 import jalview.util.Format;
 import jalview.util.MappingUtils;
 import jalview.util.QuickSort;
 
+import java.awt.Color;
 import java.util.Arrays;
 import java.util.Hashtable;
 import java.util.List;
@@ -54,6 +57,10 @@ public class AAFrequency
 {
   public static final String PROFILE = "P";
 
+  private static final String AMINO = "amino";
+
+  private static final String DNA = "DNA";
+
   /*
    * Quick look-up of String value of char 'A' to 'Z'
    */
@@ -67,8 +74,8 @@ public class AAFrequency
     }
   }
 
-  public static final ProfilesI calculate(List<SequenceI> list,
-          int start, int end)
+  public static final ProfilesI calculate(List<SequenceI> list, int start,
+          int end)
   {
     return calculate(list, start, end, false);
   }
@@ -100,6 +107,7 @@ public class AAFrequency
     }
   }
 
+
   /**
    * Calculate the consensus symbol(s) for each column in the given range.
    * 
@@ -291,7 +299,7 @@ public class AAFrequency
   /**
    * Derive the gap count annotation row.
    * 
-   * @param consensus
+   * @param gaprow
    *          the annotation row to add annotations to
    * @param profiles
    *          the source consensus data
@@ -300,12 +308,11 @@ public class AAFrequency
    * @param endCol
    *          end column (exclusive)
    */
-  public static void completeGapAnnot(AlignmentAnnotation consensus,
+  public static void completeGapAnnot(AlignmentAnnotation gaprow,
           ProfilesI profiles, int startCol, int endCol, long nseq)
   {
-    // long now = System.currentTimeMillis();
-    if (consensus == null || consensus.annotations == null
-            || consensus.annotations.length < endCol)
+    if (gaprow == null || gaprow.annotations == null
+            || gaprow.annotations.length < endCol)
     {
       /*
        * called with a bad alignment annotation row 
@@ -313,7 +320,10 @@ public class AAFrequency
        */
       return;
     }
-
+    // always set ranges again
+    gaprow.graphMax = nseq;
+    gaprow.graphMin = 0;
+    double scale = 0.8/nseq;
     for (int i = startCol; i < endCol; i++)
     {
       ProfileI profile = profiles.get(i);
@@ -323,18 +333,18 @@ public class AAFrequency
          * happens if sequences calculated over were 
          * shorter than alignment width
          */
-        consensus.annotations[i] = null;
+        gaprow.annotations[i] = null;
         return;
       }
 
-      final int gapped = profile.getGapped();
+      final int gapped = profile.getNonGapped();
 
       String description = "" + gapped;
 
-      consensus.annotations[i] = new Annotation(gapped);
+      gaprow.annotations[i] = new Annotation("", description,
+              '\0', gapped, jalview.util.ColorUtils.bleachColour(
+                      Color.DARK_GRAY, (float) scale * gapped));
     }
-    // long elapsed = System.currentTimeMillis() - now;
-    // System.out.println(-elapsed);
   }
 
   /**
@@ -406,8 +416,7 @@ public class AAFrequency
    *          calculations
    * @return
    */
-  public static int[] extractProfile(ProfileI profile,
-          boolean ignoreGaps)
+  public static int[] extractProfile(ProfileI profile, boolean ignoreGaps)
   {
     int[] rtnval = new int[64];
     ResidueCount counts = profile.getCounts();
@@ -447,6 +456,7 @@ public class AAFrequency
     return result;
   }
 
+
   /**
    * Extract a sorted extract of cDNA codon profile data. The returned array
    * contains
@@ -529,7 +539,7 @@ public class AAFrequency
     for (int col = 0; col < cols; col++)
     {
       // todo would prefer a Java bean for consensus data
-      Hashtable<String, int[]> columnHash = new Hashtable<String, int[]>();
+      Hashtable<String, int[]> columnHash = new Hashtable<>();
       // #seqs, #ungapped seqs, counts indexed by (codon encoded + 1)
       int[] codonCounts = new int[66];
       codonCounts[0] = alignment.getSequences().size();
@@ -716,4 +726,89 @@ public class AAFrequency
     }
     return scale;
   }
+
+  /**
+   * produces a HMM profile for a column in an alignment
+   * 
+   * @param aa
+   *          Alignment annotation for which the profile is being calculated
+   * @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
+   * @return
+   */
+  public static int[] getHMMProfileFor(AlignmentAnnotation aa, int column,
+          boolean removeBelowBackground)
+  {
+
+    HiddenMarkovModel hmm;
+    hmm = aa.getHMM();
+    if (hmm != null)
+    {
+      String alph = hmm.getAlphabetType();
+      int size = hmm.getNumberOfSymbols();
+      char symbols[] = new char[size];
+      int values[] = new int[size];
+      List<Character> charList = hmm.getSymbols();
+      Integer totalCount = 0;
+
+      for (int i = 0; i < size; i++)
+      {
+        char symbol = charList.get(i);
+        symbols[i] = symbol;
+        Double value;
+
+        value = hmm.getMatchEmissionProbability(column, symbol);
+        double freq;
+
+        if (AMINO.equals(alph) && removeBelowBackground)
+        {
+          freq = ResidueProperties.aminoBackgroundFrequencies.get(symbol);
+          if (value < freq)
+          {
+            value = 0d;
+          }
+        }
+        else if (DNA.equals(alph) && removeBelowBackground)
+        {
+          freq = ResidueProperties.nucleotideBackgroundFrequencies
+                  .get(symbol);
+          if (value < freq)
+          {
+            value = 0d;
+          }
+        }
+        value = value * 10000;
+        values[i] = value.intValue();
+        totalCount += value.intValue();
+      }
+
+      QuickSort.sort(values, symbols);
+
+      int[] profile = new int[3 + size * 2];
+
+      profile[0] = AlignmentAnnotation.SEQUENCE_PROFILE;
+      profile[1] = size;
+      profile[2] = totalCount / 100;
+
+      if (totalCount != 0)
+      {
+        int arrayPos = 3;
+        for (int k = size - 1; k >= 0; k--)
+        {
+          Double percentage;
+          Integer value = values[k];
+          percentage = (value.doubleValue() / totalCount.doubleValue())
+                  * 100d;
+          profile[arrayPos] = symbols[k];
+          profile[arrayPos + 1] = percentage.intValue();
+          arrayPos += 2;
+        }
+      }
+      return profile;
+    }
+    return null;
+  }
 }