JAL-1432 updated copyright notices
[jalview.git] / src / jalview / ws / jws1 / JPredClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.ws.jws1;
20
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
23 import java.util.*;
24
25 import javax.swing.*;
26
27 import ext.vamsas.*;
28 import jalview.analysis.*;
29 import jalview.bin.*;
30 import jalview.datamodel.*;
31 import jalview.gui.*;
32
33 public class JPredClient extends WS1Client
34 {
35   /**
36    * crate a new GUI JPred Job
37    * 
38    * @param sh
39    *          ServiceHandle
40    * @param title
41    *          String
42    * @param msa
43    *          boolean - true - submit alignment as a sequence profile
44    * @param alview
45    *          AlignmentView
46    * @param viewonly
47    *          TODO
48    */
49   public JPredClient(ext.vamsas.ServiceHandle sh, String title,
50           boolean msa, AlignmentView alview, AlignFrame parentFrame,
51           boolean viewonly)
52   {
53     super();
54     wsInfo = setWebService(sh);
55     startJPredClient(title, msa, alview, parentFrame, viewonly);
56
57   }
58
59   /**
60    * startJPredClient TODO: refine submission to cope with local prediction of
61    * visible regions or multiple single sequence jobs TODO: sequence
62    * representative support - could submit alignment of representatives as msa.
63    * TODO: msa hidden region prediction - submit each chunk for prediction.
64    * concatenate results of each. TODO: single seq prediction - submit each
65    * contig of each sequence for prediction (but must cope with flanking regions
66    * and short seqs)
67    * 
68    * @param title
69    *          String
70    * @param msa
71    *          boolean
72    * @param alview
73    *          AlignmentView
74    * @param viewonly
75    *          if true then the prediction will be made just on the concatenated
76    *          visible regions
77    */
78   private void startJPredClient(String title, boolean msa,
79           jalview.datamodel.AlignmentView alview, AlignFrame parentFrame,
80           boolean viewonly)
81   {
82     AlignmentView input = alview;
83     if (wsInfo == null)
84     {
85       wsInfo = setWebService();
86     }
87     Jpred server = locateWebService();
88     if (server == null)
89     {
90       Cache.log.warn("Couldn't find a Jpred webservice to invoke!");
91       return;
92     }
93     SeqCigar[] msf = null;
94     SequenceI seq = null;
95     int[] delMap = null;
96     // original JNetClient behaviour - submit full length of sequence or profile
97     // and mask result.
98     msf = input.getSequences();
99     seq = msf[0].getSeq('-');
100
101     if (viewonly)
102     {
103       delMap = alview.getVisibleContigMapFor(seq.gapMap());
104     }
105     if (msa && msf.length > 1)
106     {
107
108       String altitle = getPredictionName(WebServiceName) + " on "
109               + (viewonly ? "visible " : "") + seq.getName()
110               + " using alignment from " + title;
111
112       SequenceI aln[] = new SequenceI[msf.length];
113       for (int i = 0, j = msf.length; i < j; i++)
114       {
115         aln[i] = msf[i].getSeq('-');
116       }
117
118       Hashtable SequenceInfo = jalview.analysis.SeqsetUtils.uniquify(aln,
119               true);
120       if (viewonly)
121       {
122         // Remove hidden regions from sequence objects.
123         String seqs[] = alview.getSequenceStrings('-');
124         for (int i = 0, j = msf.length; i < j; i++)
125         {
126           aln[i].setSequence(seqs[i]);
127         }
128         seq.setSequence(seqs[0]);
129       }
130       wsInfo.setProgressText("Job details for "
131               + (viewonly ? "visible " : "") + "MSA based prediction ("
132               + title + ") on sequence :\n>" + seq.getName() + "\n"
133               + AlignSeq.extractGaps("-. ", seq.getSequenceAsString())
134               + "\n");
135       JPredThread jthread = new JPredThread(wsInfo, altitle, server,
136               SequenceInfo, aln, delMap, alview, parentFrame, WsURL);
137       wsInfo.setthisService(jthread);
138       jthread.start();
139     }
140     else
141     {
142       if (!msa && msf.length > 1)
143       {
144         throw new Error(
145                 "Implementation Error! Multiple single sequence prediction jobs are not yet supported.");
146       }
147
148       String altitle = getPredictionName(WebServiceName) + " for "
149               + (viewonly ? "visible " : "") + "sequence " + seq.getName()
150               + " from " + title;
151       String seqname = seq.getName();
152       Hashtable SequenceInfo = jalview.analysis.SeqsetUtils
153               .SeqCharacterHash(seq);
154       if (viewonly)
155       {
156         // Remove hidden regions from input sequence
157         String seqs[] = alview.getSequenceStrings('-');
158         seq.setSequence(seqs[0]);
159       }
160       wsInfo.setProgressText("Job details for prediction on "
161               + (viewonly ? "visible " : "") + "sequence :\n>" + seqname
162               + "\n"
163               + AlignSeq.extractGaps("-. ", seq.getSequenceAsString())
164               + "\n");
165       JPredThread jthread = new JPredThread(wsInfo, altitle, server, WsURL,
166               SequenceInfo, seq, delMap, alview, parentFrame);
167       wsInfo.setthisService(jthread);
168       jthread.start();
169     }
170   }
171
172   private String getPredictionName(String webServiceName)
173   {
174     if (webServiceName.toLowerCase().indexOf(
175             "secondary structure prediction") > -1)
176     {
177       return webServiceName;
178     }
179     else
180     {
181       return webServiceName + "secondary structure prediction";
182     }
183   }
184
185   public JPredClient(ext.vamsas.ServiceHandle sh, String title,
186           SequenceI seq, AlignFrame parentFrame)
187   {
188     super();
189     wsInfo = setWebService(sh);
190     startJPredClient(title, seq, parentFrame);
191   }
192
193   public JPredClient(ext.vamsas.ServiceHandle sh, String title,
194           SequenceI[] msa, AlignFrame parentFrame)
195   {
196     wsInfo = setWebService(sh);
197     startJPredClient(title, msa, parentFrame);
198   }
199
200   public JPredClient(String title, SequenceI[] msf)
201   {
202     startJPredClient(title, msf, null);
203   }
204
205   public JPredClient(String title, SequenceI seq)
206   {
207     startJPredClient(title, seq, null);
208   }
209
210   public JPredClient()
211   {
212
213     super();
214     // add a class reference to the list
215   }
216
217   private void startJPredClient(String title, SequenceI[] msf,
218           AlignFrame parentFrame)
219   {
220     if (wsInfo == null)
221     {
222       wsInfo = setWebService();
223     }
224
225     SequenceI seq = msf[0];
226
227     String altitle = "JNet prediction on " + seq.getName()
228             + " using alignment from " + title;
229
230     wsInfo.setProgressText("Job details for MSA based prediction (" + title
231             + ") on sequence :\n>" + seq.getName() + "\n"
232             + AlignSeq.extractGaps("-. ", seq.getSequenceAsString()) + "\n");
233     SequenceI aln[] = new SequenceI[msf.length];
234     for (int i = 0, j = msf.length; i < j; i++)
235     {
236       aln[i] = new jalview.datamodel.Sequence(msf[i]);
237     }
238
239     Hashtable SequenceInfo = jalview.analysis.SeqsetUtils.uniquify(aln,
240             true);
241
242     Jpred server = locateWebService();
243     if (server == null)
244     {
245       return;
246     }
247
248     JPredThread jthread = new JPredThread(wsInfo, altitle, server,
249             SequenceInfo, aln, null, null, parentFrame, WsURL);
250     wsInfo.setthisService(jthread);
251     jthread.start();
252   }
253
254   public void startJPredClient(String title, SequenceI seq,
255           AlignFrame parentFrame)
256   {
257     if (wsInfo == null)
258     {
259       wsInfo = setWebService();
260     }
261     wsInfo.setProgressText("Job details for prediction on sequence :\n>"
262             + seq.getName() + "\n"
263             + AlignSeq.extractGaps("-. ", seq.getSequenceAsString()) + "\n");
264     String altitle = "JNet prediction for sequence " + seq.getName()
265             + " from " + title;
266
267     Hashtable SequenceInfo = jalview.analysis.SeqsetUtils
268             .SeqCharacterHash(seq);
269
270     Jpred server = locateWebService();
271     if (server == null)
272     {
273       return;
274     }
275
276     JPredThread jthread = new JPredThread(wsInfo, altitle, server, WsURL,
277             SequenceInfo, seq, null, null, parentFrame);
278     wsInfo.setthisService(jthread);
279     jthread.start();
280   }
281
282   private WebserviceInfo setWebService()
283   {
284     WebServiceName = "JNetWS";
285     WebServiceJobTitle = "JNet secondary structure prediction";
286     WebServiceReference = "\"Cuff J. A and Barton G.J (2000) Application of "
287             + "multiple sequence alignment profiles to improve protein secondary structure prediction, "
288             + "Proteins 40:502-511\".";
289     WsURL = "http://www.compbio.dundee.ac.uk/JalviewWS/services/jpred";
290
291     WebserviceInfo wsInfo = new WebserviceInfo(WebServiceJobTitle,
292             WebServiceReference);
293
294     return wsInfo;
295   }
296
297   private ext.vamsas.Jpred locateWebService()
298   {
299     ext.vamsas.JpredServiceLocator loc = new JpredServiceLocator(); // Default
300     ext.vamsas.Jpred server = null;
301     try
302     {
303       server = loc.getjpred(new java.net.URL(WsURL)); // JBPNote will be set
304       // from properties
305       ((JpredSoapBindingStub) server).setTimeout(60000); // one minute stub
306       // ((JpredSoapBindingStub)this.server)._setProperty(org.apache.axis.encoding.C,
307       // Boolean.TRUE);
308
309     } catch (Exception ex)
310     {
311       JOptionPane.showMessageDialog(Desktop.desktop,
312               "The Secondary Structure Prediction Service named "
313                       + WebServiceName + " at " + WsURL
314                       + " couldn't be located.", "Internal Jalview Error",
315               JOptionPane.WARNING_MESSAGE);
316       wsInfo.setProgressText("Serious! " + WebServiceName
317               + " Service location failed\nfor URL :" + WsURL + "\n"
318               + ex.getMessage());
319       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
320
321     }
322
323     return server;
324   }
325
326   public void attachWSMenuEntry(JMenu wsmenu, final ServiceHandle sh,
327           final AlignFrame af)
328   {
329     final JMenuItem method = new JMenuItem(sh.getName());
330     method.setToolTipText(sh.getEndpointURL());
331     method.addActionListener(new ActionListener()
332     {
333       public void actionPerformed(ActionEvent e)
334       {
335         AlignmentView msa = af.gatherSeqOrMsaForSecStrPrediction();
336         if (msa.getSequences().length == 1)
337         {
338           // Single Sequence prediction
339           new jalview.ws.jws1.JPredClient(sh, af.getTitle(), false, msa,
340                   af, true);
341         }
342         else
343         {
344           if (msa.getSequences().length > 1)
345           {
346             // Sequence profile based prediction
347             new jalview.ws.jws1.JPredClient(sh, af.getTitle(), true, msa,
348                     af, true);
349           }
350         }
351       }
352     });
353     wsmenu.add(method);
354   }
355 }