JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / analysis / StructureFrequency.java
index 001a034..0fe85d9 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
+ * Copyright (C) 2015 The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  */
 package jalview.analysis;
 
-import java.util.*;
-
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.Annotation;
+import jalview.datamodel.SequenceFeature;
+import jalview.datamodel.SequenceI;
 import jalview.util.Format;
-import jalview.datamodel.*;
+
+import java.util.ArrayList;
+import java.util.Hashtable;
 
 /**
  * Takes in a vector or array of sequences and column start and column end and
@@ -36,6 +40,8 @@ import jalview.datamodel.*;
  */
 public class StructureFrequency
 {
+  public static final int STRUCTURE_PROFILE_LENGTH = 74;
+
   // No need to store 1000s of strings which are not
   // visible to the user.
   public static final String MAXCOUNT = "C";
@@ -187,9 +193,9 @@ public class StructureFrequency
       // UPDATE this for new values
       if (profile)
       {
-        residueHash.put(PROFILE, new int[][]
-        { values, new int[]
-        { jSize, (jSize - values['-']) } });
+        // TODO 1-dim array with jsize in [0], nongapped in [1]; or Pojo
+        residueHash.put(PROFILE, new int[][] { values,
+            new int[] { jSize, (jSize - values['-']) } });
 
         residueHash.put(PAIRPROFILE, pairs);
       }
@@ -232,9 +238,8 @@ public class StructureFrequency
         }
         if (profile)
         {
-          residueHash.put(PROFILE, new int[][]
-          { values, new int[]
-          { jSize, (jSize - values['-']) } });
+          residueHash.put(PROFILE, new int[][] { values,
+              new int[] { jSize, (jSize - values['-']) } });
 
           residueHash.put(PAIRPROFILE, pairs);
         }
@@ -413,15 +418,14 @@ public class StructureFrequency
          * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
          * else {
          */
-        Object[] ca = new Object[625];
+        int[][] ca = new int[625][];
         float[] vl = new float[625];
         int x = 0;
         for (int c = 65; c < 90; c++)
         {
           for (int d = 65; d < 90; d++)
           {
-            ca[x] = new int[]
-            { c, d };
+            ca[x] = new int[] { c, d };
             vl[x] = pairs[c][d];
             x++;
           }
@@ -429,15 +433,18 @@ public class StructureFrequency
         jalview.util.QuickSort.sort(vl, ca);
         int p = 0;
 
+        /*
+         * profile[1] is {total, ungappedTotal}
+         */
+        final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1
+                : 0];
         for (int c = 624; c > 0; c--)
         {
           if (vl[c] > 0)
           {
-            tval = (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
-                    : 0]);
-            mouseOver += ((p == 0) ? "" : "; ") + (char) ((int[]) ca[c])[0]
-                    + (char) ((int[]) ca[c])[1] + " " + fmt.form(tval)
-                    + "%";
+            tval = (vl[c] * 100f / divisor);
+            mouseOver += ((p == 0) ? "" : "; ") + (char) ca[c][0]
+                    + (char) ca[c][1] + " " + fmt.form(tval) + "%";
             p++;
 
           }
@@ -463,45 +470,54 @@ public class StructureFrequency
   public static int[] extractProfile(Hashtable hconsensus,
           boolean ignoreGapsInConsensusCalculation)
   {
-    int[] rtnval = new int[74]; // 2*(5*5)+2
+    int[] rtnval = new int[STRUCTURE_PROFILE_LENGTH]; // 2*(5*5)+2
     int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
     int[][] pairs = (int[][]) hconsensus
             .get(StructureFrequency.PAIRPROFILE);
 
     if (profile == null)
+    {
       return null;
+    }
 
     // TODO fix the object length, also do it in completeConsensus
-    Object[] ca = new Object[625];
+    // Object[] ca = new Object[625];
+    int[][] ca = new int[625][];
     float[] vl = new float[625];
     int x = 0;
     for (int c = 65; c < 90; c++)
     {
       for (int d = 65; d < 90; d++)
       {
-        ca[x] = new int[]
-        { c, d };
+        ca[x] = new int[] { c, d };
         vl[x] = pairs[c][d];
         x++;
       }
     }
     jalview.util.QuickSort.sort(vl, ca);
 
-    rtnval[0] = 2;
+    int valuesCount = 0;
     rtnval[1] = 0;
+    int offset = 2;
+    final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
     for (int c = 624; c > 0; c--)
     {
       if (vl[c] > 0)
       {
-        rtnval[rtnval[0]++] = ((int[]) ca[c])[0];
-        rtnval[rtnval[0]++] = ((int[]) ca[c])[1];
-        rtnval[rtnval[0]] = (int) (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
-                : 0]);
-        rtnval[1] += rtnval[rtnval[0]++];
+        rtnval[offset++] = ca[c][0];
+        rtnval[offset++] = ca[c][1];
+        rtnval[offset] = (int) (vl[c] * 100f / divisor);
+        rtnval[1] += rtnval[offset++];
+        valuesCount++;
       }
     }
+    rtnval[0] = valuesCount;
 
-    return rtnval;
+    // insert profile type code in position 0
+    int[] result = new int[rtnval.length + 1];
+    result[0] = AlignmentAnnotation.STRUCTURE_PROFILE;
+    System.arraycopy(rtnval, 0, result, 1, rtnval.length);
+    return result;
   }
 
   public static void main(String args[])