2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
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.
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.
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
22 import org.apache.axis.client.*;
23 import javax.xml.namespace.QName;
25 import jalview.datamodel.*;
30 import jalview.analysis.AlignSeq;
32 import vamsas.objects.*;
37 public class MsaWSClient
41 * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
43 ext.vamsas.MuscleWS server;
44 // JBPNote Nasty object-global state setting methods shouldn't be allowed
45 private boolean setWebService(String MsaWSName) {
46 if (MsaWServices.info.containsKey(MsaWSName)) {
47 WebServiceName = MsaWSName;
48 String[] wsinfo = (String[]) MsaWServices.info.get(MsaWSName);
50 WebServiceJobTitle = wsinfo[1];
51 WebServiceReference = wsinfo[2];
58 private boolean locateWebService() {
59 // TODO: MuscleWS transmuted to generic MsaWS client
60 MuscleWSServiceLocator loc = new MuscleWSServiceLocator(); // Default
62 this.server = (MuscleWS) loc.getMuscleWS(new java.net.URL(WsURL));
63 ((MuscleWSSoapBindingStub) this.server).setTimeout(60000); // One minute timeout
65 catch (Exception ex) {
66 wsInfo.setProgressText("Serious! "+WebServiceName+" Service location failed\nfor URL :"
67 +WsURL+"\n"+ex.getMessage());
68 wsInfo.setStatus(wsInfo.ERROR);
72 loc.getEngine().setOption("axis","1");
76 public MsaWSClient(String MsaWSName, String altitle, SequenceI[] msa, boolean submitGaps, boolean preserveOrder)
78 if (setWebService(MsaWSName)==false) {
79 JOptionPane.showMessageDialog(Desktop.desktop, "The Multiple Sequence Alignment Service named "+MsaWSName+" is unknown",
80 "Internal Jalview Error", JOptionPane.WARNING_MESSAGE);
84 wsInfo = new jalview.gui.WebserviceInfo(WebServiceJobTitle, WebServiceReference);
86 if (!locateWebService())
89 wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment")+" of "+altitle+"\nJob details\n");
91 MsaWSThread musclethread = new MsaWSThread(WebServiceName+" alignment of "+altitle, msa, submitGaps, preserveOrder);
92 wsInfo.setthisService(musclethread);
97 protected class MsaWSThread extends Thread implements WSClientI
99 String ServiceName = WebServiceName;
101 public boolean isCancellable()
107 vamsas.objects.simple.MsaResult result = null;
109 vamsas.objects.simple.SequenceSet seqs = new vamsas.objects.simple.
112 Hashtable SeqNames = null;
113 boolean submitGaps = false,// default is to strip gaps from sequences
114 preserveOrder = true; // and always store and recover sequence order
117 String alTitle; // name which will be used to form new alignment window.
118 int allowedServerExceptions = 3; // thread dies if too many exceptions.
119 MsaWSThread(String title, SequenceI[] msa, boolean subgaps, boolean presorder)
122 submitGaps = subgaps;
123 preserveOrder = presorder;
125 OutputHeader = wsInfo.getProgressText();
126 SeqNames = new Hashtable();
127 vamsas.objects.simple.Sequence[] seqarray = new vamsas.objects.simple.
128 Sequence[msa.length];
130 for (int i = 0; i < msa.length; i++)
132 String newname = jalview.analysis.SeqsetUtils.unique_name(i);
134 // TODO: JBPNote: this is a ubiquitous transformation - set of jalview seq objects to vamsas sequences with name preservation
135 SeqNames.put(newname, jalview.analysis.SeqsetUtils.SeqCharacterHash(msa[i]));
136 seqarray[i] = new vamsas.objects.simple.Sequence();
137 seqarray[i].setId(newname);
138 seqarray[i].setSeq((submitGaps) ? msa[i].getSequence()
139 : AlignSeq.extractGaps(jalview.util.Comparison.GapChars, msa[i].getSequence()));
142 this.seqs = new vamsas.objects.simple.SequenceSet();
143 this.seqs.setSeqs(seqarray);
146 boolean jobComplete = false;
148 public void cancelJob() {
149 if (jobId!=null && !jobId.equals("") && !jobComplete) {
150 String cancelledMessage="";
152 vamsas.objects.simple.WsJobId cancelledJob = server.cancel(jobId);
153 if (cancelledJob.getStatus() == 2)
156 cancelledMessage = "Job cancelled.";
157 wsInfo.setStatus(WebserviceInfo.STATE_CANCELLED_OK);
163 if (cancelledJob.getStatus() == 3)
165 // VALID UNSTOPPABLE JOB
167 "Server cannot cancel this job. just close the window.\n";
169 if (cancelledJob.getJobId() != null)
170 cancelledMessage += "[" + cancelledJob.getJobId() + "]";
171 cancelledMessage +="\n";
172 } catch (Exception exc) {
173 cancelledMessage +="\nProblems cancelling the job : Exception received...\n"+exc+"\n";
174 exc.printStackTrace();
176 wsInfo.setProgressText(OutputHeader + cancelledMessage+"\n");
180 wsInfo.setProgressText(OutputHeader + "Server cannot cancel this job because it has not been submitted properly. just close the window.\n");
190 while (!jobComplete && (allowedServerExceptions > 0))
194 if ((result = server.getResult(jobId))==null)
195 throw(new Exception("Timed out when communicating with server\nTry again later.\n"));
197 if (result.isRunning())
198 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
199 else if (result.isQueued())
200 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
202 if (result.isFinished())
210 if (result.getStatus() != null)
211 wsInfo.setProgressText(OutputHeader + "\n" + result.getStatus());
212 if (! (result.isJobFailed() || result.isServerError()))
215 // System.out.println("I'm alive "+seqid+" "+jobid);
225 allowedServerExceptions--;
226 wsInfo.appendProgressText("\n" + ServiceName + " Server exception!\n" +
228 System.err.println(ServiceName + " Server exception: " +
230 // ex.printStackTrace(); JBPNote Debug
233 if (allowedServerExceptions>0)
236 catch (InterruptedException ex1)
242 if (allowedServerExceptions == 0)
244 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
248 if (! (result != null && (result.isJobFailed() || result.isServerError())))
249 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
252 if (result.isFailed())
253 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
254 if (result.isServerError())
255 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
263 vamsas.objects.simple.WsJobId jobsubmit = server.align(seqs);
264 if (jobsubmit!=null && jobsubmit.getStatus()==1) {
265 jobId=jobsubmit.getJobId();
266 System.out.println(WsURL+" Job Id '"+jobId+"'");
268 if (jobsubmit == null) {
269 throw new Exception("Server at "+WsURL+" returned null object, it probably cannot be contacted. Try again later ?");
271 throw new Exception(jobsubmit.getJobId());
276 // TODO: JBPNote catch timeout or other fault types explicitly
277 // For unexpected errors
278 System.err.println(WebServiceName + " Client: Failed to submit the sequences for alignment.\n"+WsURL+" : " +
279 e.toString() + "\n");
280 this.allowedServerExceptions=0;
281 wsInfo.setStatus(wsInfo.STATE_STOPPED_SERVERERROR);
282 wsInfo.appendProgressText("Server problems! "+e.toString()+"\nFailed to submit sequences for alignment. Just close the window\n");
283 // e.printStackTrace(); // TODO: JBPNote DEBUG
287 private void addFloatAnnotations(Alignment al, int[] gapmap, Vector values, String Symname, String Visname, float min, float max, int winLength) {
289 Annotation[] annotations = new Annotation[al.getWidth()];
290 for (int j = 0; j < values.size(); j++)
292 float value = Float.parseFloat(values.get(j).toString());
293 annotations[gapmap[j]] = new Annotation("", value+"",' ',value);
295 al.addAnnotation(new AlignmentAnnotation(Symname, Visname, annotations, min, max, winLength));
297 private jalview.datamodel.Sequence[] getVamsasAlignment(vamsas.objects.simple.Alignment valign) {
298 vamsas.objects.simple.Sequence[] seqs = valign.getSeqs().getSeqs();
299 jalview.datamodel.Sequence[] msa = new jalview.datamodel.Sequence[seqs.length];
300 for (int i=0, j=seqs.length; i<j;i++)
301 msa[i] = new jalview.datamodel.Sequence(seqs[i].getId(), seqs[i].getSeq());
306 SequenceI[] seqs=null;
308 // OutputHeader = output.getText();
309 if (result.isFailed()) {
310 OutputHeader +="Job failed.\n";
312 if (result.getStatus()!=null) {
313 OutputHeader += "\n"+result.getStatus();
315 if (result.getMsa()!=null) {
316 OutputHeader += "\nAlignment Object Method Notes\n";
317 String lines[] = result.getMsa().getMethod();
318 for (int line=0;line<lines.length; line++)
319 OutputHeader+=lines[line]+"\n";
321 // JBPNote The returned files from a webservice could be hidden behind icons in the monitor window that, when clicked, pop up their corresponding data
322 seqs = getVamsasAlignment(result.getMsa());
325 wsInfo.setProgressText(OutputHeader);
327 AlignmentOrder msaorder = new AlignmentOrder(seqs);
330 jalview.analysis.AlignmentSorter.recoverOrder(seqs);
333 jalview.analysis.SeqsetUtils.deuniquify(SeqNames, seqs);
335 Alignment al = new Alignment(seqs);
337 // TODO: JBPNote Should also rename the query sequence sometime...
338 AlignFrame af = new AlignFrame(al);
339 af.addSortByOrderMenuItem(ServiceName+" Ordering", msaorder);
341 Desktop.addInternalFrame(af,
343 AlignFrame.NEW_WINDOW_WIDTH,
344 AlignFrame.NEW_WINDOW_HEIGHT);
346 }catch(Exception ex){ex.printStackTrace();}