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.datamodel.AlignmentAnnotation;
8 import jalview.datamodel.AlignmentI;
9 import jalview.datamodel.SequenceI;
10 import jalview.gui.AlignFrame;
11 import jalview.gui.IProgressIndicator;
12 import jalview.workers.AlignCalcWorker;
13 import jalview.ws.jws2.dm.JabaWsParamSet;
14 import jalview.ws.jws2.jabaws2.Jws2Instance;
15 import jalview.ws.params.WsParamSetI;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
22 import compbio.data.msa.SequenceAnnotation;
23 import compbio.data.sequence.FastaSequence;
24 import compbio.data.sequence.ScoreManager;
25 import compbio.metadata.Argument;
26 import compbio.metadata.ChunkHolder;
27 import compbio.metadata.JobStatus;
28 import compbio.metadata.JobSubmissionException;
29 import compbio.metadata.Option;
30 import compbio.metadata.ResultNotAvailableException;
31 import compbio.metadata.WrongParameterException;
33 public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
36 @SuppressWarnings("unchecked")
37 protected SequenceAnnotation aaservice;
39 protected ScoreManager scoremanager;
41 protected WsParamSetI preset;
43 protected List<Argument> arguments;
45 public JabawsAlignCalcWorker(AlignViewportI alignViewport,
46 AlignmentViewPanel alignPanel)
48 super(alignViewport, alignPanel);
51 IProgressIndicator guiProgress;
53 public JabawsAlignCalcWorker(Jws2Instance service, AlignFrame alignFrame,
54 WsParamSetI preset, List<Argument> paramset)
56 this(alignFrame.getCurrentView(), alignFrame.alignPanel);
57 this.guiProgress = alignFrame;
59 this.arguments = paramset;
60 aaservice = (SequenceAnnotation) service.service;
64 public WsParamSetI getPreset()
69 public List<Argument> getArguments()
75 * reconfigure and restart the AAConsClient. This method will spawn a new
76 * thread that will wait until any current jobs are finished, modify the
77 * parameters and restart the conservation calculation with the new values.
82 public void updateParameters(final WsParamSetI newpreset,
83 final List<Argument> newarguments)
85 if (calcMan.isWorking(this))
87 new Thread(new Runnable()
96 } catch (InterruptedException x)
100 updateParameters(newpreset, newarguments);
107 arguments = newarguments;
108 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 String rslt = "JOB NOT DEFINED";
149 List<compbio.data.sequence.FastaSequence> seqs = getInputSequences(alignViewport
154 calcMan.workerComplete(this);
158 AlignmentAnnotation[] aa = alignViewport.getAlignment()
159 .getAlignmentAnnotation();
160 if (guiProgress != null)
162 guiProgress.setProgressBar("JABA " + getServiceActionText(),
163 progressId = System.currentTimeMillis());
167 rslt = aaservice.analize(seqs);
173 rslt = aaservice.customAnalize(seqs, getJabaArguments());
174 } catch (WrongParameterException x)
176 throw new JobSubmissionException(
177 "Invalid paremeter set. Check Jalview implementation.", x);
181 boolean finished = false;
185 JobStatus status = aaservice.getJobStatus(rslt);
186 if (status.equals(JobStatus.FINISHED))
197 stats = aaservice.pullExecStatistics(rslt, rpos);
198 } catch (Exception x)
201 if (x.getMessage().contains(
202 "Position in a file could not be negative!"))
204 // squash index out of bounds exception- seems to happen for
205 // disorder predictors which don't (apparently) produce any
206 // progress information and JABA server throws an exception
207 // because progress length is -1.
217 System.out.print(stats.getChunk());
218 rpos = stats.getNextPosition();
220 } while (stats != null && rpos > cpos);
222 if (!finished && status.equals(JobStatus.FAILED))
227 } catch (InterruptedException x)
237 } catch (InterruptedException x)
241 scoremanager = aaservice.getAnnotation(rslt);
242 if (scoremanager != null)
244 updateResultAnnotation(true);
246 } catch (JobSubmissionException x)
249 System.err.println("submission error:");
251 calcMan.workerCannotRun(this);
252 } catch (ResultNotAvailableException x)
254 System.err.println("collection error:\nJob ID: " + rslt);
256 calcMan.workerCannotRun(this);
258 } catch (OutOfMemoryError error)
260 calcMan.workerCannotRun(this);
263 // hconsensus = null;
264 ap.raiseOOMWarning(getServiceActionText(), error);
265 } catch (Exception x)
267 calcMan.workerCannotRun(this);
270 // hconsensus = null;
272 .println("Blacklisting worker due to unexpected exception:");
277 calcMan.workerComplete(this);
280 calcMan.workerComplete(this);
281 if (guiProgress != null)
283 guiProgress.setProgressBar("", progressId);
285 ap.paintAlignment(true);
292 public void updateAnnotation()
294 updateResultAnnotation(false);
297 public abstract void updateResultAnnotation(boolean immediate);
299 public abstract String getServiceActionText();
301 boolean submitGaps = true;
303 boolean alignedSeqs = true;
305 boolean nucleotidesAllowed = false;
307 boolean proteinAllowed = false;
310 * record sequences for mapping result back to afterwards
312 protected boolean bySequence = false;
314 Map<String, SequenceI> seqNames;
316 public List<FastaSequence> getInputSequences(AlignmentI alignment)
319 if (alignment == null || alignment.getWidth() <= 0
320 || alignment.getSequences() == null
321 // || (alignedSeqs && !alignment.isAligned() && !submitGaps)
322 || alignment.isNucleotide() ? !nucleotidesAllowed
327 List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<compbio.data.sequence.FastaSequence>();
333 seqNames = new HashMap<String, SequenceI>();
335 for (SequenceI sq : ((List<SequenceI>) alignment.getSequences()))
338 if (sq.getEnd() - sq.getStart() > minlen - 1)
340 String newname = SeqsetUtils.unique_name(seqs.size() + 1);
341 // make new input sequence with or without gaps
342 if (seqNames != null)
344 seqNames.put(newname, sq);
347 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
348 (submitGaps) ? sq.getSequenceAsString() : AlignSeq
349 .extractGaps(jalview.util.Comparison.GapChars,
350 sq.getSequenceAsString())));
351 if (seq.getSequence().length() > ln)
353 ln = seq.getSequence().length();
357 if (alignedSeqs && submitGaps)
359 // try real hard to return something submittable
360 // TODO: some of AAcons measures need a minimum of two or three amino
361 // acids at each position, and aacons doesn't gracefully degrade.
362 for (int p = 0; p < seqs.size(); p++)
364 FastaSequence sq = seqs.get(p);
365 int l = sq.getSequence().length();
368 char[] padded = new char[ln];
369 System.arraycopy(sq.getSequence().toCharArray(), 0, padded, 0, sq
370 .getSequence().length());
375 seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(),
376 new String(padded)));
385 * notify manager that we have started, and wait for a free calculation slot
387 * @return true if slot is obtained and work still valid, false if another
388 * thread has done our work for us.
392 calcMan.notifyStart(this);
393 // ap.paintAlignment(false);
394 while (!calcMan.notifyWorking(this))
396 if (calcMan.isWorking(this))
404 ap.paintAlignment(false);
408 } catch (Exception ex)
410 ex.printStackTrace();
413 if (alignViewport.isClosed())