| 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.Set; |
| 13 |
|
|
| 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; |
| 30 |
|
|
| 31 |
|
import java.util.Collection; |
| 32 |
|
import uk.ac.dundee.compbio.slivkaclient.Job; |
| 33 |
|
import uk.ac.dundee.compbio.slivkaclient.RequestValues; |
| 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; |
| 38 |
|
|
| |
|
| 0% |
Uncovered Elements: 143 (143) |
Complexity: 34 |
Complexity Density: 0.35 |
|
| 39 |
|
public abstract class SlivkaWSInstance extends ServiceWithParameters |
| 40 |
|
implements JalviewServiceEndpointProviderI, JalviewWebServiceI |
| 41 |
|
{ |
| 42 |
|
protected final SlivkaClient client; |
| 43 |
|
|
| 44 |
|
protected final SlivkaService service; |
| 45 |
|
|
| 46 |
|
protected SlivkaDatastore store = null; |
| 47 |
|
|
| 48 |
|
protected static final EnumMap<Job.Status, WsJob.JobState> stateMap = new EnumMap<>(Job.Status.class); |
| |
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
| 49 |
0 |
{... |
| 50 |
0 |
stateMap.put(Job.Status.PENDING, WsJob.JobState.QUEUED); |
| 51 |
0 |
stateMap.put(Job.Status.REJECTED, WsJob.JobState.INVALID); |
| 52 |
0 |
stateMap.put(Job.Status.ACCEPTED, WsJob.JobState.QUEUED); |
| 53 |
0 |
stateMap.put(Job.Status.QUEUED, WsJob.JobState.QUEUED); |
| 54 |
0 |
stateMap.put(Job.Status.RUNNING, WsJob.JobState.RUNNING); |
| 55 |
0 |
stateMap.put(Job.Status.COMPLETED, WsJob.JobState.FINISHED); |
| 56 |
0 |
stateMap.put(Job.Status.INTERRUPTED, WsJob.JobState.CANCELLED); |
| 57 |
0 |
stateMap.put(Job.Status.DELETED, WsJob.JobState.CANCELLED); |
| 58 |
0 |
stateMap.put(Job.Status.FAILED, WsJob.JobState.FAILED); |
| 59 |
0 |
stateMap.put(Job.Status.ERROR, WsJob.JobState.SERVERERROR); |
| 60 |
0 |
stateMap.put(Job.Status.UNKNOWN, WsJob.JobState.UNKNOWN); |
| 61 |
|
} |
| 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 |
| 65 |
|
)); |
| 66 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 67 |
0 |
public SlivkaWSInstance(SlivkaClient client, SlivkaService service, String action)... |
| 68 |
|
{ |
| 69 |
0 |
super(action, action, service.getName(), "Slivka", client.getUrl().toString()); |
| 70 |
0 |
this.client = client; |
| 71 |
0 |
this.service = service; |
| 72 |
|
} |
| 73 |
|
|
| |
|
| 0% |
Uncovered Elements: 40 (40) |
Complexity: 9 |
Complexity Density: 0.28 |
|
| 74 |
0 |
protected final JobId submit(List<SequenceI> sequences,... |
| 75 |
|
WsParamSetI preset, List<ArgumentI> args) throws Throwable |
| 76 |
|
{ |
| 77 |
0 |
var parameters = service.getParameters(); |
| 78 |
0 |
var request = new RequestValues(); |
| 79 |
0 |
for (Parameter param : parameters) |
| 80 |
|
{ |
| 81 |
0 |
if (param instanceof Parameter.FileParameter) |
| 82 |
|
{ |
| 83 |
0 |
FormatAdapter fa = new FormatAdapter(); |
| 84 |
0 |
fa.setNewlineString("\r\n"); |
| 85 |
0 |
Parameter.FileParameter fileParam = (Parameter.FileParameter) param; |
| 86 |
0 |
FileFormat format; |
| 87 |
0 |
switch (fileParam.getMediaType()) |
| 88 |
|
{ |
| 89 |
0 |
case "application/pfam": |
| 90 |
0 |
format = FileFormat.Pfam; |
| 91 |
0 |
break; |
| 92 |
0 |
case "application/stockholm": |
| 93 |
0 |
format = FileFormat.Stockholm; |
| 94 |
0 |
break; |
| 95 |
0 |
default: |
| 96 |
0 |
case "application/fasta": |
| 97 |
0 |
format = FileFormat.Fasta; |
| 98 |
0 |
break; |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
0 |
InputStream stream = new ByteArrayInputStream(format.getWriter(null) |
| 104 |
|
.print(sequences.toArray(new SequenceI[0]), false) |
| 105 |
|
.getBytes()); |
| 106 |
0 |
request.addFile(param.getId(), stream); |
| 107 |
|
} |
| 108 |
|
} |
| 109 |
0 |
if (args != null) |
| 110 |
|
{ |
| 111 |
0 |
for (ArgumentI arg : args) |
| 112 |
|
{ |
| 113 |
|
|
| 114 |
|
|
| 115 |
0 |
String paramId = arg.getName().split("\\$", 2)[0]; |
| 116 |
0 |
Parameter param = service.getParameter(paramId); |
| 117 |
0 |
if (param instanceof Parameter.FlagParameter) { |
| 118 |
0 |
if (arg.getValue() != null && !arg.getValue().isBlank()) |
| 119 |
0 |
request.addData(paramId, true); |
| 120 |
|
else |
| 121 |
0 |
request.addData(paramId, false); |
| 122 |
|
} |
| 123 |
|
else |
| 124 |
|
{ |
| 125 |
0 |
request.addData(paramId, arg.getValue()); |
| 126 |
|
} |
| 127 |
|
} |
| 128 |
|
} |
| 129 |
0 |
var jobId = client.submitJob(service, request); |
| 130 |
0 |
return new JobId(service.getName(), service.getName(), jobId); |
| 131 |
|
} |
| 132 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 133 |
0 |
@Override... |
| 134 |
|
public final void updateStatus(WsJob job) |
| 135 |
|
{ |
| 136 |
0 |
try |
| 137 |
|
{ |
| 138 |
0 |
job.setState(stateMap.get(client.fetchJobStatus(job.getJobId()))); |
| 139 |
|
} catch (IOException e) |
| 140 |
|
{ |
| 141 |
0 |
throw new IOError(e); |
| 142 |
|
} |
| 143 |
|
} |
| 144 |
|
|
| |
|
| 0% |
Uncovered Elements: 41 (41) |
Complexity: 8 |
Complexity Density: 0.3 |
|
| 145 |
0 |
@Override... |
| 146 |
|
public final boolean updateJobProgress(WsJob job) throws IOException |
| 147 |
|
{ |
| 148 |
0 |
Collection<RemoteFile> files = client.fetchFilesList(job.getJobId()); |
| 149 |
0 |
RemoteFile logFile=null; |
| 150 |
0 |
for (RemoteFile f : files) |
| 151 |
|
{ |
| 152 |
0 |
if (f.getLabel().equals("log")) |
| 153 |
|
{ |
| 154 |
0 |
logFile = f; break; |
| 155 |
|
} |
| 156 |
|
} |
| 157 |
|
|
| 158 |
0 |
boolean newContent = false; |
| 159 |
0 |
if (logFile!=null) |
| 160 |
|
{ |
| 161 |
0 |
ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 162 |
0 |
client.writeFileTo(logFile, output); |
| 163 |
0 |
if (output.size() > job.getNextChunk()) |
| 164 |
|
{ |
| 165 |
0 |
newContent = true; |
| 166 |
0 |
job.setStatus(output.toString("UTF-8")); |
| 167 |
0 |
job.setnextChunk(output.size()); |
| 168 |
|
} |
| 169 |
|
} |
| 170 |
0 |
if (failedStates.contains(job.getJobState())) |
| 171 |
|
{ |
| 172 |
|
|
| 173 |
0 |
RemoteFile errLogFile = null; |
| 174 |
0 |
for (RemoteFile f : files) |
| 175 |
|
{ |
| 176 |
0 |
if (f.getLabel().equals("error-log")) |
| 177 |
|
{ |
| 178 |
0 |
errLogFile = f; |
| 179 |
0 |
break; |
| 180 |
|
} |
| 181 |
|
} |
| 182 |
|
|
| 183 |
0 |
if (errLogFile!=null) |
| 184 |
|
{ |
| 185 |
0 |
ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 186 |
0 |
client.writeFileTo(errLogFile, output); |
| 187 |
0 |
if (output.size() > 0) |
| 188 |
|
{ |
| 189 |
0 |
newContent = true; |
| 190 |
0 |
job.setStatus(job.getStatus() + "\n" + output.toString("UTF-8")); |
| 191 |
|
} |
| 192 |
|
} |
| 193 |
|
} |
| 194 |
0 |
return newContent; |
| 195 |
|
} |
| 196 |
|
|
| |
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 197 |
0 |
@Override... |
| 198 |
|
public final boolean handleSubmitError(Throwable _lex, WsJob j, WebserviceInfo wsInfo) |
| 199 |
|
{ |
| 200 |
0 |
if (_lex instanceof ClientProtocolException) |
| 201 |
|
{ |
| 202 |
0 |
j.setState(WsJob.JobState.INVALID); |
| 203 |
0 |
j.setStatus(_lex.getMessage()); |
| 204 |
0 |
return true; |
| 205 |
|
} |
| 206 |
0 |
return false; |
| 207 |
|
} |
| 208 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 209 |
0 |
@Override... |
| 210 |
|
public final boolean handleCollectionException(Exception e, WsJob msjob, WebserviceInfo wsInfo) |
| 211 |
|
{ |
| 212 |
|
|
| 213 |
0 |
return false; |
| 214 |
|
} |
| 215 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 216 |
0 |
final SlivkaService getService()... |
| 217 |
|
{ |
| 218 |
0 |
return service; |
| 219 |
|
} |
| 220 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 221 |
0 |
@Override... |
| 222 |
|
public final Object getEndpoint() |
| 223 |
|
{ |
| 224 |
0 |
return this; |
| 225 |
|
} |
| 226 |
|
|
| |
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 227 |
0 |
@Override... |
| 228 |
|
public final void initParamStore(ParamManager userParameterStore) |
| 229 |
|
{ |
| 230 |
0 |
if (store == null) |
| 231 |
|
{ |
| 232 |
0 |
store = new SlivkaDatastore(service); |
| 233 |
|
} |
| 234 |
|
} |
| 235 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 236 |
0 |
@Override... |
| 237 |
|
public boolean hasParameters() |
| 238 |
|
{ |
| 239 |
0 |
return true; |
| 240 |
|
} |
| 241 |
|
|
| |
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 242 |
0 |
@Override... |
| 243 |
|
public final ParamDatastoreI getParamStore() |
| 244 |
|
{ |
| 245 |
0 |
if (store == null) |
| 246 |
|
{ |
| 247 |
0 |
initParamStore(null); |
| 248 |
|
} |
| 249 |
0 |
return store; |
| 250 |
|
} |
| 251 |
|
|
| |
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
| 252 |
0 |
public static AlignmentI readAlignment(RemoteFile f) throws IOException... |
| 253 |
|
{ |
| 254 |
0 |
final var mimetype = f.getMediaType(); |
| 255 |
0 |
FileFormat format; |
| 256 |
0 |
if (mimetype.equals("application/clustal")) |
| 257 |
0 |
format = FileFormat.Clustal; |
| 258 |
0 |
else if (mimetype.equals("application/fasta")) |
| 259 |
0 |
format = FileFormat.Fasta; |
| 260 |
|
else |
| 261 |
0 |
return null; |
| 262 |
0 |
return new FormatAdapter().readFile(f.getContentUrl().toString(), |
| 263 |
|
DataSourceType.URL, format); |
| 264 |
|
} |
| 265 |
|
|
| 266 |
|
} |