JAL-885; Implementation of StructureFrequency.java and according
[jalview.git] / src / jalview / gui / AlignViewport.java
index 26fb890..98cae4c 100644 (file)
@@ -139,8 +139,12 @@ public class AlignViewport implements SelectionSource, VamsasSource
 
   /** DOCUMENT ME!! */
   public Hashtable[] hconsensus;
-
+  
+  public Hashtable[] hStrucConsensus;
+  
   AlignmentAnnotation consensus;
+  
+  AlignmentAnnotation strucConsensus;
 
   AlignmentAnnotation conservation;
 
@@ -151,6 +155,9 @@ public class AlignViewport implements SelectionSource, VamsasSource
   AlignmentAnnotation[] groupConservation;
 
   boolean autoCalculateConsensus = true;
+  
+  boolean autoCalculateStrucConsensus = true;
+
 
   /** DOCUMENT ME!! */
   public int ConsPercGaps = 25; // JBPNote : This should be a scalable property!
@@ -458,14 +465,20 @@ public class AlignViewport implements SelectionSource, VamsasSource
   ConservationThread conservationThread;
 
   ConsensusThread consensusThread;
+  
+  StrucConsensusThread strucConsensusThread;
 
   boolean consUpdateNeeded = false;
 
   static boolean UPDATING_CONSENSUS = false;
+  
+  static boolean UPDATING_STRUC_CONSENSUS = false;
 
   static boolean UPDATING_CONSERVATION = false;
 
   boolean updatingConsensus = false;
+  
+  boolean updatingStrucConsensus = false;
 
   boolean updatingConservation = false;
 
@@ -559,7 +572,6 @@ public class AlignViewport implements SelectionSource, VamsasSource
         AAFrequency.calculate(alignment.getSequencesArray(), 0,
                 alignment.getWidth(), hconsensus, true);
         updateAnnotation(true);
-
         if (globalColourScheme != null)
         {
           globalColourScheme.setConsensus(hconsensus);
@@ -604,6 +616,116 @@ public class AlignViewport implements SelectionSource, VamsasSource
       }
     }
   }
+  
+  //--------START Structure Conservation
+  public void updateStrucConsensus(final AlignmentPanel ap)
+  {
+    // see note in mantis : issue number 8585
+    if (strucConsensus == null || !autoCalculateStrucConsensus)
+    {
+      return;
+    }
+    strucConsensusThread = new StrucConsensusThread(ap);
+    strucConsensusThread.start();
+  }
+
+  class StrucConsensusThread extends Thread
+  {
+    AlignmentPanel ap;
+
+    public StrucConsensusThread(AlignmentPanel ap)
+    {
+      this.ap = ap;
+    }
+
+    public void run()
+    {
+      updatingStrucConsensus = true;
+      while (UPDATING_STRUC_CONSENSUS)
+      {
+        try
+        {
+          if (ap != null)
+          {
+            ap.paintAlignment(false);
+          }
+
+          Thread.sleep(200);
+        } catch (Exception ex)
+        {
+          ex.printStackTrace();
+        }
+      }
+
+      UPDATING_STRUC_CONSENSUS = true;
+
+      try
+      {
+        int aWidth = (alignment != null) ? alignment.getWidth() : -1; // null
+        // pointer
+        // possibility
+        // here.
+        if (aWidth <= 0)
+        {
+          updatingStrucConsensus = false;
+          UPDATING_STRUC_CONSENSUS = false;
+          return;
+        }
+
+        strucConsensus.annotations = null;
+        strucConsensus.annotations = new Annotation[aWidth];
+
+        hStrucConsensus = new Hashtable[aWidth];
+        AlignmentAnnotation rna = ap.av.getAlignment().getAlignmentAnnotation()[0];
+        StructureFrequency.calculate(alignment.getSequencesArray(), 0,
+                alignment.getWidth(), hStrucConsensus, true, rna);
+        //TODO AlignmentAnnotation rnaStruc!!!
+        updateAnnotation(true);
+        if (globalColourScheme != null)
+        {
+          globalColourScheme.setConsensus(hStrucConsensus);
+        }
+
+      } catch (OutOfMemoryError error)
+      {
+        alignment.deleteAnnotation(consensus);
+
+        strucConsensus = null;
+        hStrucConsensus = null;
+        new OOMWarning("calculating structure consensus", error);
+      }
+      UPDATING_STRUC_CONSENSUS = false;
+      updatingStrucConsensus = false;
+
+      if (ap != null)
+      {
+        ap.paintAlignment(true);
+      }
+    }
+
+    /**
+     * update the consensus annotation from the sequence profile data using
+     * current visualization settings.
+     */
+    public void updateAnnotation()
+    {
+      updateAnnotation(false);
+    }
+
+    protected void updateAnnotation(boolean immediate)
+    {
+      // TODO: make calls thread-safe, so if another thread calls this method,
+      // it will either return or wait until one calculation is finished.
+      if (immediate
+              || (!updatingConsensus && consensus != null && hconsensus != null))
+      {
+        StructureFrequency.completeConsensus(strucConsensus, hStrucConsensus, 0,
+                hStrucConsensus.length, ignoreGapsInConsensusCalculation,
+                showSequenceLogo);
+      }
+    }
+  }
+  //--------END   Structure Conservation
 
   /**
    * get the consensus sequence as displayed under the PID consensus annotation
@@ -1732,6 +1854,11 @@ public class AlignViewport implements SelectionSource, VamsasSource
     {
       updateConsensus(ap);
     }
+    
+    if(autoCalculateStrucConsensus)
+    {
+       updateStrucConsensus(ap);
+    }
 
     // Reset endRes of groups if beyond alignment width
     int alWidth = alignment.getWidth();