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