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
283 jobComplete = true;
\r
288 if (result.getStatus() != null)
\r
290 wsInfo.setProgressText(OutputHeader + "\n"+ result.getStatus());
\r
292 if (result.isServerError())
\r
294 jobComplete = true;
\r
299 if (! (result.isJobFailed() || result.isServerError() ||
\r
300 result.isBroken() || result.isFailed()))
\r
302 Thread.sleep(5000);
\r
304 // System.out.println("I'm alive "+seqid+" "+jobid);
\r
308 jobComplete = true;
\r
314 catch (Exception ex)
\r
316 allowedServerExceptions--;
\r
317 wsInfo.appendProgressText("\n" + ServiceName +
\r
318 " Server exception!\n" + ex.getMessage());
\r
319 System.err.println(ServiceName + " Server exception: " +
\r
322 // ex.printStackTrace(); JBPNote Debug
\r
325 if (allowedServerExceptions > 0)
\r
327 Thread.sleep(5000);
\r
330 catch (InterruptedException ex1)
\r
334 catch(OutOfMemoryError er)
\r
336 jobComplete = true;
\r
337 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
338 JOptionPane.showInternalMessageDialog(Desktop.desktop,
\r
339 "Out of memory handling result!!"
\r
340 +"\nSee help files for increasing Java Virtual Machine memory."
\r
341 ,"Out of memory", JOptionPane.WARNING_MESSAGE );
\r
342 System.out.println("MsaWSClient: "+er);
\r
347 if (allowedServerExceptions == 0)
\r
349 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
353 if (result != null)
\r
355 if ( !(result.isJobFailed() || result.isServerError()))
\r
357 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
\r
360 if (result.isBroken() || result.isFailed())
\r
362 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
365 if (result.isServerError())
\r
367 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
377 vamsas.objects.simple.WsJobId jobsubmit = server.align(seqs);
\r
379 if ( (jobsubmit != null) && (jobsubmit.getStatus() == 1))
\r
381 jobId = jobsubmit.getJobId();
\r
382 System.out.println(WsURL + " Job Id '" + jobId + "'");
\r
386 if (jobsubmit == null)
\r
388 throw new Exception("Server at " + WsURL +
\r
389 " returned null object, it probably cannot be contacted. Try again later ?");
\r
392 throw new Exception(jobsubmit.getJobId());
\r
395 catch (Exception e)
\r
397 // TODO: JBPNote catch timeout or other fault types explicitly
\r
398 // For unexpected errors
\r
399 System.err.println(WebServiceName +
\r
400 "Client: Failed to submit the sequences for alignment (probably a server side problem)\n" +
\r
401 "When contacting Server:" + WsURL + "\n" +
\r
404 this.allowedServerExceptions = 0;
\r
405 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
406 wsInfo.appendProgressText(
\r
407 "Failed to submit sequences for alignment.\n" +
\r
408 "It is most likely that there is a problem with the server.\n" +
\r
409 "Just close the window\n");
\r
411 // e.printStackTrace(); // TODO: JBPNote DEBUG
\r
415 private jalview.datamodel.Sequence[] getVamsasAlignment(
\r
416 vamsas.objects.simple.Alignment valign)
\r
418 vamsas.objects.simple.Sequence[] seqs = valign.getSeqs().getSeqs();
\r
419 jalview.datamodel.Sequence[] msa = new jalview.datamodel.Sequence[seqs.
\r
422 for (int i = 0, j = seqs.length; i < j; i++)
\r
424 msa[i] = new jalview.datamodel.Sequence(seqs[i].getId(),
\r
433 SequenceI[] seqs = null;
\r
437 // OutputHeader = output.getText();
\r
438 if (result.isFailed())
\r
440 OutputHeader += "Job failed.\n";
\r
443 if (result.getStatus() != null)
\r
445 OutputHeader += ("\n" + result.getStatus());
\r
448 if (result.getMsa() != null)
\r
450 OutputHeader += "\nAlignment Object Method Notes\n";
\r
452 String[] lines = result.getMsa().getMethod();
\r
454 for (int line = 0; line < lines.length; line++)
\r
456 OutputHeader += (lines[line] + "\n");
\r
459 // 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
460 seqs = getVamsasAlignment(result.getMsa());
\r
463 wsInfo.setProgressText(OutputHeader);
\r
467 AlignmentOrder msaorder = new AlignmentOrder(seqs);
\r
471 jalview.analysis.AlignmentSorter.recoverOrder(seqs);
\r
474 jalview.analysis.SeqsetUtils.deuniquify(SeqNames, seqs);
\r
476 Alignment al = new Alignment(seqs);
\r
477 if (dataset!=null) {
\r
478 al.setDataset(dataset);
\r
480 // TODO: JBPNote Should also rename the query sequence sometime...
\r
481 AlignFrame af = new AlignFrame(al);
\r
483 //>>>This is a fix for the moment, until a better solution is found!!<<<
\r
484 af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
\r
486 af.addSortByOrderMenuItem(ServiceName + " Ordering",
\r
489 Desktop.addInternalFrame(af, alTitle,
\r
490 AlignFrame.NEW_WINDOW_WIDTH,
\r
491 AlignFrame.NEW_WINDOW_HEIGHT);
\r
494 catch (Exception ex)
\r
496 ex.printStackTrace();
\r