JAL-1379 refactor Jws2 service client so dynamic services can be of any type of JABA...
[jalview.git] / src / jalview / ws / jws2 / JabawsCalcWorker.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ws.jws2;
22
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.Annotation;
25 import jalview.datamodel.SequenceI;
26 import jalview.gui.AlignFrame;
27 import jalview.ws.jws2.jabaws2.Jws2Instance;
28 import jalview.ws.params.WsParamSetI;
29
30 import java.util.Iterator;
31 import java.util.List;
32
33 import compbio.data.msa.SequenceAnnotation;
34 import compbio.data.sequence.Score;
35 import compbio.data.sequence.ScoreManager;
36 import compbio.metadata.Argument;
37 import compbio.metadata.ChunkHolder;
38 import compbio.metadata.JobStatus;
39 import compbio.metadata.JobSubmissionException;
40 import compbio.metadata.ResultNotAvailableException;
41 import compbio.metadata.WrongParameterException;
42
43 public abstract class JabawsCalcWorker extends AbstractJabaCalcWorker
44 {
45
46   @SuppressWarnings("unchecked")
47   protected SequenceAnnotation aaservice;
48
49   protected ScoreManager scoremanager;
50
51   public JabawsCalcWorker(Jws2Instance service, AlignFrame alignFrame,
52           WsParamSetI preset, List<Argument> paramset)
53   {
54     super(service, alignFrame, preset, paramset);
55     aaservice = (SequenceAnnotation) service.service;
56   }
57
58   @Override
59   ChunkHolder pullExecStatistics(String rslt, long rpos)
60   {
61     return aaservice.pullExecStatistics(rslt, rpos);
62   }
63
64   @Override
65   boolean collectAnnotationResultsFor(String rslt)
66           throws ResultNotAvailableException
67   {
68     scoremanager = aaservice.getAnnotation(rslt);
69     if (scoremanager != null)
70     {
71       return true;
72     }
73     return false;
74   }
75
76   @Override
77   boolean cancelJob(String rslt) throws Exception
78   {
79     return aaservice.cancelJob(rslt);
80   }
81
82   @Override
83   protected JobStatus getJobStatus(String rslt) throws Exception
84   {
85     return aaservice.getJobStatus(rslt);
86   }
87
88   @Override
89   boolean hasService()
90   {
91     return aaservice != null;
92   }
93
94   @Override
95   protected boolean isInteractiveUpdate()
96   {
97     return this instanceof AAConClient;
98   }
99
100   @Override
101   protected String submitToService(
102           List<compbio.data.sequence.FastaSequence> seqs)
103           throws JobSubmissionException
104   {
105     String rslt;
106     if (preset == null && arguments == null)
107     {
108       rslt = aaservice.analize(seqs);
109     }
110     else
111     {
112       try
113       {
114         rslt = aaservice.customAnalize(seqs, getJabaArguments());
115       } catch (WrongParameterException x)
116       {
117         throw new JobSubmissionException(
118                 "Invalid parameter set. Check Jalview implementation.", x);
119
120       }
121     }
122     return rslt;
123   }
124
125   protected void createAnnotationRowsForScores(
126           List<AlignmentAnnotation> ourAnnot, String calcId, int alWidth,
127           Score scr)
128   {
129     // simple annotation row
130     AlignmentAnnotation annotation = alignViewport.getAlignment()
131             .findOrCreateAnnotation(scr.getMethod(), calcId, true, null,
132                     null);
133     if (alWidth == gapMap.length) // scr.getScores().size())
134     {
135       constructAnnotationFromScore(annotation, 0, alWidth, scr);
136       ourAnnot.add(annotation);
137     }
138   }
139
140   protected AlignmentAnnotation createAnnotationRowsForScores(
141           List<AlignmentAnnotation> ourAnnot, String typeName,
142           String calcId, SequenceI dseq, int base, Score scr)
143   {
144     System.out.println("Creating annotation on dseq:" + dseq.getStart()
145             + " base is " + base + " and length=" + dseq.getLength()
146             + " == " + scr.getScores().size());
147     // AlignmentAnnotation annotation = new AlignmentAnnotation(
148     // scr.getMethod(), typeName, new Annotation[]
149     // {}, 0, -1, AlignmentAnnotation.LINE_GRAPH);
150     // annotation.setCalcId(calcId);
151     AlignmentAnnotation annotation = alignViewport.getAlignment()
152             .findOrCreateAnnotation(typeName, calcId, false, dseq, null);
153     constructAnnotationFromScore(annotation, 0, dseq.getLength(), scr);
154     annotation.createSequenceMapping(dseq, base, false);
155     annotation.adjustForAlignment();
156     dseq.addAlignmentAnnotation(annotation);
157     ourAnnot.add(annotation);
158     return annotation;
159   }
160
161   private void constructAnnotationFromScore(AlignmentAnnotation annotation,
162           int base, int alWidth, Score scr)
163   {
164     Annotation[] elm = new Annotation[alWidth];
165     Iterator<Float> vals = scr.getScores().iterator();
166     float m = 0f, x = 0f;
167     for (int i = 0; vals.hasNext(); i++)
168     {
169       float val = vals.next().floatValue();
170       if (i == 0)
171       {
172         m = val;
173         x = val;
174       }
175       else
176       {
177         if (m > val)
178         {
179           m = val;
180         }
181         ;
182         if (x < val)
183         {
184           x = val;
185         }
186       }
187       // if we're at a gapped column then skip to next ungapped position
188       if (gapMap != null && gapMap.length > 0)
189       {
190         while (!gapMap[i])
191         {
192           elm[i++] = new Annotation("", "", ' ', Float.NaN);
193         }
194       }
195       elm[i] = new Annotation("", "" + val, ' ', val);
196     }
197
198     annotation.annotations = elm;
199     annotation.belowAlignment = true;
200     if (x < 0)
201     {
202       x = 0;
203     }
204     x += (x - m) * 0.1;
205     annotation.graphMax = x;
206     annotation.graphMin = m;
207     annotation.validateRangeAndDisplay();
208   }
209
210 }