1 package jalview.ws.jws2;
3 import jalview.analysis.AlignSeq;
4 import jalview.analysis.SeqsetUtils;
5 import jalview.api.AlignViewportI;
6 import jalview.api.AlignmentViewPanel;
7 import jalview.datamodel.AlignmentAnnotation;
8 import jalview.datamodel.AlignmentI;
9 import jalview.datamodel.SequenceI;
10 import jalview.gui.AlignFrame;
11 import jalview.gui.IProgressIndicator;
12 import jalview.workers.AlignCalcWorker;
13 import jalview.ws.jws2.dm.JabaWsParamSet;
14 import jalview.ws.jws2.jabaws2.Jws2Instance;
15 import jalview.ws.params.WsParamSetI;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
22 import compbio.data.msa.SequenceAnnotation;
23 import compbio.data.sequence.FastaSequence;
24 import compbio.data.sequence.ScoreManager;
25 import compbio.metadata.Argument;
26 import compbio.metadata.ChunkHolder;
27 import compbio.metadata.JobStatus;
28 import compbio.metadata.JobSubmissionException;
29 import compbio.metadata.Option;
30 import compbio.metadata.ResultNotAvailableException;
31 import compbio.metadata.WrongParameterException;
33 public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
36 @SuppressWarnings("unchecked")
37 protected SequenceAnnotation aaservice;
39 protected ScoreManager scoremanager;
41 protected WsParamSetI preset;
43 protected List<Argument> arguments;
45 public JabawsAlignCalcWorker(AlignViewportI alignViewport,
46 AlignmentViewPanel alignPanel)
48 super(alignViewport, alignPanel);
51 IProgressIndicator guiProgress;
53 public JabawsAlignCalcWorker(Jws2Instance service, AlignFrame alignFrame,
54 WsParamSetI preset, List<Argument> paramset)
56 this(alignFrame.getCurrentView(), alignFrame.alignPanel);
57 this.guiProgress = alignFrame;
59 this.arguments = paramset;
60 aaservice = (SequenceAnnotation) service.service;
64 public WsParamSetI getPreset()
69 public List<Argument> getArguments()
75 * reconfigure and restart the AAConsClient. This method will spawn a new
76 * thread that will wait until any current jobs are finished, modify the
77 * parameters and restart the conservation calculation with the new values.
82 public void updateParameters(final WsParamSetI newpreset,
83 final List<Argument> newarguments)
85 if (false) // || calcMan.isWorking(this))
87 new Thread(new Runnable()
96 } catch (InterruptedException x)
100 updateParameters(newpreset, newarguments);
107 arguments = newarguments;
108 calcMan.startWorker(this);
112 public List<Option> getJabaArguments()
114 List<Option> newargs = new ArrayList<Option>();
115 if (preset != null && preset instanceof JabaWsParamSet)
117 newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
119 if (arguments != null && arguments.size() > 0)
121 for (Argument rg : arguments)
123 if (Option.class.isAssignableFrom(rg.getClass()))
125 newargs.add((Option) rg);
135 if (aaservice == null)
139 long progressId = -1;
141 String rslt = "JOB NOT DEFINED";
149 List<compbio.data.sequence.FastaSequence> seqs = getInputSequences(alignViewport
154 calcMan.workerComplete(this);
158 AlignmentAnnotation[] aa = alignViewport.getAlignment()
159 .getAlignmentAnnotation();
160 if (guiProgress != null)
162 guiProgress.setProgressBar("JABA " + getServiceActionText(),
163 progressId = System.currentTimeMillis());
167 rslt = aaservice.analize(seqs);
173 rslt = aaservice.customAnalize(seqs, getJabaArguments());
174 } catch (WrongParameterException x)
176 throw new JobSubmissionException(
177 "Invalid paremeter set. Check Jalview implementation.", x);
181 boolean finished = false;
185 JobStatus status = aaservice.getJobStatus(rslt);
186 if (status.equals(JobStatus.FINISHED))
190 if (calcMan.isPending(this) && this instanceof AAConsClient)
193 // cancel this job and yield to the new job
196 if (aaservice.cancelJob(rslt))
198 System.err.println("Cancelled AACon job: " + rslt);
202 System.err.println("FAILED TO CANCELL AACon job: " + rslt);
205 } catch (Exception x)
219 stats = aaservice.pullExecStatistics(rslt, rpos);
220 } catch (Exception x)
223 if (x.getMessage().contains(
224 "Position in a file could not be negative!"))
226 // squash index out of bounds exception- seems to happen for
227 // disorder predictors which don't (apparently) produce any
228 // progress information and JABA server throws an exception
229 // because progress length is -1.
239 System.out.print(stats.getChunk());
240 rpos = stats.getNextPosition();
242 } while (stats != null && rpos > cpos);
244 if (!finished && status.equals(JobStatus.FAILED))
249 } catch (InterruptedException x)
259 } catch (InterruptedException x)
263 scoremanager = aaservice.getAnnotation(rslt);
264 if (scoremanager != null)
266 updateResultAnnotation(true);
268 } catch (JobSubmissionException x)
271 System.err.println("submission error:");
273 calcMan.workerCannotRun(this);
274 } catch (ResultNotAvailableException x)
276 System.err.println("collection error:\nJob ID: " + rslt);
278 calcMan.workerCannotRun(this);
280 } catch (OutOfMemoryError error)
282 calcMan.workerCannotRun(this);
285 // hconsensus = null;
286 ap.raiseOOMWarning(getServiceActionText(), error);
287 } catch (Exception x)
289 calcMan.workerCannotRun(this);
292 // hconsensus = null;
294 .println("Blacklisting worker due to unexpected exception:");
299 calcMan.workerComplete(this);
302 calcMan.workerComplete(this);
303 if (guiProgress != null)
305 guiProgress.setProgressBar("", progressId);
307 ap.paintAlignment(true);
314 public void updateAnnotation()
316 updateResultAnnotation(false);
319 public abstract void updateResultAnnotation(boolean immediate);
321 public abstract String getServiceActionText();
323 boolean submitGaps = true;
325 boolean alignedSeqs = true;
327 boolean nucleotidesAllowed = false;
329 boolean proteinAllowed = false;
332 * record sequences for mapping result back to afterwards
334 protected boolean bySequence = false;
336 Map<String, SequenceI> seqNames;
338 public List<FastaSequence> getInputSequences(AlignmentI alignment)
341 if (alignment == null || alignment.getWidth() <= 0
342 || alignment.getSequences() == null
343 // || (alignedSeqs && !alignment.isAligned() && !submitGaps)
344 || alignment.isNucleotide() ? !nucleotidesAllowed
349 List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<compbio.data.sequence.FastaSequence>();
355 seqNames = new HashMap<String, SequenceI>();
357 for (SequenceI sq : ((List<SequenceI>) alignment.getSequences()))
360 if (sq.getEnd() - sq.getStart() > minlen - 1)
362 String newname = SeqsetUtils.unique_name(seqs.size() + 1);
363 // make new input sequence with or without gaps
364 if (seqNames != null)
366 seqNames.put(newname, sq);
369 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
370 (submitGaps) ? sq.getSequenceAsString() : AlignSeq
371 .extractGaps(jalview.util.Comparison.GapChars,
372 sq.getSequenceAsString())));
373 if (seq.getSequence().length() > ln)
375 ln = seq.getSequence().length();
379 if (alignedSeqs && submitGaps)
381 // try real hard to return something submittable
382 // TODO: some of AAcons measures need a minimum of two or three amino
383 // acids at each position, and aacons doesn't gracefully degrade.
384 for (int p = 0; p < seqs.size(); p++)
386 FastaSequence sq = seqs.get(p);
387 int l = sq.getSequence().length();
390 char[] padded = new char[ln];
391 System.arraycopy(sq.getSequence().toCharArray(), 0, padded, 0, sq
392 .getSequence().length());
397 seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(),
398 new String(padded)));
407 * notify manager that we have started, and wait for a free calculation slot
409 * @return true if slot is obtained and work still valid, false if another
410 * thread has done our work for us.
414 calcMan.notifyStart(this);
415 ap.paintAlignment(false);
416 while (!calcMan.notifyWorking(this))
418 if (calcMan.isWorking(this))
426 ap.paintAlignment(false);
430 } catch (Exception ex)
432 ex.printStackTrace();
435 if (alignViewport.isClosed())