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.AbstractJabaCalcWorker;
12 import jalview.ws.jws2.JabaParamStore;
13 import jalview.ws.jws2.JabaPreset;
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.aaui == null ? null : our.aaui.getCalcId();
57 public JobId submitToService(List<SequenceI> seqs, WsParamSetI preset,
58 List<ArgumentI> arguments) throws Throwable
62 List<FastaSequence> jabaseqs = new ArrayList(seqs.size());
63 for (SequenceI seq : seqs)
66 new FastaSequence(seq.getName(), seq.getSequenceAsString()));
68 if (preset == null && arguments == null)
70 rslt = service.analize(jabaseqs);
74 if (preset instanceof JabaPreset)
76 // TODO: verify behaviour is really the same, since preset analyze was
77 // never called in Jalview 2.11.x
78 rslt = service.presetAnalize(jabaseqs,
79 ((JabaPreset) preset).getJabaPreset());
83 rslt = service.customAnalize(jabaseqs,
84 JabaParamStore.getJabafromJwsArgs(preset.getArguments()));
87 else if (arguments != null && arguments.size() > 0)
91 rslt = service.customAnalize(jabaseqs,
92 JabaParamStore.getJabafromJwsArgs(arguments));
93 } catch (WrongParameterException x)
95 throw new JobSubmissionException(MessageManager.getString(
96 "exception.jobsubmission_invalid_params_set"), x);
105 return new JobId(our.getServiceType(), our.getName(), rslt);
109 public List<AlignmentAnnotation> getAlignmentAnnotation(
110 AnnotationWsJob running,
111 AbstractJabaCalcWorker abstractJabaCalcWorker) throws Throwable
113 if (scoremanager == null)
115 // TODO: raise annotation unavailable exception ?
116 scoremanager = service.getAnnotation(running.getJobId());
118 if (scoremanager == null)
123 return annotationFromScoreManager(running,
124 abstractJabaCalcWorker.getAlignViewport(),
125 abstractJabaCalcWorker.getGapMap());
129 * service specific annotation creation method
132 * @param alignViewport
136 abstract List<AlignmentAnnotation> annotationFromScoreManager(
137 AnnotationWsJob running,
138 AlignViewportI alignViewport, boolean[] gapMap);
141 * create and complete an annotation row from a JABAWS score object
143 * @param alignViewport
151 protected void createAnnotationRowsForScores(AlignViewportI alignViewport,
152 boolean[] gapMap, List<AlignmentAnnotation> ourAnnot,
154 int alWidth, Score scr)
156 // simple annotation row
157 AlignmentAnnotation annotation = alignViewport.getAlignment()
158 .findOrCreateAnnotation(scr.getMethod(), calcId, true, null,
160 if (alWidth == gapMap.length) // scr.getScores().size())
162 constructAnnotationFromScore(gapMap, annotation, 0,
164 ourAnnot.add(annotation);
169 * create a sequence associated annotation row for JABAWS score object scr
171 * @param alignViewport
181 protected AlignmentAnnotation createAnnotationRowsForScores(
182 AlignViewportI alignViewport, boolean[] gapMap,
183 List<AlignmentAnnotation> ourAnnot, String typeName,
184 String calcId, SequenceI dseq, int base, Score scr)
186 System.out.println("Creating annotation on dseq:" + dseq.getStart()
187 + " base is " + base + " and length=" + dseq.getLength()
188 + " == " + scr.getScores().size());
189 // AlignmentAnnotation annotation = new AlignmentAnnotation(
190 // scr.getMethod(), typeName, new Annotation[]
191 // {}, 0, -1, AlignmentAnnotation.LINE_GRAPH);
192 // annotation.setCalcId(calcId);
193 AlignmentAnnotation annotation = alignViewport.getAlignment()
194 .findOrCreateAnnotation(typeName, calcId, false, dseq, null);
195 constructAnnotationFromScore(gapMap, annotation, 0, dseq.getLength(),
197 annotation.createSequenceMapping(dseq, base, false);
198 annotation.adjustForAlignment();
199 dseq.addAlignmentAnnotation(annotation);
200 ourAnnot.add(annotation);
205 * replace an existing sequence associated annotation with another, creating
206 * association as necessary
209 * - annotation row used to create an instance on the dataset
212 * - label used to match existing row
214 * - calcId for existing row
216 * - alignment sequence with reference to destination dataet sequence
218 protected void replaceAnnotationOnAlignmentWith(
219 AlignmentAnnotation newAnnot, String typeName, String calcId,
222 SequenceI dsseq = aSeq.getDatasetSequence();
223 while (dsseq.getDatasetSequence() != null)
225 dsseq = dsseq.getDatasetSequence();
227 // look for same annotation on dataset and lift this one over
228 List<AlignmentAnnotation> dsan = dsseq.getAlignmentAnnotations(calcId,
230 if (dsan != null && dsan.size() > 0)
232 for (AlignmentAnnotation dssan : dsan)
234 dsseq.removeAlignmentAnnotation(dssan);
237 AlignmentAnnotation dssan = new AlignmentAnnotation(newAnnot);
238 dsseq.addAlignmentAnnotation(dssan);
239 dssan.adjustForAlignment();
243 * create column annotation elements from Jabaws score object
250 * JABAWS score object
252 protected void constructAnnotationFromScore(boolean[] gapMap,
253 AlignmentAnnotation annotation,
254 int base, int alWidth, Score scr)
256 Annotation[] elm = new Annotation[alWidth];
257 Iterator<Float> vals = scr.getScores().iterator();
258 float m = 0f, x = 0f;
259 for (int i = 0; vals.hasNext(); i++)
261 float val = vals.next().floatValue();
279 // if we're at a gapped column then skip to next ungapped position
280 if (gapMap != null && gapMap.length > 0)
282 // if gapMap is a different length to the result then it may be out of
283 // sync with the job.
284 while (i < gapMap.length && !gapMap[i])
286 elm[i++] = new Annotation("", "", ' ', Float.NaN);
289 elm[i] = new Annotation("", "" + val, ' ', val);
292 annotation.annotations = elm;
293 annotation.belowAlignment = true;
299 annotation.graphMax = x;
300 annotation.graphMin = m;
301 annotation.validateRangeAndDisplay();