df0e091a072a3f654c88c70e39b0a0a7d16795d6
[jalview.git] / src / jalview / ws / jws1 / JPredClient.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.jws1;
22
23 import jalview.analysis.AlignSeq;
24 import jalview.bin.Cache;
25 import jalview.datamodel.AlignmentView;
26 import jalview.datamodel.SeqCigar;
27 import jalview.datamodel.SequenceI;
28 import jalview.gui.AlignFrame;
29 import jalview.gui.Desktop;
30 import jalview.gui.WebserviceInfo;
31 import jalview.util.MessageManager;
32
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.util.Hashtable;
36
37 import javax.swing.JMenu;
38 import javax.swing.JMenuItem;
39 import javax.swing.JOptionPane;
40
41 import ext.vamsas.Jpred;
42 import ext.vamsas.JpredServiceLocator;
43 import ext.vamsas.JpredSoapBindingStub;
44 import ext.vamsas.ServiceHandle;
45
46 public class JPredClient extends WS1Client
47 {
48   /**
49    * crate a new GUI JPred Job
50    * 
51    * @param sh
52    *          ServiceHandle
53    * @param title
54    *          String
55    * @param msa
56    *          boolean - true - submit alignment as a sequence profile
57    * @param alview
58    *          AlignmentView
59    * @param viewonly
60    *          TODO
61    */
62   public JPredClient(ext.vamsas.ServiceHandle sh, String title,
63           boolean msa, AlignmentView alview, AlignFrame parentFrame,
64           boolean viewonly)
65   {
66     super();
67     wsInfo = setWebService(sh);
68     startJPredClient(title, msa, alview, parentFrame, viewonly);
69
70   }
71
72   /**
73    * startJPredClient TODO: refine submission to cope with local prediction of
74    * visible regions or multiple single sequence jobs TODO: sequence
75    * representative support - could submit alignment of representatives as msa.
76    * TODO: msa hidden region prediction - submit each chunk for prediction.
77    * concatenate results of each. TODO: single seq prediction - submit each
78    * contig of each sequence for prediction (but must cope with flanking regions
79    * and short seqs)
80    * 
81    * @param title
82    *          String
83    * @param msa
84    *          boolean
85    * @param alview
86    *          AlignmentView
87    * @param viewonly
88    *          if true then the prediction will be made just on the concatenated
89    *          visible regions
90    */
91   private void startJPredClient(String title, boolean msa,
92           jalview.datamodel.AlignmentView alview, AlignFrame parentFrame,
93           boolean viewonly)
94   {
95     AlignmentView input = alview;
96     if (wsInfo == null)
97     {
98       wsInfo = setWebService();
99     }
100     Jpred server = locateWebService();
101     if (server == null)
102     {
103       Cache.log.warn("Couldn't find a Jpred webservice to invoke!");
104       return;
105     }
106     SeqCigar[] msf = null;
107     SequenceI seq = null;
108     int[] delMap = null;
109     // original JNetClient behaviour - submit full length of sequence or profile
110     // and mask result.
111     msf = input.getSequences();
112     seq = msf[0].getSeq('-');
113
114     if (viewonly)
115     {
116       delMap = alview.getVisibleContigMapFor(seq.gapMap());
117     }
118     if (msa && msf.length > 1)
119     {
120
121       String altitle = getPredictionName(WebServiceName) + " on "
122               + (viewonly ? "visible " : "") + seq.getName()
123               + " using alignment from " + title;
124
125       SequenceI aln[] = new SequenceI[msf.length];
126       for (int i = 0, j = msf.length; i < j; i++)
127       {
128         aln[i] = msf[i].getSeq('-');
129       }
130
131       Hashtable SequenceInfo = jalview.analysis.SeqsetUtils.uniquify(aln,
132               true);
133       if (viewonly)
134       {
135         // Remove hidden regions from sequence objects.
136         String seqs[] = alview.getSequenceStrings('-');
137         for (int i = 0, j = msf.length; i < j; i++)
138         {
139           aln[i].setSequence(seqs[i]);
140         }
141         seq.setSequence(seqs[0]);
142       }
143       wsInfo.setProgressText("Job details for "
144               + (viewonly ? "visible " : "") + "MSA based prediction ("
145               + title + ") on sequence :\n>" + seq.getName() + "\n"
146               + AlignSeq.extractGaps("-. ", seq.getSequenceAsString())
147               + "\n");
148       JPredThread jthread = new JPredThread(wsInfo, altitle, server,
149               SequenceInfo, aln, delMap, alview, parentFrame, WsURL);
150       wsInfo.setthisService(jthread);
151       jthread.start();
152     }
153     else
154     {
155       if (!msa && msf.length > 1)
156       {
157         throw new Error(MessageManager.getString("error.implementation_error_multiple_single_sequence_prediction_jobs_not_supported"));
158       }
159
160       String altitle = getPredictionName(WebServiceName) + " for "
161               + (viewonly ? "visible " : "") + "sequence " + seq.getName()
162               + " from " + title;
163       String seqname = seq.getName();
164       Hashtable SequenceInfo = jalview.analysis.SeqsetUtils
165               .SeqCharacterHash(seq);
166       if (viewonly)
167       {
168         // Remove hidden regions from input sequence
169         String seqs[] = alview.getSequenceStrings('-');
170         seq.setSequence(seqs[0]);
171       }
172       wsInfo.setProgressText("Job details for prediction on "
173               + (viewonly ? "visible " : "") + "sequence :\n>" + seqname
174               + "\n"
175               + AlignSeq.extractGaps("-. ", seq.getSequenceAsString())
176               + "\n");
177       JPredThread jthread = new JPredThread(wsInfo, altitle, server, WsURL,
178               SequenceInfo, seq, delMap, alview, parentFrame);
179       wsInfo.setthisService(jthread);
180       jthread.start();
181     }
182   }
183
184   private String getPredictionName(String webServiceName)
185   {
186     if (webServiceName.toLowerCase().indexOf(
187             "secondary structure prediction") > -1)
188     {
189       return webServiceName;
190     }
191     else
192     {
193       return webServiceName + "secondary structure prediction";
194     }
195   }
196
197   public JPredClient(ext.vamsas.ServiceHandle sh, String title,
198           SequenceI seq, AlignFrame parentFrame)
199   {
200     super();
201     wsInfo = setWebService(sh);
202     startJPredClient(title, seq, parentFrame);
203   }
204
205   public JPredClient(ext.vamsas.ServiceHandle sh, String title,
206           SequenceI[] msa, AlignFrame parentFrame)
207   {
208     wsInfo = setWebService(sh);
209     startJPredClient(title, msa, parentFrame);
210   }
211
212   public JPredClient(String title, SequenceI[] msf)
213   {
214     startJPredClient(title, msf, null);
215   }
216
217   public JPredClient(String title, SequenceI seq)
218   {
219     startJPredClient(title, seq, null);
220   }
221
222   public JPredClient()
223   {
224
225     super();
226     // add a class reference to the list
227   }
228
229   private void startJPredClient(String title, SequenceI[] msf,
230           AlignFrame parentFrame)
231   {
232     if (wsInfo == null)
233     {
234       wsInfo = setWebService();
235     }
236
237     SequenceI seq = msf[0];
238
239     String altitle = "JNet prediction on " + seq.getName()
240             + " using alignment from " + title;
241
242     wsInfo.setProgressText("Job details for MSA based prediction (" + title
243             + ") on sequence :\n>" + seq.getName() + "\n"
244             + AlignSeq.extractGaps("-. ", seq.getSequenceAsString()) + "\n");
245     SequenceI aln[] = new SequenceI[msf.length];
246     for (int i = 0, j = msf.length; i < j; i++)
247     {
248       aln[i] = new jalview.datamodel.Sequence(msf[i]);
249     }
250
251     Hashtable SequenceInfo = jalview.analysis.SeqsetUtils.uniquify(aln,
252             true);
253
254     Jpred server = locateWebService();
255     if (server == null)
256     {
257       return;
258     }
259
260     JPredThread jthread = new JPredThread(wsInfo, altitle, server,
261             SequenceInfo, aln, null, null, parentFrame, WsURL);
262     wsInfo.setthisService(jthread);
263     jthread.start();
264   }
265
266   public void startJPredClient(String title, SequenceI seq,
267           AlignFrame parentFrame)
268   {
269     if (wsInfo == null)
270     {
271       wsInfo = setWebService();
272     }
273     wsInfo.setProgressText("Job details for prediction on sequence :\n>"
274             + seq.getName() + "\n"
275             + AlignSeq.extractGaps("-. ", seq.getSequenceAsString()) + "\n");
276     String altitle = "JNet prediction for sequence " + seq.getName()
277             + " from " + title;
278
279     Hashtable SequenceInfo = jalview.analysis.SeqsetUtils
280             .SeqCharacterHash(seq);
281
282     Jpred server = locateWebService();
283     if (server == null)
284     {
285       return;
286     }
287
288     JPredThread jthread = new JPredThread(wsInfo, altitle, server, WsURL,
289             SequenceInfo, seq, null, null, parentFrame);
290     wsInfo.setthisService(jthread);
291     jthread.start();
292   }
293
294   private WebserviceInfo setWebService()
295   {
296     WebServiceName = "JNetWS";
297     WebServiceJobTitle = MessageManager.getString("label.jnet_secondary_structure_prediction");
298     WebServiceReference = "\"Cuff J. A and Barton G.J (2000) Application of "
299             + "multiple sequence alignment profiles to improve protein secondary structure prediction, "
300             + "Proteins 40:502-511\".";
301     WsURL = "http://www.compbio.dundee.ac.uk/JalviewWS/services/jpred";
302
303     WebserviceInfo wsInfo = new WebserviceInfo(WebServiceJobTitle,
304             WebServiceReference, true);
305
306     return wsInfo;
307   }
308
309   private ext.vamsas.Jpred locateWebService()
310   {
311     ext.vamsas.JpredServiceLocator loc = new JpredServiceLocator(); // Default
312     ext.vamsas.Jpred server = null;
313     try
314     {
315       server = loc.getjpred(new java.net.URL(WsURL)); // JBPNote will be set
316       // from properties
317       ((JpredSoapBindingStub) server).setTimeout(60000); // one minute stub
318       // ((JpredSoapBindingStub)this.server)._setProperty(org.apache.axis.encoding.C,
319       // Boolean.TRUE);
320
321     } catch (Exception ex)
322     {
323       JOptionPane.showMessageDialog(Desktop.desktop,
324                   MessageManager.formatMessage("label.secondary_structure_prediction_service_couldnt_be_located", new String[]{WebServiceName,WsURL}),
325                   MessageManager.getString("label.internal_jalview_error"),
326               JOptionPane.WARNING_MESSAGE);
327       wsInfo.setProgressText(MessageManager.formatMessage("label.secondary_structure_prediction_service_couldnt_be_located", new String[]{WebServiceName,WsURL})
328                   + "\n"
329               + ex.getMessage());
330       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
331
332     }
333
334     return server;
335   }
336
337   public void attachWSMenuEntry(JMenu wsmenu, final ServiceHandle sh,
338           final AlignFrame af)
339   {
340     final JMenuItem method = new JMenuItem(sh.getName());
341     method.setToolTipText(sh.getEndpointURL());
342     method.addActionListener(new ActionListener()
343     {
344       public void actionPerformed(ActionEvent e)
345       {
346         AlignmentView msa = af.gatherSeqOrMsaForSecStrPrediction();
347         if (msa.getSequences().length == 1)
348         {
349           // Single Sequence prediction
350           new jalview.ws.jws1.JPredClient(sh, af.getTitle(), false, msa,
351                   af, true);
352         }
353         else
354         {
355           if (msa.getSequences().length > 1)
356           {
357             // Sequence profile based prediction
358             new jalview.ws.jws1.JPredClient(sh, af.getTitle(), true, msa,
359                     af, true);
360           }
361         }
362       }
363     });
364     wsmenu.add(method);
365   }
366 }