1 package jalview.ws.slivkaws;
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOError;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.util.Arrays;
9 import java.util.EnumMap;
10 import java.util.HashSet;
11 import java.util.List;
14 import jalview.datamodel.AlignmentI;
15 import jalview.datamodel.SequenceI;
16 import jalview.gui.WebserviceInfo;
17 import jalview.io.DataSourceType;
18 import jalview.io.FileFormat;
19 import jalview.io.FormatAdapter;
20 import jalview.ws.api.JalviewServiceEndpointProviderI;
21 import jalview.ws.api.JalviewWebServiceI;
22 import jalview.ws.api.JobId;
23 import jalview.ws.api.ServiceWithParameters;
24 import jalview.ws.gui.WsJob;
25 import jalview.ws.params.ArgumentI;
26 import jalview.ws.params.ParamDatastoreI;
27 import jalview.ws.params.ParamManager;
28 import jalview.ws.params.WsParamSetI;
29 import javajs.http.ClientProtocolException;
31 import java.util.Collection;
32 import uk.ac.dundee.compbio.slivkaclient.Job;
33 import uk.ac.dundee.compbio.slivkaclient.JobRequest;
34 import uk.ac.dundee.compbio.slivkaclient.Parameter;
35 import uk.ac.dundee.compbio.slivkaclient.RemoteFile;
36 import uk.ac.dundee.compbio.slivkaclient.SlivkaClient;
37 import uk.ac.dundee.compbio.slivkaclient.SlivkaService;
39 public abstract class SlivkaWSInstance extends ServiceWithParameters
40 implements JalviewServiceEndpointProviderI, JalviewWebServiceI
42 protected final SlivkaClient client;
44 protected final SlivkaService service;
46 protected SlivkaDatastore store = null;
48 protected static final EnumMap<Job.Status, WsJob.JobState> stateMap = new EnumMap<>(Job.Status.class);
50 stateMap.put(Job.Status.PENDING, WsJob.JobState.QUEUED);
51 stateMap.put(Job.Status.REJECTED, WsJob.JobState.INVALID);
52 stateMap.put(Job.Status.ACCEPTED, WsJob.JobState.QUEUED);
53 stateMap.put(Job.Status.QUEUED, WsJob.JobState.QUEUED);
54 stateMap.put(Job.Status.RUNNING, WsJob.JobState.RUNNING);
55 stateMap.put(Job.Status.COMPLETED, WsJob.JobState.FINISHED);
56 stateMap.put(Job.Status.INTERRUPTED, WsJob.JobState.CANCELLED);
57 stateMap.put(Job.Status.DELETED, WsJob.JobState.CANCELLED);
58 stateMap.put(Job.Status.FAILED, WsJob.JobState.FAILED);
59 stateMap.put(Job.Status.ERROR, WsJob.JobState.SERVERERROR);
60 stateMap.put(Job.Status.UNKNOWN, WsJob.JobState.UNKNOWN);
62 protected final Set<WsJob.JobState> failedStates = new HashSet<>(Arrays.asList(
63 WsJob.JobState.INVALID, WsJob.JobState.BROKEN, WsJob.JobState.FAILED,
64 WsJob.JobState.SERVERERROR, WsJob.JobState.CANCELLED
67 public SlivkaWSInstance(SlivkaClient client, SlivkaService service, String action)
69 super(action, action, service.getName(), "Slivka", client.getUrl().toString());
71 this.service = service;
74 protected final JobId submit(List<SequenceI> sequences,
75 WsParamSetI preset, List<ArgumentI> args) throws Throwable
77 var parameters = service.getParameters();
78 var request = new JobRequest();
79 for (Parameter param : parameters)
81 if (param instanceof Parameter.FileParameter)
83 FormatAdapter fa = new FormatAdapter();
84 fa.setNewlineString("\r\n");
85 Parameter.FileParameter fileParam = (Parameter.FileParameter) param;
87 switch (fileParam.getMediaType())
89 case "application/pfam":
90 format = FileFormat.Pfam;
92 case "application/stockholm":
93 format = FileFormat.Stockholm;
96 case "application/fasta":
97 format = FileFormat.Fasta;
101 // we avoid any use of Jalview's user facing export routines here
103 InputStream stream = new ByteArrayInputStream(format.getWriter(null)
104 .print(sequences.toArray(new SequenceI[0]), false)
106 request.addFile(param.getId(), stream);
111 for (ArgumentI arg : args)
113 // multiple choice field names are name$number to avoid duplications
114 // the number is stripped here
115 String paramId = arg.getName().split("\\$", 2)[0];
116 Parameter param = service.getParameter(paramId);
117 if (param instanceof Parameter.FlagParameter) {
118 if (arg.getValue() != null && !arg.getValue().isBlank())
119 request.addData(paramId, true);
121 request.addData(paramId, false);
125 request.addData(paramId, arg.getValue());
129 var job = service.submitJob(request);
130 return new JobId(service.getName(), service.getName(), job.getId());
134 public final void updateStatus(WsJob job)
138 var slivkaJob = client.getJob(job.getJobId());
139 job.setState(stateMap.get(slivkaJob.getStatus()));
140 } catch (IOException e)
142 throw new IOError(e);
147 public final boolean updateJobProgress(WsJob job) throws IOException
149 var slivkaJob = client.getJob(job.getJobId());
150 Collection<RemoteFile> files = slivkaJob.getResults();
151 RemoteFile logFile=null;
152 for (RemoteFile f : files)
154 if (f.getLabel().equals("log"))
160 boolean newContent = false;
163 ByteArrayOutputStream output = new ByteArrayOutputStream();
164 logFile.writeTo(output);
165 if (output.size() > job.getNextChunk())
168 job.setStatus(output.toString("UTF-8"));
169 job.setnextChunk(output.size());
172 if (failedStates.contains(job.getJobState()))
175 RemoteFile errLogFile = null;
176 for (RemoteFile f : files)
178 if (f.getLabel().equals("error-log"))
185 if (errLogFile!=null)
187 ByteArrayOutputStream output = new ByteArrayOutputStream();
188 errLogFile.writeTo(output);
189 if (output.size() > 0)
192 job.setStatus(job.getStatus() + "\n" + output.toString("UTF-8"));
200 public final boolean handleSubmitError(Throwable _lex, WsJob j, WebserviceInfo wsInfo)
202 if (_lex instanceof ClientProtocolException)
204 j.setState(WsJob.JobState.INVALID);
205 j.setStatus(_lex.getMessage());
212 public final boolean handleCollectionException(Exception e, WsJob msjob, WebserviceInfo wsInfo)
218 final SlivkaService getService()
224 public final Object getEndpoint()
230 public final void initParamStore(ParamManager userParameterStore)
234 store = new SlivkaDatastore(service);
239 public boolean hasParameters()
245 public final ParamDatastoreI getParamStore()
249 initParamStore(null);
254 public static AlignmentI readAlignment(RemoteFile f) throws IOException
256 final var mimetype = f.getMediaType();
258 if (mimetype.equals("application/clustal"))
259 format = FileFormat.Clustal;
260 else if (mimetype.equals("application/fasta"))
261 format = FileFormat.Fasta;
264 return new FormatAdapter().readFile(f.getContentUrl().toString(),
265 DataSourceType.URL, format);