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
46 AlignFrame alignFrame;
\r
50 * Creates a new MsaWSClient object that uses a service
\r
51 * given by an externally retrieved ServiceHandle
\r
53 * @param sh service handle of type AbstractName(MsaWS)
\r
54 * @param altitle DOCUMENT ME!
\r
55 * @param msa DOCUMENT ME!
\r
56 * @param submitGaps DOCUMENT ME!
\r
57 * @param preserveOrder DOCUMENT ME!
\r
60 public MsaWSClient(ext.vamsas.ServiceHandle sh, String altitle,
\r
62 boolean submitGaps, boolean preserveOrder,
\r
63 Alignment seqdataset,
\r
64 AlignFrame _alignFrame)
\r
67 alignFrame = _alignFrame;
\r
68 if (!sh.getAbstractName().equals("MsaWS"))
\r
70 JOptionPane.showMessageDialog(Desktop.desktop,
\r
71 "The Service called \n" + sh.getName() +
\r
72 "\nis not a \nMultiple Sequence Alignment Service !",
\r
73 "Internal Jalview Error",
\r
74 JOptionPane.WARNING_MESSAGE);
\r
79 if ((wsInfo = this.setWebService(sh))==null)
\r
81 JOptionPane.showMessageDialog(Desktop.desktop,
\r
82 "The Multiple Sequence Alignment Service named " +
\r
84 " is unknown", "Internal Jalview Error",
\r
85 JOptionPane.WARNING_MESSAGE);
\r
89 startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
\r
94 private void startMsaWSClient(String altitle, SequenceI[] msa,
\r
95 boolean submitGaps, boolean preserveOrder, Alignment seqdataset)
\r
97 if (!locateWebService())
\r
102 wsInfo.setProgressText( ( (submitGaps) ? "Re-alignment" : "Alignment") +
\r
103 " of " + altitle + "\nJob details\n");
\r
105 MsaWSThread musclethread = new MsaWSThread(WebServiceName +
\r
106 " alignment of " + altitle, msa,
\r
107 submitGaps, preserveOrder, seqdataset);
\r
108 wsInfo.setthisService(musclethread);
\r
109 musclethread.start();
\r
113 * Initializes the server field with a valid service implementation.
\r
115 * @return true if service was located.
\r
117 private boolean locateWebService()
\r
119 // TODO: MuscleWS transmuted to generic MsaWS client
\r
120 MuscleWSServiceLocator loc = new MuscleWSServiceLocator(); // Default
\r
124 this.server = (MuscleWS) loc.getMuscleWS(new java.net.URL(WsURL));
\r
125 ( (MuscleWSSoapBindingStub)this.server).setTimeout(60000); // One minute timeout
\r
127 catch (Exception ex)
\r
129 wsInfo.setProgressText("Serious! " + WebServiceName +
\r
130 " Service location failed\nfor URL :" + WsURL +
\r
133 wsInfo.setStatus(WebserviceInfo.ERROR);
\r
134 ex.printStackTrace();
\r
139 loc.getEngine().setOption("axis", "1");
\r
144 protected class MsaWSThread
\r
145 extends Thread implements WSClientI
\r
147 String ServiceName = WebServiceName;
\r
148 String OutputHeader;
\r
149 vamsas.objects.simple.MsaResult result = null;
\r
150 vamsas.objects.simple.SequenceSet seqs = new vamsas.objects.simple.
\r
152 Hashtable SeqNames = null;
\r
153 boolean submitGaps = false; // pass sequences including gaps to alignment service
\r
154 boolean preserveOrder = true; // and always store and recover sequence order
\r
156 String alTitle; // name which will be used to form new alignment window.
\r
157 int allowedServerExceptions = 3; // thread dies if too many exceptions.
\r
158 boolean jobComplete = false;
\r
160 Alignment dataset; // dataset to which the new alignment will be associated.
\r
162 MsaWSThread(String title, SequenceI[] msa, boolean subgaps,
\r
163 boolean presorder, Alignment seqset)
\r
166 submitGaps = subgaps;
\r
167 preserveOrder = presorder;
\r
169 OutputHeader = wsInfo.getProgressText();
\r
170 SeqNames = new Hashtable();
\r
172 vamsas.objects.simple.Sequence[] seqarray = new vamsas.objects.simple.
\r
173 Sequence[msa.length];
\r
175 for (int i = 0; i < msa.length; i++)
\r
177 String newname = jalview.analysis.SeqsetUtils.unique_name(i);
\r
179 // uniquify as we go
\r
180 // TODO: JBPNote: this is a ubiquitous transformation - set of jalview seq objects to vamsas sequences with name preservation
\r
181 SeqNames.put(newname,
\r
182 jalview.analysis.SeqsetUtils.SeqCharacterHash(msa[i]));
\r
183 seqarray[i] = new vamsas.objects.simple.Sequence();
\r
184 seqarray[i].setId(newname);
\r
185 seqarray[i].setSeq( (submitGaps) ? msa[i].getSequence()
\r
186 : AlignSeq.extractGaps(
\r
187 jalview.util.Comparison.GapChars,
\r
188 msa[i].getSequence()));
\r
191 this.seqs = new vamsas.objects.simple.SequenceSet();
\r
192 this.seqs.setSeqs(seqarray);
\r
195 public boolean isCancellable()
\r
200 public void cancelJob()
\r
202 if ( (jobId != null) && !jobId.equals("") && !jobComplete)
\r
204 String cancelledMessage = "";
\r
208 vamsas.objects.simple.WsJobId cancelledJob = server.cancel(jobId);
\r
210 if (cancelledJob.getStatus() == 2)
\r
213 cancelledMessage = "Job cancelled.";
\r
214 wsInfo.setStatus(WebserviceInfo.STATE_CANCELLED_OK);
\r
215 jobComplete = true;
\r
219 else if (cancelledJob.getStatus() == 3)
\r
221 // VALID UNSTOPPABLE JOB
\r
222 cancelledMessage +=
\r
223 "Server cannot cancel this job. just close the window.\n";
\r
226 if (cancelledJob.getJobId() != null)
\r
228 cancelledMessage += ("[" + cancelledJob.getJobId() +
\r
232 cancelledMessage += "\n";
\r
234 catch (Exception exc)
\r
236 cancelledMessage +=
\r
237 ("\nProblems cancelling the job : Exception received...\n" +
\r
239 exc.printStackTrace();
\r
242 wsInfo.setProgressText(OutputHeader + cancelledMessage + "\n");
\r
248 wsInfo.setProgressText(OutputHeader +
\r
249 "Server cannot cancel this job because it has not been submitted properly. just close the window.\n");
\r
258 while (!jobComplete && (allowedServerExceptions > 0))
\r
262 if ( (result = server.getResult(jobId)) == null)
\r
264 throw (new Exception(
\r
265 "Timed out when communicating with server\nTry again later.\n"));
\r
267 jalview.bin.Cache.log.debug("Result state " + result.getState() +
\r
268 "(ServerError=" + result.isServerError() +
\r
270 if (result.isRunning())
\r
272 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
\r
274 else if (result.isQueued())
\r
276 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
\r
279 if (result.isFinished())
\r
281 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
\r
282 wsInfo.showResultsNewFrame.addActionListener(new java.awt.event.ActionListener()
\r
284 public void actionPerformed(java.awt.event.ActionEvent evt)
\r
286 displayResults(true);
\r
289 wsInfo.mergeResults.addActionListener(new java.awt.event.ActionListener()
\r
291 public void actionPerformed(java.awt.event.ActionEvent evt)
\r
293 displayResults(false);
\r
296 wsInfo.setResultsReady();
\r
298 jobComplete = true;
\r
303 if (result.getStatus() != null)
\r
305 wsInfo.setProgressText(OutputHeader + "\n"+ result.getStatus());
\r
307 if (result.isServerError())
\r
309 jobComplete = true;
\r
314 if (! (result.isJobFailed() || result.isServerError() ||
\r
315 result.isBroken() || result.isFailed()))
\r
317 Thread.sleep(5000);
\r
319 // System.out.println("I'm alive "+seqid+" "+jobid);
\r
323 jobComplete = true;
\r
329 catch (Exception ex)
\r
331 allowedServerExceptions--;
\r
332 wsInfo.appendProgressText("\n" + ServiceName +
\r
333 " Server exception!\n" + ex.getMessage());
\r
334 System.err.println(ServiceName + " Server exception: " +
\r
337 // ex.printStackTrace(); JBPNote Debug
\r
340 if (allowedServerExceptions > 0)
\r
342 Thread.sleep(5000);
\r
345 catch (InterruptedException ex1)
\r
349 catch(OutOfMemoryError er)
\r
351 jobComplete = true;
\r
352 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
353 JOptionPane.showInternalMessageDialog(Desktop.desktop,
\r
354 "Out of memory handling result!!"
\r
355 +"\nSee help files for increasing Java Virtual Machine memory."
\r
356 ,"Out of memory", JOptionPane.WARNING_MESSAGE );
\r
357 System.out.println("MsaWSClient: "+er);
\r
362 if (allowedServerExceptions == 0)
\r
364 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
368 if (result != null)
\r
370 if ( !(result.isJobFailed() || result.isServerError()))
\r
372 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
\r
375 if (result.isBroken() || result.isFailed())
\r
377 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
380 if (result.isServerError())
\r
382 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
392 vamsas.objects.simple.WsJobId jobsubmit = server.align(seqs);
\r
394 if ( (jobsubmit != null) && (jobsubmit.getStatus() == 1))
\r
396 jobId = jobsubmit.getJobId();
\r
397 System.out.println(WsURL + " Job Id '" + jobId + "'");
\r
401 if (jobsubmit == null)
\r
403 throw new Exception("Server at " + WsURL +
\r
404 " returned null object, it probably cannot be contacted. Try again later ?");
\r
407 throw new Exception(jobsubmit.getJobId());
\r
410 catch (Exception e)
\r
412 // TODO: JBPNote catch timeout or other fault types explicitly
\r
413 // For unexpected errors
\r
414 System.err.println(WebServiceName +
\r
415 "Client: Failed to submit the sequences for alignment (probably a server side problem)\n" +
\r
416 "When contacting Server:" + WsURL + "\n" +
\r
419 this.allowedServerExceptions = 0;
\r
420 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
421 wsInfo.appendProgressText(
\r
422 "Failed to submit sequences for alignment.\n" +
\r
423 "It is most likely that there is a problem with the server.\n" +
\r
424 "Just close the window\n");
\r
426 // e.printStackTrace(); // TODO: JBPNote DEBUG
\r
430 private jalview.datamodel.Sequence[] getVamsasAlignment(
\r
431 vamsas.objects.simple.Alignment valign)
\r
433 vamsas.objects.simple.Sequence[] seqs = valign.getSeqs().getSeqs();
\r
434 jalview.datamodel.Sequence[] msa = new jalview.datamodel.Sequence[seqs.
\r
437 for (int i = 0, j = seqs.length; i < j; i++)
\r
439 msa[i] = new jalview.datamodel.Sequence(seqs[i].getId(),
\r
451 // OutputHeader = output.getText();
\r
452 if (result.isFailed())
\r
454 OutputHeader += "Job failed.\n";
\r
457 if (result.getStatus() != null)
\r
459 OutputHeader += ("\n" + result.getStatus());
\r
462 if (result.getMsa() != null)
\r
464 OutputHeader += "\nAlignment Object Method Notes\n";
\r
466 String[] lines = result.getMsa().getMethod();
\r
468 for (int line = 0; line < lines.length; line++)
\r
470 OutputHeader += (lines[line] + "\n");
\r
473 // JBPNote The returned files from a webservice could be
\r
474 // hidden behind icons in the monitor window that,
\r
475 // when clicked, pop up their corresponding data
\r
478 wsInfo.setProgressText(OutputHeader);
\r
480 catch (Exception ex)
\r
482 ex.printStackTrace();
\r
486 void displayResults(boolean newFrame)
\r
488 SequenceI [] seqs = getVamsasAlignment(result.getMsa());
\r
492 AlignmentOrder msaorder = new AlignmentOrder(seqs);
\r
496 jalview.analysis.AlignmentSorter.recoverOrder(seqs);
\r
499 jalview.analysis.SeqsetUtils.deuniquify(SeqNames, seqs);
\r
501 Alignment al = new Alignment(seqs);
\r
504 al.setDataset(dataset);
\r
509 // TODO: JBPNote Should also rename the query sequence sometime...
\r
510 AlignFrame af = new AlignFrame(al);
\r
512 //>>>This is a fix for the moment, until a better solution is found!!<<<
\r
513 af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
\r
515 af.addSortByOrderMenuItem(ServiceName + " Ordering",
\r
518 Desktop.addInternalFrame(af, alTitle,
\r
519 AlignFrame.NEW_WINDOW_WIDTH,
\r
520 AlignFrame.NEW_WINDOW_HEIGHT);
\r
525 System.out.println("MERGE WITH OLD FRAME");
\r