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