JAL-98 use SparseIntArray (wip)
[jalview.git] / src / jalview / analysis / AAFrequency.java
index fb49541..ef09fe8 100755 (executable)
@@ -25,6 +25,7 @@ import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Annotation;
 import jalview.datamodel.SequenceI;
+import jalview.ext.android.SparseIntArray;
 import jalview.util.Format;
 import jalview.util.MappingUtils;
 import jalview.util.QuickSort;
@@ -115,7 +116,7 @@ public class AAFrequency
     char c = '-';
     float percentage;
 
-    int[] values = new int[255];
+    // int[] values = new int[255];
 
     char[] seq;
 
@@ -125,7 +126,8 @@ public class AAFrequency
       maxCount = 0;
       maxResidue = "";
       nongap = 0;
-      values = new int[255];
+      // values = new int[255];
+      SparseIntArray values = new SparseIntArray();
 
       for (j = 0; j < jSize; j++)
       {
@@ -147,7 +149,8 @@ public class AAFrequency
 
           if (c == '-')
           {
-            values['-']++;
+            // values['-']++;
+            values.put('-', values.get('-') + 1);
             continue;
           }
           else if ('a' <= c && c <= 'z')
@@ -156,12 +159,14 @@ public class AAFrequency
           }
 
           nongap++;
-          values[c]++;
+          // values[c]++;
+          values.put(c, values.get(c) + 1);
 
         }
         else
         {
-          values['-']++;
+          // values['-']++;
+          values.put('-', values.get('-') + 1);
         }
       }
       if (jSize == 1)
@@ -174,20 +179,21 @@ public class AAFrequency
         for (v = 'A'; v <= 'Z'; v++)
         {
           // TODO why ignore values[v] == 1?
-          if (values[v] < 1 /* 2 */|| values[v] < maxCount)
+          int count = values.get(v); // values[v];
+          if (count < 1 /* 2 */|| count < maxCount)
           {
             continue;
           }
 
-          if (values[v] > maxCount)
+          if (count > maxCount)
           {
             maxResidue = CHARS[v - 'A'];
           }
-          else if (values[v] == maxCount)
+          else if (count == maxCount)
           {
             maxResidue += CHARS[v - 'A'];
           }
-          maxCount = values[v];
+          maxCount = count;
         }
       }
       if (maxResidue.length() == 0)
@@ -197,8 +203,9 @@ public class AAFrequency
       if (profile)
       {
         // TODO use a 1-dimensional array with jSize, nongap in [0] and [1]
-        residueHash.put(PROFILE, new int[][] { values,
-            new int[] { jSize, nongap } });
+        // residueHash.put(PROFILE, new int[][] { values,
+        // new int[] { jSize, nongap } });
+        residueHash.put(PROFILE, new Profile(values, jSize, nongap));
       }
       residueHash.put(MAXCOUNT, new Integer(maxCount));
       residueHash.put(MAXRESIDUE, maxResidue);
@@ -410,17 +417,26 @@ public class AAFrequency
           boolean ignoreGaps)
   {
     int[] rtnval = new int[64];
-    int[][] profile = (int[][]) hconsensus.get(AAFrequency.PROFILE);
+    // int[][] profile = (int[][]) hconsensus.get(AAFrequency.PROFILE);
+    Profile profile = (Profile) hconsensus.get(AAFrequency.PROFILE);
     if (profile == null)
     {
       return null;
     }
-    char[] ca = new char[profile[0].length];
-    float[] vl = new float[profile[0].length];
-    for (int c = 0; c < ca.length; c++)
+    // int profileLength = profile[0].length;
+    int profileLength = profile.profile.size();
+    char[] ca = new char[profileLength];
+    float[] vl = new float[profileLength];
+    // for (int c = 0; c < ca.length; c++)
+    // {
+    // ca[c] = (char) c;
+    // vl[c] = profile[0][c];
+    // }
+    for (int i = 0; i < profileLength; i++)
     {
-      ca[c] = (char) c;
-      vl[c] = profile[0][c];
+      int c = profile.profile.keyAt(i);
+      ca[i] = (char) c;
+      vl[i] = profile.profile.get(c);
     }
     QuickSort.sort(vl, ca);
     int nextArrayPos = 2;