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