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