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(
148                 "Implementation Error! Multiple single sequence prediction jobs are not yet supported.");
149       }
150
151       String altitle = getPredictionName(WebServiceName) + " for "
152               + (viewonly ? "visible " : "") + "sequence " + seq.getName()
153               + " from " + title;
154       String seqname = seq.getName();
155       Hashtable SequenceInfo = jalview.analysis.SeqsetUtils
156               .SeqCharacterHash(seq);
157       if (viewonly)
158       {
159         // Remove hidden regions from input sequence
160         String seqs[] = alview.getSequenceStrings('-');
161         seq.setSequence(seqs[0]);
162       }
163       wsInfo.setProgressText("Job details for prediction on "
164               + (viewonly ? "visible " : "") + "sequence :\n>" + seqname
165               + "\n"
166               + AlignSeq.extractGaps("-. ", seq.getSequenceAsString())
167               + "\n");
168       JPredThread jthread = new JPredThread(wsInfo, altitle, server, WsURL,
169               SequenceInfo, seq, delMap, alview, parentFrame);
170       wsInfo.setthisService(jthread);
171       jthread.start();
172     }
173   }
174
175   private String getPredictionName(String webServiceName)
176   {
177     if (webServiceName.toLowerCase().indexOf(
178             "secondary structure prediction") > -1)
179     {
180       return webServiceName;
181     }
182     else
183     {
184       return webServiceName + "secondary structure prediction";
185     }
186   }
187
188   public JPredClient(ext.vamsas.ServiceHandle sh, String title,
189           SequenceI seq, AlignFrame parentFrame)
190   {
191     super();
192     wsInfo = setWebService(sh);
193     startJPredClient(title, seq, parentFrame);
194   }
195
196   public JPredClient(ext.vamsas.ServiceHandle sh, String title,
197           SequenceI[] msa, AlignFrame parentFrame)
198   {
199     wsInfo = setWebService(sh);
200     startJPredClient(title, msa, parentFrame);
201   }
202
203   public JPredClient(String title, SequenceI[] msf)
204   {
205     startJPredClient(title, msf, null);
206   }
207
208   public JPredClient(String title, SequenceI seq)
209   {
210     startJPredClient(title, seq, null);
211   }
212
213   public JPredClient()
214   {
215
216     super();
217     // add a class reference to the list
218   }
219
220   private void startJPredClient(String title, SequenceI[] msf,
221           AlignFrame parentFrame)
222   {
223     if (wsInfo == null)
224     {
225       wsInfo = setWebService();
226     }
227
228     SequenceI seq = msf[0];
229
230     String altitle = "JNet prediction on " + seq.getName()
231             + " using alignment from " + title;
232
233     wsInfo.setProgressText("Job details for MSA based prediction (" + title
234             + ") on sequence :\n>" + seq.getName() + "\n"
235             + AlignSeq.extractGaps("-. ", seq.getSequenceAsString()) + "\n");
236     SequenceI aln[] = new SequenceI[msf.length];
237     for (int i = 0, j = msf.length; i < j; i++)
238     {
239       aln[i] = new jalview.datamodel.Sequence(msf[i]);
240     }
241
242     Hashtable SequenceInfo = jalview.analysis.SeqsetUtils.uniquify(aln,
243             true);
244
245     Jpred server = locateWebService();
246     if (server == null)
247     {
248       return;
249     }
250
251     JPredThread jthread = new JPredThread(wsInfo, altitle, server,
252             SequenceInfo, aln, null, null, parentFrame, WsURL);
253     wsInfo.setthisService(jthread);
254     jthread.start();
255   }
256
257   public void startJPredClient(String title, SequenceI seq,
258           AlignFrame parentFrame)
259   {
260     if (wsInfo == null)
261     {
262       wsInfo = setWebService();
263     }
264     wsInfo.setProgressText("Job details for prediction on sequence :\n>"
265             + seq.getName() + "\n"
266             + AlignSeq.extractGaps("-. ", seq.getSequenceAsString()) + "\n");
267     String altitle = "JNet prediction for sequence " + seq.getName()
268             + " from " + title;
269
270     Hashtable SequenceInfo = jalview.analysis.SeqsetUtils
271             .SeqCharacterHash(seq);
272
273     Jpred server = locateWebService();
274     if (server == null)
275     {
276       return;
277     }
278
279     JPredThread jthread = new JPredThread(wsInfo, altitle, server, WsURL,
280             SequenceInfo, seq, null, null, parentFrame);
281     wsInfo.setthisService(jthread);
282     jthread.start();
283   }
284
285   private WebserviceInfo setWebService()
286   {
287     WebServiceName = "JNetWS";
288     WebServiceJobTitle = "JNet secondary structure prediction";
289     WebServiceReference = "\"Cuff J. A and Barton G.J (2000) Application of "
290             + "multiple sequence alignment profiles to improve protein secondary structure prediction, "
291             + "Proteins 40:502-511\".";
292     WsURL = "http://www.compbio.dundee.ac.uk/JalviewWS/services/jpred";
293
294     WebserviceInfo wsInfo = new WebserviceInfo(WebServiceJobTitle,
295             WebServiceReference);
296
297     return wsInfo;
298   }
299
300   private ext.vamsas.Jpred locateWebService()
301   {
302     ext.vamsas.JpredServiceLocator loc = new JpredServiceLocator(); // Default
303     ext.vamsas.Jpred server = null;
304     try
305     {
306       server = loc.getjpred(new java.net.URL(WsURL)); // JBPNote will be set
307       // from properties
308       ((JpredSoapBindingStub) server).setTimeout(60000); // one minute stub
309       // ((JpredSoapBindingStub)this.server)._setProperty(org.apache.axis.encoding.C,
310       // Boolean.TRUE);
311
312     } catch (Exception ex)
313     {
314       JOptionPane.showMessageDialog(Desktop.desktop,
315                   MessageManager.formatMessage("label.secondary_structure_prediction_service_couldnt_be_located", new String[]{WebServiceName,WsURL}),
316                   MessageManager.getString("label.internal_jalview_error"),
317               JOptionPane.WARNING_MESSAGE);
318       wsInfo.setProgressText(MessageManager.formatMessage("label.secondary_structure_prediction_service_couldnt_be_located", new String[]{WebServiceName,WsURL})
319                   + "\n"
320               + ex.getMessage());
321       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
322
323     }
324
325     return server;
326   }
327
328   public void attachWSMenuEntry(JMenu wsmenu, final ServiceHandle sh,
329           final AlignFrame af)
330   {
331     final JMenuItem method = new JMenuItem(sh.getName());
332     method.setToolTipText(sh.getEndpointURL());
333     method.addActionListener(new ActionListener()
334     {
335       public void actionPerformed(ActionEvent e)
336       {
337         AlignmentView msa = af.gatherSeqOrMsaForSecStrPrediction();
338         if (msa.getSequences().length == 1)
339         {
340           // Single Sequence prediction
341           new jalview.ws.jws1.JPredClient(sh, af.getTitle(), false, msa,
342                   af, true);
343         }
344         else
345         {
346           if (msa.getSequences().length > 1)
347           {
348             // Sequence profile based prediction
349             new jalview.ws.jws1.JPredClient(sh, af.getTitle(), true, msa,
350                     af, true);
351           }
352         }
353       }
354     });
355     wsmenu.add(method);
356   }
357 }