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)
142 List<compbio.data.sequence.FastaSequence> seqs = getInputSequences(alignViewport
147 calcMan.workerComplete(this);
151 AlignmentAnnotation[] aa = alignViewport.getAlignment()
152 .getAlignmentAnnotation();
157 rslt = aaservice.analize(seqs);
163 rslt = aaservice.customAnalize(seqs, getJabaArguments());
164 } catch (WrongParameterException x)
166 throw new JobSubmissionException(
167 "Invalid paremeter set. Check Jalview implementation.", x);
171 boolean finished = false;
175 JobStatus status = aaservice.getJobStatus(rslt);
176 if (status.equals(JobStatus.FINISHED))
187 stats = aaservice.pullExecStatistics(rslt, rpos);
188 } catch (Exception x)
191 if (x.getMessage().contains(
192 "Position in a file could not be negative!"))
194 // squash index out of bounds exception- seems to happen for
195 // disorder predictors which don't (apparently) produce any
196 // progress information and JABA server throws an exception
197 // because progress length is -1.
207 System.out.print(stats.getChunk());
208 rpos = stats.getNextPosition();
210 } while (stats != null && rpos > cpos);
212 if (!finished && status.equals(JobStatus.FAILED))
217 } catch (InterruptedException x)
227 } catch (InterruptedException x)
231 scoremanager = aaservice.getAnnotation(rslt);
232 if (scoremanager != null)
234 updateResultAnnotation(true);
236 } catch (JobSubmissionException x)
239 System.err.println("submission error:");
241 calcMan.workerCannotRun(this);
242 } catch (ResultNotAvailableException x)
244 System.err.println("collection error:");
246 calcMan.workerCannotRun(this);
248 } catch (OutOfMemoryError error)
250 calcMan.workerCannotRun(this);
253 // hconsensus = null;
254 ap.raiseOOMWarning(getServiceActionText(), error);
255 } catch (Exception x)
257 calcMan.workerCannotRun(this);
260 // hconsensus = null;
262 .println("Blacklisting worker due to unexpected exception:");
267 calcMan.workerComplete(this);
270 ap.paintAlignment(true);
277 public void updateAnnotation()
279 updateResultAnnotation(false);
282 public abstract void updateResultAnnotation(boolean immediate);
284 public abstract String getServiceActionText();
286 boolean submitGaps = true;
288 boolean alignedSeqs = true;
290 boolean nucleotidesAllowed = false;
292 boolean proteinAllowed = false;
295 * record sequences for mapping result back to afterwards
297 protected boolean bySequence = false;
299 Map<String, SequenceI> seqNames;
301 public List<FastaSequence> getInputSequences(AlignmentI alignment)
304 if (alignment == null || alignment.getWidth() <= 0
305 || alignment.getSequences() == null
306 // || (alignedSeqs && !alignment.isAligned() && !submitGaps)
307 || alignment.isNucleotide() ? !nucleotidesAllowed
312 List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<compbio.data.sequence.FastaSequence>();
318 seqNames = new HashMap<String, SequenceI>();
320 for (SequenceI sq : ((List<SequenceI>) alignment.getSequences()))
323 if (sq.getEnd() - sq.getStart() > minlen - 1)
325 String newname = SeqsetUtils.unique_name(seqs.size() + 1);
326 // make new input sequence with or without gaps
327 if (seqNames != null)
329 seqNames.put(newname, sq);
332 seqs.add(seq=new compbio.data.sequence.FastaSequence(newname,
333 (submitGaps) ? sq.getSequenceAsString() : AlignSeq
334 .extractGaps(jalview.util.Comparison.GapChars,
335 sq.getSequenceAsString())));
336 if (seq.getSequence().length()>ln)
338 ln = seq.getSequence().length();
342 if (alignedSeqs && submitGaps)
344 // try real hard to return something submittable
345 // TODO: some of AAcons measures need a minimum of two or three amino acids at each position, and aacons doesn't gracefully degrade.
346 for (int p=0; p<seqs.size();p++)
348 FastaSequence sq=seqs.get(p);
349 int l=sq.getSequence().length();
352 char[] padded=new char[ln];
353 System.arraycopy(sq.getSequence().toCharArray(),0,padded,0,sq.getSequence().length());
358 seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(), new String(padded)));
367 * notify manager that we have started, and wait for a free calculation slot
369 * @return true if slot is obtained and work still valid, false if another
370 * thread has done our work for us.
374 calcMan.notifyStart(this);
375 // ap.paintAlignment(false);
376 while (!calcMan.notifyWorking(this))
378 if (calcMan.isWorking(this))
386 ap.paintAlignment(false);
390 } catch (Exception ex)
392 ex.printStackTrace();
395 if (alignViewport.isClosed())