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