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