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.workers.AlignCalcWorker;
12 import jalview.ws.jws2.dm.JabaWsParamSet;
13 import jalview.ws.jws2.jabaws2.Jws2Instance;
14 import jalview.ws.params.WsParamSetI;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
21 import compbio.data.msa.SequenceAnnotation;
22 import compbio.data.sequence.FastaSequence;
23 import compbio.data.sequence.ScoreManager;
24 import compbio.metadata.Argument;
25 import compbio.metadata.ChunkHolder;
26 import compbio.metadata.JobStatus;
27 import compbio.metadata.JobSubmissionException;
28 import compbio.metadata.Option;
29 import compbio.metadata.ResultNotAvailableException;
30 import compbio.metadata.WrongParameterException;
32 public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
35 @SuppressWarnings("unchecked")
36 protected SequenceAnnotation aaservice;
38 protected ScoreManager scoremanager;
40 protected WsParamSetI preset;
42 protected List<Argument> arguments;
44 public JabawsAlignCalcWorker(AlignViewportI alignViewport,
45 AlignmentViewPanel alignPanel)
47 super(alignViewport, alignPanel);
50 public JabawsAlignCalcWorker(Jws2Instance service, AlignFrame alignFrame,
51 WsParamSetI preset, List<Argument> paramset)
53 this(alignFrame.getCurrentView(), alignFrame.alignPanel);
55 this.arguments = paramset;
56 aaservice = (SequenceAnnotation) service.service;
60 public WsParamSetI getPreset()
65 public List<Argument> getArguments()
71 * reconfigure and restart the AAConsClient. This method will spawn a new
72 * thread that will wait until any current jobs are finished, modify the
73 * parameters and restart the conservation calculation with the new values.
78 public void updateParameters(final WsParamSetI newpreset,
79 final List<Argument> newarguments)
81 if (calcMan.isWorking(this))
83 new Thread(new Runnable()
92 } catch (InterruptedException x)
96 updateParameters(newpreset, newarguments);
103 arguments = newarguments;
104 calcMan.startWorker(this);
108 public List<Option> getJabaArguments()
110 List<Option> newargs = new ArrayList<Option>();
111 if (preset != null && preset instanceof JabaWsParamSet)
113 newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
115 if (arguments != null && arguments.size() > 0)
117 for (Argument rg : arguments)
119 if (Option.class.isAssignableFrom(rg.getClass()))
121 newargs.add((Option) rg);
131 if (aaservice == null)
136 String rslt="JOB NOT DEFINED";
144 List<compbio.data.sequence.FastaSequence> seqs = getInputSequences(alignViewport
149 calcMan.workerComplete(this);
153 AlignmentAnnotation[] aa = alignViewport.getAlignment()
154 .getAlignmentAnnotation();
158 rslt = aaservice.analize(seqs);
164 rslt = aaservice.customAnalize(seqs, getJabaArguments());
165 } catch (WrongParameterException x)
167 throw new JobSubmissionException(
168 "Invalid paremeter set. Check Jalview implementation.", x);
172 boolean finished = false;
176 JobStatus status = aaservice.getJobStatus(rslt);
177 if (status.equals(JobStatus.FINISHED))
188 stats = aaservice.pullExecStatistics(rslt, rpos);
189 } catch (Exception x)
192 if (x.getMessage().contains(
193 "Position in a file could not be negative!"))
195 // squash index out of bounds exception- seems to happen for
196 // disorder predictors which don't (apparently) produce any
197 // progress information and JABA server throws an exception
198 // because progress length is -1.
208 System.out.print(stats.getChunk());
209 rpos = stats.getNextPosition();
211 } while (stats != null && rpos > cpos);
213 if (!finished && status.equals(JobStatus.FAILED))
218 } catch (InterruptedException x)
228 } catch (InterruptedException x)
232 scoremanager = aaservice.getAnnotation(rslt);
233 if (scoremanager != null)
235 updateResultAnnotation(true);
237 } catch (JobSubmissionException x)
240 System.err.println("submission error:");
242 calcMan.workerCannotRun(this);
243 } catch (ResultNotAvailableException x)
245 System.err.println("collection error:\nJob ID: "+rslt);
247 calcMan.workerCannotRun(this);
249 } catch (OutOfMemoryError error)
251 calcMan.workerCannotRun(this);
254 // hconsensus = null;
255 ap.raiseOOMWarning(getServiceActionText(), error);
256 } catch (Exception x)
258 calcMan.workerCannotRun(this);
261 // hconsensus = null;
263 .println("Blacklisting worker due to unexpected exception:");
268 calcMan.workerComplete(this);
271 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)));
368 * notify manager that we have started, and wait for a free calculation slot
370 * @return true if slot is obtained and work still valid, false if another
371 * thread has done our work for us.
375 calcMan.notifyStart(this);
376 // ap.paintAlignment(false);
377 while (!calcMan.notifyWorking(this))
379 if (calcMan.isWorking(this))
387 ap.paintAlignment(false);
391 } catch (Exception ex)
393 ex.printStackTrace();
396 if (alignViewport.isClosed())