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.Annotation;
10 import jalview.datamodel.SequenceGroup;
11 import jalview.datamodel.SequenceI;
12 import jalview.gui.AlignFrame;
13 import jalview.workers.AlignCalcWorker;
14 import jalview.ws.jws2.dm.JabaWsParamSet;
15 import jalview.ws.jws2.jabaws2.Jws2Instance;
16 import jalview.ws.params.WsParamSetI;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
23 import compbio.data.msa.SequenceAnnotation;
24 import compbio.data.sequence.FastaSequence;
25 import compbio.data.sequence.ScoreManager;
26 import compbio.metadata.Argument;
27 import compbio.metadata.ChunkHolder;
28 import compbio.metadata.JobStatus;
29 import compbio.metadata.JobSubmissionException;
30 import compbio.metadata.Option;
31 import compbio.metadata.ResultNotAvailableException;
32 import compbio.metadata.WrongParameterException;
34 public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
37 @SuppressWarnings("unchecked")
38 protected SequenceAnnotation aaservice;
40 protected ScoreManager scoremanager;
42 protected WsParamSetI preset;
44 protected List<Argument> arguments;
46 public JabawsAlignCalcWorker(AlignViewportI alignViewport,
47 AlignmentViewPanel alignPanel)
49 super(alignViewport, alignPanel);
52 public JabawsAlignCalcWorker(Jws2Instance service, AlignFrame alignFrame,
53 WsParamSetI preset, List<Argument> paramset)
55 this(alignFrame.getCurrentView(), alignFrame.alignPanel);
57 this.arguments = paramset;
58 aaservice = (SequenceAnnotation) service.service;
62 public WsParamSetI getPreset()
67 public List<Argument> getArguments()
73 * reconfigure and restart the AAConsClient. This method will spawn a new
74 * thread that will wait until any current jobs are finished, modify the
75 * parameters and restart the conservation calculation with the new values.
80 public void updateParameters(final WsParamSetI newpreset,
81 final List<Argument> newarguments)
83 if (calcMan.isWorking(this))
85 new Thread(new Runnable()
94 } catch (InterruptedException x)
98 updateParameters(newpreset, newarguments);
105 arguments = newarguments;
106 calcMan.startWorker(this);
110 public List<Option> getJabaArguments()
112 List<Option> newargs = new ArrayList<Option>();
113 if (preset != null && preset instanceof JabaWsParamSet)
115 newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
117 if (arguments != null && arguments.size() > 0)
119 for (Argument rg : arguments)
121 if (Option.class.isAssignableFrom(rg.getClass()))
123 newargs.add((Option) rg);
133 if (aaservice == null)
144 List<compbio.data.sequence.FastaSequence> seqs = getInputSequences(alignViewport
149 calcMan.workerComplete(this);
153 AlignmentAnnotation[] aa = alignViewport.getAlignment()
154 .getAlignmentAnnotation();
159 rslt = aaservice.analize(seqs);
165 rslt = aaservice.customAnalize(seqs, getJabaArguments());
166 } catch (WrongParameterException x)
168 throw new JobSubmissionException(
169 "Invalid paremeter set. Check Jalview implementation.", x);
173 boolean finished = false;
177 JobStatus status = aaservice.getJobStatus(rslt);
178 if (status.equals(JobStatus.FINISHED))
189 stats = aaservice.pullExecStatistics(rslt, rpos);
190 } catch (Exception x)
193 if (x.getMessage().contains(
194 "Position in a file could not be negative!"))
196 // squash index out of bounds exception- seems to happen for
197 // disorder predictors which don't (apparently) produce any
198 // progress information and JABA server throws an exception
199 // because progress length is -1.
209 System.out.print(stats.getChunk());
210 rpos = stats.getNextPosition();
212 } while (stats != null && rpos > cpos);
214 if (!finished && status.equals(JobStatus.FAILED))
219 } catch (InterruptedException x)
229 } catch (InterruptedException x)
233 scoremanager = aaservice.getAnnotation(rslt);
234 if (scoremanager != null)
236 updateResultAnnotation(true);
238 } catch (JobSubmissionException x)
241 System.err.println("submission error:");
243 calcMan.workerCannotRun(this);
244 } catch (ResultNotAvailableException x)
246 System.err.println("collection error:");
248 calcMan.workerCannotRun(this);
250 } catch (OutOfMemoryError error)
252 calcMan.workerCannotRun(this);
255 // hconsensus = null;
256 ap.raiseOOMWarning(getServiceActionText(), error);
257 } catch (Exception x)
259 calcMan.workerCannotRun(this);
262 // hconsensus = null;
264 .println("Blacklisting worker due to unexpected exception:");
269 calcMan.workerComplete(this);
272 ap.paintAlignment(true);
278 public void updateAnnotation()
280 updateResultAnnotation(false);
283 public abstract void updateResultAnnotation(boolean immediate);
285 public abstract String getServiceActionText();
287 boolean submitGaps = true;
289 boolean alignedSeqs = true;
291 boolean nucleotidesAllowed = false;
293 boolean proteinAllowed = false;
296 * record sequences for mapping result back to afterwards
298 protected boolean bySequence = false;
300 Map<String, SequenceI> seqNames;
302 public List<FastaSequence> getInputSequences(AlignmentI alignment)
305 if (alignment == null || alignment.getWidth() <= 0
306 || alignment.getSequences() == null
307 // || (alignedSeqs && !alignment.isAligned() && !submitGaps)
308 || alignment.isNucleotide() ? !nucleotidesAllowed
313 List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<compbio.data.sequence.FastaSequence>();
319 seqNames = new HashMap<String, SequenceI>();
321 for (SequenceI sq : ((List<SequenceI>) alignment.getSequences()))
324 if (sq.getEnd() - sq.getStart() > minlen - 1)
326 String newname = SeqsetUtils.unique_name(seqs.size() + 1);
327 // make new input sequence with or without gaps
328 if (seqNames != null)
330 seqNames.put(newname, sq);
333 seqs.add(seq=new compbio.data.sequence.FastaSequence(newname,
334 (submitGaps) ? sq.getSequenceAsString() : AlignSeq
335 .extractGaps(jalview.util.Comparison.GapChars,
336 sq.getSequenceAsString())));
337 if (seq.getSequence().length()>ln)
339 ln = seq.getSequence().length();
343 if (alignedSeqs && submitGaps)
345 // try real hard to return something submittable
346 // TODO: some of AAcons measures need a minimum of two or three amino acids at each position, and aacons doesn't gracefully degrade.
347 for (int p=0; p<seqs.size();p++)
349 FastaSequence sq=seqs.get(p);
350 int l=sq.getSequence().length();
353 char[] padded=new char[ln];
354 System.arraycopy(sq.getSequence().toCharArray(),0,padded,0,sq.getSequence().length());
359 seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(), new String(padded)));
367 protected AlignmentAnnotation findOrCreate(String name, boolean autoCalc,
368 SequenceI seqRef, SequenceGroup groupRef)
370 for (AlignmentAnnotation annot : alignViewport.getAlignment()
371 .getAlignmentAnnotation())
373 if (annot.autoCalculated == autoCalc
374 && annot.getCalcId().equals(name)
375 && annot.sequenceRef == seqRef && annot.groupRef == groupRef)
380 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
381 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
382 annot.hasText = false;
383 annot.setCalcId(new String(name));
384 annot.autoCalculated = autoCalc;
387 annot.setSequenceRef(seqRef);
389 annot.groupRef = groupRef;
390 alignViewport.getAlignment().addAnnotation(annot);
396 * notify manager that we have started, and wait for a free calculation slot
398 * @return true if slot is obtained and work still valid, false if another
399 * thread has done our work for us.
403 calcMan.notifyStart(this);
404 // ap.paintAlignment(false);
405 while (!calcMan.notifyWorking(this))
407 if (calcMan.isWorking(this))
415 ap.paintAlignment(false);
419 } catch (Exception ex)
421 ex.printStackTrace();
424 if (alignViewport.isClosed())