JAL-4199 Remove viewport reference from AnnotationTask
[jalview.git] / src / jalview / ws2 / actions / annotation / AnnotationTask.java
index e2a05a0..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,7 +42,8 @@ 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();
   }
 
   /**
@@ -79,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");
@@ -90,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;
@@ -124,30 +102,17 @@ public class AnnotationTask extends BaseTask<AnnotationJob, AnnotationResult>
      * visibility */
 
     udpateCalcId(returnedAnnot);
-    int graphGroup = viewport.getAlignment().getLastGraphGroup();
-    shiftGraphGroup(returnedAnnot, graphGroup);
-    List<AlignmentAnnotation> annotations = new ArrayList<>();
     for (AlignmentAnnotation ala : returnedAnnot)
     {
       SequenceI aseq = null;
-      if (ala.sequenceRef != 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)
-      {
-        aseq.addAlignmentAnnotation(newAnnot);
-        newAnnot.adjustForAlignment();
-        AlignmentAnnotationUtils.replaceAnnotationOnAlignmentWith(
-            newAnnot, newAnnot.label, newAnnot.getCalcId());
-      }
-      annotations.add(newAnnot);
     }
 
     boolean hasFeatures = false;
@@ -166,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);
   }
 
   /**
@@ -185,20 +150,9 @@ public class AnnotationTask extends BaseTask<AnnotationJob, AnnotationResult>
     }
   }
 
-  private static void shiftGraphGroup(Iterable<AlignmentAnnotation> annotations, int shift)
-  {
-    for (AlignmentAnnotation ala : annotations)
-    {
-      if (ala.graphGroup > 0)
-      {
-        ala.graphGroup += shift;
-      }
-    }
-  }
-
   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++)
     {
@@ -231,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);
-  }
 }