8328d458ceb85e1d4b6d6e26289c26476fade864
[jalview.git] / src / jalview / ws / jws2 / JPred301Client.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.api.AlignCalcWorkerI;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.Annotation;
26 import jalview.gui.AlignFrame;
27 import jalview.ws.jws2.jabaws2.Jws2Instance;
28 import jalview.ws.params.ArgumentI;
29 import jalview.ws.params.OptionI;
30 import jalview.ws.params.WsParamSetI;
31 import jalview.ws.uimodel.AlignAnalysisUIText;
32
33 import java.awt.Color;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.List;
38
39 import compbio.data.sequence.FastaSequence;
40 import compbio.data.sequence.JpredAlignment;
41 import compbio.metadata.Argument;
42
43 public class JPred301Client extends JabawsMsaInterfaceAlignCalcWorker
44         implements AlignCalcWorkerI
45 {
46
47   /**
48    * 
49    * @return default args for this service when run as dynamic web service
50    */
51   public List<Argument> selectDefaultArgs()
52   {
53     List<ArgumentI> rgs = new ArrayList<ArgumentI>();
54     for (ArgumentI argi : service.getParamStore().getServiceParameters())
55     {
56       if (argi instanceof OptionI)
57       {
58         List<String> o = ((OptionI) argi).getPossibleValues();
59         if (o.contains("-pred-nohits"))
60         {
61           OptionI cpy = ((OptionI) argi).copy();
62           cpy.setValue("-pred-nohits");
63           rgs.add(cpy);
64         }
65       }
66     }
67     return JabaParamStore.getJabafromJwsArgs(rgs);
68   }
69
70   public JPred301Client(Jws2Instance service, AlignFrame alignFrame,
71           WsParamSetI preset, List<Argument> paramset)
72   {
73     super(service, alignFrame, preset, paramset);
74     submitGaps = true;
75     alignedSeqs = true;
76     nucleotidesAllowed = false;
77     proteinAllowed = true;
78     gapMap = new boolean[0];
79     updateParameters(null, selectDefaultArgs());
80   }
81
82   @Override
83   boolean checkValidInputSeqs(boolean dynamic, List<FastaSequence> seqs)
84   {
85     return (seqs.size() > 1);
86   }
87
88   public String getServiceActionText()
89   {
90     return "calculating consensus secondary structure prediction using JPred service";
91   }
92
93   private static HashMap<String, String[]> jpredRowLabels = new HashMap<String, String[]>();
94
95   private static HashSet<String> jpredRes_graph, jpredRes_ssonly;
96   {
97     jpredRes_ssonly = new HashSet();
98     jpredRes_ssonly.add("jnetpred".toLowerCase());
99     jpredRes_ssonly.add("jnetpssm".toLowerCase());
100     jpredRes_ssonly.add("jnethmm".toLowerCase());
101     jpredRes_graph = new HashSet();
102     jpredRes_graph.add("jnetconf".toLowerCase());
103     jpredRes_graph.add("jnet burial".toLowerCase());
104   }
105
106   /**
107    * update the consensus annotation from the sequence profile data using
108    * current visualization settings.
109    */
110   public void updateResultAnnotation(boolean immediate)
111   {
112     if (immediate || !calcMan.isWorking(this) && msascoreset != null)
113     {
114       if (msascoreset instanceof compbio.data.sequence.JpredAlignment)
115       {
116         JpredAlignment jpres = (JpredAlignment) msascoreset;
117         int alWidth = alignViewport.getAlignment().getWidth();
118         ArrayList<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
119         char[] sol = new char[jpres.getJpredSequences().get(0).getLength()];
120         boolean firstsol = true;
121         for (FastaSequence fsq : jpres.getJpredSequences())
122         {
123           String[] k = jpredRowLabels.get(fsq.getId());
124           if (k == null)
125           {
126             k = new String[]
127             { fsq.getId(), "JNet Output" };
128           }
129           if (fsq.getId().startsWith("JNETSOL"))
130           {
131             char amnt = (fsq.getId().endsWith("25") ? "3" : fsq.getId()
132                     .endsWith("5") ? "6" : "9").charAt(0);
133             char[] vseq = fsq.getSequence().toCharArray();
134             for (int spos = 0, sposL = fsq.getLength(); spos < sposL; spos++)
135             {
136               if (firstsol)
137               {
138                 sol[spos] = '0';
139               }
140               if (vseq[spos] == 'B' && (sol[spos]=='0' || sol[spos] < amnt))
141               {
142                 sol[spos] = amnt;
143               }
144             }
145             firstsol = false;
146           }
147           else
148           {
149             createAnnotationRowFromString(
150                     ourAnnot,
151                     getCalcId(),
152                     alWidth,
153                     k[0],
154                     k[1],
155                     jpredRes_graph.contains(fsq.getId()) ? AlignmentAnnotation.BAR_GRAPH
156                             : AlignmentAnnotation.NO_GRAPH, 0f, 9f,
157                     fsq.getSequence());
158           }
159
160         }
161         createAnnotationRowFromString(
162                 ourAnnot,
163                 getCalcId(),
164                 alWidth,
165                 "Jnet Burial",
166                 "<html>Prediction of Solvent Accessibility<br/>levels are<ul><li>0 - Exposed</li><li>3 - 25% or more S.A. accessible</li><li>6 - 5% or more S.A. accessible</li><li>9 - Buried (<5% exposed)</li></ul>",
167                 AlignmentAnnotation.BAR_GRAPH, 0f, 9f, new String(sol));
168         for (FastaSequence fsq : jpres.getSequences())
169         {
170           if (fsq.getId().equalsIgnoreCase("QUERY"))
171           {
172             createAnnotationRowFromString(ourAnnot, getCalcId(), alWidth,
173                     "Query", "JPred Reference Sequence",
174                     AlignmentAnnotation.NO_GRAPH, 0f, 0f, fsq.getSequence());
175           }
176         }
177         if (ourAnnot.size() > 0)
178         {
179           updateOurAnnots(ourAnnot);
180         }
181       }
182     }
183   }
184
185   private void createAnnotationRowFromString(
186           ArrayList<AlignmentAnnotation> ourAnnot, String calcId,
187           int alWidth, String label, String descr, int rowType, float min,
188           float max, String jpredPrediction)
189   {
190     // simple annotation row
191     AlignmentAnnotation annotation = alignViewport.getAlignment()
192             .findOrCreateAnnotation(label, calcId, true, null, null);
193     if (alWidth == gapMap.length) // scr.getScores().size())
194     {
195       annotation.label = new String(label);
196       annotation.description = new String(descr);
197       annotation.graph = rowType;
198       annotation.graphMin = min;
199       annotation.graphMax = max;
200       if (constructAnnotationFromString(annotation, jpredPrediction,
201               alWidth, rowType))
202       {
203         // created a valid annotation from the data
204         ourAnnot.add(annotation);
205         // annotation.validateRangeAndDisplay();
206       }
207     }
208   }
209
210   private boolean constructAnnotationFromString(
211           AlignmentAnnotation annotation, String sourceData, int alWidth,
212           int rowType)
213   {
214     if (sourceData.length() == 0 && alWidth > 0)
215     {
216       return false;
217     }
218     Annotation[] elm = new Annotation[alWidth];
219     boolean ssOnly = jpredRes_ssonly.contains(annotation.label
220             .toLowerCase());
221     boolean graphOnly = rowType != AlignmentAnnotation.NO_GRAPH;
222     if (!ssOnly && !graphOnly)
223     {
224       // for burial 'B'
225       annotation.showAllColLabels = true;
226     }
227
228     for (int i = 0, iSize = sourceData.length(); i < iSize; i++)
229     {
230       char annot = sourceData.charAt(i);
231       // if we're at a gapped column then skip to next ungapped position
232       if (gapMap != null && gapMap.length > 0)
233       {
234         while (!gapMap[i])
235         {
236           elm[i++] = new Annotation("", "", ' ', Float.NaN);
237         }
238       }
239       switch (rowType)
240       {
241       case AlignmentAnnotation.NO_GRAPH:
242         elm[i] = ssOnly ? new Annotation("", "", annot, Float.NaN,
243                 colourSS(annot)) : new Annotation("" + annot, "" + annot,
244                 '\0', Float.NaN);
245         break;
246       default:
247         try
248         {
249           elm[i] = new Annotation("" + annot, "" + annot, annot,
250                   Integer.valueOf(""+annot));
251         } catch (Exception x)
252         {
253           System.err.println("Expected numeric value in character '"
254                   + annot + "'");
255         }
256       }
257     }
258
259     annotation.annotations = elm;
260     annotation.belowAlignment = true;
261     annotation.validateRangeAndDisplay();
262     return true;
263   }
264
265   private Color colourSS(char annot)
266   {
267     switch (annot)
268     {
269     case 'H':
270       return jalview.renderer.AnnotationRenderer.HELIX_COLOUR;
271     case 'E':
272       return jalview.renderer.AnnotationRenderer.SHEET_COLOUR;
273     }
274     return jalview.renderer.AnnotationRenderer.GLYPHLINE_COLOR;
275   }
276
277   @Override
278   public String getCalcId()
279   {
280     return CALC_ID;
281   }
282
283   private static String CALC_ID = "jabaws21.JPred3Cons";
284
285   public static AlignAnalysisUIText getAlignAnalysisUITest()
286   {
287     return new AlignAnalysisUIText(
288             compbio.ws.client.Services.JpredWS.toString(),
289             jalview.ws.jws2.JPred301Client.class, CALC_ID, false, true,
290             true, "JPred Consensus",
291             "When checked, JPred consensus is updated automatically.",
292             "Change JPred Settings...",
293             "Modify settings for JPred calculations.");
294   }
295 }