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)
86 if (false) // || calcMan.isWorking(this))
88 new Thread(new Runnable()
97 } catch (InterruptedException x)
101 updateParameters(newpreset, newarguments);
108 arguments = newarguments;
109 calcMan.startWorker(this);
113 public List<Option> getJabaArguments()
115 List<Option> newargs = new ArrayList<Option>();
116 if (preset != null && preset instanceof JabaWsParamSet)
118 newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
120 if (arguments != null && arguments.size() > 0)
122 for (Argument rg : arguments)
124 if (Option.class.isAssignableFrom(rg.getClass()))
126 newargs.add((Option) rg);
136 if (aaservice == null)
140 long progressId = -1;
142 String rslt = "JOB NOT DEFINED";
150 List<compbio.data.sequence.FastaSequence> seqs = getInputSequences(alignViewport
155 calcMan.workerComplete(this);
159 AlignmentAnnotation[] aa = alignViewport.getAlignment()
160 .getAlignmentAnnotation();
161 if (guiProgress != null)
163 guiProgress.setProgressBar("JABA " + getServiceActionText(),
164 progressId = System.currentTimeMillis());
166 if (preset == null && arguments==null)
168 rslt = aaservice.analize(seqs);
174 rslt = aaservice.customAnalize(seqs, getJabaArguments());
175 } catch (WrongParameterException x)
177 throw new JobSubmissionException(
178 "Invalid paremeter set. Check Jalview implementation.", x);
182 boolean finished = false;
186 JobStatus status = aaservice.getJobStatus(rslt);
187 if (status.equals(JobStatus.FINISHED))
191 if (calcMan.isPending(this) && this instanceof AAConsClient)
194 // cancel this job and yield to the new job
197 if (aaservice.cancelJob(rslt))
199 System.err.println("Cancelled AACon job: " + rslt);
203 System.err.println("FAILED TO CANCELL AACon job: " + rslt);
206 } catch (Exception x)
220 stats = aaservice.pullExecStatistics(rslt, rpos);
221 } catch (Exception x)
224 if (x.getMessage().contains(
225 "Position in a file could not be negative!"))
227 // squash index out of bounds exception- seems to happen for
228 // disorder predictors which don't (apparently) produce any
229 // progress information and JABA server throws an exception
230 // because progress length is -1.
240 System.out.print(stats.getChunk());
241 rpos = stats.getNextPosition();
243 } while (stats != null && rpos > cpos);
245 if (!finished && status.equals(JobStatus.FAILED))
250 } catch (InterruptedException x)
260 } catch (InterruptedException x)
264 scoremanager = aaservice.getAnnotation(rslt);
265 if (scoremanager != null)
267 updateResultAnnotation(true);
269 } catch (JobSubmissionException x)
272 System.err.println("submission error:");
274 calcMan.workerCannotRun(this);
275 } catch (ResultNotAvailableException x)
277 System.err.println("collection error:\nJob ID: " + rslt);
279 calcMan.workerCannotRun(this);
281 } catch (OutOfMemoryError error)
283 calcMan.workerCannotRun(this);
286 // hconsensus = null;
287 ap.raiseOOMWarning(getServiceActionText(), error);
288 } catch (Exception x)
290 calcMan.workerCannotRun(this);
293 // hconsensus = null;
295 .println("Blacklisting worker due to unexpected exception:");
300 calcMan.workerComplete(this);
303 calcMan.workerComplete(this);
304 if (guiProgress != null && progressId!=-1)
306 guiProgress.setProgressBar("", progressId);
308 ap.paintAlignment(true);
315 public void updateAnnotation()
317 updateResultAnnotation(false);
320 public abstract void updateResultAnnotation(boolean immediate);
322 public abstract String getServiceActionText();
324 boolean submitGaps = true;
326 boolean alignedSeqs = true;
328 boolean nucleotidesAllowed = false;
330 boolean proteinAllowed = false;
333 * record sequences for mapping result back to afterwards
335 protected boolean bySequence = false;
337 Map<String, SequenceI> seqNames;
340 public List<FastaSequence> getInputSequences(AlignmentI alignment)
342 if (alignment == null || alignment.getWidth() <= 0
343 || alignment.getSequences() == null
344 // || (alignedSeqs && !alignment.isAligned() && !submitGaps)
345 || alignment.isNucleotide() ? !nucleotidesAllowed
350 List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<compbio.data.sequence.FastaSequence>();
356 seqNames = new HashMap<String, SequenceI>();
358 gapMap=new boolean[0];
359 for (SequenceI sq : ((List<SequenceI>) alignment.getSequences()))
361 if (sq.getEnd() - sq.getStart() > minlen - 1)
363 String newname = SeqsetUtils.unique_name(seqs.size() + 1);
364 // make new input sequence with or without gaps
365 if (seqNames != null)
367 seqNames.put(newname, sq);
372 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,sq.getSequenceAsString()));
373 if (gapMap==null || gapMap.length<seq.getSequence().length())
376 gapMap=new boolean[seq.getLength()];
377 System.arraycopy(tg, 0, gapMap, 0, tg.length);
378 for (int p=tg.length;p<gapMap.length;p++)
380 gapMap[p]=false; // init as a gap
383 for (int apos:sq.gapMap()) {
384 gapMap[apos]=true; // aligned.
387 seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
389 .extractGaps(jalview.util.Comparison.GapChars,
390 sq.getSequenceAsString())));
392 if (seq.getSequence().length() > ln)
394 ln = seq.getSequence().length();
398 if (alignedSeqs && submitGaps)
401 for (int i=0;i<gapMap.length;i++)
408 // try real hard to return something submittable
409 // TODO: some of AAcons measures need a minimum of two or three amino
410 // acids at each position, and aacons doesn't gracefully degrade.
411 for (int p = 0; p < seqs.size(); p++)
413 FastaSequence sq = seqs.get(p);
414 int l = sq.getSequence().length();
415 // strip gapped columns
416 char[] padded = new char[realw],orig=sq.getSequence().toCharArray();
417 for (int i=0,pp=0;i<realw; pp++)
423 padded[i++]=orig[pp];
429 seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(),
430 new String(padded)));
437 * notify manager that we have started, and wait for a free calculation slot
439 * @return true if slot is obtained and work still valid, false if another
440 * thread has done our work for us.
444 calcMan.notifyStart(this);
445 ap.paintAlignment(false);
446 while (!calcMan.notifyWorking(this))
448 if (calcMan.isWorking(this))
456 ap.paintAlignment(false);
460 } catch (Exception ex)
462 ex.printStackTrace();
465 if (alignViewport.isClosed())