JAL-4241 Create sequence mapping before adjusting annotations
[jalview.git] / src / jalview / ws / jws2 / SeqAnnotationServiceCalcWorker.java
index 132408b..e004bf5 100644 (file)
@@ -26,7 +26,9 @@ import jalview.analysis.SeqsetUtils;
 import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
 import jalview.api.FeatureColourI;
+import jalview.api.PollableAlignCalcWorkerI;
 import jalview.bin.Cache;
+import jalview.bin.Console;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AnnotatedCollectionI;
@@ -64,7 +66,7 @@ import java.util.List;
 import java.util.Map;
 
 public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
-        implements WSAnnotationCalcManagerI
+        implements WSAnnotationCalcManagerI, PollableAlignCalcWorkerI
 {
 
   protected ServiceWithParameters service;
@@ -162,17 +164,11 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
     return gapMap;
   }
 
-  public SeqAnnotationServiceCalcWorker(AlignViewportI alignViewport,
-          AlignmentViewPanel alignPanel)
-  {
-    super(alignViewport, alignPanel);
-  }
-
   public SeqAnnotationServiceCalcWorker(ServiceWithParameters service,
           AlignFrame alignFrame,
           WsParamSetI preset, List<ArgumentI> paramset)
   {
-    this(alignFrame.getCurrentView(), alignFrame.alignPanel);
+    super(alignFrame.getCurrentView(), alignFrame.alignPanel);
     // TODO: both these fields needed ?
     this.alignFrame = alignFrame;
     this.guiProgress = alignFrame;
@@ -185,7 +181,8 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
               .getEndpoint();
     } catch (ClassCastException cce)
     {
-      JvOptionPane.showMessageDialog(Desktop.desktop,
+      annotService = null;
+      JvOptionPane.showMessageDialog(Desktop.getInstance(),
               MessageManager.formatMessage(
                       "label.service_called_is_not_an_annotation_service",
                       new String[]
@@ -194,6 +191,7 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
               JvOptionPane.WARNING_MESSAGE);
 
     }
+    cancellable = CancellableI.class.isInstance(annotService);
     // configure submission flags
     proteinAllowed = service.isProteinService();
     nucleotidesAllowed = service.isNucleotideService();
@@ -218,7 +216,8 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
     return annotService != null;
   }
 
-  protected jalview.ws.api.SequenceAnnotationServiceI annotService = null;
+  protected SequenceAnnotationServiceI annotService;
+  protected final boolean cancellable;
 
   volatile JobId rslt = null;
 
@@ -226,176 +225,123 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
 
   private int min_valid_seqs;
 
-  @Override
-  public void run()
+
+  private long progressId = -1;
+  JobStateSummary job = null;
+  WebserviceInfo info = null;
+  List<SequenceI> seqs = null;
+  
+  @Override public void startUp() throws Throwable
   {
-    if (checkDone())
+    if (alignViewport.isClosed())
     {
+      abortAndDestroy();
       return;
     }
     if (!hasService())
     {
-      calcMan.workerComplete(this);
       return;
     }
 
-    long progressId = -1;
-
-    int serverErrorsLeft = 3;
-    final boolean cancellable = CancellableI.class
-            .isAssignableFrom(annotService.getClass());
     StringBuffer msg = new StringBuffer();
-    JobStateSummary job = new JobStateSummary();
-    WebserviceInfo info = new WebserviceInfo("foo", "bar", false);
-    try
+    job = new JobStateSummary();
+    info = new WebserviceInfo("foo", "bar", false);
+
+    seqs = getInputSequences(
+            alignViewport.getAlignment(),
+            bySequence ? alignViewport.getSelectionGroup() : null);
+
+    if (seqs == null || !checkValidInputSeqs(seqs))
     {
-      List<SequenceI> seqs = getInputSequences(
-              alignViewport.getAlignment(),
-              bySequence ? alignViewport.getSelectionGroup() : null);
+      jalview.bin.Console.debug(
+              "Sequences for analysis service were null or not valid");
+      return;
+    }
 
-      if (seqs == null || !checkValidInputSeqs(seqs))
-      {
-        jalview.bin.Cache.log.debug(
-                "Sequences for analysis service were null or not valid");
-        calcMan.workerComplete(this);
-        return;
-      }
+    if (guiProgress != null)
+    {
+      guiProgress.setProgressBar(service.getActionText(),
+              progressId = System.currentTimeMillis());
+    }
+    jalview.bin.Console.debug("submitted " + seqs.size()
+            + " sequences to " + service.getActionText());
 
-      if (guiProgress != null)
-      {
-        guiProgress.setProgressBar(service.getActionText(),
-                progressId = System.currentTimeMillis());
-      }
-      jalview.bin.Cache.log.debug("submitted " + seqs.size()
-              + " sequences to " + service.getActionText());
+    rslt = annotService.submitToService(seqs, getPreset(),
+            getArguments());
+    if (rslt == null)
+    {
+      return;
+    }
+    // TODO: handle job submission error reporting here.
+    Console.debug("Service " + service.getUri() + "\nSubmitted job ID: "
+            + rslt);
+    // ///
+    // otherwise, construct WsJob and any UI handlers
+    running = new AnnotationWsJob();
+    running.setJobHandle(rslt);
+    running.setSeqNames(seqNames);
+    running.setStartPos(start);
+    running.setSeqs(seqs);
+    job.updateJobPanelState(info, "", running);
+    if (guiProgress != null)
+    {
+      guiProgress.registerHandler(progressId,
+              new IProgressIndicatorHandler()
+              {
 
-      rslt = annotService.submitToService(seqs, getPreset(),
-              getArguments());
-      if (rslt == null)
-      {
-        return;
-      }
-      // TODO: handle job submission error reporting here.
-      Cache.log.debug("Service " + service.getUri() + "\nSubmitted job ID: "
-              + rslt);
-      ;
-      // ///
-      // otherwise, construct WsJob and any UI handlers
-      running = new AnnotationWsJob();
-      running.setJobHandle(rslt);
-      running.setSeqNames(seqNames);
-      running.setStartPos(start);
-      running.setSeqs(seqs);
-      job.updateJobPanelState(info, "", running);
-      if (guiProgress != null)
-      {
-        guiProgress.registerHandler(progressId,
-                new IProgressIndicatorHandler()
+                @Override
+                public boolean cancelActivity(long id)
                 {
+                  calcMan.cancelWorker(SeqAnnotationServiceCalcWorker.this);
+                  return true;
+                }
 
-                  @Override
-                  public boolean cancelActivity(long id)
-                  {
-                    ((CancellableI) annotService).cancel(running);
-                    return true;
-                  }
-
-                  @Override
-                  public boolean canCancel()
-                  {
-                    return cancellable;
-                  }
-                });
-      }
-      
-      // ///
-      // and poll for updates until job finishes, fails or becomes stale
-      
-      boolean finished = false;
-      do
-      {
-        Cache.log.debug("Updating status for annotation service.");
-        annotService.updateStatus(running);
-        job.updateJobPanelState(info, "", running);
-        if (running.isSubjobComplete())
-        {
-          Cache.log.debug(
-                  "Finished polling analysis service job: status reported is "
-                          + running.getState());
-          finished = true;
-        }
-        else
-        {
-          Cache.log.debug("Status now " + running.getState());
-        }
-
-        if (calcMan.isPending(this) && isInteractiveUpdate())
-        {
-          Cache.log.debug("Analysis service job is stale. aborting.");
-          // job has become stale.
-          if (!finished) {
-            finished = true;
-            // cancel this job and yield to the new job
-            try
-            {
-              if (cancellable
-                        && ((CancellableI) annotService).cancel(running))
-              {
-                System.err.println("Cancelled job: " + rslt);
-              }
-              else
-              {
-                System.err.println("FAILED TO CANCEL job: " + rslt);
-              }
-  
-            } catch (Exception x)
-            {
+                @Override
+                public boolean canCancel()
+                {
+                  return cancellable;
+                }
+              });
+    }
+  }
   
-            }
-          }
-          rslt = running.getJobHandle();
-          return;
-        }
-
-        // pull any stats - some services need to flush log output before
-        // results are available
-        Cache.log.debug("Updating progress log for annotation service.");
+  @Override public boolean poll() throws Throwable
+  {
+    boolean finished = false;
+    
+    Console.debug("Updating status for annotation service.");
+    annotService.updateStatus(running);
+    job.updateJobPanelState(info, "", running);
+    if (running.isSubjobComplete())
+    {
+      Console.debug(
+              "Finished polling analysis service job: status reported is "
+                      + running.getState());
+      finished = true;
+    }
+    else
+    {
+      Console.debug("Status now " + running.getState());
+    }
 
-        try
-        {
-        annotService.updateJobProgress(running);
-        } catch (Throwable thr)
-        {
-          Cache.log.debug("Ignoring exception during progress update.",
-                  thr);
-        }
-        Cache.log.trace("Result of poll: " + running.getStatus());
+    // pull any stats - some services need to flush log output before
+    // results are available
+    Console.debug("Updating progress log for annotation service.");
 
-        if (!finished && !running.isFailed())
-        {
-          try
-          {
-            Cache.log.debug("Analysis service job thread sleeping.");
-            Thread.sleep(200);
-            Cache.log.debug("Analysis service job thread woke.");
-          } catch (InterruptedException x)
-          {
-          }
-          ;
-        }
-      } while (!finished);
-
-      Cache.log.debug("Job poll loop exited. Job is " + running.getState());
-      // TODO: need to poll/retry
-      if (serverErrorsLeft > 0)
-      {
-        try
-        {
-          Thread.sleep(200);
-        } catch (InterruptedException x)
-        {
-        }
-      }
+    try
+    {
+      annotService.updateJobProgress(running);
+    } catch (Throwable thr)
+    {
+      Console.debug("Ignoring exception during progress update.",
+              thr);
+    }
+    Console.debug("Result of poll: " + running.getStatus());
+    
+    
+    if (finished)
+    {
+      Console.debug("Job poll loop exited. Job is " + running.getState());
       if (running.isFinished())
       {
         // expect there to be results to collect
@@ -406,16 +352,16 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
         // features automatically according to user preferences
         running.setFeatureRenderer(
                 ((jalview.gui.AlignmentPanel) ap).cloneFeatureRenderer());
-        Cache.log.debug("retrieving job results.");
+        Console.debug("retrieving job results.");
         final Map<String, FeatureColourI> featureColours = new HashMap<>();
         final Map<String, FeatureMatcherSetI> featureFilters = new HashMap<>();
         List<AlignmentAnnotation> returnedAnnot = annotService
                 .getAnnotationResult(running.getJobHandle(), seqs,
                         featureColours, featureFilters);
 
-        Cache.log.debug("Obtained " + (returnedAnnot == null ? "no rows"
+        Console.debug("Obtained " + (returnedAnnot == null ? "no rows"
                 : ("" + returnedAnnot.size())));
-        Cache.log.debug("There were " + featureColours.size()
+        Console.debug("There were " + featureColours.size()
                 + " feature colours and " + featureFilters.size()
                 + " filters defined.");
 
@@ -446,7 +392,7 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
 
         if (running.hasResults())
         {
-          jalview.bin.Cache.log.debug("Updating result annotation from Job "
+          jalview.bin.Console.debug("Updating result annotation from Job "
                   + rslt + " at " + service.getUri());
           updateResultAnnotation(true);
           if (running.isTransferSequenceFeatures())
@@ -456,7 +402,7 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
             // regions
             // not annotated due to gapMap/column visibility
 
-            jalview.bin.Cache.log.debug(
+            jalview.bin.Console.debug(
                     "Updating feature display settings and transferring features from Job "
                             + rslt + " at " + service.getUri());
             // TODO: consider merge rather than apply here
@@ -494,61 +440,29 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
           ap.adjustAnnotationHeight();
         }
       }
-      Cache.log.debug("Annotation Service Worker thread finished.");
-    }
-// TODO: use service specitic exception handlers
-//    catch (JobSubmissionException x)
-//    {
-//
-//      System.err.println(
-//              "submission error with " + getServiceActionText() + " :");
-//      x.printStackTrace();
-//      calcMan.disableWorker(this);
-//    } catch (ResultNotAvailableException x)
-//    {
-//      System.err.println("collection error:\nJob ID: " + rslt);
-//      x.printStackTrace();
-//      calcMan.disableWorker(this);
-//
-//    } catch (OutOfMemoryError error)
-//    {
-//      calcMan.disableWorker(this);
-//
-//      ap.raiseOOMWarning(getServiceActionText(), error);
-//    } 
-    catch (Throwable x)
-    {
-      calcMan.disableWorker(this);
+      Console.debug("Annotation Service Worker thread finished.");
 
-      System.err
-              .println("Blacklisting worker due to unexpected exception:");
-      x.printStackTrace();
-    } finally
+    }
+    
+    return finished;
+  }
+  
+  @Override public void cancel()
+  {
+    cancelCurrentJob();
+  }
+  
+  @Override public void done()
+  {
+    if (ap != null)
     {
-
-      calcMan.workerComplete(this);
-      if (ap != null)
+      if (guiProgress != null && progressId != -1)
       {
-        if (guiProgress != null && progressId != -1)
-        {
-          guiProgress.setProgressBar("", progressId);
-        }
-        // TODO: may not need to paintAlignment again !
-        ap.paintAlignment(false, false);
-      }
-      if (msg.length() > 0)
-      {
-        // TODO: stash message somewhere in annotation or alignment view.
-        // code below shows result in a text box popup
-        /*
-         * jalview.gui.CutAndPasteTransfer cap = new
-         * jalview.gui.CutAndPasteTransfer(); cap.setText(msg.toString());
-         * jalview.gui.Desktop.addInternalFrame(cap,
-         * "Job Status for "+getServiceActionText(), 600, 400);
-         */
+        guiProgress.removeProgressBar(progressId);
       }
+      // TODO: may not need to paintAlignment again !
+      ap.paintAlignment(false, false);
     }
-
   }
 
   /**
@@ -581,7 +495,7 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
     try
     {
       String id = running.getJobId();
-      if (((CancellableI) annotService).cancel(running))
+      if (cancellable && ((CancellableI) annotService).cancel(running))
       {
         System.err.println("Cancelled job " + id);
       }
@@ -788,26 +702,18 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
         {
           ala.graphGroup += graphGroup;
         }
-        SequenceI aseq = null;
-
-        /**
+        SequenceI aseq = (ala.sequenceRef == null) ? null
+                : running.getSeqNames().get(ala.sequenceRef.getName());
+        /*
          * transfer sequence refs and adjust gapmap
+         * Prepare an array for annotations and copy returned annotations to it
+         * according to gapMap if present.
          */
-        if (ala.sequenceRef != null)
-        {
-          SequenceI seq = running.getSeqNames()
-                  .get(ala.sequenceRef.getName());
-          aseq = seq;
-          while (seq.getDatasetSequence() != null)
-          {
-            seq = seq.getDatasetSequence();
-          }
-        }
         Annotation[] resAnnot = ala.annotations,
                 gappedAnnot = new Annotation[Math.max(
                         alignViewport.getAlignment().getWidth(),
                         gapMap.length)];
-        for (int p = 0, ap = start; ap < gappedAnnot.length; ap++)
+        for (int p = 0, ap = 0; ap < gappedAnnot.length; ap++)
         {
           if (gapMap != null && gapMap.length > ap && !gapMap[ap])
           {
@@ -819,12 +725,12 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
           }
         }
         ala.sequenceRef = aseq;
-        ala.annotations = gappedAnnot;
         AlignmentAnnotation newAnnot = getAlignViewport().getAlignment()
                 .updateFromOrCopyAnnotation(ala);
+        newAnnot.annotations = gappedAnnot;
         if (aseq != null)
         {
-
+          newAnnot.createSequenceMapping(aseq, aseq.findPosition(start), false);
           aseq.addAlignmentAnnotation(newAnnot);
           newAnnot.adjustForAlignment();
 
@@ -889,43 +795,6 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
     }
   }
 
-  /**
-   * notify manager that we have started, and wait for a free calculation slot
-   * 
-   * @return true if slot is obtained and work still valid, false if another
-   *         thread has done our work for us.
-   */
-  protected boolean checkDone()
-  {
-    calcMan.notifyStart(this);
-    ap.paintAlignment(false, false);
-    while (!calcMan.notifyWorking(this))
-    {
-      if (calcMan.isWorking(this))
-      {
-        return true;
-      }
-      try
-      {
-        if (ap != null)
-        {
-          ap.paintAlignment(false, false);
-        }
-
-        Thread.sleep(200);
-      } catch (Exception ex)
-      {
-        ex.printStackTrace();
-      }
-    }
-    if (alignViewport.isClosed())
-    {
-      abortAndDestroy();
-      return true;
-    }
-    return false;
-  }
-
   protected void updateOurAnnots(List<AlignmentAnnotation> ourAnnot)
   {
     List<AlignmentAnnotation> our = ourAnnots;