1 package jalview.ws.jws2;
4 import jalview.analysis.AlignSeq;
5 import jalview.analysis.SeqsetUtils;
6 import jalview.api.AlignViewportI;
7 import jalview.api.AlignmentViewPanel;
8 import jalview.datamodel.AlignmentAnnotation;
9 import jalview.datamodel.AlignmentI;
10 import jalview.datamodel.Annotation;
11 import jalview.datamodel.SequenceGroup;
12 import jalview.datamodel.SequenceI;
13 import jalview.gui.AlignFrame;
14 import jalview.workers.AlignCalcWorker;
15 import jalview.ws.jws2.dm.JabaWsParamSet;
16 import jalview.ws.jws2.jabaws2.Jws2Instance;
17 import jalview.ws.params.WsParamSetI;
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
23 import java.util.TreeSet;
25 import compbio.data.msa.SequenceAnnotation;
26 import compbio.data.sequence.Score;
27 import compbio.data.sequence.ScoreManager;
28 import compbio.metadata.Argument;
29 import compbio.metadata.ChunkHolder;
30 import compbio.metadata.JobStatus;
31 import compbio.metadata.JobSubmissionException;
32 import compbio.metadata.Option;
33 import compbio.metadata.ResultNotAvailableException;
34 import compbio.metadata.WrongParameterException;
36 public class AAConsClient extends AlignCalcWorker
39 public AAConsClient(AlignViewportI alignViewport,
40 AlignmentViewPanel alignPanel)
42 super(alignViewport, alignPanel);
45 @SuppressWarnings("unchecked")
46 SequenceAnnotation aaservice;
48 private ScoreManager scoremanager;
50 private WsParamSetI preset;
52 private List<Argument> arguments;
54 public WsParamSetI getPreset()
58 public List<Argument> getArguments()
63 * reconfigure and restart the AAConsClient. This method will spawn a new thread that will wait until any current jobs are finished, modify the parameters and restart the conservation calculation with the new values.
67 public void updateParameters(final WsParamSetI newpreset, final List<Argument> newarguments)
69 if (calcMan.isWorking(this))
71 new Thread(new Runnable() {
76 try {Thread.sleep(200);
77 } catch (InterruptedException x) {};
78 updateParameters(newpreset, newarguments);
84 arguments=newarguments;
85 calcMan.startWorker(this);
90 public AAConsClient(Jws2Instance service, AlignFrame alignFrame,
91 WsParamSetI preset, List<Argument> paramset)
93 this(alignFrame.getCurrentView(), alignFrame.alignPanel);
95 this.arguments = paramset;
96 aaservice = (SequenceAnnotation) service.service;
100 public List<Option> getJabaArguments()
102 List<Option> newargs = new ArrayList<Option>();
103 if (preset != null && preset instanceof JabaWsParamSet)
105 newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
107 if (arguments != null && arguments.size() > 0)
109 for (Argument rg : arguments)
111 if (Option.class.isAssignableFrom(rg.getClass()))
113 newargs.add((Option) rg);
123 if (aaservice == null)
134 AlignmentI alignment = alignViewport.getAlignment();
138 if (alignment == null || (aWidth = alignment.getWidth()) < 0 || alignment.isNucleotide())
140 calcMan.workerComplete(this);
145 * AlignmentAnnotation
146 * strucConsensus=alignViewport.getAlignmentStrucConsensusAnnotation();
148 * hStrucConsensus=alignViewport.getRnaStructureConsensusHash();
149 * strucConsensus.annotations = null; strucConsensus.annotations = new
150 * Annotation[aWidth];
152 * hStrucConsensus = new Hashtable[aWidth];
154 AlignmentAnnotation[] aa = alignViewport.getAlignment()
155 .getAlignmentAnnotation();
157 * AlignmentAnnotation rnaStruc = null; // select rna struct to use for
158 * calculation for (int i = 0; i < aa.length; i++) { if
159 * (aa[i].getRNAStruc() != null && aa[i].isValidStruc()) { rnaStruc =
160 * aa[i]; break; } } // check to see if its valid
162 * if (rnaStruc==null || !rnaStruc.isValidStruc()) {
163 * calcMan.workerComplete(this); return; }
165 List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<compbio.data.sequence.FastaSequence>();
167 boolean submitGaps = true;
169 for (SequenceI sq : ((List<SequenceI>) alignment.getSequences()))
172 if (sq.getEnd() - sq.getStart() > minlen - 1)
174 String newname = SeqsetUtils.unique_name(seqs.size() + 1);
175 // make new input sequence with or without gaps
176 seqs.add(new compbio.data.sequence.FastaSequence(newname,
177 (submitGaps) ? sq.getSequenceAsString() : AlignSeq
178 .extractGaps(jalview.util.Comparison.GapChars,
179 sq.getSequenceAsString())));
186 rslt=aaservice.analize(seqs);
191 rslt=aaservice.customAnalize(seqs, getJabaArguments());
192 } catch (WrongParameterException x)
194 throw new JobSubmissionException("Invalid paremeter set. Check Jalview implementation.",x);
198 boolean finished = false;
202 JobStatus status = aaservice.getJobStatus(rslt);
203 if (status.equals(JobStatus.FINISHED))
211 ChunkHolder stats = aaservice.pullExecStatistics(rslt, rpos);
214 System.out.print(stats.getChunk());
215 rpos = stats.getNextPosition();
217 } while (rpos > cpos);
219 if (!finished && status.equals(JobStatus.FAILED))
224 } catch (InterruptedException x)
234 } catch (InterruptedException x)
238 scoremanager = aaservice.getAnnotation(rslt);
239 if (scoremanager != null)
241 updateResultAnnotation(true);
243 } catch (JobSubmissionException x)
246 System.err.println("submission error:");
248 calcMan.workerCannotRun(this);
249 } catch (ResultNotAvailableException x)
251 System.err.println("collection error:");
253 calcMan.workerCannotRun(this);
255 } catch (OutOfMemoryError error)
257 calcMan.workerCannotRun(this);
260 // hconsensus = null;
261 ap.raiseOOMWarning("calculating Amino acid consensus using AACons service", error);
265 calcMan.workerCannotRun(this);
268 // hconsensus = null;
269 System.err.println("Blacklisting worker due to unexpected exception:");
274 calcMan.workerComplete(this);
277 ap.paintAlignment(true);
284 * notify manager that we have started, and wait for a free calculation slot
285 * @return true if slot is obtained and work still valid, false if another thread has done our work for us.
287 private boolean checkDone()
289 calcMan.notifyStart(this);
290 // ap.paintAlignment(false);
291 while (!calcMan.notifyWorking(this))
293 if (calcMan.isWorking(this))
301 ap.paintAlignment(false);
305 } catch (Exception ex)
307 ex.printStackTrace();
310 if (alignViewport.isClosed())
319 * update the consensus annotation from the sequence profile data using
320 * current visualization settings.
322 public void updateAnnotation()
324 updateResultAnnotation(false);
327 private AlignmentAnnotation findOrCreate(String name, boolean autoCalc,
328 SequenceI seqRef, SequenceGroup groupRef)
330 for (AlignmentAnnotation annot : alignViewport.getAlignment()
331 .getAlignmentAnnotation())
333 if (annot.autoCalculated == autoCalc
334 && annot.getCalcId().equals(name)
335 && annot.sequenceRef == seqRef && annot.groupRef == groupRef)
340 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
341 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
343 annot.setCalcId(new String(name));
344 annot.autoCalculated = autoCalc;
347 annot.setSequenceRef(seqRef);
349 annot.groupRef = groupRef;
350 alignViewport.getAlignment().addAnnotation(annot);
354 public void updateResultAnnotation(boolean immediate)
357 if (immediate || !calcMan.isWorking(this) && scoremanager != null)
359 AlignmentAnnotation annotation;
361 Map<String, TreeSet<Score>> scoremap = scoremanager.asMap();
362 int alWidth = alignViewport.getAlignment().getWidth();
363 AlignmentI alignment;
364 int ann = (alignment = alignViewport.getAlignment())
365 .getAlignmentAnnotation().length;
366 ArrayList<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
367 for (String score : scoremap.keySet())
369 TreeSet<Score> scores = scoremap.get(score);
370 for (Score scr : scores)
372 if (scr.getRanges() != null && scr.getRanges().size() > 0)
375 * annotation in range annotation = findOrCreate(scr.getMethod(),
376 * true, null, null); Annotation[] elm = new Annotation[alWidth];
377 * Iterator<Float> vals = scr.getScores().iterator(); for (Range rng
378 * : scr.getRanges()) { float val = vals.next().floatValue(); for
379 * (int i = rng.from; i <= rng.to; i++) { elm[i] = new
380 * Annotation("", "", ' ', val); } } annotation.annotations = elm;
381 * annotation.validateRangeAndDisplay();
386 // simple annotation row
387 annotation = findOrCreate(scr.getMethod(), true, null, null);
388 Annotation[] elm = new Annotation[alWidth];
389 if (alWidth == scr.getScores().size())
391 Iterator<Float> vals = scr.getScores().iterator();
392 float m = 0f, x = 0f;
393 for (int i = 0; vals.hasNext(); i++)
395 float val = vals.next().floatValue();
413 elm[i] = new Annotation("", "" + val, ' ', val);
416 annotation.annotations = elm;
417 annotation.belowAlignment = true;
423 annotation.graphMax = x;
424 annotation.graphMin = m;
425 annotation.validateRangeAndDisplay();
426 ourAnnot.add(annotation);
431 if (ourAnnot.size() > 0)
433 List<AlignmentAnnotation> our = ourAnnots;
434 ourAnnots = ourAnnot;
439 for (AlignmentAnnotation an : our)
441 if (!ourAnnots.contains(an))
443 // remove the old annotation
444 alignment.deleteAnnotation(an);
452 // alignViewport.getAlignment().getAlignmentAnnotation().length)
454 ap.adjustAnnotationHeight();
457 * else { ap.paintAlignment(true); }