2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
21 import ext.vamsas.*;
\r
23 import jalview.analysis.AlignSeq;
\r
25 import jalview.datamodel.*;
\r
27 import jalview.gui.*;
\r
31 import javax.swing.*;
\r
37 * @version $Revision$
\r
39 public class MsaWSClient
\r
43 * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
\r
45 ext.vamsas.MuscleWS server;
\r
49 * Creates a new MsaWSClient object that uses a service
\r
50 * given by an externally retrieved ServiceHandle
\r
52 * @param sh service handle of type AbstractName(MsaWS)
\r
53 * @param altitle DOCUMENT ME!
\r
54 * @param msa DOCUMENT ME!
\r
55 * @param submitGaps DOCUMENT ME!
\r
56 * @param preserveOrder DOCUMENT ME!
\r
59 public MsaWSClient(ext.vamsas.ServiceHandle sh, String altitle, SequenceI[] msa,
\r
60 boolean submitGaps, boolean preserveOrder, Alignment seqdataset)
\r
63 if (!sh.getAbstractName().equals("MsaWS"))
\r
65 JOptionPane.showMessageDialog(Desktop.desktop,
\r
66 "The Service called \n" + sh.getName() +
\r
67 "\nis not a \nMultiple Sequence Alignment Service !",
\r
68 "Internal Jalview Error",
\r
69 JOptionPane.WARNING_MESSAGE);
\r
74 if ((wsInfo = this.setWebService(sh))==null)
\r
76 JOptionPane.showMessageDialog(Desktop.desktop,
\r
77 "The Multiple Sequence Alignment Service named " +
\r
79 " is unknown", "Internal Jalview Error",
\r
80 JOptionPane.WARNING_MESSAGE);
\r
84 startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
\r
89 private void startMsaWSClient(String altitle, SequenceI[] msa,
\r
90 boolean submitGaps, boolean preserveOrder, Alignment seqdataset)
\r
92 if (!locateWebService())
\r
97 wsInfo.setProgressText( ( (submitGaps) ? "Re-alignment" : "Alignment") +
\r
98 " of " + altitle + "\nJob details\n");
\r
100 MsaWSThread musclethread = new MsaWSThread(WebServiceName +
\r
101 " alignment of " + altitle, msa,
\r
102 submitGaps, preserveOrder, seqdataset);
\r
103 wsInfo.setthisService(musclethread);
\r
104 musclethread.start();
\r
108 * Initializes the server field with a valid service implementation.
\r
110 * @return true if service was located.
\r
112 private boolean locateWebService()
\r
114 // TODO: MuscleWS transmuted to generic MsaWS client
\r
115 MuscleWSServiceLocator loc = new MuscleWSServiceLocator(); // Default
\r
119 this.server = (MuscleWS) loc.getMuscleWS(new java.net.URL(WsURL));
\r
120 ( (MuscleWSSoapBindingStub)this.server).setTimeout(60000); // One minute timeout
\r
122 catch (Exception ex)
\r
124 wsInfo.setProgressText("Serious! " + WebServiceName +
\r
125 " Service location failed\nfor URL :" + WsURL +
\r
128 wsInfo.setStatus(WebserviceInfo.ERROR);
\r
129 ex.printStackTrace();
\r
134 loc.getEngine().setOption("axis", "1");
\r
139 protected class MsaWSThread
\r
140 extends Thread implements WSClientI
\r
142 String ServiceName = WebServiceName;
\r
143 String OutputHeader;
\r
144 vamsas.objects.simple.MsaResult result = null;
\r
145 vamsas.objects.simple.SequenceSet seqs = new vamsas.objects.simple.
\r
147 Hashtable SeqNames = null;
\r
148 boolean submitGaps = false; // pass sequences including gaps to alignment service
\r
149 boolean preserveOrder = true; // and always store and recover sequence order
\r
151 String alTitle; // name which will be used to form new alignment window.
\r
152 int allowedServerExceptions = 3; // thread dies if too many exceptions.
\r
153 boolean jobComplete = false;
\r
155 Alignment dataset; // dataset to which the new alignment will be associated.
\r
157 MsaWSThread(String title, SequenceI[] msa, boolean subgaps,
\r
158 boolean presorder, Alignment seqset)
\r
161 submitGaps = subgaps;
\r
162 preserveOrder = presorder;
\r
164 OutputHeader = wsInfo.getProgressText();
\r
165 SeqNames = new Hashtable();
\r
167 vamsas.objects.simple.Sequence[] seqarray = new vamsas.objects.simple.
\r
168 Sequence[msa.length];
\r
170 for (int i = 0; i < msa.length; i++)
\r
172 String newname = jalview.analysis.SeqsetUtils.unique_name(i);
\r
174 // uniquify as we go
\r
175 // TODO: JBPNote: this is a ubiquitous transformation - set of jalview seq objects to vamsas sequences with name preservation
\r
176 SeqNames.put(newname,
\r
177 jalview.analysis.SeqsetUtils.SeqCharacterHash(msa[i]));
\r
178 seqarray[i] = new vamsas.objects.simple.Sequence();
\r
179 seqarray[i].setId(newname);
\r
180 seqarray[i].setSeq( (submitGaps) ? msa[i].getSequence()
\r
181 : AlignSeq.extractGaps(
\r
182 jalview.util.Comparison.GapChars,
\r
183 msa[i].getSequence()));
\r
186 this.seqs = new vamsas.objects.simple.SequenceSet();
\r
187 this.seqs.setSeqs(seqarray);
\r
190 public boolean isCancellable()
\r
195 public void cancelJob()
\r
197 if ( (jobId != null) && !jobId.equals("") && !jobComplete)
\r
199 String cancelledMessage = "";
\r
203 vamsas.objects.simple.WsJobId cancelledJob = server.cancel(jobId);
\r
205 if (cancelledJob.getStatus() == 2)
\r
208 cancelledMessage = "Job cancelled.";
\r
209 wsInfo.setStatus(WebserviceInfo.STATE_CANCELLED_OK);
\r
210 jobComplete = true;
\r
214 else if (cancelledJob.getStatus() == 3)
\r
216 // VALID UNSTOPPABLE JOB
\r
217 cancelledMessage +=
\r
218 "Server cannot cancel this job. just close the window.\n";
\r
221 if (cancelledJob.getJobId() != null)
\r
223 cancelledMessage += ("[" + cancelledJob.getJobId() +
\r
227 cancelledMessage += "\n";
\r
229 catch (Exception exc)
\r
231 cancelledMessage +=
\r
232 ("\nProblems cancelling the job : Exception received...\n" +
\r
234 exc.printStackTrace();
\r
237 wsInfo.setProgressText(OutputHeader + cancelledMessage + "\n");
\r
243 wsInfo.setProgressText(OutputHeader +
\r
244 "Server cannot cancel this job because it has not been submitted properly. just close the window.\n");
\r
253 while (!jobComplete && (allowedServerExceptions > 0))
\r
257 if ( (result = server.getResult(jobId)) == null)
\r
259 throw (new Exception(
\r
260 "Timed out when communicating with server\nTry again later.\n"));
\r
262 jalview.bin.Cache.log.debug("Result state " + result.getState() +
\r
263 "(ServerError=" + result.isServerError() +
\r
265 if (result.isRunning())
\r
267 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
\r
269 else if (result.isQueued())
\r
271 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
\r
274 if (result.isFinished())
\r
276 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
\r
278 jobComplete = true;
\r
283 if (result.getStatus() != null)
\r
285 wsInfo.setProgressText(OutputHeader + "\n"+ result.getStatus());
\r
287 if (result.isServerError())
\r
289 jobComplete = true;
\r
294 if (! (result.isJobFailed() || result.isServerError() ||
\r
295 result.isBroken() || result.isFailed()))
\r
297 Thread.sleep(5000);
\r
299 // System.out.println("I'm alive "+seqid+" "+jobid);
\r
303 jobComplete = true;
\r
309 catch (Exception ex)
\r
311 allowedServerExceptions--;
\r
312 wsInfo.appendProgressText("\n" + ServiceName +
\r
313 " Server exception!\n" + ex.getMessage());
\r
314 System.err.println(ServiceName + " Server exception: " +
\r
317 // ex.printStackTrace(); JBPNote Debug
\r
320 if (allowedServerExceptions > 0)
\r
322 Thread.sleep(5000);
\r
325 catch (InterruptedException ex1)
\r
329 catch(OutOfMemoryError er)
\r
331 jobComplete = true;
\r
332 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
333 JOptionPane.showInternalMessageDialog(Desktop.desktop,
\r
334 "Out of memory handling result!!"
\r
335 +"\nSee help files for increasing Java Virtual Machine memory."
\r
336 ,"Out of memory", JOptionPane.WARNING_MESSAGE );
\r
337 System.out.println("MsaWSClient: "+er);
\r
342 if (allowedServerExceptions == 0)
\r
344 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
348 if (result != null)
\r
350 if ( !(result.isJobFailed() || result.isServerError()))
\r
352 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
\r
355 if (result.isBroken() || result.isFailed())
\r
357 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
360 if (result.isServerError())
\r
362 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
372 vamsas.objects.simple.WsJobId jobsubmit = server.align(seqs);
\r
374 if ( (jobsubmit != null) && (jobsubmit.getStatus() == 1))
\r
376 jobId = jobsubmit.getJobId();
\r
377 System.out.println(WsURL + " Job Id '" + jobId + "'");
\r
381 if (jobsubmit == null)
\r
383 throw new Exception("Server at " + WsURL +
\r
384 " returned null object, it probably cannot be contacted. Try again later ?");
\r
387 throw new Exception(jobsubmit.getJobId());
\r
390 catch (Exception e)
\r
392 // TODO: JBPNote catch timeout or other fault types explicitly
\r
393 // For unexpected errors
\r
394 System.err.println(WebServiceName +
\r
395 "Client: Failed to submit the sequences for alignment (probably a server side problem)\n" +
\r
396 "When contacting Server:" + WsURL + "\n" +
\r
399 this.allowedServerExceptions = 0;
\r
400 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
401 wsInfo.appendProgressText(
\r
402 "Failed to submit sequences for alignment.\n" +
\r
403 "It is most likely that there is a problem with the server.\n" +
\r
404 "Just close the window\n");
\r
406 // e.printStackTrace(); // TODO: JBPNote DEBUG
\r
410 private jalview.datamodel.Sequence[] getVamsasAlignment(
\r
411 vamsas.objects.simple.Alignment valign)
\r
413 vamsas.objects.simple.Sequence[] seqs = valign.getSeqs().getSeqs();
\r
414 jalview.datamodel.Sequence[] msa = new jalview.datamodel.Sequence[seqs.
\r
417 for (int i = 0, j = seqs.length; i < j; i++)
\r
419 msa[i] = new jalview.datamodel.Sequence(seqs[i].getId(),
\r
428 SequenceI[] seqs = null;
\r
432 // OutputHeader = output.getText();
\r
433 if (result.isFailed())
\r
435 OutputHeader += "Job failed.\n";
\r
438 if (result.getStatus() != null)
\r
440 OutputHeader += ("\n" + result.getStatus());
\r
443 if (result.getMsa() != null)
\r
445 OutputHeader += "\nAlignment Object Method Notes\n";
\r
447 String[] lines = result.getMsa().getMethod();
\r
449 for (int line = 0; line < lines.length; line++)
\r
451 OutputHeader += (lines[line] + "\n");
\r
454 // JBPNote The returned files from a webservice could be hidden behind icons in the monitor window that, when clicked, pop up their corresponding data
\r
455 seqs = getVamsasAlignment(result.getMsa());
\r
458 wsInfo.setProgressText(OutputHeader);
\r
462 AlignmentOrder msaorder = new AlignmentOrder(seqs);
\r
466 jalview.analysis.AlignmentSorter.recoverOrder(seqs);
\r
469 jalview.analysis.SeqsetUtils.deuniquify(SeqNames, seqs);
\r
471 Alignment al = new Alignment(seqs);
\r
472 if (dataset!=null) {
\r
473 al.setDataset(dataset);
\r
475 // TODO: JBPNote Should also rename the query sequence sometime...
\r
476 AlignFrame af = new AlignFrame(al);
\r
477 af.addSortByOrderMenuItem(ServiceName + " Ordering",
\r
480 Desktop.addInternalFrame(af, alTitle,
\r
481 AlignFrame.NEW_WINDOW_WIDTH,
\r
482 AlignFrame.NEW_WINDOW_HEIGHT);
\r
485 catch (Exception ex)
\r
487 ex.printStackTrace();
\r