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