JAL-4199 Remove viewport reference from AnnotationTask
[jalview.git] / src / jalview / ws2 / actions / annotation / AnnotationTask.java
index 238439a..866f862 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>
 {
@@ -49,13 +31,9 @@ public class AnnotationTask extends BaseTask<AnnotationJob, AnnotationResult>
 
   private final AnnotationAction action;
 
-  private final AlignViewportI viewport;
+  private final AlignmentI alignment;
 
-  private JobStatus taskStatus = null;
-
-  private AlignCalcWorkerAdapter worker = null;
-
-  private DelegateJobEventListener<AnnotationResult> jobEventHandler;
+  private final AnnotatedCollectionI selectionGroup;
 
   public AnnotationTask(AnnotationWebServiceClientI client,
       AnnotationAction action, List<ArgumentI> args, Credentials credentials,
@@ -64,35 +42,10 @@ public class AnnotationTask extends BaseTask<AnnotationJob, AnnotationResult>
     super(client, args, credentials);
     this.client = client;
     this.action = action;
-    this.viewport = viewport;
+    this.alignment = viewport.getAlignment();
+    this.selectionGroup = viewport.getSelectionGroup();
   }
 
-  // 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
@@ -105,7 +58,6 @@ public class AnnotationTask extends BaseTask<AnnotationJob, AnnotationResult>
   @Override
   public List<AnnotationJob> prepareJobs() throws ServiceInputInvalidException
   {
-    AlignmentI alignment = viewport.getAlignment();
     if (alignment == null || alignment.getWidth() <= 0 ||
         alignment.getSequences() == null)
       throw new ServiceInputInvalidException("Alignment does not contain sequences");
@@ -116,7 +68,7 @@ public class AnnotationTask extends BaseTask<AnnotationJob, AnnotationResult>
       throw new ServiceInputInvalidException(
           action.getFullName() + " does not allow protein sequences");
     boolean bySequence = !action.isAlignmentAnalysis();
-    AnnotatedCollectionI inputSeqs = bySequence ? viewport.getSelectionGroup() : null;
+    AnnotatedCollectionI inputSeqs = bySequence ? selectionGroup : null;
     if (inputSeqs == null || inputSeqs.getWidth() <= 0 ||
         inputSeqs.getSequences() == null || inputSeqs.getSequences().size() < 1)
       inputSeqs = alignment;
@@ -150,27 +102,17 @@ public class AnnotationTask extends BaseTask<AnnotationJob, AnnotationResult>
      * visibility */
 
     udpateCalcId(returnedAnnot);
-    int graphGroup = getNextGraphGroup(viewport.getAlignment());
-    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);
-      ala.sequenceRef = aseq;
-      ala.annotations = gappedAnnots;
-
-      AlignmentAnnotation newAnnot = viewport.getAlignment()
-          .updateFromOrCopyAnnotation(ala);
-      if (aseq != null) // I suspect it's always true
+      SequenceI aseq = null;
+      if (ala.sequenceRef != null)
       {
-        aseq.addAlignmentAnnotation(newAnnot);
-        newAnnot.adjustForAlignment();
-        AlignmentAnnotationUtils.replaceAnnotationOnAlignmentWith(
-            newAnnot, newAnnot.label, newAnnot.getCalcId());
+        SequenceI seq = job.seqNames.get(ala.sequenceRef.getName());
+        aseq = seq.getRootDatasetSequence();
       }
-      annotations.add(newAnnot);
+      ala.sequenceRef = aseq;
+      Annotation[] gappedAnnots = createGappedAnnotations(ala.annotations, job.start, job.gapMap);
+      ala.annotations = gappedAnnots;
     }
 
     boolean hasFeatures = false;
@@ -180,7 +122,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(
@@ -189,7 +131,7 @@ public class AnnotationTask extends BaseTask<AnnotationJob, AnnotationResult>
       datasetSeq.transferAnnotation(sq, mp);
     }
 
-    return new AnnotationResult(annotations, hasFeatures, featureColours, featureFilters);
+    return new AnnotationResult(returnedAnnot, hasFeatures, featureColours, featureFilters);
   }
 
   /**
@@ -208,39 +150,9 @@ 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)
-    {
-      if (ala.graphGroup > 0)
-      {
-        ala.graphGroup += shift;
-      }
-    }
-  }
-
-  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);
+    var size = Math.max(alignment.getWidth(), gapMap.length);
     Annotation[] gappedAnnotations = new Annotation[size];
     for (int p = 0, ap = start; ap < size; ap++)
     {
@@ -273,11 +185,4 @@ public class AnnotationTask extends BaseTask<AnnotationJob, AnnotationResult>
     } while (++col <= end);
     return ranges;
   }
-
-  @Override
-  public String toString()
-  {
-    var status = taskStatus != null ? taskStatus.name() : "UNSET";
-    return String.format("%s(%x, %s)", getClass().getSimpleName(), uid, status);
-  }
 }