JAL-4199 Organise annotation task imports and fields
[jalview.git] / src / jalview / ws2 / actions / annotation / AnnotationTask.java
index 238439a..8a3b091 100644 (file)
@@ -2,19 +2,13 @@ package jalview.ws2.actions.annotation;
 
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 import jalview.analysis.AlignmentAnnotationUtils;
-import jalview.api.AlignCalcManagerI2;
-import jalview.api.AlignCalcWorkerI;
 import jalview.api.AlignViewportI;
 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;
@@ -23,25 +17,13 @@ import jalview.datamodel.ContiguousI;
 import jalview.datamodel.Mapping;
 import jalview.datamodel.SequenceI;
 import jalview.datamodel.features.FeatureMatcherSetI;
-import jalview.schemes.FeatureSettingsAdapter;
-import jalview.util.ArrayUtils;
 import jalview.util.MapList;
-import jalview.util.MathUtils;
-import jalview.util.Pair;
-import jalview.workers.AlignCalcWorker;
 import jalview.ws.params.ArgumentI;
-import jalview.ws2.actions.BaseJob;
 import jalview.ws2.actions.BaseTask;
 import jalview.ws2.actions.ServiceInputInvalidException;
-import jalview.ws2.actions.api.JobI;
-import jalview.ws2.actions.api.TaskEventListener;
-import jalview.ws2.actions.api.TaskI;
 import jalview.ws2.api.Credentials;
 import jalview.ws2.api.JobStatus;
-import jalview.ws2.api.WebServiceJobHandle;
 import jalview.ws2.client.api.AnnotationWebServiceClientI;
-import jalview.ws2.helpers.DelegateJobEventListener;
-import jalview.ws2.helpers.TaskEventSupport;
 
 public class AnnotationTask extends BaseTask<AnnotationJob, AnnotationResult>
 {
@@ -51,12 +33,6 @@ public class AnnotationTask extends BaseTask<AnnotationJob, AnnotationResult>
 
   private final AlignViewportI viewport;
 
-  private JobStatus taskStatus = null;
-
-  private AlignCalcWorkerAdapter worker = null;
-
-  private DelegateJobEventListener<AnnotationResult> jobEventHandler;
-
   public AnnotationTask(AnnotationWebServiceClientI client,
       AnnotationAction action, List<ArgumentI> args, Credentials credentials,
       AlignViewportI viewport)
@@ -67,32 +43,6 @@ public class AnnotationTask extends BaseTask<AnnotationJob, AnnotationResult>
     this.viewport = viewport;
   }
 
-  // public void start(AlignCalcManagerI2 calcManager)
-  // {
-  // if (this.worker != null)
-  // throw new IllegalStateException("task already started");
-  // this.worker = new AlignCalcWorkerAdapter(calcManager);
-  // if (taskStatus != JobStatus.CANCELLED)
-  // {
-  // List<AlignCalcWorkerI> oldWorkers = calcManager.getWorkersOfClass(
-  // AlignCalcWorkerAdapter.class);
-  // for (var worker : oldWorkers)
-  // {
-  // if (action.getWebService().getName().equalsIgnoreCase(
-  // ((AlignCalcWorkerAdapter) worker).getServiceName()))
-  // {
-  // // remove interactive workers for the same service.
-  // calcManager.removeWorker(worker);
-  // calcManager.cancelWorker(worker);
-  // }
-  // }
-  // if (action.getWebService().isInteractive())
-  // calcManager.registerWorker(worker);
-  // else
-  // calcManager.startWorker(worker);
-  // }
-  // }
-
   /**
    * Create and return a list of annotation jobs from the current state of the
    * viewport. Returned job are not started by this method and should be stored
@@ -150,20 +100,23 @@ public class AnnotationTask extends BaseTask<AnnotationJob, AnnotationResult>
      * visibility */
 
     udpateCalcId(returnedAnnot);
-    int graphGroup = getNextGraphGroup(viewport.getAlignment());
+    int graphGroup = viewport.getAlignment().getLastGraphGroup();
     shiftGraphGroup(returnedAnnot, graphGroup);
     List<AlignmentAnnotation> annotations = new ArrayList<>();
     for (AlignmentAnnotation ala : returnedAnnot)
     {
-      SequenceI seq = job.seqNames.get(ala.sequenceRef.getName());
-      SequenceI aseq = getRootDatasetSequence(seq);
-      Annotation[] gappedAnnots = createGappedAnnotations(ala.annotations, job.start, job.gapMap);
+      SequenceI aseq = null;
+      if (ala.sequenceRef != null) {
+        SequenceI seq = job.seqNames.get(ala.sequenceRef.getName());
+        aseq = seq.getRootDatasetSequence();
+      }
       ala.sequenceRef = aseq;
+      Annotation[] gappedAnnots = createGappedAnnotations(ala.annotations, job.start, job.gapMap);
       ala.annotations = gappedAnnots;
 
       AlignmentAnnotation newAnnot = viewport.getAlignment()
           .updateFromOrCopyAnnotation(ala);
-      if (aseq != null) // I suspect it's always true
+      if (aseq != null)
       {
         aseq.addAlignmentAnnotation(newAnnot);
         newAnnot.adjustForAlignment();
@@ -180,7 +133,7 @@ public class AnnotationTask extends BaseTask<AnnotationJob, AnnotationResult>
         continue;
       hasFeatures = true;
       SequenceI seq = job.seqNames.get(sq.getName());
-      SequenceI datasetSeq = getRootDatasetSequence(seq);
+      SequenceI datasetSeq = seq.getRootDatasetSequence();
       List<ContiguousI> sourceRange = findContiguousRanges(datasetSeq, job.gapMap, job.start, job.end);
       int[] sourceStartEnd = ContiguousI.toStartEndArray(sourceRange);
       Mapping mp = new Mapping(new MapList(
@@ -208,16 +161,6 @@ public class AnnotationTask extends BaseTask<AnnotationJob, AnnotationResult>
     }
   }
 
-  private static int getNextGraphGroup(AlignmentI alignment)
-  {
-    if (alignment == null || alignment.getAlignmentAnnotation() == null)
-      return 1;
-    int graphGroup = 1;
-    for (AlignmentAnnotation ala : alignment.getAlignmentAnnotation())
-      graphGroup = Math.max(graphGroup, ala.graphGroup);
-    return graphGroup;
-  }
-
   private static void shiftGraphGroup(Iterable<AlignmentAnnotation> annotations, int shift)
   {
     for (AlignmentAnnotation ala : annotations)
@@ -229,15 +172,6 @@ public class AnnotationTask extends BaseTask<AnnotationJob, AnnotationResult>
     }
   }
 
-  private static SequenceI getRootDatasetSequence(SequenceI sequence)
-  {
-    while (sequence.getDatasetSequence() != null)
-    {
-      sequence = sequence.getDatasetSequence();
-    }
-    return sequence;
-  }
-
   private Annotation[] createGappedAnnotations(Annotation[] annotations, int start, boolean[] gapMap)
   {
     var size = Math.max(viewport.getAlignment().getWidth(), gapMap.length);