1 package jalview.ws.jws2.jabaws2;
3 import jalview.api.AlignViewportI;
4 import jalview.datamodel.AlignmentAnnotation;
5 import jalview.datamodel.Annotation;
6 import jalview.datamodel.SequenceI;
7 import jalview.util.MessageManager;
8 import jalview.ws.api.JobId;
9 import jalview.ws.api.SequenceAnnotationServiceI;
10 import jalview.ws.gui.AnnotationWsJob;
11 import jalview.ws.jws2.JabaParamStore;
12 import jalview.ws.jws2.JabaPreset;
13 import jalview.ws.jws2.SeqAnnotationServiceCalcWorker;
14 import jalview.ws.params.ArgumentI;
15 import jalview.ws.params.WsParamSetI;
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
21 import compbio.data.msa.SequenceAnnotation;
22 import compbio.data.sequence.FastaSequence;
23 import compbio.data.sequence.Score;
24 import compbio.data.sequence.ScoreManager;
25 import compbio.metadata.JobSubmissionException;
26 import compbio.metadata.WrongParameterException;
28 public abstract class JabawsAnnotationInstance
29 extends JabawsServiceInstance<SequenceAnnotation>
30 implements SequenceAnnotationServiceI
34 * holds last results obtained when non-null. TODO: remove this as a field ?
36 protected ScoreManager scoremanager = null;
38 public JabawsAnnotationInstance(Jws2Instance handle)
46 * @return the calcId for this Jabaws Service (convenience method).
48 * TODO: decide if this is really convenient since manager and
49 * instance have same method !
51 public String getCalcId()
53 return our.getAlignAnalysisUI() == null ? null
54 : our.getAlignAnalysisUI().getCalcId();
58 public JobId submitToService(List<SequenceI> seqs, WsParamSetI preset,
59 List<ArgumentI> arguments) throws Throwable
63 List<FastaSequence> jabaseqs = new ArrayList(seqs.size());
64 for (SequenceI seq : seqs)
67 new FastaSequence(seq.getName(), seq.getSequenceAsString()));
69 if (preset == null && arguments == null)
71 rslt = service.analize(jabaseqs);
75 if (preset instanceof JabaPreset)
77 // TODO: verify behaviour is really the same, since preset analyze was
78 // never called in Jalview 2.11.x
79 rslt = service.presetAnalize(jabaseqs,
80 ((JabaPreset) preset).getJabaPreset());
84 rslt = service.customAnalize(jabaseqs,
85 JabaParamStore.getJabafromJwsArgs(preset.getArguments()));
88 else if (arguments != null && arguments.size() > 0)
92 rslt = service.customAnalize(jabaseqs,
93 JabaParamStore.getJabafromJwsArgs(arguments));
94 } catch (WrongParameterException x)
96 throw new JobSubmissionException(MessageManager.getString(
97 "exception.jobsubmission_invalid_params_set"), x);
106 return new JobId(our.getServiceType(), our.getName(), rslt);
110 public List<AlignmentAnnotation> getAlignmentAnnotation(
111 AnnotationWsJob running,
112 SeqAnnotationServiceCalcWorker abstractJabaCalcWorker) throws Throwable
114 if (scoremanager == null)
116 // TODO: raise annotation unavailable exception ?
117 scoremanager = service.getAnnotation(running.getJobId());
119 if (scoremanager == null)
124 return annotationFromScoreManager(running,
125 abstractJabaCalcWorker.getAlignViewport(),
126 abstractJabaCalcWorker.getGapMap());
130 * service specific annotation creation method
133 * @param alignViewport
137 abstract List<AlignmentAnnotation> annotationFromScoreManager(
138 AnnotationWsJob running,
139 AlignViewportI alignViewport, boolean[] gapMap);
142 * create and complete an annotation row from a JABAWS score object
144 * @param alignViewport
152 protected void createAnnotationRowsForScores(AlignViewportI alignViewport,
153 boolean[] gapMap, List<AlignmentAnnotation> ourAnnot,
155 int alWidth, Score scr)
157 // simple annotation row
158 AlignmentAnnotation annotation = alignViewport.getAlignment()
159 .findOrCreateAnnotation(scr.getMethod(), calcId, true, null,
161 if (alWidth == gapMap.length) // scr.getScores().size())
163 constructAnnotationFromScore(gapMap, annotation, 0,
165 ourAnnot.add(annotation);
170 * create a sequence associated annotation row for JABAWS score object scr
172 * @param alignViewport
182 protected AlignmentAnnotation createAnnotationRowsForScores(
183 AlignViewportI alignViewport, boolean[] gapMap,
184 List<AlignmentAnnotation> ourAnnot, String typeName,
185 String calcId, SequenceI dseq, int base, Score scr)
187 System.out.println("Creating annotation on dseq:" + dseq.getStart()
188 + " base is " + base + " and length=" + dseq.getLength()
189 + " == " + scr.getScores().size());
190 // AlignmentAnnotation annotation = new AlignmentAnnotation(
191 // scr.getMethod(), typeName, new Annotation[]
192 // {}, 0, -1, AlignmentAnnotation.LINE_GRAPH);
193 // annotation.setCalcId(calcId);
194 AlignmentAnnotation annotation = alignViewport.getAlignment()
195 .findOrCreateAnnotation(typeName, calcId, false, dseq, null);
196 constructAnnotationFromScore(gapMap, annotation, 0, dseq.getLength(),
198 annotation.createSequenceMapping(dseq, base, false);
199 annotation.adjustForAlignment();
200 dseq.addAlignmentAnnotation(annotation);
201 ourAnnot.add(annotation);
206 * replace an existing sequence associated annotation with another, creating
207 * association as necessary
210 * - annotation row used to create an instance on the dataset
213 * - label used to match existing row
215 * - calcId for existing row
217 * - alignment sequence with reference to destination dataet sequence
219 protected void replaceAnnotationOnAlignmentWith(
220 AlignmentAnnotation newAnnot, String typeName, String calcId,
223 SequenceI dsseq = aSeq.getDatasetSequence();
224 while (dsseq.getDatasetSequence() != null)
226 dsseq = dsseq.getDatasetSequence();
228 // look for same annotation on dataset and lift this one over
229 List<AlignmentAnnotation> dsan = dsseq.getAlignmentAnnotations(calcId,
231 if (dsan != null && dsan.size() > 0)
233 for (AlignmentAnnotation dssan : dsan)
235 dsseq.removeAlignmentAnnotation(dssan);
238 AlignmentAnnotation dssan = new AlignmentAnnotation(newAnnot);
239 dsseq.addAlignmentAnnotation(dssan);
240 dssan.adjustForAlignment();
244 * create column annotation elements from Jabaws score object
251 * JABAWS score object
253 protected void constructAnnotationFromScore(boolean[] gapMap,
254 AlignmentAnnotation annotation,
255 int base, int alWidth, Score scr)
257 Annotation[] elm = new Annotation[alWidth];
258 Iterator<Float> vals = scr.getScores().iterator();
259 float m = 0f, x = 0f;
260 for (int i = 0; vals.hasNext(); i++)
262 float val = vals.next().floatValue();
280 // if we're at a gapped column then skip to next ungapped position
281 if (gapMap != null && gapMap.length > 0)
283 // if gapMap is a different length to the result then it may be out of
284 // sync with the job.
285 while (i < gapMap.length && !gapMap[i])
287 elm[i++] = new Annotation("", "", ' ', Float.NaN);
290 elm[i] = new Annotation("", "" + val, ' ', val);
293 annotation.annotations = elm;
294 annotation.belowAlignment = true;
300 annotation.graphMax = x;
301 annotation.graphMin = m;
302 annotation.validateRangeAndDisplay();