f13672b144d54d0e02a5ed948c2264e8e3c33291
[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.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(
158                 MessageManager
159                         .getString("error.implementation_error_multiple_single_sequence_prediction_jobs_not_supported"));
160       }
161
162       String altitle = getPredictionName(WebServiceName) + " for "
163               + (viewonly ? "visible " : "") + "sequence " + seq.getName()
164               + " from " + title;
165       String seqname = seq.getName();
166       Hashtable SequenceInfo = jalview.analysis.SeqsetUtils
167               .SeqCharacterHash(seq);
168       if (viewonly)
169       {
170         // Remove hidden regions from input sequence
171         String seqs[] = alview.getSequenceStrings('-');
172         seq.setSequence(seqs[0]);
173       }
174       wsInfo.setProgressText("Job details for prediction on "
175               + (viewonly ? "visible " : "") + "sequence :\n>" + seqname
176               + "\n"
177               + AlignSeq.extractGaps("-. ", seq.getSequenceAsString())
178               + "\n");
179       JPredThread jthread = new JPredThread(wsInfo, altitle, server, WsURL,
180               SequenceInfo, seq, delMap, alview, parentFrame);
181       wsInfo.setthisService(jthread);
182       jthread.start();
183     }
184   }
185
186   private String getPredictionName(String webServiceName)
187   {
188     if (webServiceName.toLowerCase().indexOf(
189             "secondary structure prediction") > -1)
190     {
191       return webServiceName;
192     }
193     else
194     {
195       return webServiceName + "secondary structure prediction";
196     }
197   }
198
199   public JPredClient(ext.vamsas.ServiceHandle sh, String title,
200           SequenceI seq, AlignFrame parentFrame)
201   {
202     super();
203     wsInfo = setWebService(sh);
204     startJPredClient(title, seq, parentFrame);
205   }
206
207   public JPredClient(ext.vamsas.ServiceHandle sh, String title,
208           SequenceI[] msa, AlignFrame parentFrame)
209   {
210     wsInfo = setWebService(sh);
211     startJPredClient(title, msa, parentFrame);
212   }
213
214   public JPredClient(String title, SequenceI[] msf)
215   {
216     startJPredClient(title, msf, null);
217   }
218
219   public JPredClient(String title, SequenceI seq)
220   {
221     startJPredClient(title, seq, null);
222   }
223
224   public JPredClient()
225   {
226
227     super();
228     // add a class reference to the list
229   }
230
231   private void startJPredClient(String title, SequenceI[] msf,
232           AlignFrame parentFrame)
233   {
234     if (wsInfo == null)
235     {
236       wsInfo = setWebService();
237     }
238
239     SequenceI seq = msf[0];
240
241     String altitle = "JNet prediction on " + seq.getName()
242             + " using alignment from " + title;
243
244     wsInfo.setProgressText("Job details for MSA based prediction (" + title
245             + ") on sequence :\n>" + seq.getName() + "\n"
246             + AlignSeq.extractGaps("-. ", seq.getSequenceAsString()) + "\n");
247     SequenceI aln[] = new SequenceI[msf.length];
248     for (int i = 0, j = msf.length; i < j; i++)
249     {
250       aln[i] = new jalview.datamodel.Sequence(msf[i]);
251     }
252
253     Hashtable SequenceInfo = jalview.analysis.SeqsetUtils.uniquify(aln,
254             true);
255
256     Jpred server = locateWebService();
257     if (server == null)
258     {
259       return;
260     }
261
262     JPredThread jthread = new JPredThread(wsInfo, altitle, server,
263             SequenceInfo, aln, null, null, parentFrame, WsURL);
264     wsInfo.setthisService(jthread);
265     jthread.start();
266   }
267
268   public void startJPredClient(String title, SequenceI seq,
269           AlignFrame parentFrame)
270   {
271     if (wsInfo == null)
272     {
273       wsInfo = setWebService();
274     }
275     wsInfo.setProgressText("Job details for prediction on sequence :\n>"
276             + seq.getName() + "\n"
277             + AlignSeq.extractGaps("-. ", seq.getSequenceAsString()) + "\n");
278     String altitle = "JNet 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       JOptionPane
327               .showMessageDialog(
328                       Desktop.desktop,
329                       MessageManager
330                               .formatMessage(
331                                       "label.secondary_structure_prediction_service_couldnt_be_located",
332                                       new String[] { WebServiceName, WsURL }),
333                       MessageManager
334                               .getString("label.internal_jalview_error"),
335                       JOptionPane.WARNING_MESSAGE);
336       wsInfo.setProgressText(MessageManager
337               .formatMessage(
338                       "label.secondary_structure_prediction_service_couldnt_be_located",
339                       new String[] { WebServiceName, WsURL })
340               + "\n" + ex.getMessage());
341       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
342
343     }
344
345     return server;
346   }
347
348   public void attachWSMenuEntry(JMenu wsmenu, final ServiceHandle sh,
349           final AlignFrame af)
350   {
351     final JMenuItem method = new JMenuItem(sh.getName());
352     method.setToolTipText(sh.getEndpointURL());
353     method.addActionListener(new ActionListener()
354     {
355       public void actionPerformed(ActionEvent e)
356       {
357         AlignmentView msa = af.gatherSeqOrMsaForSecStrPrediction();
358         if (msa.getSequences().length == 1)
359         {
360           // Single Sequence prediction
361           new jalview.ws.jws1.JPredClient(sh, af.getTitle(), false, msa,
362                   af, true);
363         }
364         else
365         {
366           if (msa.getSequences().length > 1)
367           {
368             // Sequence profile based prediction
369             new jalview.ws.jws1.JPredClient(sh, af.getTitle(), true, msa,
370                     af, true);
371           }
372         }
373       }
374     });
375     wsmenu.add(method);
376   }
377 }