JAL-98 use SparseIntArray (wip)
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 23 Sep 2016 07:44:56 +0000 (08:44 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 23 Sep 2016 07:44:56 +0000 (08:44 +0100)
src/jalview/analysis/AAFrequency.java
src/jalview/analysis/Conservation.java
src/jalview/analysis/Profile.java [new file with mode: 0644]

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;
index 711710b..26a7b9f 100755 (executable)
@@ -24,6 +24,7 @@ import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.Annotation;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceI;
+import jalview.ext.android.SparseIntArray;
 import jalview.schemes.ResidueProperties;
 
 import java.awt.Color;
@@ -189,14 +190,15 @@ public class Conservation
   public void calculate()
   {
     int thresh, j, jSize = sequences.length;
-    int[] values; // Replaces residueHash
+    // int[] values; // Replaces residueHash
+    SparseIntArray values = new SparseIntArray();
     char c;
 
     total = new Hashtable[maxLength];
 
     for (int i = start; i <= end; i++)
     {
-      values = new int[255];
+      // values = new int[255];
 
       for (j = 0; j < jSize; j++)
       {
@@ -228,11 +230,13 @@ public class Conservation
 
             c = toUpperCase(c);
           }
-          values[c]++;
+          // values[c]++;
+          values.put(c, values.get(c) + 1);
         }
         else
         {
-          values['-']++;
+          // values['-']++;
+          values.put('-', values.get('-') + 1);
         }
       }
 
@@ -244,7 +248,8 @@ public class Conservation
       for (char v = '-'; v < 'Z'; v++)
       {
 
-        if (values[v] > thresh)
+        // if (values[v] > thresh)
+        if (values.get(v) > thresh)
         {
           String res = String.valueOf(v);
 
diff --git a/src/jalview/analysis/Profile.java b/src/jalview/analysis/Profile.java
new file mode 100644 (file)
index 0000000..ac2728b
--- /dev/null
@@ -0,0 +1,33 @@
+package jalview.analysis;
+
+import jalview.ext.android.SparseIntArray;
+
+public class Profile
+{
+  /*
+   * array of keys (chars) and values (counts) 
+   */
+  public final SparseIntArray profile;
+
+  /*
+   * the number of sequences in the profile
+   */
+  public final int ht;
+
+  /*
+   * the number of non-gapped sequences in the profile
+   */
+  public final int nonGapped;
+
+  public Profile(SparseIntArray counts, int height, int nongappedCount)
+  {
+    this.profile = counts;
+    this.ht = height;
+    this.nonGapped = nongappedCount;
+  }
+
+  public SparseIntArray getProfile()
+  {
+    return profile;
+  }
+}