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
38 * @version $Revision$
\r
40 public class MsaWSClient extends WSClient
\r
43 * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
\r
45 ext.vamsas.MuscleWS server;
\r
48 * Creates a new MsaWSClient object.
\r
50 * @param MsaWSName DOCUMENT ME!
\r
51 * @param altitle DOCUMENT ME!
\r
52 * @param msa DOCUMENT ME!
\r
53 * @param submitGaps DOCUMENT ME!
\r
54 * @param preserveOrder DOCUMENT ME!
\r
56 public MsaWSClient(String MsaWSName, String altitle, SequenceI[] msa,
\r
57 boolean submitGaps, boolean preserveOrder)
\r
59 if (setWebService(MsaWSName) == false)
\r
61 JOptionPane.showMessageDialog(Desktop.desktop,
\r
62 "The Multiple Sequence Alignment Service named " + MsaWSName +
\r
63 " is unknown", "Internal Jalview Error",
\r
64 JOptionPane.WARNING_MESSAGE);
\r
69 wsInfo = new jalview.gui.WebserviceInfo(WebServiceJobTitle,
\r
70 WebServiceReference);
\r
72 if (!locateWebService())
\r
77 wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment") +
\r
78 " of " + altitle + "\nJob details\n");
\r
80 MsaWSThread musclethread = new MsaWSThread(WebServiceName +
\r
81 " alignment of " + altitle, msa, submitGaps, preserveOrder);
\r
82 wsInfo.setthisService(musclethread);
\r
83 musclethread.start();
\r
86 // JBPNote Nasty object-global state setting methods shouldn't be allowed
\r
87 private boolean setWebService(String MsaWSName)
\r
89 if (MsaWServices.info.containsKey(MsaWSName))
\r
91 WebServiceName = MsaWSName;
\r
93 String[] wsinfo = (String[]) MsaWServices.info.get(MsaWSName);
\r
95 WebServiceJobTitle = wsinfo[1];
\r
96 WebServiceReference = wsinfo[2];
\r
109 * @return DOCUMENT ME!
\r
111 private boolean locateWebService()
\r
113 // TODO: MuscleWS transmuted to generic MsaWS client
\r
114 MuscleWSServiceLocator loc = new MuscleWSServiceLocator(); // Default
\r
118 this.server = (MuscleWS) loc.getMuscleWS(new java.net.URL(WsURL));
\r
119 ((MuscleWSSoapBindingStub) this.server).setTimeout(60000); // One minute timeout
\r
121 catch (Exception ex)
\r
123 wsInfo.setProgressText("Serious! " + WebServiceName +
\r
124 " Service location failed\nfor URL :" + WsURL + "\n" +
\r
126 wsInfo.setStatus(wsInfo.ERROR);
\r
127 ex.printStackTrace();
\r
132 loc.getEngine().setOption("axis", "1");
\r
137 protected class MsaWSThread extends Thread implements WSClientI
\r
139 String ServiceName = WebServiceName;
\r
140 String OutputHeader;
\r
141 vamsas.objects.simple.MsaResult result = null;
\r
142 vamsas.objects.simple.SequenceSet seqs = new vamsas.objects.simple.SequenceSet();
\r
143 Hashtable SeqNames = null;
\r
144 boolean submitGaps = false; // and always store and recover sequence order
\r
145 boolean preserveOrder = true; // and always store and recover sequence order
\r
147 String alTitle; // name which will be used to form new alignment window.
\r
148 int allowedServerExceptions = 3; // thread dies if too many exceptions.
\r
149 boolean jobComplete = false;
\r
151 MsaWSThread(String title, SequenceI[] msa, boolean subgaps,
\r
155 submitGaps = subgaps;
\r
156 preserveOrder = presorder;
\r
158 OutputHeader = wsInfo.getProgressText();
\r
159 SeqNames = new Hashtable();
\r
161 vamsas.objects.simple.Sequence[] seqarray = new vamsas.objects.simple.Sequence[msa.length];
\r
163 for (int i = 0; i < msa.length; i++)
\r
165 String newname = jalview.analysis.SeqsetUtils.unique_name(i);
\r
167 // uniquify as we go
\r
168 // TODO: JBPNote: this is a ubiquitous transformation - set of jalview seq objects to vamsas sequences with name preservation
\r
169 SeqNames.put(newname,
\r
170 jalview.analysis.SeqsetUtils.SeqCharacterHash(msa[i]));
\r
171 seqarray[i] = new vamsas.objects.simple.Sequence();
\r
172 seqarray[i].setId(newname);
\r
173 seqarray[i].setSeq((submitGaps) ? msa[i].getSequence()
\r
174 : AlignSeq.extractGaps(
\r
175 jalview.util.Comparison.GapChars, msa[i].getSequence()));
\r
178 this.seqs = new vamsas.objects.simple.SequenceSet();
\r
179 this.seqs.setSeqs(seqarray);
\r
182 public boolean isCancellable()
\r
187 public void cancelJob()
\r
189 if ((jobId != null) && !jobId.equals("") && !jobComplete)
\r
191 String cancelledMessage = "";
\r
195 vamsas.objects.simple.WsJobId cancelledJob = server.cancel(jobId);
\r
197 if (cancelledJob.getStatus() == 2)
\r
200 cancelledMessage = "Job cancelled.";
\r
201 wsInfo.setStatus(WebserviceInfo.STATE_CANCELLED_OK);
\r
202 jobComplete = true;
\r
206 else if (cancelledJob.getStatus() == 3)
\r
208 // VALID UNSTOPPABLE JOB
\r
209 cancelledMessage += "Server cannot cancel this job. just close the window.\n";
\r
212 if (cancelledJob.getJobId() != null)
\r
214 cancelledMessage += ("[" + cancelledJob.getJobId() +
\r
218 cancelledMessage += "\n";
\r
220 catch (Exception exc)
\r
222 cancelledMessage += ("\nProblems cancelling the job : Exception received...\n" +
\r
224 exc.printStackTrace();
\r
227 wsInfo.setProgressText(OutputHeader + cancelledMessage + "\n");
\r
233 wsInfo.setProgressText(OutputHeader +
\r
234 "Server cannot cancel this job because it has not been submitted properly. just close the window.\n");
\r
243 while (!jobComplete && (allowedServerExceptions > 0))
\r
247 if ((result = server.getResult(jobId)) == null)
\r
249 throw (new Exception(
\r
250 "Timed out when communicating with server\nTry again later.\n"));
\r
253 if (result.isRunning())
\r
255 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
\r
257 else if (result.isQueued())
\r
259 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
\r
262 if (result.isFinished())
\r
265 jobComplete = true;
\r
270 if (result.getStatus() != null)
\r
272 wsInfo.setProgressText(OutputHeader + "\n" +
\r
273 result.getStatus());
\r
276 if (!(result.isJobFailed() || result.isServerError()))
\r
278 Thread.sleep(5000);
\r
280 // System.out.println("I'm alive "+seqid+" "+jobid);
\r
288 catch (Exception ex)
\r
290 allowedServerExceptions--;
\r
291 wsInfo.appendProgressText("\n" + ServiceName +
\r
292 " Server exception!\n" + ex.getMessage());
\r
293 System.err.println(ServiceName + " Server exception: " +
\r
296 // ex.printStackTrace(); JBPNote Debug
\r
299 if (allowedServerExceptions > 0)
\r
301 Thread.sleep(5000);
\r
304 catch (InterruptedException ex1)
\r
310 if (allowedServerExceptions == 0)
\r
312 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
316 if (!((result != null) &&
\r
317 (result.isJobFailed() || result.isServerError())))
\r
319 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
\r
323 if (result.isFailed())
\r
325 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
328 if (result.isServerError())
\r
330 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
340 vamsas.objects.simple.WsJobId jobsubmit = server.align(seqs);
\r
342 if ((jobsubmit != null) && (jobsubmit.getStatus() == 1))
\r
344 jobId = jobsubmit.getJobId();
\r
345 System.out.println(WsURL + " Job Id '" + jobId + "'");
\r
349 if (jobsubmit == null)
\r
351 throw new Exception("Server at " + WsURL +
\r
352 " returned null object, it probably cannot be contacted. Try again later ?");
\r
355 throw new Exception(jobsubmit.getJobId());
\r
358 catch (Exception e)
\r
360 // TODO: JBPNote catch timeout or other fault types explicitly
\r
361 // For unexpected errors
\r
362 System.err.println(WebServiceName +
\r
363 "Client: Failed to submit the sequences for alignment (probably a server side problem)\n" +
\r
364 "When contacting Server:" + WsURL + "\n" + e.toString() +
\r
366 this.allowedServerExceptions = 0;
\r
367 wsInfo.setStatus(wsInfo.STATE_STOPPED_SERVERERROR);
\r
368 wsInfo.appendProgressText(
\r
369 "Failed to submit sequences for alignment.\n" +
\r
370 "It is most likely that there is a problem with the server.\n" +
\r
371 "Just close the window\n");
\r
373 // e.printStackTrace(); // TODO: JBPNote DEBUG
\r
377 private void addFloatAnnotations(Alignment al, int[] gapmap,
\r
378 Vector values, String Symname, String Visname, float min,
\r
379 float max, int winLength)
\r
381 Annotation[] annotations = new Annotation[al.getWidth()];
\r
383 for (int j = 0; j < values.size(); j++)
\r
385 float value = Float.parseFloat(values.get(j).toString());
\r
386 annotations[gapmap[j]] = new Annotation("", value + "", ' ',
\r
390 al.addAnnotation(new AlignmentAnnotation(Symname, Visname,
\r
391 annotations, min, max, winLength));
\r
394 private jalview.datamodel.Sequence[] getVamsasAlignment(
\r
395 vamsas.objects.simple.Alignment valign)
\r
397 vamsas.objects.simple.Sequence[] seqs = valign.getSeqs().getSeqs();
\r
398 jalview.datamodel.Sequence[] msa = new jalview.datamodel.Sequence[seqs.length];
\r
400 for (int i = 0, j = seqs.length; i < j; i++)
\r
401 msa[i] = new jalview.datamodel.Sequence(seqs[i].getId(),
\r
409 SequenceI[] seqs = null;
\r
413 // OutputHeader = output.getText();
\r
414 if (result.isFailed())
\r
416 OutputHeader += "Job failed.\n";
\r
419 if (result.getStatus() != null)
\r
421 OutputHeader += ("\n" + result.getStatus());
\r
424 if (result.getMsa() != null)
\r
426 OutputHeader += "\nAlignment Object Method Notes\n";
\r
428 String[] lines = result.getMsa().getMethod();
\r
430 for (int line = 0; line < lines.length; line++)
\r
431 OutputHeader += (lines[line] + "\n");
\r
433 // 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
434 seqs = getVamsasAlignment(result.getMsa());
\r
437 wsInfo.setProgressText(OutputHeader);
\r
441 AlignmentOrder msaorder = new AlignmentOrder(seqs);
\r
445 jalview.analysis.AlignmentSorter.recoverOrder(seqs);
\r
448 jalview.analysis.SeqsetUtils.deuniquify(SeqNames, seqs);
\r
450 Alignment al = new Alignment(seqs);
\r
452 // TODO: JBPNote Should also rename the query sequence sometime...
\r
453 AlignFrame af = new AlignFrame(al);
\r
454 af.addSortByOrderMenuItem(ServiceName + " Ordering",
\r
457 Desktop.addInternalFrame(af, alTitle,
\r
458 AlignFrame.NEW_WINDOW_WIDTH,
\r
459 AlignFrame.NEW_WINDOW_HEIGHT);
\r
462 catch (Exception ex)
\r
464 ex.printStackTrace();
\r