1 package jalview.ws.jws2;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Iterator;
9 import compbio.data.msa.SequenceAnnotation;
10 import compbio.data.sequence.FastaSequence;
11 import compbio.data.sequence.Score;
12 import compbio.data.sequence.ScoreManager;
13 import compbio.metadata.Argument;
14 import compbio.metadata.ChunkHolder;
15 import compbio.metadata.JobStatus;
16 import compbio.metadata.JobSubmissionException;
17 import compbio.metadata.Option;
18 import compbio.metadata.ResultNotAvailableException;
19 import compbio.metadata.WrongParameterException;
20 import jalview.analysis.AlignSeq;
21 import jalview.analysis.SeqsetUtils;
22 import jalview.api.AlignViewportI;
23 import jalview.api.AlignmentViewPanel;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.Annotation;
27 import jalview.datamodel.SequenceI;
28 import jalview.gui.AlignFrame;
29 import jalview.gui.IProgressIndicator;
30 import jalview.workers.AlignCalcWorker;
31 import jalview.ws.jws2.dm.JabaWsParamSet;
32 import jalview.ws.jws2.jabaws2.Jws2Instance;
33 import jalview.ws.params.WsParamSetI;
35 public abstract class JabawsCalcWorker extends AlignCalcWorker
38 protected Jws2Instance service;
39 @SuppressWarnings("unchecked")
40 protected SequenceAnnotation aaservice;
41 protected ScoreManager scoremanager;
42 protected WsParamSetI preset;
43 protected List<Argument> arguments;
44 protected IProgressIndicator guiProgress;
46 public JabawsCalcWorker(AlignViewportI alignViewport,
47 AlignmentViewPanel alignPanel)
49 super(alignViewport, alignPanel);
52 public JabawsCalcWorker(Jws2Instance service, AlignFrame alignFrame,
53 WsParamSetI preset, List<Argument> paramset)
55 this(alignFrame.getCurrentView(), alignFrame.alignPanel);
56 this.guiProgress = alignFrame;
58 this.arguments = paramset;
59 this.service = service;
60 aaservice = (SequenceAnnotation) service.service;
64 public WsParamSetI getPreset()
69 public List<Argument> getArguments()
75 * reconfigure and restart the AAConClient. 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, final List<Argument> newarguments)
85 arguments = newarguments;
86 calcMan.startWorker(this);
89 public List<Option> getJabaArguments()
91 List<Option> newargs = new ArrayList<Option>();
92 if (preset != null && preset instanceof JabaWsParamSet)
94 newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
96 if (arguments != null && arguments.size() > 0)
98 for (Argument rg : arguments)
100 if (Option.class.isAssignableFrom(rg.getClass()))
102 newargs.add((Option) rg);
112 if (aaservice == null)
116 long progressId = -1;
118 int serverErrorsLeft = 3;
120 String rslt = "JOB NOT DEFINED";
121 StringBuffer msg = new StringBuffer();
128 List<compbio.data.sequence.FastaSequence> seqs = getInputSequences(alignViewport
133 calcMan.workerComplete(this);
137 AlignmentAnnotation[] aa = alignViewport.getAlignment()
138 .getAlignmentAnnotation();
139 if (guiProgress != null)
141 guiProgress.setProgressBar("JABA " + getServiceActionText(),
142 progressId = System.currentTimeMillis());
144 if (preset == null && arguments == null)
146 rslt = aaservice.analize(seqs);
152 rslt = aaservice.customAnalize(seqs, getJabaArguments());
153 } catch (WrongParameterException x)
155 throw new JobSubmissionException(
156 "Invalid parameter set. Check Jalview implementation.", x);
160 boolean finished = false;
164 JobStatus status = aaservice.getJobStatus(rslt);
165 if (status.equals(JobStatus.FINISHED))
169 if (calcMan.isPending(this) && this instanceof AAConClient)
172 // cancel this job and yield to the new job
175 if (aaservice.cancelJob(rslt))
177 System.err.println("Cancelled AACon job: " + rslt);
181 System.err.println("FAILED TO CANCEL AACon job: " + rslt);
184 } catch (Exception x)
192 ChunkHolder stats = null;
196 boolean retry = false;
201 stats = aaservice.pullExecStatistics(rslt, rpos);
202 } catch (Exception x)
205 if (x.getMessage().contains(
206 "Position in a file could not be negative!"))
208 // squash index out of bounds exception- seems to happen for
209 // disorder predictors which don't (apparently) produce any
210 // progress information and JABA server throws an exception
211 // because progress length is -1.
216 if (--serverErrorsLeft > 0)
222 } catch (InterruptedException q)
236 System.out.print(stats.getChunk());
238 rpos = stats.getNextPosition();
240 } while (stats != null && rpos > cpos);
242 if (!finished && status.equals(JobStatus.FAILED))
247 } catch (InterruptedException x)
253 if (serverErrorsLeft > 0)
258 } catch (InterruptedException x)
262 scoremanager = aaservice.getAnnotation(rslt);
263 if (scoremanager != null)
265 jalview.bin.Cache.log
266 .debug("Updating result annotation from Job " + rslt
267 + " at " + service.getUri());
268 updateResultAnnotation(true);
269 ap.adjustAnnotationHeight();
274 catch (JobSubmissionException x)
277 System.err.println("submission error with " + getServiceActionText()
280 calcMan.workerCannotRun(this);
281 } catch (ResultNotAvailableException x)
283 System.err.println("collection error:\nJob ID: " + rslt);
285 calcMan.workerCannotRun(this);
287 } catch (OutOfMemoryError error)
289 calcMan.workerCannotRun(this);
292 // hconsensus = null;
293 ap.raiseOOMWarning(getServiceActionText(), error);
294 } catch (Exception x)
296 calcMan.workerCannotRun(this);
299 // hconsensus = null;
301 .println("Blacklisting worker due to unexpected exception:");
306 calcMan.workerComplete(this);
309 calcMan.workerComplete(this);
310 if (guiProgress != null && progressId != -1)
312 guiProgress.setProgressBar("", progressId);
314 ap.paintAlignment(true);
316 if (msg.length() > 0)
318 // TODO: stash message somewhere in annotation or alignment view.
319 // code below shows result in a text box popup
321 * jalview.gui.CutAndPasteTransfer cap = new
322 * jalview.gui.CutAndPasteTransfer(); cap.setText(msg.toString());
323 * jalview.gui.Desktop.addInternalFrame(cap,
324 * "Job Status for "+getServiceActionText(), 600, 400);
332 public void updateAnnotation()
334 updateResultAnnotation(false);
337 public abstract void updateResultAnnotation(boolean immediate);
339 public abstract String getServiceActionText();
341 protected boolean submitGaps = true;
342 protected boolean alignedSeqs = true;
343 protected boolean nucleotidesAllowed = false;
344 protected boolean proteinAllowed = false;
346 * record sequences for mapping result back to afterwards
348 protected boolean bySequence = false;
349 protected Map<String, SequenceI> seqNames;
350 protected boolean[] gapMap;
353 public List<FastaSequence> getInputSequences(AlignmentI alignment)
355 if (alignment == null || alignment.getWidth() <= 0
356 || alignment.getSequences() == null
357 // || (alignedSeqs && !alignment.isAligned() && !submitGaps)
358 || alignment.isNucleotide() ? !nucleotidesAllowed
363 List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<compbio.data.sequence.FastaSequence>();
369 seqNames = new HashMap<String, SequenceI>();
371 gapMap = new boolean[0];
372 for (SequenceI sq : ((List<SequenceI>) alignment.getSequences()))
374 if (sq.getEnd() - sq.getStart() > minlen - 1)
376 String newname = SeqsetUtils.unique_name(seqs.size() + 1);
377 // make new input sequence with or without gaps
378 if (seqNames != null)
380 seqNames.put(newname, sq);
385 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
386 sq.getSequenceAsString()));
387 if (gapMap == null || gapMap.length < seq.getSequence().length())
389 boolean[] tg = gapMap;
390 gapMap = new boolean[seq.getLength()];
391 System.arraycopy(tg, 0, gapMap, 0, tg.length);
392 for (int p = tg.length; p < gapMap.length; p++)
394 gapMap[p] = false; // init as a gap
397 for (int apos : sq.gapMap())
399 gapMap[apos] = true; // aligned.
404 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
405 AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
406 sq.getSequenceAsString())));
408 if (seq.getSequence().length() > ln)
410 ln = seq.getSequence().length();
414 if (alignedSeqs && submitGaps)
417 for (int i = 0; i < gapMap.length; i++)
424 // try real hard to return something submittable
425 // TODO: some of AAcon measures need a minimum of two or three amino
426 // acids at each position, and AAcon doesn't gracefully degrade.
427 for (int p = 0; p < seqs.size(); p++)
429 FastaSequence sq = seqs.get(p);
430 int l = sq.getSequence().length();
431 // strip gapped columns
432 char[] padded = new char[realw], orig = sq.getSequence()
434 for (int i = 0, pp = 0; i < realw; pp++)
438 if (orig.length > pp)
440 padded[i++] = orig[pp];
448 seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(),
449 new String(padded)));
456 * notify manager that we have started, and wait for a free calculation slot
458 * @return true if slot is obtained and work still valid, false if another
459 * thread has done our work for us.
463 calcMan.notifyStart(this);
464 ap.paintAlignment(false);
465 while (!calcMan.notifyWorking(this))
467 if (calcMan.isWorking(this))
475 ap.paintAlignment(false);
479 } catch (Exception ex)
481 ex.printStackTrace();
484 if (alignViewport.isClosed())
492 protected void createAnnotationRowsForScores(List<AlignmentAnnotation> ourAnnot, String calcId,
493 int alWidth, Score scr)
495 // simple annotation row
496 AlignmentAnnotation annotation = alignViewport.getAlignment()
497 .findOrCreateAnnotation(scr.getMethod(), calcId, true, null,
499 if (alWidth == gapMap.length) // scr.getScores().size())
501 constructAnnotationFromScore(annotation, 0, alWidth, scr);
502 ourAnnot.add(annotation);
506 protected AlignmentAnnotation createAnnotationRowsForScores(List<AlignmentAnnotation> ourAnnot, String typeName,
507 String calcId, SequenceI dseq, int base, Score scr)
509 System.out.println("Creating annotation on dseq:" + dseq.getStart()
510 + " base is " + base + " and length=" + dseq.getLength()
511 + " == " + scr.getScores().size());
512 // AlignmentAnnotation annotation = new AlignmentAnnotation(
513 // scr.getMethod(), typeName, new Annotation[]
514 // {}, 0, -1, AlignmentAnnotation.LINE_GRAPH);
515 // annotation.setCalcId(calcId);
516 AlignmentAnnotation annotation = alignViewport.getAlignment()
517 .findOrCreateAnnotation(typeName, calcId, false, dseq, null);
518 constructAnnotationFromScore(annotation, 0, dseq.getLength(), scr);
519 annotation.createSequenceMapping(dseq, base, false);
520 annotation.adjustForAlignment();
521 dseq.addAlignmentAnnotation(annotation);
522 ourAnnot.add(annotation);
526 private void constructAnnotationFromScore(AlignmentAnnotation annotation, int base,
527 int alWidth, Score scr)
529 Annotation[] elm = new Annotation[alWidth];
530 Iterator<Float> vals = scr.getScores().iterator();
531 float m = 0f, x = 0f;
532 for (int i = 0; vals.hasNext(); i++)
534 float val = vals.next().floatValue();
552 // if we're at a gapped column then skip to next ungapped position
553 if (gapMap != null && gapMap.length > 0)
557 elm[i++] = new Annotation("", "", ' ', Float.NaN);
560 elm[i] = new Annotation("", "" + val, ' ', val);
563 annotation.annotations = elm;
564 annotation.belowAlignment = true;
570 annotation.graphMax = x;
571 annotation.graphMin = m;
572 annotation.validateRangeAndDisplay();
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();