JAL-1620 version bump and release notes
[jalview.git] / src / jalview / ws / jws2 / JabawsMsaInterfaceAlignCalcWorker.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
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 java.util.Iterator;
24 import java.util.List;
25
26 import compbio.data.msa.MsaWS;
27 import compbio.data.msa.SequenceAnnotation;
28 import compbio.data.sequence.Alignment;
29 import compbio.data.sequence.Score;
30 import compbio.data.sequence.ScoreManager;
31 import compbio.metadata.Argument;
32 import compbio.metadata.ChunkHolder;
33 import compbio.metadata.JobStatus;
34 import compbio.metadata.JobSubmissionException;
35 import compbio.metadata.ResultNotAvailableException;
36 import compbio.metadata.WrongParameterException;
37 import jalview.api.AlignViewportI;
38 import jalview.api.AlignmentViewPanel;
39 import jalview.datamodel.AlignmentAnnotation;
40 import jalview.datamodel.Annotation;
41 import jalview.datamodel.SequenceI;
42 import jalview.gui.AlignFrame;
43 import jalview.util.MessageManager;
44 import jalview.workers.AlignCalcWorker;
45 import jalview.ws.jws2.jabaws2.Jws2Instance;
46 import jalview.ws.params.WsParamSetI;
47
48 public abstract class JabawsMsaInterfaceAlignCalcWorker extends AbstractJabaCalcWorker
49 {
50
51   @SuppressWarnings("unchecked")
52   protected MsaWS msaservice;
53
54   protected Alignment msascoreset;
55
56   public JabawsMsaInterfaceAlignCalcWorker(AlignViewportI alignViewport,
57           AlignmentViewPanel alignPanel)
58   {
59     super(alignViewport, alignPanel);
60   }
61
62   public JabawsMsaInterfaceAlignCalcWorker(Jws2Instance service, AlignFrame alignFrame,
63           WsParamSetI preset, List<Argument> paramset)
64   {
65     this(alignFrame.getCurrentView(), alignFrame.alignPanel);
66     this.guiProgress = alignFrame;
67     this.preset = preset;
68     this.arguments = paramset;
69     this.service = service;
70     msaservice = (MsaWS) service.service;
71
72   }
73
74   @Override
75   ChunkHolder pullExecStatistics(String rslt, long rpos)
76   {
77     return msaservice.pullExecStatistics(rslt, rpos);
78   }
79
80   @Override
81   boolean collectAnnotationResultsFor(String rslt)
82           throws ResultNotAvailableException
83   {
84     msascoreset = msaservice.getResult(rslt);
85     if (msascoreset != null)
86     {
87       return true;
88     }
89     return false;
90   }
91
92   @Override
93   boolean cancelJob(String rslt) throws Exception
94   {
95     return msaservice.cancelJob(rslt);
96   }
97
98   @Override
99   protected JobStatus getJobStatus(String rslt) throws Exception
100   {
101     return msaservice.getJobStatus(rslt);
102   }
103
104   @Override
105   boolean hasService()
106   {
107     return msaservice != null;
108   }
109
110   @Override
111   protected boolean isInteractiveUpdate()
112   {
113     return false; // this instanceof AAConClient;
114   }
115
116   @Override
117   protected String submitToService(
118           List<compbio.data.sequence.FastaSequence> seqs)
119           throws JobSubmissionException
120   {
121     String rslt;
122     if (preset == null && arguments == null)
123     {
124       rslt = msaservice.align(seqs);
125     }
126     else
127     {
128       try
129       {
130         rslt = msaservice.customAlign(seqs, getJabaArguments());
131       } catch (WrongParameterException x)
132       {
133           throw new JobSubmissionException(MessageManager.getString("exception.jobsubmission_invalid_params_set"), x);
134       }
135     }
136     return rslt;
137   }
138
139   protected void createAnnotationRowsForScores(
140           List<AlignmentAnnotation> ourAnnot, String calcId, int alWidth,
141           Score scr)
142   {
143     // simple annotation row
144     AlignmentAnnotation annotation = alignViewport.getAlignment()
145             .findOrCreateAnnotation(scr.getMethod(), calcId, true, null,
146                     null);
147     if (alWidth == gapMap.length) // scr.getScores().size())
148     {
149       constructAnnotationFromScore(annotation, 0, alWidth, scr);
150       ourAnnot.add(annotation);
151     }
152   }
153
154   protected AlignmentAnnotation createAnnotationRowsForScores(
155           List<AlignmentAnnotation> ourAnnot, String typeName,
156           String calcId, SequenceI dseq, int base, Score scr)
157   {
158     System.out.println("Creating annotation on dseq:" + dseq.getStart()
159             + " base is " + base + " and length=" + dseq.getLength()
160             + " == " + scr.getScores().size());
161     // AlignmentAnnotation annotation = new AlignmentAnnotation(
162     // scr.getMethod(), typeName, new Annotation[]
163     // {}, 0, -1, AlignmentAnnotation.LINE_GRAPH);
164     // annotation.setCalcId(calcId);
165     AlignmentAnnotation annotation = alignViewport.getAlignment()
166             .findOrCreateAnnotation(typeName, calcId, false, dseq, null);
167     constructAnnotationFromScore(annotation, 0, dseq.getLength(), scr);
168     annotation.createSequenceMapping(dseq, base, false);
169     annotation.adjustForAlignment();
170     dseq.addAlignmentAnnotation(annotation);
171     ourAnnot.add(annotation);
172     return annotation;
173   }
174
175   private void constructAnnotationFromScore(AlignmentAnnotation annotation,
176           int base, int alWidth, Score scr)
177   {
178     Annotation[] elm = new Annotation[alWidth];
179     Iterator<Float> vals = scr.getScores().iterator();
180     float m = 0f, x = 0f;
181     for (int i = 0; vals.hasNext(); i++)
182     {
183       float val = vals.next().floatValue();
184       if (i == 0)
185       {
186         m = val;
187         x = val;
188       }
189       else
190       {
191         if (m > val)
192         {
193           m = val;
194         }
195         ;
196         if (x < val)
197         {
198           x = val;
199         }
200       }
201       // if we're at a gapped column then skip to next ungapped position
202       if (gapMap != null && gapMap.length > 0)
203       {
204         while (!gapMap[i])
205         {
206           elm[i++] = new Annotation("", "", ' ', Float.NaN);
207         }
208       }
209       elm[i] = new Annotation("", "" + val, ' ', val);
210     }
211
212     annotation.annotations = elm;
213     annotation.belowAlignment = true;
214     if (x < 0)
215     {
216       x = 0;
217     }
218     x += (x - m) * 0.1;
219     annotation.graphMax = x;
220     annotation.graphMin = m;
221     annotation.validateRangeAndDisplay();
222   }
223
224 }