2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.jws2;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
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;
40 import jalview.analysis.AlignSeq;
41 import jalview.analysis.SeqsetUtils;
42 import jalview.api.AlignViewportI;
43 import jalview.api.AlignmentViewPanel;
44 import jalview.datamodel.AlignmentAnnotation;
45 import jalview.datamodel.AlignmentI;
46 import jalview.datamodel.AnnotatedCollectionI;
47 import jalview.datamodel.Annotation;
48 import jalview.datamodel.SequenceI;
49 import jalview.gui.AlignFrame;
50 import jalview.gui.IProgressIndicator;
51 import jalview.workers.AlignCalcWorker;
52 import jalview.ws.jws2.dm.JabaWsParamSet;
53 import jalview.ws.jws2.jabaws2.Jws2Instance;
54 import jalview.ws.params.WsParamSetI;
56 public abstract class JabawsCalcWorker extends AlignCalcWorker
59 protected Jws2Instance service;
61 @SuppressWarnings("unchecked")
62 protected SequenceAnnotation aaservice;
64 protected ScoreManager scoremanager;
66 protected WsParamSetI preset;
68 protected List<Argument> arguments;
70 protected IProgressIndicator guiProgress;
72 public JabawsCalcWorker(AlignViewportI alignViewport,
73 AlignmentViewPanel alignPanel)
75 super(alignViewport, alignPanel);
78 public JabawsCalcWorker(Jws2Instance service, AlignFrame alignFrame,
79 WsParamSetI preset, List<Argument> paramset)
81 this(alignFrame.getCurrentView(), alignFrame.alignPanel);
82 this.guiProgress = alignFrame;
84 this.arguments = paramset;
85 this.service = service;
86 aaservice = (SequenceAnnotation) service.service;
90 public WsParamSetI getPreset()
95 public List<Argument> getArguments()
101 * reconfigure and restart the AAConClient. This method will spawn a new
102 * thread that will wait until any current jobs are finished, modify the
103 * parameters and restart the conservation calculation with the new values.
106 * @param newarguments
108 public void updateParameters(final WsParamSetI newpreset,
109 final List<Argument> newarguments)
112 arguments = newarguments;
113 calcMan.startWorker(this);
116 public List<Option> getJabaArguments()
118 List<Option> newargs = new ArrayList<Option>();
119 if (preset != null && preset instanceof JabaWsParamSet)
121 newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
123 if (arguments != null && arguments.size() > 0)
125 for (Argument rg : arguments)
127 if (Option.class.isAssignableFrom(rg.getClass()))
129 newargs.add((Option) rg);
139 if (aaservice == null)
143 long progressId = -1;
145 int serverErrorsLeft = 3;
147 String rslt = "JOB NOT DEFINED";
148 StringBuffer msg = new StringBuffer();
155 List<compbio.data.sequence.FastaSequence> seqs = getInputSequences(
156 alignViewport.getAlignment(),
157 bySequence ? alignViewport.getSelectionGroup() : null);
161 calcMan.workerComplete(this);
165 AlignmentAnnotation[] aa = alignViewport.getAlignment()
166 .getAlignmentAnnotation();
167 if (guiProgress != null)
169 guiProgress.setProgressBar("JABA " + getServiceActionText(),
170 progressId = System.currentTimeMillis());
172 if (preset == null && arguments == null)
174 rslt = aaservice.analize(seqs);
180 rslt = aaservice.customAnalize(seqs, getJabaArguments());
181 } catch (WrongParameterException x)
183 throw new JobSubmissionException(
184 "Invalid parameter set. Check Jalview implementation.", x);
188 boolean finished = false;
192 JobStatus status = aaservice.getJobStatus(rslt);
193 if (status.equals(JobStatus.FINISHED))
197 if (calcMan.isPending(this) && this instanceof AAConClient)
200 // cancel this job and yield to the new job
203 if (aaservice.cancelJob(rslt))
205 System.err.println("Cancelled AACon job: " + rslt);
209 System.err.println("FAILED TO CANCEL AACon job: " + rslt);
212 } catch (Exception x)
220 ChunkHolder stats = null;
224 boolean retry = false;
229 stats = aaservice.pullExecStatistics(rslt, rpos);
230 } catch (Exception x)
233 if (x.getMessage().contains(
234 "Position in a file could not be negative!"))
236 // squash index out of bounds exception- seems to happen for
237 // disorder predictors which don't (apparently) produce any
238 // progress information and JABA server throws an exception
239 // because progress length is -1.
244 if (--serverErrorsLeft > 0)
250 } catch (InterruptedException q)
264 System.out.print(stats.getChunk());
266 rpos = stats.getNextPosition();
268 } while (stats != null && rpos > cpos);
270 if (!finished && status.equals(JobStatus.FAILED))
275 } catch (InterruptedException x)
281 if (serverErrorsLeft > 0)
286 } catch (InterruptedException x)
290 scoremanager = aaservice.getAnnotation(rslt);
291 if (scoremanager != null)
293 jalview.bin.Cache.log
294 .debug("Updating result annotation from Job " + rslt
295 + " at " + service.getUri());
296 updateResultAnnotation(true);
297 ap.adjustAnnotationHeight();
302 catch (JobSubmissionException x)
305 System.err.println("submission error with " + getServiceActionText()
308 calcMan.workerCannotRun(this);
309 } catch (ResultNotAvailableException x)
311 System.err.println("collection error:\nJob ID: " + rslt);
313 calcMan.workerCannotRun(this);
315 } catch (OutOfMemoryError error)
317 calcMan.workerCannotRun(this);
320 // hconsensus = null;
321 ap.raiseOOMWarning(getServiceActionText(), error);
322 } catch (Exception x)
324 calcMan.workerCannotRun(this);
327 // hconsensus = null;
329 .println("Blacklisting worker due to unexpected exception:");
334 calcMan.workerComplete(this);
337 calcMan.workerComplete(this);
338 if (guiProgress != null && progressId != -1)
340 guiProgress.setProgressBar("", progressId);
342 ap.paintAlignment(true);
344 if (msg.length() > 0)
346 // TODO: stash message somewhere in annotation or alignment view.
347 // code below shows result in a text box popup
349 * jalview.gui.CutAndPasteTransfer cap = new
350 * jalview.gui.CutAndPasteTransfer(); cap.setText(msg.toString());
351 * jalview.gui.Desktop.addInternalFrame(cap,
352 * "Job Status for "+getServiceActionText(), 600, 400);
360 public void updateAnnotation()
362 updateResultAnnotation(false);
365 public abstract void updateResultAnnotation(boolean immediate);
367 public abstract String getServiceActionText();
369 protected boolean submitGaps = true;
371 protected boolean alignedSeqs = true;
373 protected boolean nucleotidesAllowed = false;
375 protected boolean proteinAllowed = false;
378 * record sequences for mapping result back to afterwards
380 protected boolean bySequence = false;
382 protected Map<String, SequenceI> seqNames;
384 protected boolean[] gapMap;
390 public List<FastaSequence> getInputSequences(AlignmentI alignment,
391 AnnotatedCollectionI inputSeqs)
393 if (alignment == null || alignment.getWidth() <= 0
394 || alignment.getSequences() == null || alignment.isNucleotide() ? !nucleotidesAllowed
399 if (inputSeqs == null || inputSeqs.getWidth() <= 0
400 || inputSeqs.getSequences() == null
401 || inputSeqs.getSequences().size() < 1)
403 inputSeqs = alignment;
406 List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<compbio.data.sequence.FastaSequence>();
412 seqNames = new HashMap<String, SequenceI>();
414 gapMap = new boolean[0];
415 start = inputSeqs.getStartRes();
416 end = inputSeqs.getEndRes();
418 for (SequenceI sq : ((List<SequenceI>) inputSeqs.getSequences()))
420 if (bySequence ? sq.findPosition(end + 1)
421 - sq.findPosition(start + 1) > minlen - 1 : sq.getEnd()
422 - sq.getStart() > minlen - 1)
424 String newname = SeqsetUtils.unique_name(seqs.size() + 1);
425 // make new input sequence with or without gaps
426 if (seqNames != null)
428 seqNames.put(newname, sq);
433 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
434 sq.getSequenceAsString()));
435 if (gapMap == null || gapMap.length < seq.getSequence().length())
437 boolean[] tg = gapMap;
438 gapMap = new boolean[seq.getLength()];
439 System.arraycopy(tg, 0, gapMap, 0, tg.length);
440 for (int p = tg.length; p < gapMap.length; p++)
442 gapMap[p] = false; // init as a gap
445 for (int apos : sq.gapMap())
447 gapMap[apos] = true; // aligned.
452 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
453 AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
454 sq.getSequenceAsString(start, end + 1))));
456 if (seq.getSequence().length() > ln)
458 ln = seq.getSequence().length();
462 if (alignedSeqs && submitGaps)
465 for (int i = 0; i < gapMap.length; i++)
472 // try real hard to return something submittable
473 // TODO: some of AAcon measures need a minimum of two or three amino
474 // acids at each position, and AAcon doesn't gracefully degrade.
475 for (int p = 0; p < seqs.size(); p++)
477 FastaSequence sq = seqs.get(p);
478 int l = sq.getSequence().length();
479 // strip gapped columns
480 char[] padded = new char[realw], orig = sq.getSequence()
482 for (int i = 0, pp = 0; i < realw; pp++)
486 if (orig.length > pp)
488 padded[i++] = orig[pp];
496 seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(),
497 new String(padded)));
504 * notify manager that we have started, and wait for a free calculation slot
506 * @return true if slot is obtained and work still valid, false if another
507 * thread has done our work for us.
511 calcMan.notifyStart(this);
512 ap.paintAlignment(false);
513 while (!calcMan.notifyWorking(this))
515 if (calcMan.isWorking(this))
523 ap.paintAlignment(false);
527 } catch (Exception ex)
529 ex.printStackTrace();
532 if (alignViewport.isClosed())
540 protected void createAnnotationRowsForScores(
541 List<AlignmentAnnotation> ourAnnot, String calcId, int alWidth,
544 // simple annotation row
545 AlignmentAnnotation annotation = alignViewport.getAlignment()
546 .findOrCreateAnnotation(scr.getMethod(), calcId, true, null,
548 if (alWidth == gapMap.length) // scr.getScores().size())
550 constructAnnotationFromScore(annotation, 0, alWidth, scr);
551 ourAnnot.add(annotation);
555 protected AlignmentAnnotation createAnnotationRowsForScores(
556 List<AlignmentAnnotation> ourAnnot, String typeName,
557 String calcId, SequenceI dseq, int base, Score scr)
559 System.out.println("Creating annotation on dseq:" + dseq.getStart()
560 + " base is " + base + " and length=" + dseq.getLength()
561 + " == " + scr.getScores().size());
562 // AlignmentAnnotation annotation = new AlignmentAnnotation(
563 // scr.getMethod(), typeName, new Annotation[]
564 // {}, 0, -1, AlignmentAnnotation.LINE_GRAPH);
565 // annotation.setCalcId(calcId);
566 AlignmentAnnotation annotation = alignViewport.getAlignment()
567 .findOrCreateAnnotation(typeName, calcId, false, dseq, null);
568 constructAnnotationFromScore(annotation, 0, dseq.getLength(), scr);
569 annotation.createSequenceMapping(dseq, base, false);
570 annotation.adjustForAlignment();
571 dseq.addAlignmentAnnotation(annotation);
572 ourAnnot.add(annotation);
576 private void constructAnnotationFromScore(AlignmentAnnotation annotation,
577 int base, int alWidth, Score scr)
579 Annotation[] elm = new Annotation[alWidth];
580 Iterator<Float> vals = scr.getScores().iterator();
581 float m = 0f, x = 0f;
582 for (int i = 0; vals.hasNext(); i++)
584 float val = vals.next().floatValue();
602 // if we're at a gapped column then skip to next ungapped position
603 if (gapMap != null && gapMap.length > 0)
607 elm[i++] = new Annotation("", "", ' ', Float.NaN);
610 elm[i] = new Annotation("", "" + val, ' ', val);
613 annotation.annotations = elm;
614 annotation.belowAlignment = true;
620 annotation.graphMax = x;
621 annotation.graphMin = m;
622 annotation.validateRangeAndDisplay();
625 protected void updateOurAnnots(List<AlignmentAnnotation> ourAnnot)
627 List<AlignmentAnnotation> our = ourAnnots;
628 ourAnnots = ourAnnot;
629 AlignmentI alignment = alignViewport.getAlignment();
634 for (AlignmentAnnotation an : our)
636 if (!ourAnnots.contains(an))
638 // remove the old annotation
639 alignment.deleteAnnotation(an);
645 ap.adjustAnnotationHeight();