JAL-3066 Working custom parameters for clustalo
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Fri, 6 Sep 2019 17:07:04 +0000 (18:07 +0100)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Fri, 6 Sep 2019 17:07:04 +0000 (18:07 +0100)
src/jalview/ws/AWSThread.java
src/jalview/ws/jws2/Jws2Client.java
src/jalview/ws/slivkaws/ClustaloWS.java
src/jalview/ws/slivkaws/SlivkaDatastore.java [new file with mode: 0644]
src/jalview/ws/slivkaws/SlivkaParamSet.java [new file with mode: 0644]
src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java

index 80918f9..36e51c5 100644 (file)
@@ -116,9 +116,13 @@ public abstract class AWSThread extends Thread
         {
           StartJob(jobs[j]);
         }
-
+        Cache.log.debug("Job " + jobs[j] + " is " + (jobs[j].submitted ? "submitted" : "not submitted"));
         if (jobs[j].submitted && !jobs[j].subjobComplete)
         {
+          jalview.bin.Cache.log.debug(
+              "Polling Job " + j + " Result state was:" + jobs[j].getState() + "(ServerError=" + jobs[j].isServerError()
+                  + ")"
+          );
           try
           {
             pollJob(jobs[j]);
@@ -188,7 +192,7 @@ public abstract class AWSThread extends Thread
           Cache.log.debug("Interrupted sleep waiting for next job poll.",
                   e);
         }
-        // System.out.println("I'm alive "+alTitle);
+        // System.out.println("I'm alive ?");
       }
     }
     if (jobComplete && jobs != null)
@@ -383,7 +387,7 @@ public abstract class AWSThread extends Thread
               .getCodonFrames();
       if (cf != null)
       {
-        codonframe = new ArrayList<AlignedCodonFrame>();
+        codonframe = new ArrayList<>();
         codonframe.addAll(cf);
       }
     }
index 1c13891..2094202 100644 (file)
@@ -27,7 +27,6 @@ import jalview.gui.JvSwingUtils;
 import jalview.util.MessageManager;
 import jalview.ws.api.ServiceWithParameters;
 import jalview.ws.jws2.dm.AAConSettings;
-import jalview.ws.jws2.dm.JabaWsParamSet;
 import jalview.ws.jws2.jabaws2.Jws2Instance;
 import jalview.ws.params.ArgumentI;
 import jalview.ws.params.WsParamSetI;
@@ -57,21 +56,21 @@ public abstract class Jws2Client extends jalview.ws.WSClient
     super(_alignFrame, preset, arguments);
     if (preset != null)
     {
-      if (!((preset instanceof JabaPreset)
-              || preset instanceof JabaWsParamSet))
-      {
-        /*
-         * { this.preset = ((JabaPreset) preset).p; } else if (preset instanceof
-         * JabaWsParamSet) { List<Argument> newargs = new ArrayList<Argument>();
-         * JabaWsParamSet pset = ((JabaWsParamSet) preset); for (Option opt :
-         * pset.getjabaArguments()) { newargs.add(opt); } if (arguments != null
-         * && arguments.size() > 0) { // merge arguments with preset's own
-         * arguments. for (Argument opt : arguments) { newargs.add(opt); } }
-         * paramset = newargs; } else {
-         */
-        throw new Error(MessageManager.getString(
-                "error.implementation_error_can_only_instantiate_jaba_param_sets"));
-      }
+//      if (!((preset instanceof JabaPreset)
+//              || preset instanceof JabaWsParamSet))
+//      {
+//        /*
+//         * { this.preset = ((JabaPreset) preset).p; } else if (preset instanceof
+//         * JabaWsParamSet) { List<Argument> newargs = new ArrayList<Argument>();
+//         * JabaWsParamSet pset = ((JabaWsParamSet) preset); for (Option opt :
+//         * pset.getjabaArguments()) { newargs.add(opt); } if (arguments != null
+//         * && arguments.size() > 0) { // merge arguments with preset's own
+//         * arguments. for (Argument opt : arguments) { newargs.add(opt); } }
+//         * paramset = newargs; } else {
+//         */
+//        throw new Error(MessageManager.getString(
+//                "error.implementation_error_can_only_instantiate_jaba_param_sets"));
+//      }
     }
     else
     {
index 645d6f2..6a2dae6 100644 (file)
@@ -12,6 +12,7 @@ import jalview.ws.api.MultipleSequenceAlignmentI;
 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;
 
@@ -19,22 +20,25 @@ import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.EnumMap;
+import java.util.HashMap;
 import java.util.List;
 
-import org.apache.http.entity.ContentType;
-
+import uk.ac.dundee.compbio.slivkaclient.FieldType;
 import uk.ac.dundee.compbio.slivkaclient.Form;
+import uk.ac.dundee.compbio.slivkaclient.FormField;
 import uk.ac.dundee.compbio.slivkaclient.FormValidationException;
 import uk.ac.dundee.compbio.slivkaclient.JobState;
 import uk.ac.dundee.compbio.slivkaclient.RemoteFile;
 import uk.ac.dundee.compbio.slivkaclient.Service;
 import uk.ac.dundee.compbio.slivkaclient.SlivkaClient;
+import uk.ac.dundee.compbio.slivkaclient.ValidationException;
 
 public class ClustaloWS extends ServiceWithParameters
     implements MultipleSequenceAlignmentI, JalviewServiceEndpointProviderI
 {
   private SlivkaClient client;
   private Service service;
+  private SlivkaDatastore store = null;
 
   private EnumMap<JobState, WsJob.JobState> stateMap = new EnumMap<>(JobState.class);
   {
@@ -50,12 +54,12 @@ public class ClustaloWS extends ServiceWithParameters
 
   ClustaloWS(SlivkaClient client)
   {
-    super("ClustalO", "Alignment", "ClustalO", "foo", "http://foo.foo");
+    super("ClustalO", "Alignment", "ClustalO", "Slivka", client.getUrl().toString());
 
     this.client = client;
     try
     {
-      service = client.getService("clustalo");
+      this.service = client.getService("clustalo");
     } catch (IOException e)
     {
       throw new RuntimeException(e);
@@ -74,6 +78,11 @@ public class ClustaloWS extends ServiceWithParameters
     }
   }
 
+  Service getService()
+  {
+    return service;
+  }
+
   @Override
   public boolean updateJobProgress(WsJob job)
   {
@@ -93,20 +102,47 @@ public class ClustaloWS extends ServiceWithParameters
   }
 
   @Override
-  public JobId align(List<SequenceI> toalign, WsParamSetI parameters, List<ArgumentI> list) throws FormValidationException, IOException
+  public JobId align(List<SequenceI> toalign, WsParamSetI parameters, List<ArgumentI> list)
+      throws IOException
   {
     StringBuilder builder = new StringBuilder();
     for (SequenceI seq : toalign)
     {
       builder.append(">").append(seq.getName()).append("\n");
-      builder.append(seq.getSequence()).append("\n\n");
+      builder.append(seq.getSequence()).append("\n");
     }
     InputStream stream = new ByteArrayInputStream(builder.toString().getBytes());
-    RemoteFile file = client.uploadFile(stream, "input.fasta", ContentType.create("application/fasta"));
+    RemoteFile file = client.uploadFile(stream, "input.fasta", "application/fasta");
     Form form = service.getForm();
-    form.insert("input", file);
-    String jobUUID = form.submit();
-    return new JobId("clustalo", "clustalo", jobUUID);
+    HashMap<String, String> values = new HashMap<>(list != null ? list.size() : 0);
+    if (list != null)
+    {
+      for (ArgumentI arg : list)
+      {
+        values.put(arg.getName(), arg.getValue());
+      }
+    }
+    for (FormField field : form.getFields())
+    {
+      if (field.getType() == FieldType.FILE)
+      {
+        form.insert(field.getName(), file);
+      } else
+      {
+        form.insert(field.getName(), field.valueOf(values.get(field.getName())));
+      }
+    }
+    try
+    {
+      return new JobId("clustalo", "clustalo", form.submit());
+    } catch (FormValidationException e)
+    {
+      for (ValidationException ve : e.getErrors())
+      {
+        System.out.format("%s: %s%n", ve.getField().getName(), ve.getMessage());
+      }
+      return null;
+    }
   }
 
   @Override
@@ -133,7 +169,28 @@ public class ClustaloWS extends ServiceWithParameters
   @Override
   public void initParamStore(ParamManager userParameterStore)
   {
+    if (store == null)
+    {
+      try
+      {
+        store = new SlivkaDatastore(service);
+      } catch (IOException e)
+      {
+        throw new RuntimeException(e);
+      }
+    }
+  }
+
+  @Override
+  public boolean hasParameters()
+  {
+    return true;
+  }
 
+  @Override
+  public ParamDatastoreI getParamStore()
+  {
+    return store;
   }
 
   @Override
diff --git a/src/jalview/ws/slivkaws/SlivkaDatastore.java b/src/jalview/ws/slivkaws/SlivkaDatastore.java
new file mode 100644 (file)
index 0000000..24d11c7
--- /dev/null
@@ -0,0 +1,82 @@
+package jalview.ws.slivkaws;
+
+import jalview.ws.params.ArgumentI;
+import jalview.ws.params.ParamDatastoreI;
+import jalview.ws.params.WsParamSetI;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import uk.ac.dundee.compbio.slivkaclient.Service;
+
+public class SlivkaDatastore implements ParamDatastoreI
+{
+  private SlivkaParamSet defaultPreset;
+  private List<WsParamSetI> presets = new ArrayList<>();
+
+  SlivkaDatastore(Service service) throws IOException {
+    defaultPreset = new SlivkaParamSet(service);
+    presets.add(defaultPreset);
+  }
+
+  @Override
+  public List<WsParamSetI> getPresets()
+  {
+    return presets;
+  }
+
+  @Override
+  public WsParamSetI getPreset(String name)
+  {
+    for (WsParamSetI preset : presets)
+    {
+      if (preset.getName().equals(name))
+      {
+        return preset;
+      }
+    }
+    return null;
+  }
+
+  @Override
+  public List<ArgumentI> getServiceParameters()
+  {
+    return new ArrayList<>(defaultPreset.getArguments());
+  }
+
+  @Override
+  public boolean presetExists(String name)
+  {
+    return name.equals(defaultPreset.getName());
+  }
+
+  @Override
+  public void deletePreset(String name)
+  {
+  }
+
+  @Override
+  public void storePreset(String presetName, String text, List<ArgumentI> jobParams)
+  {
+  }
+
+  @Override
+  public void updatePreset(String oldName, String presetName, String text, List<ArgumentI> jobParams)
+  {
+  }
+
+  @Override
+  public WsParamSetI parseServiceParameterFile(String name, String description, String[] serviceURL, String parameters)
+      throws IOException
+  {
+    return null;
+  }
+
+  @Override
+  public String generateServiceParameterFile(WsParamSetI pset) throws IOException
+  {
+    return null;
+  }
+
+}
diff --git a/src/jalview/ws/slivkaws/SlivkaParamSet.java b/src/jalview/ws/slivkaws/SlivkaParamSet.java
new file mode 100644 (file)
index 0000000..7feb2f1
--- /dev/null
@@ -0,0 +1,127 @@
+package jalview.ws.slivkaws;
+
+import jalview.ws.params.ArgumentI;
+import jalview.ws.params.WsParamSetI;
+import jalview.ws.params.simple.BooleanOption;
+import jalview.ws.params.simple.DoubleParameter;
+import jalview.ws.params.simple.IntegerParameter;
+import jalview.ws.params.simple.RadioChoiceParameter;
+import jalview.ws.params.simple.StringParameter;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import uk.ac.dundee.compbio.slivkaclient.BooleanField;
+import uk.ac.dundee.compbio.slivkaclient.ChoiceField;
+import uk.ac.dundee.compbio.slivkaclient.DecimalField;
+import uk.ac.dundee.compbio.slivkaclient.Form;
+import uk.ac.dundee.compbio.slivkaclient.FormField;
+import uk.ac.dundee.compbio.slivkaclient.IntegerField;
+import uk.ac.dundee.compbio.slivkaclient.Service;
+import uk.ac.dundee.compbio.slivkaclient.TextField;
+
+
+
+public class SlivkaParamSet implements WsParamSetI
+{
+  private Service service;
+  private List<ArgumentI> args = new ArrayList<>();
+
+  SlivkaParamSet(Service service) throws IOException {
+    this.service = service;
+    Form form = service.getForm();
+    for (FormField field : form.getFields())
+    {
+      ArgumentI arg;
+      switch (field.getType()) {
+      case BOOLEAN:
+        BooleanField boolField = (BooleanField) field;
+        arg = new BooleanOption(
+            field.getName(), field.getDescription(), field.isRequired(), boolField.getDefault(),
+            boolField.getDefault(), null
+        );
+        break;
+      case TEXT:
+        TextField textField = (TextField) field;
+        arg = new StringParameter(
+            field.getName(), field.getDescription(), field.isRequired(), textField.getDefault(),
+            textField.getDefault()
+        );
+        break;
+      case INTEGER:
+        IntegerField intField = (IntegerField) field;
+        arg = new IntegerParameter(
+            field.getName(), field.getDescription(), field.isRequired(), intField.getDefault(),
+            intField.getMin(), intField.getMax()
+        );
+        break;
+      case DECIMAL:
+        DecimalField doubleField = (DecimalField) field;
+        arg = new DoubleParameter(
+            field.getName(), field.getDescription(), field.isRequired(), doubleField.getDefault(),
+            doubleField.getMin(), doubleField.getMax()
+        );
+        break;
+      case CHOICE:
+        ChoiceField choiceField = (ChoiceField) field;
+        arg = new RadioChoiceParameter(
+            field.getName(), field.getDescription(), new ArrayList<>(choiceField.getChoices()), choiceField.getDefault()
+        );
+        break;
+      case FILE:
+      default:
+        continue;
+      }
+      args.add(arg);
+    }
+  }
+
+  @Override
+  public String getName()
+  {
+    return "Default";
+  }
+
+  @Override
+  public String getDescription()
+  {
+    return "";
+  }
+
+  @Override
+  public String[] getApplicableUrls()
+  {
+    return new String[] { service.getURL().toString() };
+  }
+
+  @Override
+  public String getSourceFile()
+  {
+    return null;
+  }
+
+  @Override
+  public void setSourceFile(String newfile)
+  {
+  }
+
+  @Override
+  public boolean isModifiable()
+  {
+    return true;
+  }
+
+  @Override
+  public List<ArgumentI> getArguments()
+  {
+    return args;
+  }
+
+  @Override
+  public void setArguments(List<ArgumentI> args)
+  {
+    throw new RuntimeException();
+  }
+
+}
index b799c5a..dfa3d89 100644 (file)
@@ -6,6 +6,7 @@ import jalview.ws.WSMenuEntryProviderI;
 import jalview.ws.jws2.MsaWSClient;
 
 import java.awt.event.ActionEvent;
+import java.io.IOException;
 import java.net.URISyntaxException;
 
 import javax.swing.JMenu;
@@ -43,29 +44,40 @@ public class SlivkaWSDiscoverer
   public void attachWSMenuEntry(JMenu wsmenu, final AlignFrame alignFrame)
   {
     JMenu submenu = new JMenu("Slivka");
-    JMenuItem menuItem = new JMenuItem("ClustalO with defaults");
-    menuItem.addActionListener((ActionEvent e) -> {
 
+    JMenuItem noParamMenuItem = new JMenuItem("ClustalO with defaults");
+    noParamMenuItem.addActionListener((ActionEvent e) -> {
       AlignmentView msa = alignFrame.gatherSequencesForAlignment();
-
       if (msa != null)
       {
-        new MsaWSClient(clustalo, alignFrame.getTitle(), msa, false,
-            true,
+        new MsaWSClient(
+            clustalo, alignFrame.getTitle(), msa, false, true,
             alignFrame.getViewport().getAlignment().getDataset(),
             alignFrame);
       }
-//
-//      List<SequenceI> sequences = alignFrame.getViewport().getAlignment().getSequences();
-//      try
-//      {
-//        clustalo.align(sequences, null, null);
-//      } catch (FormValidationException | IOException exc)
-//      {
-//        throw new RuntimeException(exc);
-//      }
     });
-    submenu.add(menuItem);
+    submenu.add(noParamMenuItem);
+
+    JMenuItem parametrisedMenuItem = new JMenuItem("ClustalO with custom parameters");
+    parametrisedMenuItem.addActionListener((ActionEvent evt) -> {
+      AlignmentView msa = alignFrame.gatherSequencesForAlignment();
+      if (msa != null)
+      {
+        try
+        {
+          SlivkaParamSet paramSet = new SlivkaParamSet(clustalo.getService());
+          new MsaWSClient(
+              clustalo, paramSet, null, true, alignFrame.getTitle(), msa, false, true,
+              alignFrame.getViewport().getAlignment().getDataset(), alignFrame
+          );
+        } catch (IOException e)
+        {
+          e.printStackTrace();
+        }
+      }
+    });
+    submenu.add(parametrisedMenuItem);
+
     wsmenu.add(submenu);
   }