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.AnnotatedCollectionI;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.SequenceI;
29 import jalview.gui.AlignFrame;
30 import jalview.gui.IProgressIndicator;
31 import jalview.workers.AlignCalcWorker;
32 import jalview.ws.jws2.dm.JabaWsParamSet;
33 import jalview.ws.jws2.jabaws2.Jws2Instance;
34 import jalview.ws.params.WsParamSetI;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.List;
42 import compbio.data.msa.SequenceAnnotation;
43 import compbio.data.sequence.FastaSequence;
44 import compbio.data.sequence.Score;
45 import compbio.data.sequence.ScoreManager;
46 import compbio.metadata.Argument;
47 import compbio.metadata.ChunkHolder;
48 import compbio.metadata.JobStatus;
49 import compbio.metadata.JobSubmissionException;
50 import compbio.metadata.Option;
51 import compbio.metadata.ResultNotAvailableException;
52 import compbio.metadata.WrongParameterException;
54 public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
58 @SuppressWarnings("unchecked")
59 protected SequenceAnnotation aaservice;
61 protected ScoreManager scoremanager;
63 protected WsParamSetI preset;
65 protected List<Argument> arguments;
67 public JabawsAlignCalcWorker(AlignViewportI alignViewport,
68 AlignmentViewPanel alignPanel)
70 super(alignViewport, alignPanel);
73 IProgressIndicator guiProgress;
75 public JabawsAlignCalcWorker(Jws2Instance service, AlignFrame alignFrame,
76 WsParamSetI preset, List<Argument> paramset)
78 this(alignFrame.getCurrentView(), alignFrame.alignPanel);
79 this.guiProgress = alignFrame;
81 this.arguments = paramset;
82 this.service = service;
83 aaservice = (SequenceAnnotation) service.service;
87 public WsParamSetI getPreset()
92 public List<Argument> getArguments()
98 * reconfigure and restart the AAConClient. This method will spawn a new
99 * thread that will wait until any current jobs are finished, modify the
100 * parameters and restart the conservation calculation with the new values.
103 * @param newarguments
105 public void updateParameters(final WsParamSetI newpreset,
106 final List<Argument> newarguments)
109 arguments = newarguments;
110 calcMan.startWorker(this);
113 public List<Option> getJabaArguments()
115 List<Option> newargs = new ArrayList<Option>();
116 if (preset != null && preset instanceof JabaWsParamSet)
118 newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
120 if (arguments != null && arguments.size() > 0)
122 for (Argument rg : arguments)
124 if (Option.class.isAssignableFrom(rg.getClass()))
126 newargs.add((Option) rg);
136 if (aaservice == null)
140 long progressId = -1;
142 int serverErrorsLeft = 3;
144 String rslt = "JOB NOT DEFINED";
145 StringBuffer msg = new StringBuffer();
152 List<compbio.data.sequence.FastaSequence> seqs = getInputSequences(alignViewport
153 .getAlignment(), bySequence ? alignViewport.getSelectionGroup() : null);
157 calcMan.workerComplete(this);
161 AlignmentAnnotation[] aa = alignViewport.getAlignment()
162 .getAlignmentAnnotation();
163 if (guiProgress != null)
165 guiProgress.setProgressBar("JABA " + getServiceActionText(),
166 progressId = System.currentTimeMillis());
168 if (preset == null && arguments == null)
170 rslt = aaservice.analize(seqs);
176 rslt = aaservice.customAnalize(seqs, getJabaArguments());
177 } catch (WrongParameterException x)
179 throw new JobSubmissionException(
180 "Invalid parameter set. Check Jalview implementation.", x);
184 boolean finished = false;
188 JobStatus status = aaservice.getJobStatus(rslt);
189 if (status.equals(JobStatus.FINISHED))
193 if (calcMan.isPending(this) && this instanceof AAConClient)
196 // cancel this job and yield to the new job
199 if (aaservice.cancelJob(rslt))
201 System.err.println("Cancelled AACon job: " + rslt);
205 System.err.println("FAILED TO CANCEL AACon job: " + rslt);
208 } catch (Exception x)
216 ChunkHolder stats = null;
220 boolean retry = false;
225 stats = aaservice.pullExecStatistics(rslt, rpos);
226 } catch (Exception x)
229 if (x.getMessage().contains(
230 "Position in a file could not be negative!"))
232 // squash index out of bounds exception- seems to happen for
233 // disorder predictors which don't (apparently) produce any
234 // progress information and JABA server throws an exception
235 // because progress length is -1.
240 if (--serverErrorsLeft > 0)
246 } catch (InterruptedException q)
260 System.out.print(stats.getChunk());
262 rpos = stats.getNextPosition();
264 } while (stats != null && rpos > cpos);
266 if (!finished && status.equals(JobStatus.FAILED))
271 } catch (InterruptedException x)
277 if (serverErrorsLeft > 0)
282 } catch (InterruptedException x)
286 scoremanager = aaservice.getAnnotation(rslt);
287 if (scoremanager != null)
289 jalview.bin.Cache.log
290 .debug("Updating result annotation from Job " + rslt
291 + " at " + service.getUri());
292 updateResultAnnotation(true);
293 ap.adjustAnnotationHeight();
298 catch (JobSubmissionException x)
301 System.err.println("submission error with " + getServiceActionText()
304 calcMan.workerCannotRun(this);
305 } catch (ResultNotAvailableException x)
307 System.err.println("collection error:\nJob ID: " + rslt);
309 calcMan.workerCannotRun(this);
311 } catch (OutOfMemoryError error)
313 calcMan.workerCannotRun(this);
316 // hconsensus = null;
317 ap.raiseOOMWarning(getServiceActionText(), error);
318 } catch (Exception x)
320 calcMan.workerCannotRun(this);
323 // hconsensus = null;
325 .println("Blacklisting worker due to unexpected exception:");
330 calcMan.workerComplete(this);
333 calcMan.workerComplete(this);
334 if (guiProgress != null && progressId != -1)
336 guiProgress.setProgressBar("", progressId);
338 ap.paintAlignment(true);
340 if (msg.length() > 0)
342 // TODO: stash message somewhere in annotation or alignment view.
343 // code below shows result in a text box popup
345 * jalview.gui.CutAndPasteTransfer cap = new
346 * jalview.gui.CutAndPasteTransfer(); cap.setText(msg.toString());
347 * jalview.gui.Desktop.addInternalFrame(cap,
348 * "Job Status for "+getServiceActionText(), 600, 400);
356 public void updateAnnotation()
358 updateResultAnnotation(false);
361 public abstract void updateResultAnnotation(boolean immediate);
363 public abstract String getServiceActionText();
365 boolean submitGaps = true;
367 boolean alignedSeqs = true;
369 boolean nucleotidesAllowed = false;
371 boolean proteinAllowed = false;
374 * record sequences for mapping result back to afterwards
376 protected boolean bySequence = false;
378 Map<String, SequenceI> seqNames;
384 public List<FastaSequence> getInputSequences(AlignmentI alignment, AnnotatedCollectionI inputSeqs)
386 if (alignment == null || alignment.getWidth() <= 0
387 || alignment.getSequences() == null
388 // || (alignedSeqs && !alignment.isAligned() && !submitGaps)
389 || alignment.isNucleotide() ? !nucleotidesAllowed
394 if (inputSeqs==null || inputSeqs.getWidth()<=0 || inputSeqs.getSequences()==null || inputSeqs.getSequences().size()<1)
396 inputSeqs = alignment;
399 List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<compbio.data.sequence.FastaSequence>();
405 seqNames = new HashMap<String, SequenceI>();
407 gapMap = new boolean[0];
408 for (SequenceI sq : (List<SequenceI>) inputSeqs.getSequences())
410 if (sq.getEnd() - sq.getStart() > minlen - 1)
412 String newname = SeqsetUtils.unique_name(seqs.size() + 1);
413 // make new input sequence with or without gaps
414 if (seqNames != null)
416 seqNames.put(newname, sq);
421 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
422 sq.getSequenceAsString()));
423 if (gapMap == null || gapMap.length < seq.getSequence().length())
425 boolean[] tg = gapMap;
426 gapMap = new boolean[seq.getLength()];
427 System.arraycopy(tg, 0, gapMap, 0, tg.length);
428 for (int p = tg.length; p < gapMap.length; p++)
430 gapMap[p] = false; // init as a gap
433 for (int apos : sq.gapMap())
435 gapMap[apos] = true; // aligned.
440 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
441 AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
442 sq.getSequenceAsString())));
444 if (seq.getSequence().length() > ln)
446 ln = seq.getSequence().length();
450 if (alignedSeqs && submitGaps)
453 for (int i = 0; i < gapMap.length; i++)
460 // try real hard to return something submittable
461 // TODO: some of AAcon measures need a minimum of two or three amino
462 // acids at each position, and AAcon doesn't gracefully degrade.
463 for (int p = 0; p < seqs.size(); p++)
465 FastaSequence sq = seqs.get(p);
466 int l = sq.getSequence().length();
467 // strip gapped columns
468 char[] padded = new char[realw], orig = sq.getSequence()
470 for (int i = 0, pp = 0; i < realw; pp++)
474 if (orig.length > pp)
476 padded[i++] = orig[pp];
484 seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(),
485 new String(padded)));
492 * notify manager that we have started, and wait for a free calculation slot
494 * @return true if slot is obtained and work still valid, false if another
495 * thread has done our work for us.
499 calcMan.notifyStart(this);
500 ap.paintAlignment(false);
501 while (!calcMan.notifyWorking(this))
503 if (calcMan.isWorking(this))
511 ap.paintAlignment(false);
515 } catch (Exception ex)
517 ex.printStackTrace();
520 if (alignViewport.isClosed())
528 protected void createAnnotationRowsForScores(
529 List<AlignmentAnnotation> ourAnnot, String calcId, int alWidth,
532 // simple annotation row
533 AlignmentAnnotation annotation = alignViewport.getAlignment()
534 .findOrCreateAnnotation(scr.getMethod(), calcId, true, null,
536 if (alWidth == gapMap.length) // scr.getScores().size())
538 constructAnnotationFromScore(annotation, 0, alWidth, scr);
539 ourAnnot.add(annotation);
543 protected AlignmentAnnotation createAnnotationRowsForScores(
544 List<AlignmentAnnotation> ourAnnot, String typeName,
545 String calcId, SequenceI dseq, int base, Score scr)
547 System.out.println("Creating annotation on dseq:" + dseq.getStart()
548 + " base is " + base + " and length=" + dseq.getLength()
549 + " == " + scr.getScores().size());
550 // AlignmentAnnotation annotation = new AlignmentAnnotation(
551 // scr.getMethod(), typeName, new Annotation[]
552 // {}, 0, -1, AlignmentAnnotation.LINE_GRAPH);
553 // annotation.setCalcId(calcId);
554 AlignmentAnnotation annotation = alignViewport.getAlignment()
555 .findOrCreateAnnotation(typeName, calcId, false, dseq, null);
556 constructAnnotationFromScore(annotation, 0, dseq.getLength(), scr);
557 annotation.createSequenceMapping(dseq, base, false);
558 annotation.adjustForAlignment();
559 dseq.addAlignmentAnnotation(annotation);
560 ourAnnot.add(annotation);
564 private void constructAnnotationFromScore(AlignmentAnnotation annotation,
565 int base, int alWidth, Score scr)
567 Annotation[] elm = new Annotation[alWidth];
568 Iterator<Float> vals = scr.getScores().iterator();
569 float m = 0f, x = 0f;
570 for (int i = 0; vals.hasNext(); i++)
572 float val = vals.next().floatValue();
590 // if we're at a gapped column then skip to next ungapped position
591 if (gapMap != null && gapMap.length > 0)
595 elm[i++] = new Annotation("", "", ' ', Float.NaN);
598 elm[i] = new Annotation("", "" + val, ' ', val);
601 annotation.annotations = elm;
602 annotation.belowAlignment = true;
608 annotation.graphMax = x;
609 annotation.graphMin = m;
610 annotation.validateRangeAndDisplay();
613 protected void updateOurAnnots(List<AlignmentAnnotation> ourAnnot)
615 List<AlignmentAnnotation> our = ourAnnots;
616 ourAnnots = ourAnnot;
617 AlignmentI alignment = alignViewport.getAlignment();
622 for (AlignmentAnnotation an : our)
624 if (!ourAnnots.contains(an))
626 // remove the old annotation
627 alignment.deleteAnnotation(an);
633 ap.adjustAnnotationHeight();