Merge branch 'bug/JAL-3807_jpred-with-slivka' into alpha/JAL-3066_Jalview_212_slivka...
[jalview.git] / src / jalview / ws / slivkaws / SlivkaWSInstance.java
index 33c1205..fa54cf0 100644 (file)
@@ -1,17 +1,5 @@
 package jalview.ws.slivkaws;
 
-import jalview.datamodel.SequenceI;
-import jalview.gui.WebserviceInfo;
-import jalview.ws.api.JalviewServiceEndpointProviderI;
-import jalview.ws.api.JalviewWebServiceI;
-import jalview.ws.api.JobId;
-import jalview.ws.api.ServiceWithParameters;
-import jalview.ws.gui.WsJob;
-import jalview.ws.params.ArgumentI;
-import jalview.ws.params.ParamDatastoreI;
-import jalview.ws.params.ParamManager;
-import jalview.ws.params.WsParamSetI;
-
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOError;
@@ -24,7 +12,23 @@ import java.util.List;
 import java.util.Optional;
 import java.util.Set;
 
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.SequenceI;
+import jalview.gui.WebserviceInfo;
+import jalview.io.DataSourceType;
+import jalview.io.FileFormat;
+import jalview.io.FormatAdapter;
+import jalview.ws.api.JalviewServiceEndpointProviderI;
+import jalview.ws.api.JalviewWebServiceI;
+import jalview.ws.api.JobId;
+import jalview.ws.api.ServiceWithParameters;
+import jalview.ws.gui.WsJob;
+import jalview.ws.params.ArgumentI;
+import jalview.ws.params.ParamDatastoreI;
+import jalview.ws.params.ParamManager;
+import jalview.ws.params.WsParamSetI;
 import uk.ac.dundee.compbio.slivkaclient.FieldType;
+import uk.ac.dundee.compbio.slivkaclient.FileField;
 import uk.ac.dundee.compbio.slivkaclient.FormField;
 import uk.ac.dundee.compbio.slivkaclient.FormValidationException;
 import uk.ac.dundee.compbio.slivkaclient.JobState;
@@ -51,7 +55,7 @@ public abstract class SlivkaWSInstance extends ServiceWithParameters
     stateMap.put(JobState.QUEUED, WsJob.JobState.QUEUED);
     stateMap.put(JobState.RUNNING, WsJob.JobState.RUNNING);
     stateMap.put(JobState.COMPLETED, WsJob.JobState.FINISHED);
-    stateMap.put(JobState.INTERRUPED, WsJob.JobState.CANCELLED);
+    stateMap.put(JobState.INTERRUPTED, WsJob.JobState.CANCELLED);
     stateMap.put(JobState.DELETED, WsJob.JobState.CANCELLED);
     stateMap.put(JobState.FAILED, WsJob.JobState.FAILED);
     stateMap.put(JobState.ERROR, WsJob.JobState.SERVERERROR);
@@ -64,7 +68,7 @@ public abstract class SlivkaWSInstance extends ServiceWithParameters
 
   public SlivkaWSInstance(SlivkaClient client, SlivkaService service, String action)
   {
-    super(service.getName(), action, service.getLabel(), "Slivka", client.getUrl().toString());
+    super(action, action, service.getLabel(), "Slivka", client.getUrl().toString());
     this.client = client;
     this.service = service;
   }
@@ -73,21 +77,34 @@ public abstract class SlivkaWSInstance extends ServiceWithParameters
           WsParamSetI preset, List<ArgumentI> args) throws Throwable
   {
     SlivkaForm form = service.getForm();
-    Optional<FormField> inputField = form.getFields().stream()
-            .filter(f -> f.getType() == FieldType.FILE).findFirst();
-    if (inputField.isPresent())
+    for (FormField field : form.getFields())
     {
-      StringBuilder builder = new StringBuilder();
-      for (SequenceI seq : sequences)
+      if (field.getType() == FieldType.FILE)
       {
-        builder.append(">").append(seq.getName()).append("\n")
-                .append(seq.getSequence()).append("\n");
+        FormatAdapter fa = new FormatAdapter();
+        fa.setNewlineString("\r\n");
+        FileField fileField = (FileField) field;
+        FileFormat format;
+        switch (fileField.getMediaType())
+        {
+        case "application/pfam":
+          format = FileFormat.Pfam;
+          break;
+        case "application/stockholm":
+          format = FileFormat.Stockholm;
+          break;
+        default:
+        case "application/fasta":
+          format = FileFormat.Fasta;
+          break;
+        }
+        InputStream stream = new ByteArrayInputStream(
+            fa.formatSequences(format, sequences.toArray(new SequenceI[0]))
+                .getBytes());
+        RemoteFile rf = client.uploadFile(stream, "input",
+            fileField.getMediaType());
+        form.insert(field.getName(), rf);
       }
-      InputStream stream = new ByteArrayInputStream(
-              builder.toString().getBytes());
-      RemoteFile file = client.uploadFile(stream, "input.fa",
-              "application/fasta");
-      form.insert(inputField.get().getName(), file);
     }
     if (args != null)
     {
@@ -129,13 +146,20 @@ public abstract class SlivkaWSInstance extends ServiceWithParameters
   public final boolean updateJobProgress(WsJob job) throws IOException
   {
     List<RemoteFile> files = client.getJobResults(job.getJobId());
-    Optional<RemoteFile> logFile = files.stream()
-            .filter(f -> f.getLabel().equals("log")).findFirst();
+    RemoteFile logFile=null;
+    for (RemoteFile f : files)
+    {
+      if (f.getLabel().equals("log"))
+      {
+        logFile = f; break;
+      }
+    }
+
     boolean newContent = false;
-    if (logFile.isPresent())
+    if (logFile!=null)
     {
       ByteArrayOutputStream output = new ByteArrayOutputStream();
-      logFile.get().writeTo(output);
+      logFile.writeTo(output);
       if (output.size() > job.getNextChunk())
       {
         newContent = true;
@@ -145,12 +169,21 @@ public abstract class SlivkaWSInstance extends ServiceWithParameters
     }
     if (failedStates.contains(job.getJobState()))
     {
-      Optional<RemoteFile> errLogFile = files.stream()
-              .filter(f -> f.getLabel().equals("error-log")).findFirst();
-      if (errLogFile.isPresent())
+      
+      RemoteFile errLogFile = null;
+      for (RemoteFile f : files)
+      {
+        if (f.getLabel().equals("error-log"))
+        {
+          errLogFile = f;
+          break;
+        }
+      }
+
+      if (errLogFile!=null)
       {
         ByteArrayOutputStream output = new ByteArrayOutputStream();
-        errLogFile.get().writeTo(output);
+        errLogFile.writeTo(output);
         if (output.size() > 0)
         {
           newContent = true;
@@ -228,5 +261,19 @@ public abstract class SlivkaWSInstance extends ServiceWithParameters
     }
     return store;
   }
+  
+  public static AlignmentI readAlignment(RemoteFile f) throws IOException
+  {
+    final var mimetype = f.getMimeType();
+    FileFormat format;
+    if (mimetype.equals("application/clustal"))
+      format = FileFormat.Clustal;
+    else if (mimetype.equals("application/fasta"))
+      format = FileFormat.Fasta;
+    else
+      return null;
+    return new FormatAdapter().readFile(f.getURL().toString(),
+        DataSourceType.URL, format);
+  }
 
 }