3 import org.apache.axis.client.*;
4 import javax.xml.namespace.QName;
6 import jalview.datamodel.*;
11 import jalview.analysis.AlignSeq;
13 import vamsas.objects.*;
18 public class MsaWSClient
22 * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
24 ext.vamsas.MuscleWS server;
26 private boolean setWebService(String MsaWSName) {
27 if (MsaWServices.info.containsKey(MsaWSName)) {
28 WebServiceName = MsaWSName;
29 String[] wsinfo = (String[]) MsaWServices.info.get(MsaWSName);
31 WebServiceJobTitle = wsinfo[1];
32 WebServiceReference = wsinfo[2];
40 public MsaWSClient(String MsaWSName, String altitle, SequenceI[] msa, boolean submitGaps, boolean preserveOrder)
42 if (setWebService(MsaWSName)==false) {
43 JOptionPane.showMessageDialog(Desktop.desktop, "The Multiple Sequence Alignment Service named "+MsaWSName+" is unknown",
44 "Internal Jalview Error", JOptionPane.WARNING_MESSAGE);
48 wsInfo = new jalview.gui.WebserviceInfo(WebServiceJobTitle, WebServiceReference);
50 wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment")+" of "+altitle+"\nJob details\n");
52 // TODO: MuscleWS transmuted to generic MsaWS client
53 MuscleWSServiceLocator loc = new MuscleWSServiceLocator(); // Default
55 this.server = (MuscleWS) loc.getMuscleWS(new java.net.URL(WsURL));
56 ((MuscleWSSoapBindingStub) this.server).setTimeout(60000); // One minute timeout
58 catch (Exception ex) {
59 wsInfo.setProgressText("Serious! "+WebServiceName+" Service location failed\nfor URL :"
60 +WsURL+"\n"+ex.getMessage());
61 wsInfo.setStatus(wsInfo.ERROR);
64 loc.getEngine().setOption("axis","1");
65 MsaWSThread musclethread = new MsaWSThread(WebServiceName+" alignment of "+altitle, msa, submitGaps, preserveOrder);
66 wsInfo.setthisService(musclethread);
71 protected class MsaWSThread extends Thread implements WSClientI
73 String ServiceName = WebServiceName;
75 public boolean isCancellable()
81 vamsas.objects.simple.MsaResult result = null;
83 vamsas.objects.simple.SequenceSet seqs = new vamsas.objects.simple.
86 Hashtable SeqNames = null;
87 boolean submitGaps = false,// default is to strip gaps from sequences
88 preserveOrder = true; // and always store and recover sequence order
91 String alTitle; // name which will be used to form new alignment window.
92 int allowedServerExceptions = 3; // thread dies if too many exceptions.
93 MsaWSThread(String title, SequenceI[] msa, boolean subgaps, boolean presorder)
97 preserveOrder = presorder;
99 OutputHeader = wsInfo.getProgressText();
100 SeqNames = new Hashtable();
101 vamsas.objects.simple.Sequence[] seqarray = new vamsas.objects.simple.
102 Sequence[msa.length];
104 for (int i = 0; i < msa.length; i++)
106 String newname = jalview.analysis.SeqsetUtils.unique_name(i);
108 // TODO: JBPNote: this is a ubiquitous transformation - set of jalview seq objects to vamsas sequences with name preservation
109 SeqNames.put(newname, jalview.analysis.SeqsetUtils.SeqCharacterHash(msa[i]));
110 seqarray[i] = new vamsas.objects.simple.Sequence();
111 seqarray[i].setId(newname);
112 seqarray[i].setSeq((submitGaps) ? msa[i].getSequence()
113 : AlignSeq.extractGaps(jalview.util.Comparison.GapChars, msa[i].getSequence()));
116 this.seqs = new vamsas.objects.simple.SequenceSet();
117 this.seqs.setSeqs(seqarray);
120 boolean jobComplete = false;
122 public void cancelJob() {
123 if (jobId!=null && !jobId.equals("") && !jobComplete) {
124 String cancelledMessage="";
126 vamsas.objects.simple.WsJobId cancelledJob = server.cancel(jobId);
127 if (cancelledJob.getStatus() == 2)
130 cancelledMessage = "Job cancelled.";
131 wsInfo.setStatus(WebserviceInfo.STATE_CANCELLED_OK);
137 if (cancelledJob.getStatus() == 3)
139 // VALID UNSTOPPABLE JOB
141 "Server cannot cancel this job. just close the window.\n";
143 if (cancelledJob.getJobId() != null)
144 cancelledMessage += "[" + cancelledJob.getJobId() + "]";
145 cancelledMessage +="\n";
146 } catch (Exception exc) {
147 cancelledMessage +="\nProblems cancelling the job : Exception received...\n"+exc+"\n";
148 exc.printStackTrace();
150 wsInfo.setProgressText(OutputHeader + cancelledMessage+"\n");
154 wsInfo.setProgressText(OutputHeader + "Server cannot cancel this job because it has not been submitted properly. just close the window.\n");
164 while (!jobComplete && (allowedServerExceptions > 0))
168 if ((result = server.getResult(jobId))==null)
169 throw(new Exception("Timed out when communicating with server\nTry again later.\n"));
171 if (result.isRunning())
172 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
173 else if (result.isQueued())
174 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
176 if (result.isFinished())
184 if (result.getStatus() != null)
185 wsInfo.setProgressText(OutputHeader + "\n" + result.getStatus());
186 if (! (result.isJobFailed() || result.isServerError()))
189 // System.out.println("I'm alive "+seqid+" "+jobid);
199 allowedServerExceptions--;
200 wsInfo.appendProgressText("\n" + ServiceName + " Server exception!\n" +
202 System.err.println(ServiceName + " Server exception: " +
204 // ex.printStackTrace(); JBPNote Debug
207 if (allowedServerExceptions>0)
210 catch (InterruptedException ex1)
216 if (allowedServerExceptions == 0)
218 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
222 if (! (result != null && (result.isJobFailed() || result.isServerError())))
223 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
226 if (result.isFailed())
227 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
228 if (result.isServerError())
229 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
237 vamsas.objects.simple.WsJobId jobsubmit = server.align(seqs);
238 if (jobsubmit!=null && jobsubmit.getStatus()==1) {
239 jobId=jobsubmit.getJobId();
240 System.out.println(WsURL+" Job Id '"+jobId+"'");
242 if (jobsubmit == null) {
243 throw new Exception("Server at "+WsURL+" returned null object, it probably cannot be contacted. Try again later ?");
245 throw new Exception(jobsubmit.getJobId());
250 // TODO: JBPNote catch timeout or other fault types explicitly
251 // For unexpected errors
252 System.err.println(WebServiceName + " Client: Failed to submit the sequences for alignment.\n"+WsURL+" : " +
253 e.toString() + "\n");
254 this.allowedServerExceptions=0;
255 wsInfo.setStatus(wsInfo.STATE_STOPPED_SERVERERROR);
256 wsInfo.appendProgressText("Server problems! "+e.toString()+"\nFailed to submit sequences for alignment. Just close the window\n");
257 // e.printStackTrace(); // TODO: JBPNote DEBUG
261 private void addFloatAnnotations(Alignment al, int[] gapmap, Vector values, String Symname, String Visname, float min, float max, int winLength) {
263 Annotation[] annotations = new Annotation[al.getWidth()];
264 for (int j = 0; j < values.size(); j++)
266 float value = Float.parseFloat(values.get(j).toString());
267 annotations[gapmap[j]] = new Annotation("", value+"",' ',value);
269 al.addAnnotation(new AlignmentAnnotation(Symname, Visname, annotations, min, max, winLength));
271 private jalview.datamodel.Sequence[] getVamsasAlignment(vamsas.objects.simple.Alignment valign) {
272 vamsas.objects.simple.Sequence[] seqs = valign.getSeqs().getSeqs();
273 jalview.datamodel.Sequence[] msa = new jalview.datamodel.Sequence[seqs.length];
274 for (int i=0, j=seqs.length; i<j;i++)
275 msa[i] = new jalview.datamodel.Sequence(seqs[i].getId(), seqs[i].getSeq());
280 SequenceI[] seqs=null;
282 // OutputHeader = output.getText();
283 if (result.isFailed()) {
284 OutputHeader +="Job failed.\n";
286 if (result.getStatus()!=null) {
287 OutputHeader += "\n"+result.getStatus();
289 if (result.getMsa()!=null) {
290 OutputHeader += "\nAlignment Object Method Notes\n";
291 String lines[] = result.getMsa().getMethod();
292 for (int line=0;line<lines.length; line++)
293 OutputHeader+=lines[line]+"\n";
295 // JBPNote The returned files from a webservice could be hidden behind icons in the monitor window that, when clicked, pop up their corresponding data
296 seqs = getVamsasAlignment(result.getMsa());
299 wsInfo.setProgressText(OutputHeader);
301 AlignmentOrder msaorder = new AlignmentOrder(seqs);
304 jalview.analysis.AlignmentSorter.recoverOrder(seqs);
307 jalview.analysis.SeqsetUtils.deuniquify(SeqNames, seqs);
309 Alignment al = new Alignment(seqs);
311 // TODO: JBPNote Should also rename the query sequence sometime...
312 AlignFrame af = new AlignFrame(al);
313 af.addSortByOrderMenuItem(ServiceName+" Ordering", msaorder);
315 Desktop.addInternalFrame(af,
317 AlignFrame.NEW_WINDOW_WIDTH,
318 AlignFrame.NEW_WINDOW_HEIGHT);
320 }catch(Exception ex){ex.printStackTrace();}