2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.ws.jws2;
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 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.Iterator;
38 import java.util.List;
41 import compbio.data.msa.SequenceAnnotation;
42 import compbio.data.sequence.FastaSequence;
43 import compbio.data.sequence.Score;
44 import compbio.data.sequence.ScoreManager;
45 import compbio.metadata.Argument;
46 import compbio.metadata.ChunkHolder;
47 import compbio.metadata.JobStatus;
48 import compbio.metadata.JobSubmissionException;
49 import compbio.metadata.Option;
50 import compbio.metadata.ResultNotAvailableException;
51 import compbio.metadata.WrongParameterException;
53 public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
57 @SuppressWarnings("unchecked")
58 protected SequenceAnnotation aaservice;
60 protected ScoreManager scoremanager;
62 protected WsParamSetI preset;
64 protected List<Argument> arguments;
66 public JabawsAlignCalcWorker(AlignViewportI alignViewport,
67 AlignmentViewPanel alignPanel)
69 super(alignViewport, alignPanel);
72 IProgressIndicator guiProgress;
74 public JabawsAlignCalcWorker(Jws2Instance service, AlignFrame alignFrame,
75 WsParamSetI preset, List<Argument> paramset)
77 this(alignFrame.getCurrentView(), alignFrame.alignPanel);
78 this.guiProgress = alignFrame;
80 this.arguments = paramset;
81 this.service = service;
82 aaservice = (SequenceAnnotation) service.service;
86 public WsParamSetI getPreset()
91 public List<Argument> getArguments()
97 * reconfigure and restart the AAConClient. This method will spawn a new
98 * thread that will wait until any current jobs are finished, modify the
99 * parameters and restart the conservation calculation with the new values.
102 * @param newarguments
104 public void updateParameters(final WsParamSetI newpreset,
105 final List<Argument> newarguments)
108 arguments = newarguments;
109 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 int serverErrorsLeft = 3;
143 String rslt = "JOB NOT DEFINED";
144 StringBuffer msg = new StringBuffer();
151 List<compbio.data.sequence.FastaSequence> seqs = getInputSequences(alignViewport
156 calcMan.workerComplete(this);
160 AlignmentAnnotation[] aa = alignViewport.getAlignment()
161 .getAlignmentAnnotation();
162 if (guiProgress != null)
164 guiProgress.setProgressBar("JABA " + getServiceActionText(),
165 progressId = System.currentTimeMillis());
167 if (preset == null && arguments == null)
169 rslt = aaservice.analize(seqs);
175 rslt = aaservice.customAnalize(seqs, getJabaArguments());
176 } catch (WrongParameterException x)
178 throw new JobSubmissionException(
179 "Invalid paremeter set. Check Jalview implementation.", x);
183 boolean finished = false;
187 JobStatus status = aaservice.getJobStatus(rslt);
188 if (status.equals(JobStatus.FINISHED))
192 if (calcMan.isPending(this) && this instanceof AAConClient)
195 // cancel this job and yield to the new job
198 if (aaservice.cancelJob(rslt))
200 System.err.println("Cancelled AACon job: " + rslt);
204 System.err.println("FAILED TO CANCELL AACon job: " + rslt);
207 } catch (Exception x)
215 ChunkHolder stats = null;
219 boolean retry = false;
224 stats = aaservice.pullExecStatistics(rslt, rpos);
225 } catch (Exception x)
228 if (x.getMessage().contains(
229 "Position in a file could not be negative!"))
231 // squash index out of bounds exception- seems to happen for
232 // disorder predictors which don't (apparently) produce any
233 // progress information and JABA server throws an exception
234 // because progress length is -1.
239 if (--serverErrorsLeft > 0)
245 } catch (InterruptedException q)
259 System.out.print(stats.getChunk());
261 rpos = stats.getNextPosition();
263 } while (stats != null && rpos > cpos);
265 if (!finished && status.equals(JobStatus.FAILED))
270 } catch (InterruptedException x)
276 if (serverErrorsLeft > 0)
281 } catch (InterruptedException x)
285 scoremanager = aaservice.getAnnotation(rslt);
286 if (scoremanager != null)
288 jalview.bin.Cache.log
289 .debug("Updating result annotation from Job " + rslt
290 + " at " + service.getUri());
291 updateResultAnnotation(true);
292 ap.adjustAnnotationHeight();
297 catch (JobSubmissionException x)
300 System.err.println("submission error with " + getServiceActionText()
303 calcMan.workerCannotRun(this);
304 } catch (ResultNotAvailableException x)
306 System.err.println("collection error:\nJob ID: " + rslt);
308 calcMan.workerCannotRun(this);
310 } catch (OutOfMemoryError error)
312 calcMan.workerCannotRun(this);
315 // hconsensus = null;
316 ap.raiseOOMWarning(getServiceActionText(), error);
317 } catch (Exception x)
319 calcMan.workerCannotRun(this);
322 // hconsensus = null;
324 .println("Blacklisting worker due to unexpected exception:");
329 calcMan.workerComplete(this);
332 calcMan.workerComplete(this);
333 if (guiProgress != null && progressId != -1)
335 guiProgress.setProgressBar("", progressId);
337 ap.paintAlignment(true);
339 if (msg.length() > 0)
341 // TODO: stash message somewhere in annotation or alignment view.
342 // code below shows result in a text box popup
344 * jalview.gui.CutAndPasteTransfer cap = new
345 * jalview.gui.CutAndPasteTransfer(); cap.setText(msg.toString());
346 * jalview.gui.Desktop.addInternalFrame(cap,
347 * "Job Status for "+getServiceActionText(), 600, 400);
355 public void updateAnnotation()
357 updateResultAnnotation(false);
360 public abstract void updateResultAnnotation(boolean immediate);
362 public abstract String getServiceActionText();
364 boolean submitGaps = true;
366 boolean alignedSeqs = true;
368 boolean nucleotidesAllowed = false;
370 boolean proteinAllowed = false;
373 * record sequences for mapping result back to afterwards
375 protected boolean bySequence = false;
377 Map<String, SequenceI> seqNames;
383 public List<FastaSequence> getInputSequences(AlignmentI alignment)
385 if (alignment == null || alignment.getWidth() <= 0
386 || alignment.getSequences() == null
387 // || (alignedSeqs && !alignment.isAligned() && !submitGaps)
388 || alignment.isNucleotide() ? !nucleotidesAllowed
393 List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<compbio.data.sequence.FastaSequence>();
399 seqNames = new HashMap<String, SequenceI>();
401 gapMap = new boolean[0];
402 for (SequenceI sq : ((List<SequenceI>) alignment.getSequences()))
404 if (sq.getEnd() - sq.getStart() > minlen - 1)
406 String newname = SeqsetUtils.unique_name(seqs.size() + 1);
407 // make new input sequence with or without gaps
408 if (seqNames != null)
410 seqNames.put(newname, sq);
415 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
416 sq.getSequenceAsString()));
417 if (gapMap == null || gapMap.length < seq.getSequence().length())
419 boolean[] tg = gapMap;
420 gapMap = new boolean[seq.getLength()];
421 System.arraycopy(tg, 0, gapMap, 0, tg.length);
422 for (int p = tg.length; p < gapMap.length; p++)
424 gapMap[p] = false; // init as a gap
427 for (int apos : sq.gapMap())
429 gapMap[apos] = true; // aligned.
434 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
435 AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
436 sq.getSequenceAsString())));
438 if (seq.getSequence().length() > ln)
440 ln = seq.getSequence().length();
444 if (alignedSeqs && submitGaps)
447 for (int i = 0; i < gapMap.length; i++)
454 // try real hard to return something submittable
455 // TODO: some of AAcon measures need a minimum of two or three amino
456 // acids at each position, and AAcon doesn't gracefully degrade.
457 for (int p = 0; p < seqs.size(); p++)
459 FastaSequence sq = seqs.get(p);
460 int l = sq.getSequence().length();
461 // strip gapped columns
462 char[] padded = new char[realw], orig = sq.getSequence()
464 for (int i = 0, pp = 0; i < realw; pp++)
468 if (orig.length > pp)
470 padded[i++] = orig[pp];
478 seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(),
479 new String(padded)));
486 * notify manager that we have started, and wait for a free calculation slot
488 * @return true if slot is obtained and work still valid, false if another
489 * thread has done our work for us.
493 calcMan.notifyStart(this);
494 ap.paintAlignment(false);
495 while (!calcMan.notifyWorking(this))
497 if (calcMan.isWorking(this))
505 ap.paintAlignment(false);
509 } catch (Exception ex)
511 ex.printStackTrace();
514 if (alignViewport.isClosed())
522 protected void createAnnotationRowsForScores(
523 List<AlignmentAnnotation> ourAnnot, String calcId, int alWidth,
526 // simple annotation row
527 AlignmentAnnotation annotation = alignViewport.getAlignment()
528 .findOrCreateAnnotation(scr.getMethod(), calcId, true, null,
530 if (alWidth == gapMap.length) // scr.getScores().size())
532 constructAnnotationFromScore(annotation, 0, alWidth, scr);
533 ourAnnot.add(annotation);
537 protected AlignmentAnnotation createAnnotationRowsForScores(
538 List<AlignmentAnnotation> ourAnnot, String typeName,
539 String calcId, SequenceI dseq, int base, Score scr)
541 System.out.println("Creating annotation on dseq:" + dseq.getStart()
542 + " base is " + base + " and length=" + dseq.getLength()
543 + " == " + scr.getScores().size());
544 // AlignmentAnnotation annotation = new AlignmentAnnotation(
545 // scr.getMethod(), typeName, new Annotation[]
546 // {}, 0, -1, AlignmentAnnotation.LINE_GRAPH);
547 // annotation.setCalcId(calcId);
548 AlignmentAnnotation annotation = alignViewport.getAlignment()
549 .findOrCreateAnnotation(typeName, calcId, false, dseq, null);
550 constructAnnotationFromScore(annotation, 0, dseq.getLength(), scr);
551 annotation.createSequenceMapping(dseq, base, false);
552 annotation.adjustForAlignment();
553 dseq.addAlignmentAnnotation(annotation);
554 ourAnnot.add(annotation);
558 private void constructAnnotationFromScore(AlignmentAnnotation annotation,
559 int base, int alWidth, Score scr)
561 Annotation[] elm = new Annotation[alWidth];
562 Iterator<Float> vals = scr.getScores().iterator();
563 float m = 0f, x = 0f;
564 for (int i = 0; vals.hasNext(); i++)
566 float val = vals.next().floatValue();
584 // if we're at a gapped column then skip to next ungapped position
585 if (gapMap != null && gapMap.length > 0)
589 elm[i++] = new Annotation("", "", ' ', Float.NaN);
592 elm[i] = new Annotation("", "" + val, ' ', val);
595 annotation.annotations = elm;
596 annotation.belowAlignment = true;
602 annotation.graphMax = x;
603 annotation.graphMin = m;
604 annotation.validateRangeAndDisplay();
607 protected void updateOurAnnots(List<AlignmentAnnotation> ourAnnot)
609 List<AlignmentAnnotation> our = ourAnnots;
610 ourAnnots = ourAnnot;
611 AlignmentI alignment = alignViewport.getAlignment();
616 for (AlignmentAnnotation an : our)
618 if (!ourAnnots.contains(an))
620 // remove the old annotation
621 alignment.deleteAnnotation(an);
627 ap.adjustAnnotationHeight();