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 this.service = service;
61 aaservice = (SequenceAnnotation) service.service;
65 public WsParamSetI getPreset()
70 public List<Argument> getArguments()
76 * reconfigure and restart the AAConsClient. This method will spawn a new
77 * thread that will wait until any current jobs are finished, modify the
78 * parameters and restart the conservation calculation with the new values.
83 public void updateParameters(final WsParamSetI newpreset,
84 final List<Argument> newarguments)
87 arguments = newarguments;
88 calcMan.startWorker(this);
91 public List<Option> getJabaArguments()
93 List<Option> newargs = new ArrayList<Option>();
94 if (preset != null && preset instanceof JabaWsParamSet)
96 newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
98 if (arguments != null && arguments.size() > 0)
100 for (Argument rg : arguments)
102 if (Option.class.isAssignableFrom(rg.getClass()))
104 newargs.add((Option) rg);
114 if (aaservice == null)
118 long progressId = -1;
120 String rslt = "JOB NOT DEFINED";
121 StringBuffer msg=new StringBuffer();
128 List<compbio.data.sequence.FastaSequence> seqs = getInputSequences(alignViewport
133 calcMan.workerComplete(this);
137 AlignmentAnnotation[] aa = alignViewport.getAlignment()
138 .getAlignmentAnnotation();
139 if (guiProgress != null)
141 guiProgress.setProgressBar("JABA " + getServiceActionText(),
142 progressId = System.currentTimeMillis());
144 if (preset == null && arguments==null)
146 rslt = aaservice.analize(seqs);
152 rslt = aaservice.customAnalize(seqs, getJabaArguments());
153 } catch (WrongParameterException x)
155 throw new JobSubmissionException(
156 "Invalid paremeter set. Check Jalview implementation.", x);
160 boolean finished = false;
164 JobStatus status = aaservice.getJobStatus(rslt);
165 if (status.equals(JobStatus.FINISHED))
169 if (calcMan.isPending(this) && this instanceof AAConsClient)
172 // cancel this job and yield to the new job
175 if (aaservice.cancelJob(rslt))
177 System.err.println("Cancelled AACon job: " + rslt);
181 System.err.println("FAILED TO CANCELL AACon job: " + rslt);
184 } catch (Exception x)
198 stats = aaservice.pullExecStatistics(rslt, rpos);
199 } catch (Exception x)
202 if (x.getMessage().contains(
203 "Position in a file could not be negative!"))
205 // squash index out of bounds exception- seems to happen for
206 // disorder predictors which don't (apparently) produce any
207 // progress information and JABA server throws an exception
208 // because progress length is -1.
218 System.out.print(stats.getChunk());
220 rpos = stats.getNextPosition();
222 } while (stats != null && rpos > cpos);
224 if (!finished && status.equals(JobStatus.FAILED))
229 } catch (InterruptedException x)
238 } catch (InterruptedException x)
242 scoremanager = aaservice.getAnnotation(rslt);
243 if (scoremanager != null)
245 updateResultAnnotation(true);
247 } catch (JobSubmissionException x)
250 System.err.println("submission error with "+getServiceActionText()+" :");
252 calcMan.workerCannotRun(this);
253 } catch (ResultNotAvailableException x)
255 System.err.println("collection error:\nJob ID: " + rslt);
257 calcMan.workerCannotRun(this);
259 } catch (OutOfMemoryError error)
261 calcMan.workerCannotRun(this);
264 // hconsensus = null;
265 ap.raiseOOMWarning(getServiceActionText(), error);
266 } catch (Exception x)
268 calcMan.workerCannotRun(this);
271 // hconsensus = null;
273 .println("Blacklisting worker due to unexpected exception:");
278 calcMan.workerComplete(this);
281 calcMan.workerComplete(this);
282 if (guiProgress != null && progressId!=-1)
284 guiProgress.setProgressBar("", progressId);
286 ap.paintAlignment(true);
290 // TODO: stash message somewhere in annotation or alignment view.
291 // code below shows result in a text box popup
292 /* jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
293 cap.setText(msg.toString());
294 jalview.gui.Desktop.addInternalFrame(cap, "Job Status for "+getServiceActionText(), 600, 400); */
301 public void updateAnnotation()
303 updateResultAnnotation(false);
306 public abstract void updateResultAnnotation(boolean immediate);
308 public abstract String getServiceActionText();
310 boolean submitGaps = true;
312 boolean alignedSeqs = true;
314 boolean nucleotidesAllowed = false;
316 boolean proteinAllowed = false;
319 * record sequences for mapping result back to afterwards
321 protected boolean bySequence = false;
323 Map<String, SequenceI> seqNames;
326 public List<FastaSequence> getInputSequences(AlignmentI alignment)
328 if (alignment == null || alignment.getWidth() <= 0
329 || alignment.getSequences() == null
330 // || (alignedSeqs && !alignment.isAligned() && !submitGaps)
331 || alignment.isNucleotide() ? !nucleotidesAllowed
336 List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<compbio.data.sequence.FastaSequence>();
342 seqNames = new HashMap<String, SequenceI>();
344 gapMap=new boolean[0];
345 for (SequenceI sq : ((List<SequenceI>) alignment.getSequences()))
347 if (sq.getEnd() - sq.getStart() > minlen - 1)
349 String newname = SeqsetUtils.unique_name(seqs.size() + 1);
350 // make new input sequence with or without gaps
351 if (seqNames != null)
353 seqNames.put(newname, sq);
358 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,sq.getSequenceAsString()));
359 if (gapMap==null || gapMap.length<seq.getSequence().length())
362 gapMap=new boolean[seq.getLength()];
363 System.arraycopy(tg, 0, gapMap, 0, tg.length);
364 for (int p=tg.length;p<gapMap.length;p++)
366 gapMap[p]=false; // init as a gap
369 for (int apos:sq.gapMap()) {
370 gapMap[apos]=true; // aligned.
373 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
375 .extractGaps(jalview.util.Comparison.GapChars,
376 sq.getSequenceAsString())));
378 if (seq.getSequence().length() > ln)
380 ln = seq.getSequence().length();
384 if (alignedSeqs && submitGaps)
387 for (int i=0;i<gapMap.length;i++)
394 // try real hard to return something submittable
395 // TODO: some of AAcons measures need a minimum of two or three amino
396 // acids at each position, and aacons doesn't gracefully degrade.
397 for (int p = 0; p < seqs.size(); p++)
399 FastaSequence sq = seqs.get(p);
400 int l = sq.getSequence().length();
401 // strip gapped columns
402 char[] padded = new char[realw],orig=sq.getSequence().toCharArray();
403 for (int i=0,pp=0;i<realw; pp++)
409 padded[i++]=orig[pp];
415 seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(),
416 new String(padded)));
423 * notify manager that we have started, and wait for a free calculation slot
425 * @return true if slot is obtained and work still valid, false if another
426 * thread has done our work for us.
430 calcMan.notifyStart(this);
431 ap.paintAlignment(false);
432 while (!calcMan.notifyWorking(this))
434 if (calcMan.isWorking(this))
442 ap.paintAlignment(false);
446 } catch (Exception ex)
448 ex.printStackTrace();
451 if (alignViewport.isClosed())