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