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.AnnotatedCollectionI;
10 import jalview.datamodel.SequenceI;
11 import jalview.gui.AlignFrame;
12 import jalview.gui.IProgressIndicator;
13 import jalview.workers.AlignCalcWorker;
14 import jalview.ws.jws2.dm.AAConSettings;
15 import jalview.ws.jws2.dm.JabaWsParamSet;
16 import jalview.ws.jws2.jabaws2.Jws2Instance;
17 import jalview.ws.params.WsParamSetI;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
24 import compbio.data.sequence.FastaSequence;
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;
32 public abstract class AbstractJabaCalcWorker extends AlignCalcWorker
35 protected Jws2Instance service;
37 protected WsParamSetI preset;
39 protected List<Argument> arguments;
41 protected IProgressIndicator guiProgress;
43 protected boolean submitGaps = true;
46 * Recover any existing parameters for this service
48 protected void initViewportParams()
50 if (getCalcId() != null)
52 ((jalview.gui.AlignViewport) alignViewport).setCalcIdSettingsFor(
54 new AAConSettings(true, service, this.preset,
55 (arguments != null) ? JabaParamStore
56 .getJwsArgsfromJaba(arguments) : null), true);
62 * @return null or a string used to recover all annotation generated by this
65 public abstract String getCalcId();
67 public WsParamSetI getPreset()
72 public List<Argument> getArguments()
78 * reconfigure and restart the AAConClient. This method will spawn a new
79 * thread that will wait until any current jobs are finished, modify the
80 * parameters and restart the conservation calculation with the new values.
85 public void updateParameters(final WsParamSetI newpreset,
86 final List<Argument> newarguments)
89 arguments = newarguments;
90 calcMan.startWorker(this);
94 public List<Option> getJabaArguments()
96 List<Option> newargs = new ArrayList<Option>();
97 if (preset != null && preset instanceof JabaWsParamSet)
99 newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
101 if (arguments != null && arguments.size() > 0)
103 for (Argument rg : arguments)
105 if (Option.class.isAssignableFrom(rg.getClass()))
107 newargs.add((Option) rg);
114 protected boolean alignedSeqs = true;
116 protected boolean nucleotidesAllowed = false;
118 protected boolean proteinAllowed = false;
121 * record sequences for mapping result back to afterwards
123 protected boolean bySequence = false;
125 protected Map<String, SequenceI> seqNames;
127 protected boolean[] gapMap;
135 public AbstractJabaCalcWorker(AlignViewportI alignViewport,
136 AlignmentViewPanel alignPanel)
138 super(alignViewport, alignPanel);
141 public AbstractJabaCalcWorker(Jws2Instance service,
142 AlignFrame alignFrame, WsParamSetI preset, List<Argument> paramset)
144 this(alignFrame.getCurrentView(), alignFrame.alignPanel);
145 this.guiProgress = alignFrame;
146 this.preset = preset;
147 this.arguments = paramset;
148 this.service = service;
153 * @return true if the submission thread should attempt to submit data
155 abstract boolean hasService();
157 volatile String rslt = "JOB NOT DEFINED";
166 long progressId = -1;
168 int serverErrorsLeft = 3;
170 StringBuffer msg = new StringBuffer();
177 List<compbio.data.sequence.FastaSequence> seqs = getInputSequences(
178 alignViewport.getAlignment(),
179 bySequence ? alignViewport.getSelectionGroup() : null);
181 if (seqs == null || !checkValidInputSeqs(true, seqs))
183 calcMan.workerComplete(this);
187 AlignmentAnnotation[] aa = alignViewport.getAlignment()
188 .getAlignmentAnnotation();
189 if (guiProgress != null)
191 guiProgress.setProgressBar("JABA " + getServiceActionText(),
192 progressId = System.currentTimeMillis());
194 rslt = submitToService(seqs);
196 boolean finished = false;
200 JobStatus status = getJobStatus(rslt);
201 if (status.equals(JobStatus.FINISHED))
205 if (calcMan.isPending(this) && isInteractiveUpdate())
208 // cancel this job and yield to the new job
213 System.err.println("Cancelled AACon job: " + rslt);
217 System.err.println("FAILED TO CANCEL AACon job: " + rslt);
220 } catch (Exception x)
224 rslt = "CANCELLED JOB";
228 ChunkHolder stats = null;
232 boolean retry = false;
237 stats = pullExecStatistics(rslt, rpos);
238 } catch (Exception x)
241 if (x.getMessage().contains(
242 "Position in a file could not be negative!"))
244 // squash index out of bounds exception- seems to happen for
245 // disorder predictors which don't (apparently) produce any
246 // progress information and JABA server throws an exception
247 // because progress length is -1.
252 if (--serverErrorsLeft > 0)
258 } catch (InterruptedException q)
272 System.out.print(stats.getChunk());
274 rpos = stats.getNextPosition();
276 } while (stats != null && rpos > cpos);
278 if (!finished && status.equals(JobStatus.FAILED))
283 } catch (InterruptedException x)
289 if (serverErrorsLeft > 0)
294 } catch (InterruptedException x)
297 if (collectAnnotationResultsFor(rslt))
299 jalview.bin.Cache.log
300 .debug("Updating result annotation from Job " + rslt
301 + " at " + service.getUri());
302 updateResultAnnotation(true);
303 ap.adjustAnnotationHeight();
308 catch (JobSubmissionException x)
311 System.err.println("submission error with " + getServiceActionText()
314 calcMan.workerCannotRun(this);
315 } catch (ResultNotAvailableException x)
317 System.err.println("collection error:\nJob ID: " + rslt);
319 calcMan.workerCannotRun(this);
321 } catch (OutOfMemoryError error)
323 calcMan.workerCannotRun(this);
326 // hconsensus = null;
327 ap.raiseOOMWarning(getServiceActionText(), error);
328 } catch (Exception x)
330 calcMan.workerCannotRun(this);
333 // hconsensus = null;
335 .println("Blacklisting worker due to unexpected exception:");
340 calcMan.workerComplete(this);
343 calcMan.workerComplete(this);
344 if (guiProgress != null && progressId != -1)
346 guiProgress.setProgressBar("", progressId);
348 ap.paintAlignment(true);
350 if (msg.length() > 0)
352 // TODO: stash message somewhere in annotation or alignment view.
353 // code below shows result in a text box popup
355 * jalview.gui.CutAndPasteTransfer cap = new
356 * jalview.gui.CutAndPasteTransfer(); cap.setText(msg.toString());
357 * jalview.gui.Desktop.addInternalFrame(cap,
358 * "Job Status for "+getServiceActionText(), 600, 400);
366 * validate input for dynamic/non-dynamic update context
369 * @return true if input is valid
371 abstract boolean checkValidInputSeqs(boolean dynamic, List<FastaSequence> seqs);
373 abstract String submitToService(
374 List<compbio.data.sequence.FastaSequence> seqs)
375 throws JobSubmissionException;
377 abstract boolean cancelJob(String rslt) throws Exception;
379 abstract JobStatus getJobStatus(String rslt) throws Exception;
381 abstract ChunkHolder pullExecStatistics(String rslt, long rpos);
383 abstract boolean collectAnnotationResultsFor(String rslt)
384 throws ResultNotAvailableException;
386 public void cancelCurrentJob()
393 System.err.println("Cancelled job "+id);
397 System.err.println("Job "+id+" couldn't be cancelled.");
399 } catch (Exception q)
406 * Interactive updating. Analysis calculations that work on the currently
407 * displayed alignment data should cancel existing jobs when the input data
410 * @return true if a running job should be cancelled because new input data is
411 * available for analysis
413 abstract boolean isInteractiveUpdate();
415 public List<FastaSequence> getInputSequences(AlignmentI alignment,
416 AnnotatedCollectionI inputSeqs)
418 if (alignment == null || alignment.getWidth() <= 0
419 || alignment.getSequences() == null || alignment.isNucleotide() ? !nucleotidesAllowed
424 if (inputSeqs == null || inputSeqs.getWidth() <= 0
425 || inputSeqs.getSequences() == null
426 || inputSeqs.getSequences().size() < 1)
428 inputSeqs = alignment;
431 List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<compbio.data.sequence.FastaSequence>();
437 seqNames = new HashMap<String, SequenceI>();
439 gapMap = new boolean[0];
440 start = inputSeqs.getStartRes();
441 end = inputSeqs.getEndRes();
443 for (SequenceI sq : ((List<SequenceI>) inputSeqs.getSequences()))
445 if (bySequence ? sq.findPosition(end + 1)
446 - sq.findPosition(start + 1) > minlen - 1 : sq.getEnd()
447 - sq.getStart() > minlen - 1)
449 String newname = SeqsetUtils.unique_name(seqs.size() + 1);
450 // make new input sequence with or without gaps
451 if (seqNames != null)
453 seqNames.put(newname, sq);
458 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
459 sq.getSequenceAsString()));
460 if (gapMap == null || gapMap.length < seq.getSequence().length())
462 boolean[] tg = gapMap;
463 gapMap = new boolean[seq.getLength()];
464 System.arraycopy(tg, 0, gapMap, 0, tg.length);
465 for (int p = tg.length; p < gapMap.length; p++)
467 gapMap[p] = false; // init as a gap
470 for (int apos : sq.gapMap())
472 gapMap[apos] = true; // aligned.
477 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
478 AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
479 sq.getSequenceAsString(start, end + 1))));
481 if (seq.getSequence().length() > ln)
483 ln = seq.getSequence().length();
487 if (alignedSeqs && submitGaps)
490 for (int i = 0; i < gapMap.length; i++)
497 // try real hard to return something submittable
498 // TODO: some of AAcon measures need a minimum of two or three amino
499 // acids at each position, and AAcon doesn't gracefully degrade.
500 for (int p = 0; p < seqs.size(); p++)
502 FastaSequence sq = seqs.get(p);
503 int l = sq.getSequence().length();
504 // strip gapped columns
505 char[] padded = new char[realw], orig = sq.getSequence()
507 for (int i = 0, pp = 0; i < realw; pp++)
511 if (orig.length > pp)
513 padded[i++] = orig[pp];
521 seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(),
522 new String(padded)));
529 public void updateAnnotation()
531 updateResultAnnotation(false);
534 public abstract void updateResultAnnotation(boolean immediate);
536 public abstract String getServiceActionText();
539 * notify manager that we have started, and wait for a free calculation slot
541 * @return true if slot is obtained and work still valid, false if another
542 * thread has done our work for us.
544 protected boolean checkDone()
546 calcMan.notifyStart(this);
547 ap.paintAlignment(false);
548 while (!calcMan.notifyWorking(this))
550 if (calcMan.isWorking(this))
558 ap.paintAlignment(false);
562 } catch (Exception ex)
564 ex.printStackTrace();
567 if (alignViewport.isClosed())
575 protected void updateOurAnnots(List<AlignmentAnnotation> ourAnnot)
577 List<AlignmentAnnotation> our = ourAnnots;
578 ourAnnots = ourAnnot;
579 AlignmentI alignment = alignViewport.getAlignment();
584 for (AlignmentAnnotation an : our)
586 if (!ourAnnots.contains(an))
588 // remove the old annotation
589 alignment.deleteAnnotation(an);
595 ap.adjustAnnotationHeight();