Formatting
[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 = "JNet prediction 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       String altitle = "JNet prediction for " + (viewonly ? "visible " : "") +
158           "sequence " + seq.getName() +
159           " from " +
160           title;
161       String seqname = seq.getName();
162       Hashtable SequenceInfo = jalview.analysis.SeqsetUtils.SeqCharacterHash(
163           seq);
164       if (viewonly)
165       {
166         // Remove hidden regions from input sequence
167         String seqs[] = alview.getSequenceStrings('-');
168         seq.setSequence(seqs[0]);
169       }
170       wsInfo.setProgressText("Job details for prediction on " +
171                              (viewonly ? "visible " : "") + "sequence :\n>" +
172                              seqname + "\n" +
173                              AlignSeq.extractGaps("-. ",
174                                                   seq.getSequenceAsString()) +
175                              "\n");
176       JPredThread jthread = new JPredThread(wsInfo, altitle, server, WsURL,
177                                             SequenceInfo, seq, delMap, alview,
178                                             parentFrame);
179       wsInfo.setthisService(jthread);
180       jthread.start();
181     }
182   }
183
184   public JPredClient(ext.vamsas.ServiceHandle sh, String title, SequenceI seq,
185                      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, SequenceI[] msa,
193                      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   private void startJPredClient(String title, SequenceI[] msf,
210                                 AlignFrame parentFrame)
211   {
212     if (wsInfo == null)
213     {
214       wsInfo = setWebService();
215     }
216
217     SequenceI seq = msf[0];
218
219     String altitle = "JNet prediction on " + seq.getName() +
220         " using alignment from " + title;
221
222     wsInfo.setProgressText("Job details for MSA based prediction (" +
223                            title + ") on sequence :\n>" + seq.getName() + "\n" +
224                            AlignSeq.extractGaps("-. ", seq.getSequenceAsString()) +
225                            "\n");
226     SequenceI aln[] = new SequenceI[msf.length];
227     for (int i = 0, j = msf.length; i < j; i++)
228     {
229       aln[i] = new jalview.datamodel.Sequence(msf[i]);
230     }
231
232     Hashtable SequenceInfo = jalview.analysis.SeqsetUtils.uniquify(aln, true);
233
234     Jpred server = locateWebService();
235     if (server == null)
236     {
237       return;
238     }
239
240     JPredThread jthread = new JPredThread(wsInfo, altitle, server, SequenceInfo,
241                                           aln, null, null, parentFrame, WsURL);
242     wsInfo.setthisService(jthread);
243     jthread.start();
244   }
245
246   public void startJPredClient(String title, SequenceI seq,
247                                AlignFrame parentFrame)
248   {
249     if (wsInfo == null)
250     {
251       wsInfo = setWebService();
252     }
253     wsInfo.setProgressText("Job details for prediction on sequence :\n>" +
254                            seq.getName() + "\n" +
255                            AlignSeq.extractGaps("-. ", seq.getSequenceAsString()) +
256                            "\n");
257     String altitle = "JNet prediction for sequence " + seq.getName() + " from " +
258         title;
259
260     Hashtable SequenceInfo = jalview.analysis.SeqsetUtils.SeqCharacterHash(seq);
261
262     Jpred server = locateWebService();
263     if (server == null)
264     {
265       return;
266     }
267
268     JPredThread jthread = new JPredThread(wsInfo, altitle, server, WsURL,
269                                           SequenceInfo, seq, null, null,
270                                           parentFrame);
271     wsInfo.setthisService(jthread);
272     jthread.start();
273   }
274
275   private WebserviceInfo setWebService()
276   {
277     WebServiceName = "JNetWS";
278     WebServiceJobTitle = "JNet secondary structure prediction";
279     WebServiceReference =
280         "\"Cuff J. A and Barton G.J (2000) Application of " +
281         "multiple sequence alignment profiles to improve protein secondary structure prediction, " +
282         "Proteins 40:502-511\".";
283     WsURL = "http://www.compbio.dundee.ac.uk/JalviewWS/services/jpred";
284
285     WebserviceInfo wsInfo = new WebserviceInfo(WebServiceJobTitle,
286                                                WebServiceReference);
287
288     return wsInfo;
289   }
290
291   private ext.vamsas.Jpred locateWebService()
292   {
293     ext.vamsas.JpredServiceLocator loc = new JpredServiceLocator(); // Default
294     ext.vamsas.Jpred server = null;
295     try
296     {
297       server = loc.getjpred(new java.net.URL(WsURL)); // JBPNote will be set from properties
298       ( (JpredSoapBindingStub) server).setTimeout(60000); // one minute stub
299       //((JpredSoapBindingStub)this.server)._setProperty(org.apache.axis.encoding.C, Boolean.TRUE);
300
301     }
302     catch (Exception ex)
303     {
304       JOptionPane.showMessageDialog(Desktop.desktop,
305                                     "The Secondary Structure Prediction Service named " +
306                                     WebServiceName + " at " + WsURL +
307                                     " couldn't be located.",
308                                     "Internal Jalview Error",
309                                     JOptionPane.WARNING_MESSAGE);
310       wsInfo.setProgressText("Serious! " + WebServiceName +
311                              " Service location failed\nfor URL :" + WsURL +
312                              "\n" +
313                              ex.getMessage());
314       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
315
316     }
317
318     return server;
319   }
320 }