JAL-2738 copy to spikes/mungo
[jalview.git] / src / jalview / ws / jws1 / JPredClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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.JvOptionPane;
31 import jalview.gui.WebserviceInfo;
32 import jalview.util.MessageManager;
33
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.util.Hashtable;
37
38 import javax.swing.JMenu;
39 import javax.swing.JMenuItem;
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, boolean msa,
63           AlignmentView alview, AlignFrame parentFrame, boolean viewonly)
64   {
65     super();
66     wsInfo = setWebService(sh);
67     startJPredClient(title, msa, alview, parentFrame, viewonly);
68
69   }
70
71   /**
72    * startJPredClient TODO: refine submission to cope with local prediction of
73    * visible regions or multiple single sequence jobs TODO: sequence
74    * representative support - could submit alignment of representatives as msa.
75    * TODO: msa hidden region prediction - submit each chunk for prediction.
76    * concatenate results of each. TODO: single seq prediction - submit each
77    * contig of each sequence for prediction (but must cope with flanking regions
78    * and short seqs)
79    * 
80    * @param title
81    *          String
82    * @param msa
83    *          boolean
84    * @param alview
85    *          AlignmentView
86    * @param viewonly
87    *          if true then the prediction will be made just on the concatenated
88    *          visible regions
89    */
90   private void startJPredClient(String title, boolean msa,
91           jalview.datamodel.AlignmentView alview, AlignFrame parentFrame,
92           boolean viewonly)
93   {
94     AlignmentView input = alview;
95     if (wsInfo == null)
96     {
97       wsInfo = setWebService();
98     }
99     Jpred server = locateWebService();
100     if (server == null)
101     {
102       Cache.log.warn("Couldn't find a Jpred webservice to invoke!");
103       return;
104     }
105     SeqCigar[] msf = null;
106     SequenceI seq = null;
107     int[] delMap = null;
108     // original JNetClient behaviour - submit full length of sequence or profile
109     // and mask result.
110     msf = input.getSequences();
111     seq = msf[0].getSeq('-');
112
113     if (viewonly)
114     {
115       delMap = alview.getVisibleContigMapFor(seq.gapMap());
116     }
117     if (msa && msf.length > 1)
118     {
119
120       String altitle = getPredictionName(WebServiceName) + " on "
121               + (viewonly ? "visible " : "") + seq.getName()
122               + " using alignment from " + title;
123
124       SequenceI aln[] = new SequenceI[msf.length];
125       for (int i = 0, j = msf.length; i < j; i++)
126       {
127         aln[i] = msf[i].getSeq('-');
128       }
129
130       Hashtable SequenceInfo = jalview.analysis.SeqsetUtils.uniquify(aln,
131               true);
132       if (viewonly)
133       {
134         // Remove hidden regions from sequence objects.
135         String seqs[] = alview.getSequenceStrings('-');
136         for (int i = 0, j = msf.length; i < j; i++)
137         {
138           aln[i].setSequence(seqs[i]);
139         }
140         seq.setSequence(seqs[0]);
141       }
142       wsInfo.setProgressText("Job details for "
143               + (viewonly ? "visible " : "") + "MSA based prediction ("
144               + title + ") on sequence :\n>" + seq.getName() + "\n"
145               + AlignSeq.extractGaps("-. ", seq.getSequenceAsString())
146               + "\n");
147       JPredThread jthread = new JPredThread(wsInfo, altitle, server,
148               SequenceInfo, aln, delMap, alview, parentFrame, WsURL);
149       wsInfo.setthisService(jthread);
150       jthread.start();
151     }
152     else
153     {
154       if (!msa && msf.length > 1)
155       {
156         throw new Error(MessageManager.getString(
157                 "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()
187             .indexOf("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 = "JPred 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())
245             + "\n");
246     SequenceI aln[] = new SequenceI[msf.length];
247     for (int i = 0, j = msf.length; i < j; i++)
248     {
249       aln[i] = new jalview.datamodel.Sequence(msf[i]);
250     }
251
252     Hashtable SequenceInfo = jalview.analysis.SeqsetUtils.uniquify(aln,
253             true);
254
255     Jpred server = locateWebService();
256     if (server == null)
257     {
258       return;
259     }
260
261     JPredThread jthread = new JPredThread(wsInfo, altitle, server,
262             SequenceInfo, aln, null, null, parentFrame, WsURL);
263     wsInfo.setthisService(jthread);
264     jthread.start();
265   }
266
267   public void startJPredClient(String title, SequenceI seq,
268           AlignFrame parentFrame)
269   {
270     if (wsInfo == null)
271     {
272       wsInfo = setWebService();
273     }
274     wsInfo.setProgressText("Job details for prediction on sequence :\n>"
275             + seq.getName() + "\n"
276             + AlignSeq.extractGaps("-. ", seq.getSequenceAsString())
277             + "\n");
278     String altitle = "JPred prediction for sequence " + seq.getName()
279             + " from " + title;
280
281     Hashtable SequenceInfo = jalview.analysis.SeqsetUtils
282             .SeqCharacterHash(seq);
283
284     Jpred server = locateWebService();
285     if (server == null)
286     {
287       return;
288     }
289
290     JPredThread jthread = new JPredThread(wsInfo, altitle, server, WsURL,
291             SequenceInfo, seq, null, null, parentFrame);
292     wsInfo.setthisService(jthread);
293     jthread.start();
294   }
295
296   private WebserviceInfo setWebService()
297   {
298     WebServiceName = "JNetWS";
299     WebServiceJobTitle = MessageManager
300             .getString("label.jnet_secondary_structure_prediction");
301     WebServiceReference = "\"Cuff J. A and Barton G.J (2000) Application of "
302             + "multiple sequence alignment profiles to improve protein secondary structure prediction, "
303             + "Proteins 40:502-511\".";
304     WsURL = "http://www.compbio.dundee.ac.uk/JalviewWS/services/jpred";
305
306     WebserviceInfo wsInfo = new WebserviceInfo(WebServiceJobTitle,
307             WebServiceReference, true);
308
309     return wsInfo;
310   }
311
312   private ext.vamsas.Jpred locateWebService()
313   {
314     ext.vamsas.JpredServiceLocator loc = new JpredServiceLocator(); // Default
315     ext.vamsas.Jpred server = null;
316     try
317     {
318       server = loc.getjpred(new java.net.URL(WsURL)); // JBPNote will be set
319       // from properties
320       ((JpredSoapBindingStub) server).setTimeout(60000); // one minute stub
321       // ((JpredSoapBindingStub)this.server)._setProperty(org.apache.axis.encoding.C,
322       // Boolean.TRUE);
323
324     } catch (Exception ex)
325     {
326       JvOptionPane.showMessageDialog(Desktop.desktop,
327               MessageManager.formatMessage(
328                       "label.secondary_structure_prediction_service_couldnt_be_located",
329                       new String[]
330                       { WebServiceName, WsURL }),
331               MessageManager.getString("label.internal_jalview_error"),
332               JvOptionPane.WARNING_MESSAGE);
333       wsInfo.setProgressText(MessageManager.formatMessage(
334               "label.secondary_structure_prediction_service_couldnt_be_located",
335               new String[]
336               { WebServiceName, WsURL }) + "\n" + ex.getMessage());
337       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
338
339     }
340
341     return server;
342   }
343
344   @Override
345   public void attachWSMenuEntry(JMenu wsmenu, final ServiceHandle sh,
346           final AlignFrame af)
347   {
348     final JMenuItem method = new JMenuItem(sh.getName());
349     method.setToolTipText(sh.getEndpointURL());
350     method.addActionListener(new ActionListener()
351     {
352       @Override
353       public void actionPerformed(ActionEvent e)
354       {
355         AlignmentView msa = af.gatherSeqOrMsaForSecStrPrediction();
356         if (msa.getSequences().length == 1)
357         {
358           // Single Sequence prediction
359           new jalview.ws.jws1.JPredClient(sh, af.getTitle(), false, msa, af,
360                   true);
361         }
362         else
363         {
364           if (msa.getSequences().length > 1)
365           {
366             // Sequence profile based prediction
367             new jalview.ws.jws1.JPredClient(sh, af.getTitle(), true, msa,
368                     af, true);
369           }
370         }
371       }
372     });
373     wsmenu.add(method);
374   }
375 }