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.bin.Cache;
8 import jalview.datamodel.AlignmentAnnotation;
9 import jalview.datamodel.AlignmentI;
10 import jalview.datamodel.Annotation;
11 import jalview.datamodel.SequenceFeature;
12 import jalview.datamodel.SequenceI;
13 import jalview.gui.AlignFrame;
14 import jalview.gui.IProgressIndicator;
15 import jalview.workers.AlignCalcWorker;
16 import jalview.ws.jws2.dm.JabaWsParamSet;
17 import jalview.ws.jws2.jabaws2.Jws2Instance;
18 import jalview.ws.params.WsParamSetI;
20 import java.awt.Color;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
27 import com.sun.xml.internal.ws.client.ClientTransportException;
29 import compbio.data.msa.SequenceAnnotation;
30 import compbio.data.sequence.FastaSequence;
31 import compbio.data.sequence.Score;
32 import compbio.data.sequence.ScoreManager;
33 import compbio.metadata.Argument;
34 import compbio.metadata.ChunkHolder;
35 import compbio.metadata.JobStatus;
36 import compbio.metadata.JobSubmissionException;
37 import compbio.metadata.Option;
38 import compbio.metadata.ResultNotAvailableException;
39 import compbio.metadata.WrongParameterException;
41 public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
45 @SuppressWarnings("unchecked")
46 protected SequenceAnnotation aaservice;
48 protected ScoreManager scoremanager;
50 protected WsParamSetI preset;
52 protected List<Argument> arguments;
54 public JabawsAlignCalcWorker(AlignViewportI alignViewport,
55 AlignmentViewPanel alignPanel)
57 super(alignViewport, alignPanel);
60 IProgressIndicator guiProgress;
62 public JabawsAlignCalcWorker(Jws2Instance service, AlignFrame alignFrame,
63 WsParamSetI preset, List<Argument> paramset)
65 this(alignFrame.getCurrentView(), alignFrame.alignPanel);
66 this.guiProgress = alignFrame;
68 this.arguments = paramset;
69 this.service = service;
70 aaservice = (SequenceAnnotation) service.service;
74 public WsParamSetI getPreset()
79 public List<Argument> getArguments()
85 * reconfigure and restart the AAConsClient. This method will spawn a new
86 * thread that will wait until any current jobs are finished, modify the
87 * parameters and restart the conservation calculation with the new values.
92 public void updateParameters(final WsParamSetI newpreset,
93 final List<Argument> newarguments)
96 arguments = newarguments;
97 calcMan.startWorker(this);
100 public List<Option> getJabaArguments()
102 List<Option> newargs = new ArrayList<Option>();
103 if (preset != null && preset instanceof JabaWsParamSet)
105 newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
107 if (arguments != null && arguments.size() > 0)
109 for (Argument rg : arguments)
111 if (Option.class.isAssignableFrom(rg.getClass()))
113 newargs.add((Option) rg);
123 if (aaservice == null)
127 long progressId = -1;
129 int serverErrorsLeft = 3;
131 String rslt = "JOB NOT DEFINED";
132 StringBuffer msg = new StringBuffer();
139 List<compbio.data.sequence.FastaSequence> seqs = getInputSequences(alignViewport
144 calcMan.workerComplete(this);
148 AlignmentAnnotation[] aa = alignViewport.getAlignment()
149 .getAlignmentAnnotation();
150 if (guiProgress != null)
152 guiProgress.setProgressBar("JABA " + getServiceActionText(),
153 progressId = System.currentTimeMillis());
155 if (preset == null && arguments == null)
157 rslt = aaservice.analize(seqs);
163 rslt = aaservice.customAnalize(seqs, getJabaArguments());
164 } catch (WrongParameterException x)
166 throw new JobSubmissionException(
167 "Invalid paremeter set. Check Jalview implementation.", x);
171 boolean finished = false;
175 JobStatus status = aaservice.getJobStatus(rslt);
176 if (status.equals(JobStatus.FINISHED))
180 if (calcMan.isPending(this) && this instanceof AAConsClient)
183 // cancel this job and yield to the new job
186 if (aaservice.cancelJob(rslt))
188 System.err.println("Cancelled AACon job: " + rslt);
192 System.err.println("FAILED TO CANCELL AACon job: " + rslt);
195 } catch (Exception x)
203 ChunkHolder stats = null;
207 boolean retry = false;
212 stats = aaservice.pullExecStatistics(rslt, rpos);
213 } catch (Exception x)
216 if (x.getMessage().contains(
217 "Position in a file could not be negative!"))
219 // squash index out of bounds exception- seems to happen for
220 // disorder predictors which don't (apparently) produce any
221 // progress information and JABA server throws an exception
222 // because progress length is -1.
227 if (--serverErrorsLeft > 0)
232 } catch (InterruptedException q) {};
241 System.out.print(stats.getChunk());
243 rpos = stats.getNextPosition();
245 } while (stats != null && rpos > cpos);
247 if (!finished && status.equals(JobStatus.FAILED))
252 } catch (InterruptedException x)
258 if (serverErrorsLeft>0)
263 } catch (InterruptedException x)
267 scoremanager = aaservice.getAnnotation(rslt);
268 if (scoremanager != null)
270 jalview.bin.Cache.log
271 .debug("Updating result annotation from Job " + rslt
272 + " at " + service.getUri());
273 updateResultAnnotation(true);
278 catch (JobSubmissionException x)
281 System.err.println("submission error with " + getServiceActionText()
284 calcMan.workerCannotRun(this);
285 } catch (ResultNotAvailableException x)
287 System.err.println("collection error:\nJob ID: " + rslt);
289 calcMan.workerCannotRun(this);
291 } catch (OutOfMemoryError error)
293 calcMan.workerCannotRun(this);
296 // hconsensus = null;
297 ap.raiseOOMWarning(getServiceActionText(), error);
298 } catch (Exception x)
300 calcMan.workerCannotRun(this);
303 // hconsensus = null;
305 .println("Blacklisting worker due to unexpected exception:");
310 calcMan.workerComplete(this);
313 calcMan.workerComplete(this);
314 if (guiProgress != null && progressId != -1)
316 guiProgress.setProgressBar("", progressId);
318 ap.paintAlignment(true);
320 if (msg.length() > 0)
322 // TODO: stash message somewhere in annotation or alignment view.
323 // code below shows result in a text box popup
325 * jalview.gui.CutAndPasteTransfer cap = new
326 * jalview.gui.CutAndPasteTransfer(); cap.setText(msg.toString());
327 * jalview.gui.Desktop.addInternalFrame(cap,
328 * "Job Status for "+getServiceActionText(), 600, 400);
336 public void updateAnnotation()
338 updateResultAnnotation(false);
341 public abstract void updateResultAnnotation(boolean immediate);
343 public abstract String getServiceActionText();
345 boolean submitGaps = true;
347 boolean alignedSeqs = true;
349 boolean nucleotidesAllowed = false;
351 boolean proteinAllowed = false;
354 * record sequences for mapping result back to afterwards
356 protected boolean bySequence = false;
358 Map<String, SequenceI> seqNames;
364 public List<FastaSequence> getInputSequences(AlignmentI alignment)
366 if (alignment == null || alignment.getWidth() <= 0
367 || alignment.getSequences() == null
368 // || (alignedSeqs && !alignment.isAligned() && !submitGaps)
369 || alignment.isNucleotide() ? !nucleotidesAllowed
374 List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<compbio.data.sequence.FastaSequence>();
380 seqNames = new HashMap<String, SequenceI>();
382 gapMap = new boolean[0];
383 for (SequenceI sq : ((List<SequenceI>) alignment.getSequences()))
385 if (sq.getEnd() - sq.getStart() > minlen - 1)
387 String newname = SeqsetUtils.unique_name(seqs.size() + 1);
388 // make new input sequence with or without gaps
389 if (seqNames != null)
391 seqNames.put(newname, sq);
396 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
397 sq.getSequenceAsString()));
398 if (gapMap == null || gapMap.length < seq.getSequence().length())
400 boolean[] tg = gapMap;
401 gapMap = new boolean[seq.getLength()];
402 System.arraycopy(tg, 0, gapMap, 0, tg.length);
403 for (int p = tg.length; p < gapMap.length; p++)
405 gapMap[p] = false; // init as a gap
408 for (int apos : sq.gapMap())
410 gapMap[apos] = true; // aligned.
415 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
416 AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
417 sq.getSequenceAsString())));
419 if (seq.getSequence().length() > ln)
421 ln = seq.getSequence().length();
425 if (alignedSeqs && submitGaps)
428 for (int i = 0; i < gapMap.length; i++)
435 // try real hard to return something submittable
436 // TODO: some of AAcons measures need a minimum of two or three amino
437 // acids at each position, and aacons doesn't gracefully degrade.
438 for (int p = 0; p < seqs.size(); p++)
440 FastaSequence sq = seqs.get(p);
441 int l = sq.getSequence().length();
442 // strip gapped columns
443 char[] padded = new char[realw], orig = sq.getSequence()
445 for (int i = 0, pp = 0; i < realw; pp++)
449 if (orig.length > pp)
451 padded[i++] = orig[pp];
459 seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(),
460 new String(padded)));
467 * notify manager that we have started, and wait for a free calculation slot
469 * @return true if slot is obtained and work still valid, false if another
470 * thread has done our work for us.
474 calcMan.notifyStart(this);
475 ap.paintAlignment(false);
476 while (!calcMan.notifyWorking(this))
478 if (calcMan.isWorking(this))
486 ap.paintAlignment(false);
490 } catch (Exception ex)
492 ex.printStackTrace();
495 if (alignViewport.isClosed())
503 protected void createAnnotationRowsForScores(
504 List<AlignmentAnnotation> ourAnnot, String calcId, int alWidth,
507 // simple annotation row
508 AlignmentAnnotation annotation = alignViewport.getAlignment()
509 .findOrCreateAnnotation(scr.getMethod(), calcId, true, null,
511 if (alWidth == gapMap.length) // scr.getScores().size())
513 constructAnnotationFromScore(annotation, 0, alWidth, scr);
514 ourAnnot.add(annotation);
518 protected AlignmentAnnotation createAnnotationRowsForScores(
519 List<AlignmentAnnotation> ourAnnot, String typeName,
520 String calcId, SequenceI dseq, int base, Score scr)
522 System.out.println("Creating annotation on dseq:" + dseq.getStart()
523 + " base is " + base + " and length=" + dseq.getLength()
524 + " == " + scr.getScores().size());
525 // AlignmentAnnotation annotation = new AlignmentAnnotation(
526 // scr.getMethod(), typeName, new Annotation[]
527 // {}, 0, -1, AlignmentAnnotation.LINE_GRAPH);
528 // annotation.setCalcId(calcId);
529 AlignmentAnnotation annotation = alignViewport.getAlignment()
530 .findOrCreateAnnotation(typeName, calcId, false, dseq, null);
531 constructAnnotationFromScore(annotation, base, dseq.getLength(), scr);
532 annotation.createSequenceMapping(dseq, dseq.findPosition(base), false);
533 annotation.adjustForAlignment();
534 dseq.addAlignmentAnnotation(annotation);
535 ourAnnot.add(annotation);
539 private void constructAnnotationFromScore(AlignmentAnnotation annotation,
540 int base, int alWidth, Score scr)
542 Annotation[] elm = new Annotation[alWidth];
543 Iterator<Float> vals = scr.getScores().iterator();
544 float m = 0f, x = 0f;
545 for (int i = base; vals.hasNext(); i++)
547 float val = vals.next().floatValue();
565 // if we're at a gapped column then skip to next ungapped position
566 if (gapMap != null && gapMap.length > 0)
570 elm[i++] = new Annotation("", "", ' ', Float.NaN);
573 elm[i] = new Annotation("", "" + val, ' ', val);
576 annotation.annotations = elm;
577 annotation.belowAlignment = true;
583 annotation.graphMax = x;
584 annotation.graphMin = m;
585 annotation.validateRangeAndDisplay();
588 protected void updateOurAnnots(List<AlignmentAnnotation> ourAnnot)
590 List<AlignmentAnnotation> our = ourAnnots;
591 ourAnnots = ourAnnot;
592 AlignmentI alignment = alignViewport.getAlignment();
597 for (AlignmentAnnotation an : our)
599 if (!ourAnnots.contains(an))
601 // remove the old annotation
602 alignment.deleteAnnotation(an);
608 ap.adjustAnnotationHeight();