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