2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.ws.rest;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Hashtable;
25 import java.util.Vector;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.AlignmentOrder;
30 import jalview.datamodel.SequenceGroup;
31 import jalview.datamodel.SequenceI;
32 import jalview.io.packed.JalviewDataset;
33 import jalview.ws.AWsJob;
34 import jalview.ws.rest.params.Alignment;
35 import jalview.ws.rest.params.SeqGroupIndexVector;
37 public class RestJob extends AWsJob
40 // TODO: input alignmentview and other data for this job
41 RestServiceDescription rsd;
55 * dataset associated with this input data.
59 AlignmentOrder inputOrder;
62 * context of input data with respect to an AlignmentView's visible contigs.
66 private AlignmentI contextAl = null;
69 * create a rest job using data bounded by the given start/end column.
72 * @param restJobThread
75 * visible contigs of an alignment view from which _input was derived
77 public RestJob(int jobNum, RestJobThread restJobThread,
78 AlignmentI _input, int[] viscontigs)
80 rsd = restJobThread.restClient.service;
82 if (viscontigs != null)
84 origviscontig = new int[viscontigs.length];
85 System.arraycopy(viscontigs, 0, origviscontig, 0, viscontigs.length);
87 // get sequences for the alignmentI
88 // get groups trimmed to alignment columns
89 // get any annotation trimmed to start/end columns, too.
90 squniq = jalview.analysis.SeqsetUtils.uniquify(_input.getSequencesArray(), true);
92 // form alignment+groups+annotation,preprocess and then record references
94 ArrayList<InputType> alinp = new ArrayList<InputType>();
95 int paramsWithData = 0;
96 // TODO: JAL-715 - generalise the following validation logic for all parameter types
97 // we cheat for moment - since we know a-priori what data is available and
98 // what inputs we have implemented so far
99 for (Map.Entry<String, InputType> prm : rsd.inputParams.entrySet())
101 if (!prm.getValue().isConstant())
103 if (prm.getValue() instanceof Alignment)
105 alinp.add(prm.getValue());
109 if (prm.getValue() instanceof SeqGroupIndexVector
110 && _input.getGroups() != null
111 && _input.getGroups().size() >= -1 + prm.getValue().min)
113 // the test above is not rigorous but fixes JAL-1298, since submission will fail if the partition set doesn't contain at least one partition
114 alinp.add(prm.getValue());
118 statMessage = ("Not enough groups defined on the alignment - need at least " + prm
128 if ((paramsWithData + alinp.size()) == rsd.inputParams.size())
130 inputOrder = new AlignmentOrder(_input);
131 if ((dsForIO = _input.getDataset()) == null)
133 _input.setDataset(null);
135 dsForIO = _input.getDataset();
136 if (contextAl == null)
140 setAlignmentForInputs(alinp, _input);
145 // not enough data, so we bail.
150 boolean validInput = false;
153 public boolean hasResults()
155 return gotresult && (parsedResults ? validJvresults : true);
159 public boolean hasValidInput()
165 public boolean isRunning()
167 return running; // TODO: can we check the response body for status messages
172 public boolean isQueued()
178 public boolean isFinished()
180 return resSet != null;
184 public boolean isFailed()
186 // TODO logic for error
191 public boolean isBroken()
193 // TODO logic for error
198 public boolean isServerError()
200 // TODO logic for error
205 public boolean hasStatus()
207 return statMessage != null;
210 protected String statMessage = null;
212 public HttpResultSet resSet;
215 public String getStatus()
221 public boolean hasResponse()
223 return statMessage != null || resSet != null;
227 public void clearResponse()
229 // only clear the transient server response
236 * @see jalview.ws.AWsJob#getState()
239 public String getState()
241 // TODO generate state string - prolly should have a default abstract method
243 return "Job is clueless";
246 public String getPostUrl()
249 // TODO Auto-generated method stub
253 public Set<Map.Entry<String, InputType>> getInputParams()
255 return rsd.inputParams.entrySet();
258 // return the URL that should be polled for this job
259 public String getPollUrl()
261 return rsd.getDecoratedResultUrl(jobId);
266 * @return the context for parsing results from service
268 public JalviewDataset newJalviewDataset()
272 context = new JalviewDataset(dsForIO, null, squniq, null);
273 if (contextAl != null)
275 // TODO devise way of merging new annotation onto (identical) existing
276 // annotation that was used as input
277 // delete all input annotation
278 if (contextAl.getAlignmentAnnotation() != null)
280 for (AlignmentAnnotation alan : contextAl
281 .getAlignmentAnnotation())
283 contextAl.deleteAnnotation(alan);
286 // TODO devise way of merging new groups onto (identical) existing
287 // groups when they were used as input to service
288 // delete all existing groups
289 if (contextAl.getGroups() != null)
291 contextAl.deleteAllGroups();
293 context.addAlignment(contextAl);
301 * Extract list of sequence IDs for input parameter 'token' with given
308 public SequenceI[] getSequencesForInput(String token,
309 InputType.molType type) throws NoValidInputDataException
311 Object sgdat = inputData.get(token);
312 // can we form an alignment from this data ?
315 throw new NoValidInputDataException(
316 "No Sequence vector data bound to input '" + token
317 + "' for service at " + rsd.postUrl);
319 if (sgdat instanceof AlignmentI)
321 return ((AlignmentI) sgdat).getSequencesArray();
323 if (sgdat instanceof SequenceGroup)
325 return ((SequenceGroup) sgdat).getSequencesAsArray(null);
327 if (sgdat instanceof Vector)
329 if (((Vector) sgdat).size() > 0
330 && ((Vector) sgdat).get(0) instanceof SequenceI)
332 SequenceI[] sq = new SequenceI[((Vector) sgdat).size()];
333 ((Vector) sgdat).copyInto(sq);
337 throw new NoValidInputDataException(
338 "No Sequence vector data bound to input '" + token
339 + "' for service at " + rsd.postUrl);
343 * binding between input data (AlignmentI, SequenceGroup, NJTree) and input
346 private Hashtable<String, Object> inputData = new Hashtable<String, Object>();
349 * is the job fully submitted to server and apparently in progress ?
351 public boolean running = false;
357 * - reference to object to be stored as input. Note - input data may
358 * be modifed by formatter
360 public void setAlignmentForInputs(Collection<InputType> itypes,
363 for (InputType itype : itypes)
365 if (!rsd.inputParams.values().contains(itype))
367 throw new IllegalArgumentException("InputType " + itype.getClass()
368 + " is not valid for service at " + rsd.postUrl);
370 if (itype instanceof AlignmentProcessor)
372 ((AlignmentProcessor) itype).prepareAlignment(al);
374 // stash a reference for recall when the alignment data is formatted
375 inputData.put(itype.token, al);
384 * @return alignment object bound to the given token
385 * @throws NoValidInputDataException
387 public AlignmentI getAlignmentForInput(String token,
388 InputType.molType type) throws NoValidInputDataException
390 Object al = inputData.get(token);
391 // can we form an alignment from this data ?
392 if (al == null || !(al instanceof AlignmentI))
394 throw new NoValidInputDataException(
395 "No alignment data bound to input '" + token
396 + "' for service at " + rsd.postUrl);
398 return (AlignmentI) al;
402 * test to see if the job has data of type cl that's needed for the job to run
405 * @return true or false
407 public boolean hasDataOfType(Class cl)
409 if (AlignmentI.class.isAssignableFrom(cl))
413 // TODO: add more source data types
419 * context used to parse results from service
421 JalviewDataset context = null;
423 protected boolean parsedResults = false;
425 protected boolean validJvresults = false;
427 Object[] jvresultobj = null;
430 * process the results obtained from the server into jalview datamodel objects
431 * ready to be merged/added to the users' view. Use hasResults to test if
432 * results were added to context.
434 public void parseResultSet() throws Exception, Error
438 parsedResults = true;
439 jvresultobj = resSet.parseResultSet();
440 validJvresults = true;
446 * @return true if job has an input alignment and it was annotated when
447 * results were parsed
449 public boolean isInputContextModified()
451 return contextAl != null && validJvresults
452 && context.getAl().get(0).isModified();
457 * @return true if the ID/metadata for the input sequences were saved and
458 * sequence IDs renamed.
460 public boolean isInputUniquified()
462 // TODO Auto-generated method stub
467 * Return map between ordering of alignment submitted as input, and ordering
468 * of alignment as provided by user
470 * @return int[sequence index in submitted data]==sequence index in input.
472 public int[] getOrderMap()
474 SequenceI[] contseq = contextAl.getSequencesArray();
475 int map[] = new int[contseq.length];
476 for (int i = 0; i < contseq.length; i++)
478 // TODO: optimise for large N - build a lookup hash for IDs returning
479 // order, and then lookup each sequ's original order
480 map[i] = inputOrder.getOrder().indexOf(contseq[i]);