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());
128 abstract List<AlignmentAnnotation> annotationFromScoreManager(
129 AnnotationWsJob running,
130 AlignViewportI alignViewport, boolean[] gapMap);
132 // From JabawsCalcWorker
134 protected void createAnnotationRowsForScores(AlignViewportI alignViewport,
135 boolean[] gapMap, List<AlignmentAnnotation> ourAnnot,
137 int alWidth, Score scr)
139 // simple annotation row
140 AlignmentAnnotation annotation = alignViewport.getAlignment()
141 .findOrCreateAnnotation(scr.getMethod(), calcId, true, null,
143 if (alWidth == gapMap.length) // scr.getScores().size())
145 constructAnnotationFromScore(gapMap, annotation, 0,
147 ourAnnot.add(annotation);
151 protected AlignmentAnnotation createAnnotationRowsForScores(
152 AlignViewportI alignViewport, boolean[] gapMap,
153 List<AlignmentAnnotation> ourAnnot, String typeName,
154 String calcId, SequenceI dseq, int base, Score scr)
156 System.out.println("Creating annotation on dseq:" + dseq.getStart()
157 + " base is " + base + " and length=" + dseq.getLength()
158 + " == " + scr.getScores().size());
159 // AlignmentAnnotation annotation = new AlignmentAnnotation(
160 // scr.getMethod(), typeName, new Annotation[]
161 // {}, 0, -1, AlignmentAnnotation.LINE_GRAPH);
162 // annotation.setCalcId(calcId);
163 AlignmentAnnotation annotation = alignViewport.getAlignment()
164 .findOrCreateAnnotation(typeName, calcId, false, dseq, null);
165 constructAnnotationFromScore(gapMap, annotation, 0, dseq.getLength(),
167 annotation.createSequenceMapping(dseq, base, false);
168 annotation.adjustForAlignment();
169 dseq.addAlignmentAnnotation(annotation);
170 ourAnnot.add(annotation);
174 protected void replaceAnnotationOnAlignmentWith(
175 AlignmentAnnotation newAnnot, String typeName, String calcId,
178 SequenceI dsseq = aSeq.getDatasetSequence();
179 while (dsseq.getDatasetSequence() != null)
181 dsseq = dsseq.getDatasetSequence();
183 // look for same annotation on dataset and lift this one over
184 List<AlignmentAnnotation> dsan = dsseq.getAlignmentAnnotations(calcId,
186 if (dsan != null && dsan.size() > 0)
188 for (AlignmentAnnotation dssan : dsan)
190 dsseq.removeAlignmentAnnotation(dssan);
193 AlignmentAnnotation dssan = new AlignmentAnnotation(newAnnot);
194 dsseq.addAlignmentAnnotation(dssan);
195 dssan.adjustForAlignment();
198 protected void constructAnnotationFromScore(boolean[] gapMap,
199 AlignmentAnnotation annotation,
200 int base, int alWidth, Score scr)
202 Annotation[] elm = new Annotation[alWidth];
203 Iterator<Float> vals = scr.getScores().iterator();
204 float m = 0f, x = 0f;
205 for (int i = 0; vals.hasNext(); i++)
207 float val = vals.next().floatValue();
225 // if we're at a gapped column then skip to next ungapped position
226 if (gapMap != null && gapMap.length > 0)
228 // if gapMap is a different length to the result then it may be out of
229 // sync with the job.
230 while (i < gapMap.length && !gapMap[i])
232 elm[i++] = new Annotation("", "", ' ', Float.NaN);
235 elm[i] = new Annotation("", "" + val, ' ', val);
238 annotation.annotations = elm;
239 annotation.belowAlignment = true;
245 annotation.graphMax = x;
246 annotation.graphMin = m;
247 annotation.validateRangeAndDisplay();