AAFrequency optimized
authoramwaterhouse <Andrew Waterhouse>
Fri, 6 Oct 2006 13:52:06 +0000 (13:52 +0000)
committeramwaterhouse <Andrew Waterhouse>
Fri, 6 Oct 2006 13:52:06 +0000 (13:52 +0000)
src/jalview/analysis/AAFrequency.java
src/jalview/appletgui/AlignFrame.java
src/jalview/appletgui/AlignViewport.java
src/jalview/appletgui/OverviewPanel.java
src/jalview/datamodel/Alignment.java
src/jalview/datamodel/AlignmentI.java
src/jalview/gui/AlignFrame.java
src/jalview/gui/AlignViewport.java
src/jalview/gui/Jalview2XML.java
src/jalview/gui/Jalview2XML_V1.java

index 6fa9248..15618de 100755 (executable)
@@ -35,105 +35,108 @@ import java.util.*;
  */\r
 public class AAFrequency\r
 {\r
-    /** Takes in a vector of sequences and column start and column end\r
-    * and returns a vector of size (end-start+1). Each element of the\r
+    /** Takes in a !!ARRAY!! of sequences and column start and column end\r
+    * and fills given Vector of size (end-start+1). Each element of the\r
     * vector contains a hashtable with the keys being residues and\r
     * the values being the count of each residue in that column.\r
     * This class is used extensively in calculating alignment colourschemes\r
     * that depend on the amount of conservation in each alignment column. */\r
-    public static final Vector calculate(Vector sequences, int start, int end)\r
+\r
+   public static final Hashtable [] calculate(Vector sequences, int start, int end)\r
+   {\r
+     SequenceI [] seqs = new SequenceI[sequences.size()];\r
+     int width = 0;\r
+     for(int i=0; i<sequences.size(); i++)\r
+     {\r
+       seqs[i] = (SequenceI) sequences.elementAt(i);\r
+       if(seqs[i].getLength()>width)\r
+         width = seqs[i].getLength();\r
+     }\r
+\r
+     Hashtable [] reply = new Hashtable[width];\r
+\r
+     calculate(seqs, start, end, reply);\r
+\r
+     return reply;\r
+   }\r
+\r
+public static final void calculate(SequenceI[] sequences,\r
+                                  int start, int end,\r
+                                  Hashtable [] result)\r
+{\r
+  Hashtable residueHash;\r
+  int maxCount, nongap, i, j, v, jSize = sequences.length;\r
+  String maxResidue;\r
+  char c;\r
+  float percentage;\r
+\r
+  int[] values = new int[132];\r
+\r
+  for (i = start; i < end; i++)\r
+  {\r
+    residueHash = new Hashtable();\r
+    maxCount = 0;\r
+    maxResidue = "-";\r
+    nongap = 0;\r
+    values = new int[132];\r
+\r
+    for (j = 0; j < jSize; j++)\r
     {\r
-      Vector result = new Vector();\r
-      Hashtable residueHash;\r
-      int count, maxCount, nongap, i, j, jSize = sequences.size();\r
-      String maxResidue, sequence, res;\r
-      float percentage;\r
+      if (sequences[j].getLength() > i)\r
+      {\r
+        c = sequences[j].getCharAt(i);\r
 \r
-        for (i = start; i <= end; i++)\r
+        if ('a' <= c && c <= 'z')\r
+        {\r
+          c -= ('a' - 'A');\r
+        }\r
+\r
+        if (jalview.util.Comparison.isGap(c))\r
+        {\r
+          c = '-'; // we always use this for gaps in the property vectors\r
+        }\r
+        else\r
         {\r
-            residueHash = new Hashtable();\r
-            maxCount = 0;\r
-            maxResidue = "-";\r
-            nongap = 0;\r
-\r
-            for (j = 0; j < jSize; j++)\r
-            {\r
-                if (sequences.elementAt(j) instanceof Sequence)\r
-                {\r
-                    sequence = ((Sequence) sequences.elementAt(j)).getSequence();\r
-\r
-                    if (sequence.length() > i)\r
-                    {\r
-                        res = String.valueOf(Character.toUpperCase(sequence.charAt(i)));\r
-\r
-                        if (jalview.util.Comparison.isGap(res.charAt(0)))\r
-                        {\r
-                            res = "-"; // we always use this for gaps in the property vectors\r
-                        }\r
-                        else\r
-                        {   nongap++;    }\r
-\r
-                        if (residueHash.containsKey(res))\r
-                        {\r
-                            count = ((Integer) residueHash.get(res)).intValue();\r
-                            count++;\r
-\r
-                            if (!jalview.util.Comparison.isGap(res.charAt(0)) &&\r
-                                    (count >= maxCount))\r
-                            {\r
-                                if (count > maxCount)\r
-                                {\r
-                                    maxResidue = res;\r
-                                }\r
-                                else if (maxResidue.indexOf(res) == -1)\r
-                                {\r
-                                    maxResidue += res;\r
-                                }\r
-\r
-                                maxCount = count;\r
-                            }\r
-\r
-                            residueHash.put(res, new Integer(count));\r
-                        }\r
-                        else\r
-                        {\r
-                            residueHash.put(res, new Integer(1));\r
-                        }\r
-                    }\r
-                    else\r
-                    {\r
-                        if (residueHash.containsKey("-"))\r
-                        {\r
-                            count = ((Integer) residueHash.get("-")).intValue();\r
-                            count++;\r
-                            residueHash.put("-", new Integer(count));\r
-                        }\r
-                        else\r
-                        {\r
-                            residueHash.put("-", new Integer(1));\r
-                        }\r
-                    }\r
-                }\r
-            }\r
-\r
-            residueHash.put("maxCount", new Integer(maxCount));\r
-            residueHash.put("maxResidue", maxResidue);\r
-\r
-\r
-            //Size is redundant at present if we calculate percentage here\r
-            //residueHash.put("size", new Integer(jSize));\r
-            //residueHash.put("nogaps", new Integer(nongap));\r
-\r
-            percentage = ((float)maxCount*100) / (float)jSize;\r
-            residueHash.put("pid_gaps", new Float(percentage) );\r
-\r
-            percentage = ((float)maxCount*100) / (float)nongap;\r
-            residueHash.put("pid_nogaps", new Float(percentage) );\r
-            result.addElement(residueHash);\r
+          nongap++;\r
         }\r
 \r
+        values[c]++;\r
 \r
+      }\r
+      else\r
+      {\r
+        values['-']++;\r
+      }\r
+    }\r
 \r
-        return result;\r
+    for (v = 'A'; v < 'Z'; v++)\r
+    {\r
+      if (values[v] == 0 || values[v] < maxCount)\r
+        continue;\r
+\r
+      if (values[v] > maxCount)\r
+      {\r
+        maxResidue = String.valueOf( (char) v);\r
+      }\r
+      else if (values[v] == maxCount)\r
+      {\r
+        maxResidue += String.valueOf( (char) v);\r
+      }\r
+      maxCount = values[v];\r
     }\r
+\r
+\r
+    residueHash.put("maxCount", new Integer(maxCount));\r
+    residueHash.put("maxResidue", maxResidue);\r
+\r
+    percentage = ( (float) maxCount * 100) / (float) jSize;\r
+    residueHash.put("pid_gaps", new Float(percentage));\r
+\r
+    percentage = ( (float) maxCount * 100) / (float) nongap;\r
+    residueHash.put("pid_nogaps", new Float(percentage));\r
+    result[i] = residueHash;\r
+  }\r
+}\r
 }\r
+\r
+\r
index 9152939..8e54ba9 100755 (executable)
@@ -1247,7 +1247,7 @@ public void itemStateChanged(ItemEvent evt)
                           viewport.alignment.getWidth());\r
       }\r
 \r
-      cs.setConsensus(viewport.vconsensus);\r
+      cs.setConsensus(viewport.hconsensus);\r
       if (cs.conservationApplied())\r
       {\r
         Alignment al = (Alignment) viewport.alignment;\r
@@ -1487,7 +1487,7 @@ public void itemStateChanged(ItemEvent evt)
         cs.setConservation(null);\r
       }\r
 \r
-      cs.setConsensus(viewport.vconsensus);\r
+      cs.setConsensus(viewport.hconsensus);\r
 \r
     }\r
     viewport.setGlobalColourScheme(cs);\r
index fc46386..998a4a4 100755 (executable)
@@ -87,7 +87,7 @@ public class AlignViewport
   boolean showHiddenMarkers = true;\r
 \r
 \r
-  public Vector vconsensus;\r
+  public Hashtable [] hconsensus;\r
   AlignmentAnnotation consensus;\r
   AlignmentAnnotation conservation;\r
   AlignmentAnnotation quality;\r
@@ -179,7 +179,7 @@ public class AlignViewport
         globalColourScheme = ColourSchemeProperty.getColour(alignment, colour);\r
         if (globalColourScheme != null)\r
         {\r
-          globalColourScheme.setConsensus(vconsensus);\r
+          globalColourScheme.setConsensus(hconsensus);\r
         }\r
       }\r
 \r
@@ -305,37 +305,28 @@ public class AlignViewport
 \r
   public void updateConsensus()\r
   {\r
-    Annotation[] annotations = new Annotation[alignment.getWidth()];\r
-\r
     // this routine prevents vconsensus becoming a new object each time\r
     // consenus is calculated. Important for speed of Blosum62\r
     // and PID colouring of alignment\r
-    if (vconsensus == null)\r
-    {\r
-      vconsensus = alignment.getAAFrequency();\r
-    }\r
-    else\r
-    {\r
-      Vector temp = alignment.getAAFrequency();\r
-      vconsensus.removeAllElements();\r
-      Enumeration e = temp.elements();\r
-      while (e.hasMoreElements())\r
-      {\r
-        vconsensus.addElement(e.nextElement());\r
-      }\r
-    }\r
-    Hashtable hash = null;\r
-    for (int i = 0; i < alignment.getWidth(); i++)\r
+    int aWidth = alignment.getWidth();\r
+\r
+    Annotation[] annotations = new Annotation[aWidth];\r
+\r
+    hconsensus = new Hashtable[aWidth];\r
+    AAFrequency.calculate(alignment.getSequencesArray(),\r
+                          0, aWidth,\r
+                          hconsensus);\r
+\r
+    for (int i = 0; i < aWidth; i++)\r
     {\r
-      hash = (Hashtable) vconsensus.elementAt(i);\r
       float value = 0;\r
       if(ignoreGapsInConsensusCalculation)\r
-        value = ((Float)hash.get("pid_nogaps")).floatValue();\r
+        value = ((Float)hconsensus[i].get("pid_nogaps")).floatValue();\r
       else\r
-        value = ((Float)hash.get("pid_gaps")).floatValue();\r
+        value = ((Float)hconsensus[i].get("pid_gaps")).floatValue();\r
 \r
-      String maxRes = hash.get("maxResidue").toString();\r
-      String mouseOver = hash.get("maxResidue") + " ";\r
+      String maxRes = hconsensus[i].get("maxResidue").toString();\r
+      String mouseOver = hconsensus[i].get("maxResidue") + " ";\r
       if (maxRes.length() > 1)\r
       {\r
         mouseOver = "[" + maxRes + "] ";\r
@@ -363,7 +354,7 @@ public class AlignViewport
     }\r
 \r
     if(globalColourScheme!=null)\r
-          globalColourScheme.setConsensus(vconsensus);\r
+          globalColourScheme.setConsensus(hconsensus);\r
 \r
   }\r
   /**\r
index cbbfcff..867ce56 100755 (executable)
@@ -68,7 +68,7 @@ public class OverviewPanel
     float initialScale = (float) av.alignment.getWidth() /\r
         (float) av.alignment.getHeight();\r
 \r
-    if(av.vconsensus==null)\r
+    if(av.hconsensus==null)\r
           graphHeight = 0;\r
 \r
     if (av.alignment.getWidth() > av.alignment.getHeight())\r
index 90a8b31..a770264 100755 (executable)
@@ -565,15 +565,6 @@ public class Alignment implements AlignmentI
         return gapCharacter;
     }
 
-    /**
-     * DOCUMENT ME!
-     *
-     * @return DOCUMENT ME!
-     */
-    public Vector getAAFrequency()
-    {
-        return AAFrequency.calculate(sequences, 0, getWidth());
-    }
 
     /**
      * DOCUMENT ME!
index bcfcc81..aca108d 100755 (executable)
@@ -242,12 +242,6 @@ public interface AlignmentI
      */
     public char getGapCharacter();
 
-    /**
-     * DOCUMENT ME!
-     *
-     * @return DOCUMENT ME!
-     */
-    public Vector getAAFrequency();
 
     /**
      * Returns true if alignment is nucleotide sequence
index 79f1e8b..1bcd39e 100755 (executable)
@@ -115,7 +115,7 @@ public class AlignFrame
   {
     this.setDropTarget(new java.awt.dnd.DropTarget(this, this));
 
-    if (viewport.vconsensus == null)
+    if (viewport.hconsensus == null)
     {
       //Out of memory calculating consensus.
       BLOSUM62Colour.setEnabled(false);
@@ -832,7 +832,7 @@ public class AlignFrame
    */
   void updateEditMenuBar()
   {
-    if (historyList.size() > 0)
+  /*  if (historyList.size() > 0)
     {
       undoMenuItem.setEnabled(true);
 
@@ -856,7 +856,7 @@ public class AlignFrame
     {
       redoMenuItem.setEnabled(false);
       redoMenuItem.setText("Redo");
-    }
+    }*/
   }
 
   /**
@@ -1614,7 +1614,7 @@ public class AlignFrame
    if (av.padGaps)
        av.getAlignment().padGaps();
 
-     if (av.vconsensus != null && av.autoCalculateConsensus)
+     if (av.hconsensus != null && av.autoCalculateConsensus)
      {
        av.updateConsensus();
        av.updateConservation();
@@ -1643,7 +1643,7 @@ public class AlignFrame
                           viewport.alignment.getWidth());
       }
 
-      cs.setConsensus(viewport.vconsensus);
+      cs.setConsensus(viewport.hconsensus);
       if (cs.conservationApplied())
       {
         Alignment al = (Alignment) viewport.alignment;
@@ -2089,7 +2089,7 @@ public class AlignFrame
         cs.setConservation(null);
       }
 
-      cs.setConsensus(viewport.vconsensus);
+      cs.setConsensus(viewport.hconsensus);
     }
 
     viewport.setGlobalColourScheme(cs);
index 6d0f766..07a7453 100755 (executable)
@@ -84,7 +84,7 @@ public class AlignViewport
 
 
     /** DOCUMENT ME!! */
-    public Vector vconsensus;
+    public Hashtable [] hconsensus;
     AlignmentAnnotation consensus;
     AlignmentAnnotation conservation;
     AlignmentAnnotation quality;
@@ -187,7 +187,7 @@ public class AlignViewport
 
         // We must set conservation and consensus before setting colour,
         // as Blosum and Clustal require this to be done
-        if(vconsensus==null && !isDataset)
+        if(hconsensus==null && !isDataset)
         {
           updateConservation();
           updateConsensus();
@@ -206,7 +206,7 @@ public class AlignViewport
 
             if (globalColourScheme != null)
             {
-                globalColourScheme.setConsensus(vconsensus);
+                globalColourScheme.setConsensus(hconsensus);
             }
         }
     }
@@ -236,6 +236,8 @@ public class AlignViewport
       if(alignment.isNucleotide())
           return;
 
+     // System.out.println("UPDATING CONSERVATION");
+
       try{
         Conservation cons = new jalview.analysis.Conservation("All",
             jalview.schemes.ResidueProperties.propHash, 3,
@@ -360,48 +362,33 @@ public class AlignViewport
       }
     }
 
+
     /**
      * DOCUMENT ME!
      */
     public void updateConsensus()
     {
       try{
-        Annotation[] annotations = new Annotation[alignment.getWidth()];
-
-        // this routine prevents vconsensus becoming a new object each time
-        // consenus is calculated. Important for speed of Blosum62
-        // and PID colouring of alignment
-        if (vconsensus == null)
-        {
-          vconsensus = alignment.getAAFrequency();
-        }
-        else
-        {
-          Vector temp = alignment.getAAFrequency();
-          vconsensus.clear();
-
-          Enumeration e = temp.elements();
+        int aWidth = alignment.getWidth();
 
-          while (e.hasMoreElements())
-          {
-            vconsensus.add(e.nextElement());
-          }
-        }
+        Annotation[] annotations = new Annotation[aWidth];
 
-        Hashtable hash = null;
+        hconsensus = new Hashtable[aWidth];
+        AAFrequency.calculate(alignment.getSequencesArray(),
+                              0,
+                              alignment.getWidth(),
+                              hconsensus);
 
-        for (int i = 0; i < alignment.getWidth(); i++)
+        for (int  i = 0; i < aWidth; i++)
         {
-          hash = (Hashtable) vconsensus.elementAt(i);
-
           float value = 0;
           if (ignoreGapsInConsensusCalculation)
-            value = ( (Float) hash.get("pid_nogaps")).floatValue();
+            value = ( (Float) hconsensus[i].get("pid_nogaps")).floatValue();
           else
-            value = ( (Float) hash.get("pid_gaps")).floatValue();
+            value = ( (Float) hconsensus[i].get("pid_gaps")).floatValue();
 
-          String maxRes = hash.get("maxResidue").toString();
-          String mouseOver = hash.get("maxResidue") + " ";
+          String maxRes = hconsensus[i].get("maxResidue").toString();
+          String mouseOver = hconsensus[i].get("maxResidue") + " ";
 
           if (maxRes.length() > 1)
           {
@@ -429,7 +416,7 @@ public class AlignViewport
         }
 
         if (globalColourScheme != null)
-          globalColourScheme.setConsensus(vconsensus);
+          globalColourScheme.setConsensus(hconsensus);
 
       }catch(OutOfMemoryError error)
       {
index e118309..a38c530 100755 (executable)
@@ -1481,7 +1481,7 @@ public class Jalview2XML
             if(cs!=null)
             {
               cs.setThreshold(view.getPidThreshold(), true);
-              cs.setConsensus(af.viewport.vconsensus);
+              cs.setConsensus(af.viewport.hconsensus);
             }
         }
 
index 6a4fba8..428789f 100755 (executable)
@@ -388,7 +388,7 @@ public class Jalview2XML_V1
             if(cs!=null)\r
             {\r
               cs.setThreshold(view.getPidThreshold(), true);\r
-              cs.setConsensus(af.viewport.vconsensus);\r
+              cs.setConsensus(af.viewport.hconsensus);\r
             }\r
         }\r
 \r