95abe3cb5e0fa5bd7ae1f68630b05701bb565c19
[jalview.git] / src / jalview / ws / jws2 / jabaws2 / JabawsAnnotationInstance.java
1 package jalview.ws.jws2.jabaws2;
2
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;
16
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
20
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;
27
28 public abstract class JabawsAnnotationInstance
29         extends JabawsServiceInstance<SequenceAnnotation>
30         implements SequenceAnnotationServiceI
31 {
32
33   /**
34    * holds last results obtained when non-null. TODO: remove this as a field ?
35    */
36   protected ScoreManager scoremanager = null;
37
38   public JabawsAnnotationInstance(Jws2Instance handle)
39   {
40     super(handle);
41   }
42
43
44   /**
45    * 
46    * @return the calcId for this Jabaws Service (convenience method).
47    * 
48    *         TODO: decide if this is really convenient since manager and
49    *         instance have same method !
50    */
51   public String getCalcId()
52   {
53     return our.aaui == null ? null : our.aaui.getCalcId();
54   }
55
56   @Override
57   public JobId submitToService(List<SequenceI> seqs, WsParamSetI preset,
58           List<ArgumentI> arguments) throws Throwable
59   {
60     String rslt = null;
61     scoremanager = null;
62     List<FastaSequence> jabaseqs = new ArrayList(seqs.size());
63     for (SequenceI seq : seqs)
64     {
65       jabaseqs.add(
66               new FastaSequence(seq.getName(), seq.getSequenceAsString()));
67     }
68     if (preset == null && arguments == null)
69     {
70       rslt = service.analize(jabaseqs);
71     }
72     if (preset != null)
73     {
74       if (preset instanceof JabaPreset)
75       {
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());
80       }
81       else
82       {
83         rslt = service.customAnalize(jabaseqs,
84                 JabaParamStore.getJabafromJwsArgs(preset.getArguments()));
85       }
86     }
87     else if (arguments != null && arguments.size() > 0)
88     {
89       try
90       {
91         rslt = service.customAnalize(jabaseqs,
92                 JabaParamStore.getJabafromJwsArgs(arguments));
93       } catch (WrongParameterException x)
94       {
95         throw new JobSubmissionException(MessageManager.getString(
96                 "exception.jobsubmission_invalid_params_set"), x);
97
98       }
99     }
100
101     if (rslt == null)
102     {
103       return null;
104     }
105     return new JobId(our.getServiceType(), our.getName(), rslt);
106   }
107
108   @Override
109   public List<AlignmentAnnotation> getAlignmentAnnotation(
110           AnnotationWsJob running,
111           AbstractJabaCalcWorker abstractJabaCalcWorker) throws Throwable
112   {
113     if (scoremanager == null)
114     {
115       // TODO: raise annotation unavailable exception ?
116       scoremanager = service.getAnnotation(running.getJobId());
117     }
118     if (scoremanager == null)
119     {
120       return List.of();
121     }
122
123     return annotationFromScoreManager(running,
124             abstractJabaCalcWorker.getAlignViewport(),
125             abstractJabaCalcWorker.getGapMap());
126   }
127
128   abstract List<AlignmentAnnotation> annotationFromScoreManager(
129           AnnotationWsJob running,
130           AlignViewportI alignViewport, boolean[] gapMap);
131
132   // From JabawsCalcWorker
133
134   protected void createAnnotationRowsForScores(AlignViewportI alignViewport,
135           boolean[] gapMap, List<AlignmentAnnotation> ourAnnot,
136           String calcId,
137           int alWidth, Score scr)
138   {
139     // simple annotation row
140     AlignmentAnnotation annotation = alignViewport.getAlignment()
141             .findOrCreateAnnotation(scr.getMethod(), calcId, true, null,
142                     null);
143     if (alWidth == gapMap.length) // scr.getScores().size())
144     {
145       constructAnnotationFromScore(gapMap, annotation, 0,
146               alWidth, scr);
147       ourAnnot.add(annotation);
148     }
149   }
150
151   protected AlignmentAnnotation createAnnotationRowsForScores(
152           AlignViewportI alignViewport, boolean[] gapMap,
153           List<AlignmentAnnotation> ourAnnot, String typeName,
154           String calcId, SequenceI dseq, int base, Score scr)
155   {
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(),
166             scr);
167     annotation.createSequenceMapping(dseq, base, false);
168     annotation.adjustForAlignment();
169     dseq.addAlignmentAnnotation(annotation);
170     ourAnnot.add(annotation);
171     return annotation;
172   }
173
174   protected void replaceAnnotationOnAlignmentWith(
175           AlignmentAnnotation newAnnot, String typeName, String calcId,
176           SequenceI aSeq)
177   {
178     SequenceI dsseq = aSeq.getDatasetSequence();
179     while (dsseq.getDatasetSequence() != null)
180     {
181       dsseq = dsseq.getDatasetSequence();
182     }
183     // look for same annotation on dataset and lift this one over
184     List<AlignmentAnnotation> dsan = dsseq.getAlignmentAnnotations(calcId,
185             typeName);
186     if (dsan != null && dsan.size() > 0)
187     {
188       for (AlignmentAnnotation dssan : dsan)
189       {
190         dsseq.removeAlignmentAnnotation(dssan);
191       }
192     }
193     AlignmentAnnotation dssan = new AlignmentAnnotation(newAnnot);
194     dsseq.addAlignmentAnnotation(dssan);
195     dssan.adjustForAlignment();
196   }
197
198   protected void constructAnnotationFromScore(boolean[] gapMap,
199           AlignmentAnnotation annotation,
200           int base, int alWidth, Score scr)
201   {
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++)
206     {
207       float val = vals.next().floatValue();
208       if (i == 0)
209       {
210         m = val;
211         x = val;
212       }
213       else
214       {
215         if (m > val)
216         {
217           m = val;
218         }
219         ;
220         if (x < val)
221         {
222           x = val;
223         }
224       }
225       // if we're at a gapped column then skip to next ungapped position
226       if (gapMap != null && gapMap.length > 0)
227       {
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])
231         {
232           elm[i++] = new Annotation("", "", ' ', Float.NaN);
233         }
234       }
235       elm[i] = new Annotation("", "" + val, ' ', val);
236     }
237
238     annotation.annotations = elm;
239     annotation.belowAlignment = true;
240     if (x < 0)
241     {
242       x = 0;
243     }
244     x += (x - m) * 0.1;
245     annotation.graphMax = x;
246     annotation.graphMin = m;
247     annotation.validateRangeAndDisplay();
248   }
249
250 }