Merge branch 'bug/JAL-98consensusMemory' into develop
[jalview.git] / src / jalview / analysis / Conservation.java
index 75dec63..8127747 100755 (executable)
@@ -24,12 +24,13 @@ 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;
-import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
+import java.util.TreeMap;
 import java.util.Vector;
 
 /**
@@ -188,21 +189,22 @@ public class Conservation
    */
   public void calculate()
   {
-    int thresh, j, jSize = sequences.length;
-    int[] values; // Replaces residueHash
-    char c;
+    int jSize = sequences.length;
+    // int[] values; // Replaces residueHash
+    SparseIntArray values = new SparseIntArray();
 
-    total = new Hashtable[maxLength];
+    total = new Map[maxLength];
 
     for (int i = start; i <= end; i++)
     {
-      values = new int[255];
+      // values = new int[255];
+      values.clear();
 
-      for (j = 0; j < jSize; j++)
+      for (int j = 0; j < jSize; j++)
       {
         if (sequences[j].getLength() > i)
         {
-          c = sequences[j].getCharAt(i);
+          char c = sequences[j].getCharAt(i);
 
           if (canonicaliseAa)
           { // lookup the base aa code symbol
@@ -228,23 +230,29 @@ public class Conservation
 
             c = toUpperCase(c);
           }
-          values[c]++;
+          // values[c]++;
+          values.add(c, 1);
         }
         else
         {
-          values['-']++;
+          // values['-']++;
+          values.add('-', 1);
         }
       }
 
       // What is the count threshold to count the residues in residueHash()
-      thresh = (threshold * (jSize)) / 100;
+      int thresh = (threshold * jSize) / 100;
 
       // loop over all the found residues
-      Hashtable<String, Integer> resultHash = new Hashtable<String, Integer>();
-      for (char v = '-'; v < 'Z'; v++)
+      // Hashtable<String, Integer> resultHash = new Hashtable<String,
+      // Integer>();
+      Map<String, Integer> resultHash = new TreeMap<String, Integer>();
+      // for (char v = '-'; v < 'Z'; v++)
+      for (int key = 0; key < values.size(); key++)
       {
-
-        if (values[v] > thresh)
+        char v = (char) values.keyAt(key);
+        // if (values[v] > thresh)
+        if (values.valueAt(key) > thresh)
         {
           String res = String.valueOf(v);
 
@@ -359,7 +367,7 @@ public class Conservation
    */
   public void verdict(boolean consflag, float percentageGaps)
   {
-    StringBuffer consString = new StringBuffer();
+    StringBuilder consString = new StringBuilder(end);
 
     // NOTE THIS SHOULD CHECK IF THE CONSEQUENCE ALREADY
     // EXISTS AND NOT OVERWRITE WITH '-', BUT THIS CASE
@@ -374,7 +382,9 @@ public class Conservation
       int[] gapcons = countConsNGaps(i);
       int totGaps = gapcons[1];
       float pgaps = ((float) totGaps * 100) / sequences.length;
-      consSymbs[i - start] = new String();
+      StringBuilder positives = new StringBuilder(64);
+      StringBuilder negatives = new StringBuilder(32);
+      // consSymbs[i - start] = "";
 
       if (percentageGaps > pgaps)
       {
@@ -389,7 +399,9 @@ public class Conservation
           {
             if (result == 1)
             {
-              consSymbs[i - start] = type + " " + consSymbs[i - start];
+              // consSymbs[i - start] = type + " " + consSymbs[i - start];
+              positives.append(positives.length() == 0 ? "" : " ");
+              positives.append(type);
               count++;
             }
           }
@@ -399,16 +411,31 @@ public class Conservation
             {
               if (result == 0)
               {
-                consSymbs[i - start] = consSymbs[i - start] + " !" + type;
+                /*
+                 * add negatively conserved properties on the end
+                 */
+                // consSymbs[i - start] = consSymbs[i - start] + " !" + type;
+                negatives.append(negatives.length() == 0 ? "" : " ");
+                negatives.append("!").append(type);
               }
               else
               {
-                consSymbs[i - start] = type + " " + consSymbs[i - start];
+                /*
+                 * put positively conserved properties on the front
+                 */
+                // consSymbs[i - start] = type + " " + consSymbs[i - start];
+                positives.append(positives.length() == 0 ? "" : " ");
+                positives.append(type);
               }
               count++;
             }
           }
         }
+        if (negatives.length() > 0)
+        {
+          positives.append(" ").append(negatives);
+        }
+        consSymbs[i - start] = positives.toString();
 
         if (count < 10)
         {