JAL-1379 a JPred v3.0.1 client via JABA 2.1
[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.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.List;
37
38 import compbio.data.sequence.FastaSequence;
39 import compbio.data.sequence.JpredAlignment;
40 import compbio.metadata.Argument;
41
42 public class JPred301Client extends JabawsMsaInterfaceAlignCalcWorker
43         implements AlignCalcWorkerI
44 {
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   public String getServiceActionText()
88   {
89     return "calculating consensus secondary structure prediction using JPred service";
90   }
91   private static HashMap<String, String[]> jpredRowLabels = new HashMap<String,String[]>();
92   private static HashSet<String>jpredRes_graph,jpredRes_ssonly;
93   {
94     jpredRes_ssonly=new HashSet();
95     jpredRes_ssonly.add("jnetpred".toLowerCase());
96     jpredRes_graph=new HashSet();
97     jpredRes_graph.add("jnetconf".toLowerCase());
98     
99   }
100   /**
101    * update the consensus annotation from the sequence profile data using
102    * current visualization settings.
103    */
104   public void updateResultAnnotation(boolean immediate)
105   {
106     if (immediate || !calcMan.isWorking(this) && msascoreset != null)
107     {
108       if (msascoreset instanceof compbio.data.sequence.JpredAlignment)
109       {
110         JpredAlignment jpres = (JpredAlignment) msascoreset;
111         int alWidth = alignViewport.getAlignment().getWidth();
112         ArrayList<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
113         for (FastaSequence fsq:jpres.getJpredSequences())
114         {
115           String[] k = jpredRowLabels.get(fsq.getId());
116           if (k==null)
117           {
118             k = new String[] { fsq.getId(), "JNet Output"};
119           }
120           createAnnotationRowFromString(ourAnnot, getCalcId(), alWidth,
121                   k[0],k[1],
122                   jpredRes_graph.contains(fsq.getId()) ?  AlignmentAnnotation.BAR_GRAPH : AlignmentAnnotation.NO_GRAPH, 0f, 0f,
123                   fsq.getSequence());
124
125         }
126         for (FastaSequence fsq: jpres.getSequences())
127         {
128           if (fsq.getId().equalsIgnoreCase("QUERY"))
129           {
130             createAnnotationRowFromString(ourAnnot, getCalcId(), alWidth,
131                     "Query", "JPred Reference Sequence",
132                     AlignmentAnnotation.NO_GRAPH, 0f, 0f,
133                     fsq.getSequence());
134           }
135         }
136         if (ourAnnot.size() > 0)
137         {
138           updateOurAnnots(ourAnnot);
139         }
140       }
141     }
142   }
143
144   private void createAnnotationRowFromString(
145           ArrayList<AlignmentAnnotation> ourAnnot, String calcId,
146           int alWidth, String label, String descr, int rowType, float min,
147           float max, String jpredPrediction)
148   {
149     // simple annotation row
150     AlignmentAnnotation annotation = alignViewport.getAlignment()
151             .findOrCreateAnnotation(label, calcId, true, null, null);
152     if (alWidth == gapMap.length) // scr.getScores().size())
153     {
154       annotation.label = new String(label);
155       annotation.description = new String(descr);
156       annotation.graph = rowType;
157       annotation.graphMin = min;
158       annotation.graphMax = max;
159       if (constructAnnotationFromString(annotation, jpredPrediction,
160               alWidth, rowType))
161       {
162         // created a valid annotation from the data
163         ourAnnot.add(annotation);
164       }
165     }
166   }
167
168   private boolean constructAnnotationFromString(
169           AlignmentAnnotation annotation, String sourceData, int alWidth,
170           int rowType)
171   {
172     if (sourceData.length()==0 && alWidth>0)
173     {
174       return false;
175     }
176     Annotation[] elm = new Annotation[alWidth];
177
178     for (int i = 0, iSize = sourceData.length(); i < iSize; i++)
179     {
180       char annot = sourceData.charAt(i);
181       // if we're at a gapped column then skip to next ungapped position
182       if (gapMap != null && gapMap.length > 0)
183       {
184         while (!gapMap[i])
185         {
186           elm[i++] = new Annotation("", "", ' ', Float.NaN);
187         }
188       }
189       switch (rowType)
190       {
191       case AlignmentAnnotation.NO_GRAPH:
192         elm[i] = new Annotation("" + annot, "" + annot, annot, Float.NaN);
193         break;
194       default:
195         try
196         {
197           elm[i] = new Annotation("" + annot, "" + annot, annot,
198                   Integer.valueOf(annot));
199         } catch (Exception x)
200         {
201           System.err.println("Expected numeric value in character '"
202                   + annot + "'");
203         }
204       }
205     }
206
207     annotation.annotations = elm;
208     annotation.belowAlignment = true;
209     annotation.validateRangeAndDisplay();
210     return true;
211   }
212
213   @Override
214   public String getCalcId()
215   {
216     return CALC_ID;
217   }
218
219   private static String CALC_ID = "jabaws21.JPred3Cons";
220
221   public static AlignAnalysisUIText getAlignAnalysisUITest()
222   {
223     return new AlignAnalysisUIText(
224             compbio.ws.client.Services.JpredWS.toString(),
225             jalview.ws.jws2.JPred301Client.class, CALC_ID, false, true,
226             true, "JPred Consensus",
227             "When checked, JPred consensus is updated automatically.",
228             "Change JPred Settings...",
229             "Modify settings for JPred calculations.");
230   }
231 }