JAL-4241 Create sequence mapping before adjusting annotations
[jalview.git] / src / jalview / ws / jws2 / SeqAnnotationServiceCalcWorker.java
index aafbbab..e004bf5 100644 (file)
@@ -28,6 +28,7 @@ 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;
@@ -252,7 +253,7 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
 
     if (seqs == null || !checkValidInputSeqs(seqs))
     {
-      jalview.bin.Cache.log.debug(
+      jalview.bin.Console.debug(
               "Sequences for analysis service were null or not valid");
       return;
     }
@@ -262,7 +263,7 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
       guiProgress.setProgressBar(service.getActionText(),
               progressId = System.currentTimeMillis());
     }
-    jalview.bin.Cache.log.debug("submitted " + seqs.size()
+    jalview.bin.Console.debug("submitted " + seqs.size()
             + " sequences to " + service.getActionText());
 
     rslt = annotService.submitToService(seqs, getPreset(),
@@ -272,7 +273,7 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
       return;
     }
     // TODO: handle job submission error reporting here.
-    Cache.log.debug("Service " + service.getUri() + "\nSubmitted job ID: "
+    Console.debug("Service " + service.getUri() + "\nSubmitted job ID: "
             + rslt);
     // ///
     // otherwise, construct WsJob and any UI handlers
@@ -308,39 +309,39 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
   {
     boolean finished = false;
     
-    Cache.log.debug("Updating status for annotation service.");
+    Console.debug("Updating status for annotation service.");
     annotService.updateStatus(running);
     job.updateJobPanelState(info, "", running);
     if (running.isSubjobComplete())
     {
-      Cache.log.debug(
+      Console.debug(
               "Finished polling analysis service job: status reported is "
                       + running.getState());
       finished = true;
     }
     else
     {
-      Cache.log.debug("Status now " + running.getState());
+      Console.debug("Status now " + running.getState());
     }
 
     // pull any stats - some services need to flush log output before
     // results are available
-    Cache.log.debug("Updating progress log for annotation service.");
+    Console.debug("Updating progress log for annotation service.");
 
     try
     {
       annotService.updateJobProgress(running);
     } catch (Throwable thr)
     {
-      Cache.log.debug("Ignoring exception during progress update.",
+      Console.debug("Ignoring exception during progress update.",
               thr);
     }
-    Cache.log.debug("Result of poll: " + running.getStatus());
+    Console.debug("Result of poll: " + running.getStatus());
     
     
     if (finished)
     {
-      Cache.log.debug("Job poll loop exited. Job is " + running.getState());
+      Console.debug("Job poll loop exited. Job is " + running.getState());
       if (running.isFinished())
       {
         // expect there to be results to collect
@@ -351,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.");
 
@@ -391,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())
@@ -401,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
@@ -439,7 +440,7 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
           ap.adjustAnnotationHeight();
         }
       }
-      Cache.log.debug("Annotation Service Worker thread finished.");
+      Console.debug("Annotation Service Worker thread finished.");
 
     }
     
@@ -701,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])
           {
@@ -732,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();