JAL-1601 Create action and task for sec. str. pred. jobs
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Thu, 5 Oct 2023 13:12:55 +0000 (15:12 +0200)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Thu, 5 Oct 2023 13:13:37 +0000 (15:13 +0200)
src/jalview/ws2/actions/secstructpred/SecStructPredAction.java [new file with mode: 0644]
src/jalview/ws2/actions/secstructpred/SecStructPredTask.java [new file with mode: 0644]

diff --git a/src/jalview/ws2/actions/secstructpred/SecStructPredAction.java b/src/jalview/ws2/actions/secstructpred/SecStructPredAction.java
new file mode 100644 (file)
index 0000000..a74965e
--- /dev/null
@@ -0,0 +1,58 @@
+package jalview.ws2.actions.secstructpred;
+
+import java.util.List;
+import java.util.Objects;
+
+import jalview.api.AlignViewportI;
+import jalview.datamodel.AlignmentI;
+import jalview.viewmodel.AlignmentViewport;
+import jalview.ws.params.ArgumentI;
+import jalview.ws2.actions.BaseAction;
+import jalview.ws2.api.Credentials;
+import jalview.ws2.client.api.SecStructPredWebServiceClientI;
+import jalview.ws2.client.api.WebServiceClientI;
+
+public class SecStructPredAction extends BaseAction<AlignmentI>
+{
+  public static class Builder extends BaseAction.Builder<SecStructPredAction>
+  {
+    protected SecStructPredWebServiceClientI client;
+    
+    private Builder(SecStructPredWebServiceClientI client)
+    {
+      super();
+      Objects.requireNonNull(client);
+      this.client = client;
+    }
+    
+    public SecStructPredAction build()
+    {
+      return new SecStructPredAction(this);
+    }
+  }
+  
+  public static Builder newBuilder(SecStructPredWebServiceClientI client)
+  {
+    return new Builder(client);
+  }
+  
+  protected final SecStructPredWebServiceClientI client;
+  
+  public SecStructPredAction(Builder builder)
+  {
+    super(builder);
+    client = builder.client;
+  }
+  
+  public SecStructPredTask createTask(AlignViewportI viewport,
+      List<ArgumentI> args, Credentials credentials)
+  {
+    return new SecStructPredTask(client, args, credentials, viewport);
+  }
+
+  @Override
+  public boolean isActive(AlignmentViewport viewport)
+  {
+    return false;
+  }
+}
diff --git a/src/jalview/ws2/actions/secstructpred/SecStructPredTask.java b/src/jalview/ws2/actions/secstructpred/SecStructPredTask.java
new file mode 100644 (file)
index 0000000..1bbd494
--- /dev/null
@@ -0,0 +1,104 @@
+package jalview.ws2.actions.secstructpred;
+
+import java.io.IOException;
+import java.util.List;
+
+import jalview.analysis.AlignmentAnnotationUtils;
+import jalview.api.AlignViewportI;
+import jalview.bin.Console;
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.AlignmentView;
+import jalview.io.AlignFile;
+import jalview.io.JPredFile;
+import jalview.io.JnetAnnotationMaker;
+import jalview.util.Comparison;
+import jalview.ws.params.ArgumentI;
+import jalview.ws2.actions.BaseJob;
+import jalview.ws2.actions.BaseTask;
+import jalview.ws2.actions.ServiceInputInvalidException;
+import jalview.ws2.api.Credentials;
+import jalview.ws2.api.JobStatus;
+import jalview.ws2.client.api.SecStructPredWebServiceClientI;
+
+public class SecStructPredTask extends BaseTask<BaseJob, AlignmentI>
+{
+  private final SecStructPredWebServiceClientI client;
+
+  private final AlignmentView alignmentView;
+
+  private final AlignmentI currentView;
+
+  private final char gapChar;
+
+  SecStructPredTask(SecStructPredWebServiceClientI client, List<ArgumentI> args,
+      Credentials credentials, AlignViewportI viewport)
+  {
+    super(client, args, credentials);
+    this.client = client;
+    this.alignmentView = viewport.getAlignmentView(true);
+    this.currentView = viewport.getAlignment();
+    this.gapChar = viewport.getGapCharacter();
+  }
+
+  @Override
+  protected List<BaseJob> prepareJobs() throws ServiceInputInvalidException
+  {
+    AlignmentI alignment = alignmentView
+        .getVisibleAlignment(Comparison.GAP_DASH);
+    var job = new BaseJob(alignment.getSequences())
+    {
+      @Override
+      public boolean isInputValid()
+      {
+        return true;
+      }
+    };
+    job.setStatus(JobStatus.READY);
+    return List.of(job);
+  }
+
+  @Override
+  protected AlignmentI collectResult(List<BaseJob> jobs) throws IOException
+  {
+    var job = jobs.get(0); // There shouldn't be more than one job
+    var status = job.getStatus();
+    Console
+        .info(String
+            .format("sec str pred job \"%s\" finished with status %s",
+                job.getServerJob().getJobId(), status));
+    if (status != JobStatus.COMPLETED)
+      return null;
+    JPredFile predictionFile = client.getPredictionFile(job.getServerJob());
+    AlignFile alignmentFile = client.getAlignmentFile(job.getServerJob());
+    // Object[] alnAndHiddenCols =
+    // alignmentView.getAlignmentAndHiddenColumns(gapChar);
+    // SequenceI[] sequences = (SequenceI[]) alnAndHiddenCols[0];
+    var sequences = alignmentFile.getSeqsAsArray();
+    // HiddenColumns hiddenCols = (HiddenColumns) alnAndHiddenCols[1];
+    var aln = new Alignment(sequences);
+    int firstSeq = 0;
+    aln.setDataset(currentView.getDataset());
+    try
+    {
+      JnetAnnotationMaker.add_annotation(predictionFile, aln, firstSeq, false);
+    } catch (Exception e)
+    {
+      throw new IOException(e);
+    }
+
+    for (AlignmentAnnotation alnAnnot : aln.getAlignmentAnnotation())
+    {
+      if (alnAnnot.sequenceRef != null)
+      {
+        AlignmentAnnotationUtils
+            .replaceAnnotationOnAlignmentWith(alnAnnot, alnAnnot.label,
+                getClass().getSimpleName());
+      }
+    }
+    aln.setSeqrep(aln.getSequenceAt(firstSeq));
+    return aln;
+  }
+
+}