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