1 package jalview.ws2.actions.annotation;
3 import java.util.ArrayList;
4 import java.util.Collections;
6 import java.util.concurrent.CopyOnWriteArrayList;
7 import java.util.function.Consumer;
9 import jalview.analysis.AlignmentAnnotationUtils;
10 import jalview.api.AlignViewportI;
11 import jalview.api.AlignmentViewPanel;
12 import jalview.api.PollableAlignCalcWorkerI;
13 import jalview.datamodel.AlignmentAnnotation;
14 import jalview.datamodel.AlignmentI;
15 import jalview.workers.AlignCalcWorker;
16 import jalview.ws.params.ArgumentI;
17 import jalview.ws2.actions.api.TaskEventListener;
18 import jalview.ws2.actions.api.TaskI;
19 import jalview.ws2.api.Credentials;
21 public class AlignCalcWorkerAdapter extends AlignCalcWorker implements PollableAlignCalcWorkerI
23 private static int calcCount = 0;
25 private final int calcNumber = calcCount++;
27 private final AnnotationAction action;
29 private final List<ArgumentI> args;
31 private final Credentials credentials;
33 private TaskI<AnnotationResult> currentTask = null;
35 private TaskEventListener<AnnotationResult> taskListener = new TaskEventListener<>()
38 public void taskCompleted(TaskI source, AnnotationResult result)
40 int graphGroup = alignViewport.getAlignment().getLastGraphGroup();
41 List<AlignmentAnnotation> annotations = new ArrayList<>();
42 for (AlignmentAnnotation ala : result.getAnnotations())
44 if (ala.graphGroup > 0)
45 ala.graphGroup += graphGroup;
46 var newAnnot = alignViewport.getAlignment()
47 .updateFromOrCopyAnnotation(ala);
48 if (ala.sequenceRef != null)
50 ala.sequenceRef.addAlignmentAnnotation(newAnnot);
51 newAnnot.adjustForAlignment();
52 AlignmentAnnotationUtils.replaceAnnotationOnAlignmentWith(
53 newAnnot, newAnnot.label, newAnnot.getCalcId());
55 annotations.add(newAnnot);
57 updateOurAnnots(annotations);
58 listener.workerHasResult(
59 AlignCalcWorkerAdapter.this,
62 result.transferFeatures,
63 result.featureColours,
64 result.featureFilters));
68 public AlignCalcWorkerAdapter(AlignViewportI alignViewport, AlignmentViewPanel alignPanel, AnnotationAction action,
69 List<ArgumentI> args, Credentials credentials)
71 super(alignViewport, alignPanel);
74 this.credentials = credentials;
78 public String getCalcName()
80 return action.getWebService().getName();
84 public void updateAnnotation()
89 private void updateOurAnnots(List<AlignmentAnnotation> newAnnots)
91 List<AlignmentAnnotation> oldAnnots = ourAnnots != null ? ourAnnots : List.of();
92 AlignmentI alignment = alignViewport.getAlignment();
93 for (AlignmentAnnotation annotation : oldAnnots)
94 if (!newAnnots.contains(annotation))
95 alignment.deleteAnnotation(annotation);
96 for (AlignmentAnnotation annotation : newAnnots)
97 alignment.validateAnnotation(annotation);
98 ourAnnots = Collections.synchronizedList(newAnnots);
102 public synchronized void startUp() throws Throwable
104 if (alignViewport.isClosed())
106 calcMan.disableWorker(this);
108 throw new IllegalStateException("Starting calculation for closed viewport");
110 currentTask = action.createTask(alignViewport, args, credentials);
111 currentTask.addTaskEventListener(taskListener);
113 listener.workerStarted(this);
117 public boolean poll() throws Throwable
119 return currentTask.poll();
123 public synchronized void cancel()
127 currentTask.cancel();
130 currentTask.removeTaskEventListener(taskListener);
131 listener.workerStopped(this);
136 public synchronized void done()
140 currentTask.complete();
141 } catch (Exception e)
145 currentTask.removeTaskEventListener(taskListener);
146 listener.workerStopped(this);
151 public String toString()
153 return "AlignCalcWorkerAdapter-" + calcNumber + " for " + getCalcName();
156 public interface WorkerListener extends java.util.EventListener
158 default void workerStarted(AlignCalcWorkerAdapter source)
162 default void workerStopped(AlignCalcWorkerAdapter source)
166 default void workerHasResult(AlignCalcWorkerAdapter source, AnnotationResult result)
170 static final WorkerListener NULL_LISTENER = new WorkerListener()
175 private WorkerListener listener = WorkerListener.NULL_LISTENER;
177 public void setWorkerListener(WorkerListener listener)
179 if (listener == null) listener = WorkerListener.NULL_LISTENER;
180 this.listener = listener;