JAL-3807 JPred can now successfully submit and track the job.
[jalview.git] / src / jalview / ws / slivkaws / SlivkaWSInstance.java
index f86725b..8aa56fa 100644 (file)
@@ -1,22 +1,10 @@
 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;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.util.Arrays;
 import java.util.EnumMap;
 import java.util.HashSet;
@@ -24,6 +12,21 @@ 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.FormField;
 import uk.ac.dundee.compbio.slivkaclient.FormValidationException;
@@ -46,9 +49,13 @@ public abstract class SlivkaWSInstance extends ServiceWithParameters
   protected static final EnumMap<JobState, WsJob.JobState> stateMap = new EnumMap<>(JobState.class);
   {
     stateMap.put(JobState.PENDING, WsJob.JobState.QUEUED);
+    stateMap.put(JobState.REJECTED, WsJob.JobState.INVALID);
+    stateMap.put(JobState.ACCEPTED, WsJob.JobState.QUEUED);
     stateMap.put(JobState.QUEUED, WsJob.JobState.QUEUED);
     stateMap.put(JobState.RUNNING, WsJob.JobState.RUNNING);
     stateMap.put(JobState.COMPLETED, WsJob.JobState.FINISHED);
+    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);
     stateMap.put(JobState.UNKNOWN, WsJob.JobState.UNKNOWN);
@@ -60,7 +67,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;
   }
@@ -89,6 +96,8 @@ public abstract class SlivkaWSInstance extends ServiceWithParameters
     {
       for (ArgumentI arg : args)
       {
+        // multiple choice field names are name$number to avoid duplications
+        // the number is stripped here
         String fieldName = arg.getName().split("\\$", 2)[0];
         FormField field = form.getField(fieldName);
         if (field.getType() == FieldType.BOOLEAN)
@@ -100,7 +109,7 @@ public abstract class SlivkaWSInstance extends ServiceWithParameters
         }
         else
         {
-          form.insert(fieldName, field.valueOf(arg.getValue()));
+          form.insert(fieldName, arg.getValue());
         }
       }
     }
@@ -123,43 +132,52 @@ 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)
     {
-      InputStream stream = logFile.get().getContent();
-      long nextChunk = stream.skip(job.getNextChunk());
-      int len = appendJobStatus(job, stream);
-      job.setnextChunk(nextChunk + len);
-      newContent |= len > 0;
+      ByteArrayOutputStream output = new ByteArrayOutputStream();
+      logFile.writeTo(output);
+      if (output.size() > job.getNextChunk())
+      {
+        newContent = true;
+        job.setStatus(output.toString("UTF-8"));
+        job.setnextChunk(output.size());
+      }
     }
     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)
       {
-        newContent |= appendJobStatus(job, errLogFile.get().getContent()) > 0;
+        if (f.getLabel().equals("error-log"))
+        {
+          errLogFile = f;
+          break;
+        }
       }
-    }
-    return newContent;
-  }
 
-  private int appendJobStatus(WsJob job, InputStream stream) throws IOException
-  {
-    StringBuilder builder = new StringBuilder(job.getStatus());
-    InputStreamReader reader = new InputStreamReader(stream);
-    char[] buffer = new char[4096];
-    int chunkLen = 0;
-    int len = 0;
-    while ((len = reader.read(buffer)) != -1)
-    {
-      chunkLen += len;
-      builder.append(buffer, 0, len);
+      if (errLogFile!=null)
+      {
+        ByteArrayOutputStream output = new ByteArrayOutputStream();
+        errLogFile.writeTo(output);
+        if (output.size() > 0)
+        {
+          newContent = true;
+          job.setStatus(job.getStatus() + "\n" + output.toString("UTF-8"));
+        }
+      }
     }
-    job.setStatus(builder.toString());
-    return chunkLen;
+    return newContent;
   }
 
   @Override
@@ -229,5 +247,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 == "application/clustal")
+      format = FileFormat.Clustal;
+    else if (mimetype == "application/fasta")
+      format = FileFormat.Fasta;
+    else
+      return null;
+    return new FormatAdapter().readFile(f.getURL().toString(),
+        DataSourceType.URL, format);
+  }
 
 }