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