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