2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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 jalview.analysis.AlignSeq;
24 import jalview.analysis.SeqsetUtils;
25 import jalview.api.AlignViewportI;
26 import jalview.api.AlignmentViewPanel;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.AnnotatedCollectionI;
30 import jalview.datamodel.SequenceI;
31 import jalview.gui.AlignFrame;
32 import jalview.gui.IProgressIndicator;
33 import jalview.schemes.ResidueProperties;
34 import jalview.workers.AlignCalcWorker;
35 import jalview.ws.jws2.dm.AAConSettings;
36 import jalview.ws.jws2.dm.JabaWsParamSet;
37 import jalview.ws.jws2.jabaws2.Jws2Instance;
38 import jalview.ws.params.WsParamSetI;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.List;
45 import compbio.data.sequence.FastaSequence;
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;
53 public abstract class AbstractJabaCalcWorker extends AlignCalcWorker
56 protected Jws2Instance service;
58 protected WsParamSetI preset;
60 protected List<Argument> arguments;
62 protected IProgressIndicator guiProgress;
64 protected boolean submitGaps = true;
67 * by default, we filter out non-standard residues before submission
69 protected boolean filterNonStandardResidues = true;
72 * Recover any existing parameters for this service
74 protected void initViewportParams()
76 if (getCalcId() != null)
78 ((jalview.gui.AlignViewport) alignViewport).setCalcIdSettingsFor(
80 new AAConSettings(true, service, this.preset,
81 (arguments != null) ? JabaParamStore
82 .getJwsArgsfromJaba(arguments) : null), true);
88 * @return null or a string used to recover all annotation generated by this
91 public abstract String getCalcId();
93 public WsParamSetI getPreset()
98 public List<Argument> getArguments()
104 * reconfigure and restart the AAConClient. This method will spawn a new
105 * thread that will wait until any current jobs are finished, modify the
106 * parameters and restart the conservation calculation with the new values.
109 * @param newarguments
111 public void updateParameters(final WsParamSetI newpreset,
112 final List<Argument> newarguments)
115 arguments = newarguments;
116 calcMan.startWorker(this);
117 initViewportParams();
120 public List<Option> getJabaArguments()
122 List<Option> newargs = new ArrayList<>();
123 if (preset != null && preset instanceof JabaWsParamSet)
125 newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
127 if (arguments != null && arguments.size() > 0)
129 for (Argument rg : arguments)
131 if (Option.class.isAssignableFrom(rg.getClass()))
133 newargs.add((Option) rg);
140 protected boolean alignedSeqs = true;
142 protected boolean nucleotidesAllowed = false;
144 protected boolean proteinAllowed = false;
147 * record sequences for mapping result back to afterwards
149 protected boolean bySequence = false;
151 protected Map<String, SequenceI> seqNames;
153 protected boolean[] gapMap;
161 public AbstractJabaCalcWorker(AlignViewportI alignViewport,
162 AlignmentViewPanel alignPanel)
164 super(alignViewport, alignPanel);
167 public AbstractJabaCalcWorker(Jws2Instance service,
168 AlignFrame alignFrame, WsParamSetI preset, List<Argument> paramset)
170 this(alignFrame.getCurrentView(), alignFrame.alignPanel);
171 this.guiProgress = alignFrame;
172 this.preset = preset;
173 this.arguments = paramset;
174 this.service = service;
179 * @return true if the submission thread should attempt to submit data
181 abstract boolean hasService();
183 volatile String rslt = "JOB NOT DEFINED";
192 long progressId = -1;
194 int serverErrorsLeft = 3;
196 StringBuffer msg = new StringBuffer();
203 List<compbio.data.sequence.FastaSequence> seqs = getInputSequences(
204 alignViewport.getAlignment(),
205 bySequence ? alignViewport.getSelectionGroup() : null);
207 if (seqs == null || !checkValidInputSeqs(true, seqs))
209 calcMan.workerComplete(this);
213 AlignmentAnnotation[] aa = alignViewport.getAlignment()
214 .getAlignmentAnnotation();
215 if (guiProgress != null)
217 guiProgress.setProgressBar("JABA " + getServiceActionText(),
218 progressId = System.currentTimeMillis());
220 rslt = submitToService(seqs);
222 boolean finished = false;
226 JobStatus status = getJobStatus(rslt);
227 if (status.equals(JobStatus.FINISHED))
231 if (calcMan.isPending(this) && isInteractiveUpdate())
234 // cancel this job and yield to the new job
239 System.err.println("Cancelled AACon job: " + rslt);
243 System.err.println("FAILED TO CANCEL AACon job: " + rslt);
246 } catch (Exception x)
250 rslt = "CANCELLED JOB";
254 ChunkHolder stats = null;
258 boolean retry = false;
263 stats = pullExecStatistics(rslt, rpos);
264 } catch (Exception x)
267 if (x.getMessage().contains(
268 "Position in a file could not be negative!"))
270 // squash index out of bounds exception- seems to happen for
271 // disorder predictors which don't (apparently) produce any
272 // progress information and JABA server throws an exception
273 // because progress length is -1.
278 if (--serverErrorsLeft > 0)
284 } catch (InterruptedException q)
298 System.out.print(stats.getChunk());
300 rpos = stats.getNextPosition();
302 } while (stats != null && rpos > cpos);
304 if (!finished && status.equals(JobStatus.FAILED))
309 } catch (InterruptedException x)
315 if (serverErrorsLeft > 0)
320 } catch (InterruptedException x)
323 if (collectAnnotationResultsFor(rslt))
325 jalview.bin.Cache.log
326 .debug("Updating result annotation from Job " + rslt
327 + " at " + service.getUri());
328 updateResultAnnotation(true);
329 ap.adjustAnnotationHeight();
334 catch (JobSubmissionException x)
337 System.err.println("submission error with " + getServiceActionText()
340 calcMan.disableWorker(this);
341 } catch (ResultNotAvailableException x)
343 System.err.println("collection error:\nJob ID: " + rslt);
345 calcMan.disableWorker(this);
347 } catch (OutOfMemoryError error)
349 calcMan.disableWorker(this);
352 // hconsensus = null;
353 ap.raiseOOMWarning(getServiceActionText(), error);
354 } catch (Exception x)
356 calcMan.disableWorker(this);
359 // hconsensus = null;
361 .println("Blacklisting worker due to unexpected exception:");
366 calcMan.workerComplete(this);
369 calcMan.workerComplete(this);
370 if (guiProgress != null && progressId != -1)
372 guiProgress.setProgressBar("", progressId);
374 ap.paintAlignment(true);
376 if (msg.length() > 0)
378 // TODO: stash message somewhere in annotation or alignment view.
379 // code below shows result in a text box popup
381 * jalview.gui.CutAndPasteTransfer cap = new
382 * jalview.gui.CutAndPasteTransfer(); cap.setText(msg.toString());
383 * jalview.gui.Desktop.addInternalFrame(cap,
384 * "Job Status for "+getServiceActionText(), 600, 400);
392 * validate input for dynamic/non-dynamic update context
396 * @return true if input is valid
398 abstract boolean checkValidInputSeqs(boolean dynamic,
399 List<FastaSequence> seqs);
401 abstract String submitToService(
402 List<compbio.data.sequence.FastaSequence> seqs)
403 throws JobSubmissionException;
405 abstract boolean cancelJob(String rslt) throws Exception;
407 abstract JobStatus getJobStatus(String rslt) throws Exception;
409 abstract ChunkHolder pullExecStatistics(String rslt, long rpos);
411 abstract boolean collectAnnotationResultsFor(String rslt)
412 throws ResultNotAvailableException;
414 public void cancelCurrentJob()
421 System.err.println("Cancelled job " + id);
425 System.err.println("Job " + id + " couldn't be cancelled.");
427 } catch (Exception q)
434 * Interactive updating. Analysis calculations that work on the currently
435 * displayed alignment data should cancel existing jobs when the input data
438 * @return true if a running job should be cancelled because new input data is
439 * available for analysis
441 abstract boolean isInteractiveUpdate();
443 public List<FastaSequence> getInputSequences(AlignmentI alignment,
444 AnnotatedCollectionI inputSeqs)
446 if (alignment == null || alignment.getWidth() <= 0
447 || alignment.getSequences() == null || alignment.isNucleotide() ? !nucleotidesAllowed
452 if (inputSeqs == null || inputSeqs.getWidth() <= 0
453 || inputSeqs.getSequences() == null
454 || inputSeqs.getSequences().size() < 1)
456 inputSeqs = alignment;
459 List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<>();
465 seqNames = new HashMap<>();
467 gapMap = new boolean[0];
468 start = inputSeqs.getStartRes();
469 end = inputSeqs.getEndRes();
471 for (SequenceI sq : (inputSeqs.getSequences()))
473 if (bySequence ? sq.findPosition(end + 1)
474 - sq.findPosition(start + 1) > minlen - 1 : sq.getEnd()
475 - sq.getStart() > minlen - 1)
477 String newname = SeqsetUtils.unique_name(seqs.size() + 1);
478 // make new input sequence with or without gaps
479 if (seqNames != null)
481 seqNames.put(newname, sq);
486 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
487 sq.getSequenceAsString()));
488 if (gapMap == null || gapMap.length < seq.getSequence().length())
490 boolean[] tg = gapMap;
491 gapMap = new boolean[seq.getLength()];
492 System.arraycopy(tg, 0, gapMap, 0, tg.length);
493 for (int p = tg.length; p < gapMap.length; p++)
495 gapMap[p] = false; // init as a gap
498 for (int apos : sq.gapMap())
500 char sqc = sq.getCharAt(apos);
501 if (!filterNonStandardResidues
502 || (sq.isProtein() ? ResidueProperties.aaIndex[sqc] < 20
503 : ResidueProperties.nucleotideIndex[sqc] < 5))
505 gapMap[apos] = true; // aligned and real amino acid residue
512 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
513 AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
514 sq.getSequenceAsString(start, end + 1))));
516 if (seq.getSequence().length() > ln)
518 ln = seq.getSequence().length();
522 if (alignedSeqs && submitGaps)
525 for (int i = 0; i < gapMap.length; i++)
532 // try real hard to return something submittable
533 // TODO: some of AAcon measures need a minimum of two or three amino
534 // acids at each position, and AAcon doesn't gracefully degrade.
535 for (int p = 0; p < seqs.size(); p++)
537 FastaSequence sq = seqs.get(p);
538 int l = sq.getSequence().length();
539 // strip gapped columns
540 char[] padded = new char[realw], orig = sq.getSequence()
542 for (int i = 0, pp = 0; i < realw; pp++)
546 if (orig.length > pp)
548 padded[i++] = orig[pp];
556 seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(),
557 new String(padded)));
564 public void updateAnnotation()
566 updateResultAnnotation(false);
569 public abstract void updateResultAnnotation(boolean immediate);
571 public abstract String getServiceActionText();
574 * notify manager that we have started, and wait for a free calculation slot
576 * @return true if slot is obtained and work still valid, false if another
577 * thread has done our work for us.
579 protected boolean checkDone()
581 calcMan.notifyStart(this);
582 ap.paintAlignment(false);
583 while (!calcMan.notifyWorking(this))
585 if (calcMan.isWorking(this))
593 ap.paintAlignment(false);
597 } catch (Exception ex)
599 ex.printStackTrace();
602 if (alignViewport.isClosed())
610 protected void updateOurAnnots(List<AlignmentAnnotation> ourAnnot)
612 List<AlignmentAnnotation> our = ourAnnots;
613 ourAnnots = ourAnnot;
614 AlignmentI alignment = alignViewport.getAlignment();
619 for (AlignmentAnnotation an : our)
621 if (!ourAnnots.contains(an))
623 // remove the old annotation
624 alignment.deleteAnnotation(an);
630 ap.adjustAnnotationHeight();