JAL-1807
[jalview.git] / src / jalview / workers / ConservationThread.java
index 32e13f2..ac37f46 100644 (file)
@@ -49,81 +49,89 @@ public class ConservationThread extends AlignCalcWorker implements
 
   AlignmentAnnotation conservation, quality;
 
-  int alWidth;
+       @Override
+       protected void run1(int state) {
+               while (!interrupted()) {
+                       try {
+                               switch (state) {
+                               case INIT:
+                                       if (calcMan.isPending(this))
+                                               return;
+                                       calcMan.notifyStart(this);
+                                       state = LOOP_STANDBY;
+                                       break;
+                               case LOOP_STANDBY:
+                                       while (!calcMan.notifyWorking(this)) {
+                                               if (ap != null) {
+                                                       ap.paintAlignment(false);
+                                               }
+                                               try {
+                                                       if (sleepAndReturn(200, state))
+                                                               return;
+                                               } catch (InterruptedException e) {
+                                                       state = DONE;
+                                                       break;
+                                               }
+                                       }
+                                       if (alignViewport.isClosed()) {
+                                               abortAndDestroy();
+                                               state = DONE;
+                                               break;
+                                       }
+                     List<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
+                     alignment = alignViewport.getAlignment();
+                     conservation = alignViewport.getAlignmentConservationAnnotation();
+                     quality = alignViewport.getAlignmentQualityAnnot();
+                     ourAnnot.add(conservation);
+                     ourAnnot.add(quality);
+                     ourAnnots = ourAnnot;
+                     ConsPercGaps = alignViewport.getConsPercGaps();
+                     // AlignViewport.UPDATING_CONSERVATION = true;
+
+                     if (alignment == null || (aWidth = alignment.getWidth()) < 0)
+                     {
+                       calcMan.workerComplete(this);
+                       // .updatingConservation = false;
+                       // AlignViewport.UPDATING_CONSERVATION = false;
+
+                       return;
+                     }
+                                       state = LOOP_CALCULATE;
+                                       break;
+                               case LOOP_CALCULATE:
+                                       iFirst = iLast;
+                                       nPer = aWidth  + 1; // for now - one big chunk
+                                       iLast = Math.min(iLast + nPer, aWidth);
+                                       if (iLast == iFirst) {
+                                               state = DONE;
+                                       } else {
+                                               computeConsensus();
+                                               if (sleepAndReturn(0, state))
+                                                       return;
+                                       }
+                                       break;
+                               case DONE:
+                     updateResultAnnotation(true);
+                                       return;
+                               }
+                       } catch (OutOfMemoryError error) {
+                               calcMan.workerCannotRun(this);
+             ap.raiseOOMWarning("calculating conservation", error);
+                       } catch (Throwable e) {
+                               System.out.println("Error in ConsensusThread: " + e);
+                               e.printStackTrace();
+                               calcMan.workerComplete(this);
+                       }
+               }
+       }
+
+       private void computeConsensus() {
+    cons = Conservation.calculateConservation("All",
+        ResidueProperties.propHash, 3,
+        alignment.getSequences(), 0, aWidth - 1, false,
+        ConsPercGaps, quality != null);
+       }
 
-  @Override
-  public void run()
-  {
-    try
-    {
-      calcMan.notifyStart(this); // updatingConservation = true;
-
-      while (!calcMan.notifyWorking(this))
-      {
-        try
-        {
-          if (ap != null)
-          {
-            // ap.paintAlignment(false);
-          }
-          Thread.sleep(200);
-        } catch (Exception ex)
-        {
-          ex.printStackTrace();
-        }
-      }
-      if (alignViewport.isClosed())
-      {
-        abortAndDestroy();
-        return;
-      }
-      List<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
-      AlignmentI alignment = alignViewport.getAlignment();
-      conservation = alignViewport.getAlignmentConservationAnnotation();
-      quality = alignViewport.getAlignmentQualityAnnot();
-      ourAnnot.add(conservation);
-      ourAnnot.add(quality);
-      ourAnnots = ourAnnot;
-      ConsPercGaps = alignViewport.getConsPercGaps();
-      // AlignViewport.UPDATING_CONSERVATION = true;
-
-      if (alignment == null || (alWidth = alignment.getWidth()) < 0)
-      {
-        calcMan.workerComplete(this);
-        // .updatingConservation = false;
-        // AlignViewport.UPDATING_CONSERVATION = false;
-
-        return;
-      }
-      try
-      {
-        cons = Conservation.calculateConservation("All",
-                ResidueProperties.propHash, 3,
-                alignment.getSequences(), 0, alWidth - 1, false,
-                ConsPercGaps, quality != null);
-      } catch (IndexOutOfBoundsException x)
-      {
-        // probable race condition. just finish and return without any fuss.
-        calcMan.workerComplete(this);
-        return;
-      }
-      updateResultAnnotation(true);
-    } catch (OutOfMemoryError error)
-    {
-      ap.raiseOOMWarning("calculating conservation", error);
-      calcMan.workerCannotRun(this);
-      // alignViewport.conservation = null;
-      // this.alignViewport.quality = null;
-
-    }
-    calcMan.workerComplete(this);
-
-    if (ap != null)
-    {
-      ap.paintAlignment(true);
-    }
-
-  }
 
   private void updateResultAnnotation(boolean b)
   {
@@ -131,7 +139,7 @@ public class ConservationThread extends AlignCalcWorker implements
             && conservation != null && quality != null)
     {
       alignViewport.setConservation(cons);
-      cons.completeAnnotations(conservation, quality, 0, alWidth);
+      cons.completeAnnotations(conservation, quality, 0, aWidth);
     }
   }
 
@@ -141,4 +149,5 @@ public class ConservationThread extends AlignCalcWorker implements
     updateResultAnnotation(false);
 
   }
+
 }