JAL-98 javadoc corrected
[jalview.git] / src / jalview / analysis / ResidueCount.java
index 2c7cb20..75decf2 100644 (file)
@@ -19,7 +19,7 @@ public class ResidueCount
   public class SymbolCounts
   {
     /**
-     * the symbols seen (as char values)
+     * the symbols seen (as char values), in no particular order
      */
     public final char[] symbols;
 
@@ -40,7 +40,7 @@ public class ResidueCount
   /*
    * nucleotide symbols to count (including N unknown)
    */
-  private static final String NUCS = "ACGTUN";
+  private static final String NUCS = "ACGNTU";
 
   /*
    * amino acid symbols to count (including X unknown)
@@ -134,10 +134,11 @@ public class ResidueCount
    * @param c
    * @return the new value of the count for the character
    */
-  public int add(char c)
+  public int add(final char c)
   {
+    char u = toUpperCase(c);
     int newValue = 0;
-    int offset = getOffset(c);
+    int offset = getOffset(u);
 
     /*
      * offset 0 is reserved for gap counting, so 0 here means either
@@ -145,13 +146,13 @@ public class ResidueCount
      */
     if (offset == 0)
     {
-      if (Comparison.isGap(c))
+      if (Comparison.isGap(u))
       {
         newValue = addGap();
       }
       else
       {
-        newValue = addOtherCharacter(c);
+        newValue = addOtherCharacter(u);
       }
     }
     else
@@ -209,22 +210,13 @@ public class ResidueCount
   }
 
   /**
+   * Returns this character's offset in the count array
+   * 
    * @param c
    * @return
    */
   int getOffset(char c)
   {
-    /*
-     * ensure upper-case (fails fast if it already is!)
-     */
-    if ('a' <= c && c <= 'z')
-    {
-      c = (char) (c + TOUPPERCASE);
-    }
-
-    /*
-     * locate this character's offset in the count array
-     */
     int offset = 0;
     if ('A' <= c && c <= 'Z')
     {
@@ -234,6 +226,20 @@ public class ResidueCount
   }
 
   /**
+   * @param c
+   * @return
+   */
+  protected char toUpperCase(final char c)
+  {
+    char u = c;
+    if ('a' <= c && c <= 'z')
+    {
+      u = (char) (c + TOUPPERCASE);
+    }
+    return u;
+  }
+
+  /**
    * Increment count for some unanticipated character. The first time this
    * called, a SparseCount is instantiated to hold these 'extra' counts.
    * 
@@ -305,7 +311,8 @@ public class ResidueCount
    */
   public void put(char c, int count)
   {
-    int offset = getOffset(c);
+    char u = toUpperCase(c);
+    int offset = getOffset(u);
 
     /*
      * offset 0 is reserved for gap counting, so 0 here means either
@@ -313,13 +320,13 @@ public class ResidueCount
      */
     if (offset == 0)
     {
-      if (Comparison.isGap(c))
+      if (Comparison.isGap(u))
       {
-        addGap();
+        set(0, count);
       }
       else
       {
-        setOtherCharacter(c, count);
+        setOtherCharacter(u, count);
         maxCount = Math.max(maxCount, count);
       }
     }
@@ -365,13 +372,14 @@ public class ResidueCount
    */
   public int getCount(char c)
   {
-    int offset = getOffset(c);
+    char u = toUpperCase(c);
+    int offset = getOffset(u);
     if (offset == 0)
     {
-      if (!Comparison.isGap(c))
+      if (!Comparison.isGap(u))
       {
         // should have called getGapCount()
-        return otherData == null ? 0 : otherData.get(c);
+        return otherData == null ? 0 : otherData.get(u);
       }
     }
     return useIntCounts ? intCounts[offset] : counts[offset];
@@ -395,8 +403,8 @@ public class ResidueCount
   /**
    * Returns the character (or concatenated characters) for the symbol(s) with
    * the given count in the profile. Can be used to get the modal residue by
-   * supplying the modal count value. Returns an empty string if symbol has the
-   * given count. The symbols are in alphabetic order of standard peptide or
+   * supplying the modal count value. Returns an empty string if no symbol has
+   * the given count. The symbols are in alphabetic order of standard peptide or
    * nucleotide characters, followed by 'other' symbols if any.
    * 
    * @return
@@ -449,7 +457,7 @@ public class ResidueCount
   }
 
   /**
-   * Returns the highest count for any symbol in the profile (excluding gap)
+   * Returns the highest count for any symbol(s) in the profile (excluding gap)
    * 
    * @return
    */
@@ -486,6 +494,11 @@ public class ResidueCount
         }
       }
     }
+
+    /*
+     * include 'other' characters recorded (even if count is zero
+     * though that would be a strange use case)
+     */
     if (otherData != null)
     {
       size += otherData.size();
@@ -495,17 +508,16 @@ public class ResidueCount
   }
 
   /**
-   * Returns those symbols that have a non-zero count (excluding the gap
-   * symbol), with their counts. The symbols are in no special order. Returns an
-   * array of size 2 whose first element is a char array of symbols, and second
-   * element an int array of corresponding counts.
+   * Returns a data bean holding those symbols that have a non-zero count
+   * (excluding the gap symbol), with their counts.
    * 
-   * @return an array [[char1, char2, ...] [char1Count, char2Count, ...] ... ]
+   * @return
    */
   public SymbolCounts getSymbolCounts()
   {
-    char[] symbols = new char[size()];
-    int[] values = new int[size()];
+    int size = size();
+    char[] symbols = new char[size];
+    int[] values = new int[size];
     int j = 0;
 
     if (useIntCounts)
@@ -540,13 +552,9 @@ public class ResidueCount
     {
       for (int i = 0; i < otherData.size(); i++)
       {
-        int value = otherData.valueAt(i);
-        if (value > 0)
-        {
-          symbols[j] = (char) otherData.keyAt(i);
-          values[j] = otherData.valueAt(i);
-          j++;
-        }
+        symbols[j] = (char) otherData.keyAt(i);
+        values[j] = otherData.valueAt(i);
+        j++;
       }
     }
 
@@ -566,7 +574,6 @@ public class ResidueCount
    */
   public String getTooltip(int normaliseBy, int percentageDecPl)
   {
-    StringBuilder sb = new StringBuilder(64);
     SymbolCounts symbolCounts = getSymbolCounts();
     char[] ca = symbolCounts.symbols;
     int[] vl = symbolCounts.values;
@@ -579,20 +586,18 @@ public class ResidueCount
     /*
      * traverse in reverse order (highest count first) to build tooltip
      */
-    for (int p = 0, c = ca.length - 1; c >= 0; c--)
+    boolean first = true;
+    StringBuilder sb = new StringBuilder(64);
+    for (int c = ca.length - 1; c >= 0; c--)
     {
       final char residue = ca[c];
-      if (residue != '-')
-      {
-        // TODO combine residues which share a percentage
-        // (see AAFrequency.completeCdnaConsensus)
-        float tval = (vl[c] * 100f) / normaliseBy;
-        sb.append((((p == 0) ? "" : "; "))).append(residue)
-                .append(" ");
-        Format.appendPercentage(sb, tval, percentageDecPl);
-        sb.append("%");
-        p++;
-      }
+      // TODO combine residues which share a percentage
+      // (see AAFrequency.completeCdnaConsensus)
+      float tval = (vl[c] * 100f) / normaliseBy;
+      sb.append(first ? "" : "; ").append(residue).append(" ");
+      Format.appendPercentage(sb, tval, percentageDecPl);
+      sb.append("%");
+      first = false;
     }
     return sb.toString();
   }