2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.rest;
23 import jalview.analysis.SeqsetUtils.SequenceInfo;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.AlignmentOrder;
27 import jalview.datamodel.SequenceGroup;
28 import jalview.datamodel.SequenceI;
29 import jalview.io.packed.JalviewDataset;
30 import jalview.ws.AWsJob;
31 import jalview.ws.rest.params.Alignment;
32 import jalview.ws.rest.params.SeqGroupIndexVector;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.Hashtable;
39 import java.util.Vector;
41 public class RestJob extends AWsJob
44 // TODO: input alignmentview and other data for this job
45 RestServiceDescription rsd;
56 Map<String, SequenceInfo> squniq;
59 * dataset associated with this input data.
63 AlignmentOrder inputOrder;
66 * context of input data with respect to an AlignmentView's visible contigs.
70 private AlignmentI contextAl = null;
73 * create a rest job using data bounded by the given start/end column.
76 * @param restJobThread
79 * visible contigs of an alignment view from which _input was derived
81 public RestJob(int jobNum, RestJobThread restJobThread, AlignmentI _input,
84 rsd = restJobThread.restClient.service;
86 if (viscontigs != null)
88 origviscontig = new int[viscontigs.length];
89 System.arraycopy(viscontigs, 0, origviscontig, 0, viscontigs.length);
91 // get sequences for the alignmentI
92 // get groups trimmed to alignment columns
93 // get any annotation trimmed to start/end columns, too.
94 squniq = jalview.analysis.SeqsetUtils
95 .uniquify(_input.getSequencesArray(), true);
97 // form alignment+groups+annotation,preprocess and then record references
99 ArrayList<InputType> alinp = new ArrayList<InputType>();
100 int paramsWithData = 0;
101 // TODO: JAL-715 - generalise the following validation logic for all
103 // we cheat for moment - since we know a-priori what data is available and
104 // what inputs we have implemented so far
105 for (Map.Entry<String, InputType> prm : rsd.inputParams.entrySet())
107 if (!prm.getValue().isConstant())
109 if (prm.getValue() instanceof Alignment)
111 alinp.add(prm.getValue());
115 if (prm.getValue() instanceof SeqGroupIndexVector
116 && _input.getGroups() != null
117 && _input.getGroups().size() >= -1 + prm.getValue().min)
119 // the test above is not rigorous but fixes JAL-1298, since
120 // submission will fail if the partition set doesn't contain at
121 // least one partition
122 alinp.add(prm.getValue());
126 statMessage = ("Not enough groups defined on the alignment - need at least "
127 + prm.getValue().min);
136 if ((paramsWithData + alinp.size()) == rsd.inputParams.size())
138 inputOrder = new AlignmentOrder(_input);
139 if ((dsForIO = _input.getDataset()) == null)
141 _input.setDataset(null);
143 dsForIO = _input.getDataset();
144 if (contextAl == null)
148 setAlignmentForInputs(alinp, _input);
153 // not enough data, so we bail.
158 boolean validInput = false;
161 public boolean hasResults()
163 return gotresult && (parsedResults ? validJvresults : true);
167 public boolean hasValidInput()
173 public boolean isRunning()
175 return running; // TODO: can we check the response body for status messages
180 public boolean isQueued()
186 public boolean isFinished()
188 return resSet != null;
192 public boolean isFailed()
194 // TODO logic for error
199 public boolean isBroken()
201 // TODO logic for error
206 public boolean isServerError()
208 // TODO logic for error
213 public boolean hasStatus()
215 return statMessage != null;
218 protected String statMessage = null;
220 public HttpResultSet resSet;
223 public String getStatus()
229 public boolean hasResponse()
231 return statMessage != null || resSet != null;
235 public void clearResponse()
237 // only clear the transient server response
244 * @see jalview.ws.AWsJob#getState()
247 public String getState()
249 // TODO generate state string - prolly should have a default abstract method
251 return "Job is clueless";
254 public String getPostUrl()
257 // TODO Auto-generated method stub
261 public Set<Map.Entry<String, InputType>> getInputParams()
263 return rsd.inputParams.entrySet();
266 // return the URL that should be polled for this job
267 public String getPollUrl()
269 return rsd.getDecoratedResultUrl(jobId);
274 * @return the context for parsing results from service
276 public JalviewDataset newJalviewDataset()
280 context = new JalviewDataset(dsForIO, null, squniq, null);
281 if (contextAl != null)
283 // TODO devise way of merging new annotation onto (identical) existing
284 // annotation that was used as input
285 // delete all input annotation
286 if (contextAl.getAlignmentAnnotation() != null)
288 for (AlignmentAnnotation alan : contextAl
289 .getAlignmentAnnotation())
291 contextAl.deleteAnnotation(alan);
294 // TODO devise way of merging new groups onto (identical) existing
295 // groups when they were used as input to service
296 // delete all existing groups
297 if (contextAl.getGroups() != null)
299 contextAl.deleteAllGroups();
301 context.addAlignment(contextAl);
309 * Extract list of sequence IDs for input parameter 'token' with given
316 public SequenceI[] getSequencesForInput(String token,
317 InputType.molType type) throws NoValidInputDataException
319 Object sgdat = inputData.get(token);
320 // can we form an alignment from this data ?
323 throw new NoValidInputDataException(
324 "No Sequence vector data bound to input '" + token
325 + "' for service at " + rsd.postUrl);
327 if (sgdat instanceof AlignmentI)
329 return ((AlignmentI) sgdat).getSequencesArray();
331 if (sgdat instanceof SequenceGroup)
333 return ((SequenceGroup) sgdat).getSequencesAsArray(null);
335 if (sgdat instanceof Vector)
337 if (((Vector) sgdat).size() > 0
338 && ((Vector) sgdat).get(0) instanceof SequenceI)
340 SequenceI[] sq = new SequenceI[((Vector) sgdat).size()];
341 ((Vector) sgdat).copyInto(sq);
345 throw new NoValidInputDataException(
346 "No Sequence vector data bound to input '" + token
347 + "' for service at " + rsd.postUrl);
351 * binding between input data (AlignmentI, SequenceGroup, NJTree) and input
354 private Hashtable<String, Object> inputData = new Hashtable<String, Object>();
357 * is the job fully submitted to server and apparently in progress ?
359 public boolean running = false;
365 * - reference to object to be stored as input. Note - input data may
366 * be modifed by formatter
368 public void setAlignmentForInputs(Collection<InputType> itypes,
371 for (InputType itype : itypes)
373 if (!rsd.inputParams.values().contains(itype))
375 throw new IllegalArgumentException("InputType " + itype.getClass()
376 + " is not valid for service at " + rsd.postUrl);
378 if (itype instanceof AlignmentProcessor)
380 ((AlignmentProcessor) itype).prepareAlignment(al);
382 // stash a reference for recall when the alignment data is formatted
383 inputData.put(itype.token, al);
392 * @return alignment object bound to the given token
393 * @throws NoValidInputDataException
395 public AlignmentI getAlignmentForInput(String token,
396 InputType.molType type) throws NoValidInputDataException
398 Object al = inputData.get(token);
399 // can we form an alignment from this data ?
400 if (al == null || !(al instanceof AlignmentI))
402 throw new NoValidInputDataException(
403 "No alignment data bound to input '" + token
404 + "' for service at " + rsd.postUrl);
406 return (AlignmentI) al;
410 * test to see if the job has data of type cl that's needed for the job to run
413 * @return true or false
415 public boolean hasDataOfType(Class cl)
417 if (AlignmentI.class.isAssignableFrom(cl))
421 // TODO: add more source data types
427 * context used to parse results from service
429 JalviewDataset context = null;
431 protected boolean parsedResults = false;
433 protected boolean validJvresults = false;
435 Object[] jvresultobj = null;
438 * process the results obtained from the server into jalview datamodel objects
439 * ready to be merged/added to the users' view. Use hasResults to test if
440 * results were added to context.
442 public void parseResultSet() throws Exception, Error
446 parsedResults = true;
447 jvresultobj = resSet.parseResultSet();
448 validJvresults = true;
454 * @return true if job has an input alignment and it was annotated when
455 * results were parsed
457 public boolean isInputContextModified()
459 return contextAl != null && validJvresults
460 && context.getAl().get(0).isModified();
465 * @return true if the ID/metadata for the input sequences were saved and
466 * sequence IDs renamed.
468 public boolean isInputUniquified()
470 // TODO Auto-generated method stub
475 * Return map between ordering of alignment submitted as input, and ordering
476 * of alignment as provided by user
478 * @return int[sequence index in submitted data]==sequence index in input.
480 public int[] getOrderMap()
482 SequenceI[] contseq = contextAl.getSequencesArray();
483 int map[] = new int[contseq.length];
484 for (int i = 0; i < contseq.length; i++)
486 // TODO: optimise for large N - build a lookup hash for IDs returning
487 // order, and then lookup each sequ's original order
488 map[i] = inputOrder.getOrder().indexOf(contseq[i]);