Merge branch 'bug/JAL-3807_jpred-with-slivka' into alpha/JAL-3066_Jalview_212_slivka...
[jalview.git] / src / jalview / ws / slivkaws / SlivkaWSInstance.java
1 package jalview.ws.slivkaws;
2
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;
12 import java.util.Optional;
13 import java.util.Set;
14
15 import jalview.datamodel.AlignmentI;
16 import jalview.datamodel.SequenceI;
17 import jalview.gui.WebserviceInfo;
18 import jalview.io.DataSourceType;
19 import jalview.io.FileFormat;
20 import jalview.io.FormatAdapter;
21 import jalview.ws.api.JalviewServiceEndpointProviderI;
22 import jalview.ws.api.JalviewWebServiceI;
23 import jalview.ws.api.JobId;
24 import jalview.ws.api.ServiceWithParameters;
25 import jalview.ws.gui.WsJob;
26 import jalview.ws.params.ArgumentI;
27 import jalview.ws.params.ParamDatastoreI;
28 import jalview.ws.params.ParamManager;
29 import jalview.ws.params.WsParamSetI;
30 import uk.ac.dundee.compbio.slivkaclient.FieldType;
31 import uk.ac.dundee.compbio.slivkaclient.FileField;
32 import uk.ac.dundee.compbio.slivkaclient.FormField;
33 import uk.ac.dundee.compbio.slivkaclient.FormValidationException;
34 import uk.ac.dundee.compbio.slivkaclient.JobState;
35 import uk.ac.dundee.compbio.slivkaclient.RemoteFile;
36 import uk.ac.dundee.compbio.slivkaclient.SlivkaClient;
37 import uk.ac.dundee.compbio.slivkaclient.SlivkaForm;
38 import uk.ac.dundee.compbio.slivkaclient.SlivkaService;
39 import uk.ac.dundee.compbio.slivkaclient.ValidationException;
40
41 public abstract class SlivkaWSInstance extends ServiceWithParameters
42     implements JalviewServiceEndpointProviderI, JalviewWebServiceI
43 {
44   protected final SlivkaClient client;
45
46   protected final SlivkaService service;
47
48   protected SlivkaDatastore store = null;
49
50   protected static final EnumMap<JobState, WsJob.JobState> stateMap = new EnumMap<>(JobState.class);
51   {
52     stateMap.put(JobState.PENDING, WsJob.JobState.QUEUED);
53     stateMap.put(JobState.REJECTED, WsJob.JobState.INVALID);
54     stateMap.put(JobState.ACCEPTED, WsJob.JobState.QUEUED);
55     stateMap.put(JobState.QUEUED, WsJob.JobState.QUEUED);
56     stateMap.put(JobState.RUNNING, WsJob.JobState.RUNNING);
57     stateMap.put(JobState.COMPLETED, WsJob.JobState.FINISHED);
58     stateMap.put(JobState.INTERRUPTED, WsJob.JobState.CANCELLED);
59     stateMap.put(JobState.DELETED, WsJob.JobState.CANCELLED);
60     stateMap.put(JobState.FAILED, WsJob.JobState.FAILED);
61     stateMap.put(JobState.ERROR, WsJob.JobState.SERVERERROR);
62     stateMap.put(JobState.UNKNOWN, WsJob.JobState.UNKNOWN);
63   }
64   protected final Set<WsJob.JobState> failedStates = new HashSet<>(Arrays.asList(
65       WsJob.JobState.INVALID, WsJob.JobState.BROKEN, WsJob.JobState.FAILED,
66       WsJob.JobState.SERVERERROR, WsJob.JobState.CANCELLED
67   ));
68
69   public SlivkaWSInstance(SlivkaClient client, SlivkaService service, String action)
70   {
71     super(action, action, service.getLabel(), "Slivka", client.getUrl().toString());
72     this.client = client;
73     this.service = service;
74   }
75
76   protected final JobId submit(List<SequenceI> sequences,
77           WsParamSetI preset, List<ArgumentI> args) throws Throwable
78   {
79     SlivkaForm form = service.getForm();
80     for (FormField field : form.getFields())
81     {
82       if (field.getType() == FieldType.FILE)
83       {
84         FormatAdapter fa = new FormatAdapter();
85         fa.setNewlineString("\r\n");
86         FileField fileField = (FileField) field;
87         FileFormat format;
88         switch (fileField.getMediaType())
89         {
90         case "application/pfam":
91           format = FileFormat.Pfam;
92           break;
93         case "application/stockholm":
94           format = FileFormat.Stockholm;
95           break;
96         default:
97         case "application/fasta":
98           format = FileFormat.Fasta;
99           break;
100         }
101         InputStream stream = new ByteArrayInputStream(
102             fa.formatSequences(format, sequences.toArray(new SequenceI[0]))
103                 .getBytes());
104         RemoteFile rf = client.uploadFile(stream, "input",
105             fileField.getMediaType());
106         form.insert(field.getName(), rf);
107       }
108     }
109     if (args != null)
110     {
111       for (ArgumentI arg : args)
112       {
113         // multiple choice field names are name$number to avoid duplications
114         // the number is stripped here
115         String fieldName = arg.getName().split("\\$", 2)[0];
116         FormField field = form.getField(fieldName);
117         if (field.getType() == FieldType.BOOLEAN)
118         {
119           form.insert(fieldName,
120                   (arg.getValue() != null && !arg.getValue().isBlank())
121                           ? true
122                           : false);
123         }
124         else
125         {
126           form.insert(fieldName, arg.getValue());
127         }
128       }
129     }
130     return new JobId(service.getName(), service.getName(), form.submit());
131   }
132
133   @Override
134   public final void updateStatus(WsJob job)
135   {
136     try
137     {
138       job.setState(stateMap.get(client.getJobState(job.getJobId())));
139     } catch (IOException e)
140     {
141       throw new IOError(e);
142     }
143   }
144
145   @Override
146   public final boolean updateJobProgress(WsJob job) throws IOException
147   {
148     List<RemoteFile> files = client.getJobResults(job.getJobId());
149     RemoteFile logFile=null;
150     for (RemoteFile f : files)
151     {
152       if (f.getLabel().equals("log"))
153       {
154         logFile = f; break;
155       }
156     }
157
158     boolean newContent = false;
159     if (logFile!=null)
160     {
161       ByteArrayOutputStream output = new ByteArrayOutputStream();
162       logFile.writeTo(output);
163       if (output.size() > job.getNextChunk())
164       {
165         newContent = true;
166         job.setStatus(output.toString("UTF-8"));
167         job.setnextChunk(output.size());
168       }
169     }
170     if (failedStates.contains(job.getJobState()))
171     {
172       
173       RemoteFile errLogFile = null;
174       for (RemoteFile f : files)
175       {
176         if (f.getLabel().equals("error-log"))
177         {
178           errLogFile = f;
179           break;
180         }
181       }
182
183       if (errLogFile!=null)
184       {
185         ByteArrayOutputStream output = new ByteArrayOutputStream();
186         errLogFile.writeTo(output);
187         if (output.size() > 0)
188         {
189           newContent = true;
190           job.setStatus(job.getStatus() + "\n" + output.toString("UTF-8"));
191         }
192       }
193     }
194     return newContent;
195   }
196
197   @Override
198   public final boolean handleSubmitError(Throwable _lex, WsJob j, WebserviceInfo wsInfo)
199   {
200     if (_lex instanceof FormValidationException)
201     {
202       FormValidationException formError = (FormValidationException) _lex;
203       String[] messages = new String[formError.getErrors().size()];
204       int i = 0;
205       for (ValidationException e : formError.getErrors())
206       {
207         messages[i++] = String.format("%s: %s,", e.getField().getName(), e.getMessage());
208       }
209       j.setState(WsJob.JobState.INVALID);
210       j.setStatus(String.join(", ", messages));
211       return true;
212     }
213     return false;
214   }
215
216   @Override
217   public final boolean handleCollectionException(Exception e, WsJob msjob, WebserviceInfo wsInfo)
218   {
219     // TODO
220     return false;
221   }
222
223   final SlivkaService getService()
224   {
225     return service;
226   }
227
228   @Override
229   public final Object getEndpoint()
230   {
231     return this;
232   }
233
234   @Override
235   public final void initParamStore(ParamManager userParameterStore)
236   {
237     if (store == null)
238     {
239       try
240       {
241         store = new SlivkaDatastore(service);
242       } catch (IOException e)
243       {
244         throw new IOError(e);
245       }
246     }
247   }
248
249   @Override
250   public boolean hasParameters()
251   {
252     return true;
253   }
254
255   @Override
256   public final ParamDatastoreI getParamStore()
257   {
258     if (store == null)
259     {
260       initParamStore(null);
261     }
262     return store;
263   }
264   
265   public static AlignmentI readAlignment(RemoteFile f) throws IOException
266   {
267     final var mimetype = f.getMimeType();
268     FileFormat format;
269     if (mimetype.equals("application/clustal"))
270       format = FileFormat.Clustal;
271     else if (mimetype.equals("application/fasta"))
272       format = FileFormat.Fasta;
273     else
274       return null;
275     return new FormatAdapter().readFile(f.getURL().toString(),
276         DataSourceType.URL, format);
277   }
278
279 }