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