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
23 import javax.swing.*;
\r
25 import ext.vamsas.*;
\r
26 import jalview.analysis.*;
\r
27 import jalview.bin.*;
\r
28 import jalview.datamodel.*;
\r
29 import jalview.datamodel.Alignment;
\r
30 import jalview.gui.*;
\r
31 import jalview.io.*;
\r
32 import jalview.ws.WSThread.*;
\r
33 import vamsas.objects.simple.*;
\r
34 import jalview.util.Comparison;
\r
36 public class JPredClient
\r
39 public JPredClient(ext.vamsas.ServiceHandle sh, String title, SequenceI seq)
\r
41 wsInfo = setWebService(sh);
\r
42 startJPredClient(title, seq);
\r
45 public JPredClient(ext.vamsas.ServiceHandle sh, String title, SequenceI[] msa)
\r
47 wsInfo = setWebService(sh);
\r
48 startJPredClient(title, msa);
\r
51 public JPredClient(String title, SequenceI[] msf)
\r
53 startJPredClient(title, msf);
\r
56 public JPredClient(String title, SequenceI seq)
\r
58 startJPredClient(title, seq);
\r
61 private void startJPredClient(String title, SequenceI[] msf)
\r
65 wsInfo = setWebService();
\r
68 SequenceI seq = msf[0];
\r
70 String altitle = "JNet prediction on " + seq.getName() +
\r
71 " using alignment from " + title;
\r
73 wsInfo.setProgressText("Job details for MSA based prediction (" +
\r
74 title + ") on sequence :\n>" + seq.getName() + "\n" +
\r
75 AlignSeq.extractGaps("-. ", seq.getSequence()) +
\r
77 SequenceI aln[] = new SequenceI[msf.length];
\r
78 for (int i = 0, j = msf.length; i < j; i++)
\r
80 aln[i] = new jalview.datamodel.Sequence(msf[i]);
\r
83 Hashtable SequenceInfo = jalview.analysis.SeqsetUtils.uniquify(aln, true);
\r
85 Jpred server = locateWebService();
\r
91 JPredThread jthread = new JPredThread(wsInfo, altitle, server, SequenceInfo, aln);
\r
92 wsInfo.setthisService(jthread);
\r
96 public void startJPredClient(String title, SequenceI seq)
\r
100 wsInfo = setWebService();
\r
102 wsInfo.setProgressText("Job details for prediction on sequence :\n>" +
\r
103 seq.getName() + "\n" +
\r
104 AlignSeq.extractGaps("-. ", seq.getSequence()) +
\r
106 String altitle = "JNet prediction for sequence " + seq.getName() + " from " +
\r
109 Hashtable SequenceInfo = jalview.analysis.SeqsetUtils.SeqCharacterHash(seq);
\r
111 Jpred server = locateWebService();
\r
117 JPredThread jthread = new JPredThread(wsInfo, altitle, server, SequenceInfo, seq);
\r
118 wsInfo.setthisService(jthread);
\r
122 private WebserviceInfo setWebService()
\r
124 WebServiceName = "JNetWS";
\r
125 WebServiceJobTitle = "JNet secondary structure prediction";
\r
126 WebServiceReference =
\r
127 "\"Cuff J. A and Barton G.J (2000) Application of " +
\r
128 "multiple sequence alignment profiles to improve protein secondary structure prediction, " +
\r
129 "Proteins 40:502-511\".";
\r
130 WsURL = "http://www.compbio.dundee.ac.uk/JalviewWS/services/jpred";
\r
132 WebserviceInfo wsInfo = new WebserviceInfo(WebServiceJobTitle,
\r
133 WebServiceReference);
\r
138 private ext.vamsas.Jpred locateWebService()
\r
140 ext.vamsas.JpredServiceLocator loc = new JpredServiceLocator(); // Default
\r
141 ext.vamsas.Jpred server=null;
\r
144 server = loc.getjpred(new java.net.URL(WsURL)); // JBPNote will be set from properties
\r
145 ( (JpredSoapBindingStub)server).setTimeout(60000); // one minute stub
\r
146 //((JpredSoapBindingStub)this.server)._setProperty(org.apache.axis.encoding.C, Boolean.TRUE);
\r
149 catch (Exception ex)
\r
151 JOptionPane.showMessageDialog(Desktop.desktop,
\r
152 "The Secondary Structure Prediction Service named " +
\r
153 WebServiceName + " at " + WsURL +
\r
154 " couldn't be located.",
\r
155 "Internal Jalview Error",
\r
156 JOptionPane.WARNING_MESSAGE);
\r
157 wsInfo.setProgressText("Serious! " + WebServiceName +
\r
158 " Service location failed\nfor URL :" + WsURL +
\r
161 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
170 implements WSClientI
\r
173 extends WSThread.WSJob
\r
176 vamsas.objects.simple.Sequence sequence;
\r
177 vamsas.objects.simple.Msfalignment msa;
\r
178 java.util.Hashtable SequenceInfo = null;
\r
181 * @return true if getResultSet will return a valid alignment and prediction result.
\r
183 public boolean hasResults()
\r
185 if (subjobComplete && result != null && result.isFinished()
\r
186 && ( (JpredResult) result).getPredfile() != null &&
\r
187 ( (JpredResult) result).getAligfile() != null)
\r
194 boolean hasValidInput()
\r
196 if (sequence != null)
\r
203 public Alignment getResultSet()
\r
206 if (result == null || !result.isFinished())
\r
210 Alignment al = null;
\r
211 int FirstSeq = -1; // the position of the query sequence in Alignment al
\r
212 boolean noMsa = true; // set if no MSA has been returned by JPred
\r
214 JpredResult result = (JpredResult)this.result;
\r
216 jalview.bin.Cache.log.debug("Parsing output from JNet job.");
\r
217 // JPredFile prediction = new JPredFile("C:/JalviewX/files/jpred.txt", "File");
\r
218 jalview.io.JPredFile prediction = new jalview.io.JPredFile(result.
\r
221 SequenceI[] preds = prediction.getSeqsAsArray();
\r
222 jalview.bin.Cache.log.debug("Got prediction profile.");
\r
224 if ( (this.msa != null) && (result.getAligfile() != null))
\r
226 jalview.bin.Cache.log.debug("Getting associated alignment.");
\r
227 // we ignore the returned alignment if we only predicted on a single sequence
\r
228 String format = new jalview.io.IdentifyFile().Identify(result.
\r
232 if (jalview.io.FormatAdapter.isValidFormat(format))
\r
234 al = new Alignment(new FormatAdapter().readFile(result.getAligfile(),
\r
236 SequenceI sqs[] = new SequenceI[al.getHeight()];
\r
237 for (int i = 0, j = al.getHeight(); i < j; i++)
\r
239 sqs[i] = al.getSequenceAt(i);
\r
241 if (!jalview.analysis.SeqsetUtils.deuniquify( (Hashtable)
\r
242 SequenceInfo, sqs))
\r
244 throw (new Exception(
\r
245 "Couldn't recover sequence properties for alignment."));
\r
253 throw (new Exception(
\r
254 "Unknown format "+format+" for file : \n" +
\r
255 result.getAligfile()));
\r
260 al = new Alignment(preds);
\r
261 FirstSeq = prediction.getQuerySeqPosition();
\r
262 if (!jalview.analysis.SeqsetUtils.SeqCharacterUnhash(
\r
263 al.getSequenceAt(FirstSeq), SequenceInfo))
\r
265 throw (new Exception(
\r
266 "Couldn't recover sequence properties for JNet Query sequence!"));
\r
270 al.setDataset(null);
\r
272 jalview.io.JnetAnnotationMaker.add_annotation(prediction, al, FirstSeq,
\r
274 return al; // , FirstSeq, noMsa};
\r
276 public JPredJob(Hashtable SequenceInfo, SequenceI seq)
\r
279 String sq = AlignSeq.extractGaps(Comparison.GapChars, seq.getSequence());
\r
280 if (sq.length() >= 20)
\r
282 this.SequenceInfo = SequenceInfo;
\r
283 sequence = new vamsas.objects.simple.Sequence();
\r
284 sequence.setId(seq.getName());
\r
285 sequence.setSeq(sq);
\r
289 public JPredJob(Hashtable SequenceInfo, SequenceI[] msf)
\r
291 this(SequenceInfo, msf[0]);
\r
292 if (sequence != null)
\r
294 if (msf.length > 1)
\r
296 msa = new vamsas.objects.simple.Msfalignment();
\r
297 jalview.io.PileUpfile pileup = new jalview.io.PileUpfile();
\r
298 msa.setMsf(pileup.print(msf));
\r
303 ext.vamsas.Jpred server;
\r
304 String altitle = "";
\r
305 JPredThread(WebserviceInfo wsinfo, String altitle, ext.vamsas.Jpred server) {
\r
306 this.altitle = altitle;
\r
307 this.server = server;
\r
308 this.wsInfo = wsinfo;
\r
311 // String OutputHeader;
\r
312 // vamsas.objects.simple.JpredResult result;
\r
314 JPredThread(WebserviceInfo wsinfo, String altitle, ext.vamsas.Jpred server, Hashtable SequenceInfo,SequenceI seq)
\r
316 this(wsinfo, altitle, server);
\r
317 JPredJob job = new JPredJob(SequenceInfo, seq);
\r
318 if (job.hasValidInput())
\r
320 OutputHeader = wsInfo.getProgressText();
\r
328 JPredThread(WebserviceInfo wsinfo, String altitle, ext.vamsas.Jpred server, Hashtable SequenceInfo, SequenceI[] msf)
\r
330 this(wsinfo, altitle, server);
\r
331 JPredJob job = new JPredJob(SequenceInfo, msf);
\r
332 if (job.hasValidInput())
\r
337 OutputHeader = wsInfo.getProgressText();
\r
347 while (!jobComplete && (allowedServerExceptions > 0))
\r
351 if ( (result = server.getresult(jobId)) == null)
\r
353 throw (new Exception(
\r
354 "Timed out when communicating with server\nTry again later.\n"));
\r
356 if (result.getState()==0)
\r
357 jalview.bin.Cache.log.debug("Finished "+jobId);
\r
358 if (result.isRunning())
\r
360 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
\r
362 if (result.isQueued())
\r
364 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
\r
367 wsInfo.setProgressText(OutputHeader + "\n" +
\r
368 result.getStatus());
\r
370 if (result.isFinished())
\r
374 jobComplete = true;
\r
377 // catch exceptions
\r
378 if (! (result.isJobFailed() || result.isServerError()))
\r
382 Thread.sleep(5000);
\r
384 catch (InterruptedException ex1)
\r
388 // System.out.println("I'm alive "+seqid+" "+jobid);
\r
392 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
394 jobComplete = true;
\r
398 catch (Exception ex)
\r
400 allowedServerExceptions--;
\r
402 wsInfo.appendProgressText("\nJPredWS Server exception!\n" +
\r
407 if (allowedServerExceptions > 0)
\r
409 Thread.sleep(5000);
\r
412 catch (InterruptedException ex1)
\r
416 catch (OutOfMemoryError er)
\r
418 jobComplete = true;
\r
419 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
420 JOptionPane.showInternalMessageDialog(Desktop.desktop,
\r
421 "Out of memory handling result!!"
\r
423 "\nSee help files for increasing Java Virtual Machine memory."
\r
425 JOptionPane.WARNING_MESSAGE);
\r
426 System.out.println("JPredClient: "+er);
\r
431 if (! (result.isJobFailed() || result.isServerError()))
\r
433 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
\r
437 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
441 void StartJob(WSJob j)
\r
443 if (! (j instanceof JPredJob))
\r
445 throw new Error("Implementation error - StartJob(JpredJob) called on " +
\r
450 JPredJob job = (JPredJob) j;
\r
451 if (job.msa != null)
\r
453 job.jobId = server.predictOnMsa(job.msa);
\r
456 if (job.sequence!=null)
\r
458 job.jobId = server.predict(job.sequence);
\r
461 if (job.jobId != null)
\r
463 if (job.jobId.startsWith("Broken"))
\r
465 job.result = (vamsas.objects.simple.Result)new JpredResult();
\r
466 job.result.setInvalid(true);
\r
467 job.result.setStatus("Submission " + job.jobId);
\r
471 job.submitted = true;
\r
472 job.subjobComplete = false;
\r
473 Cache.log.info(WsURL + " Job Id '" + job.jobId + "'");
\r
478 throw new Exception("Server timed out - try again later\n");
\r
481 catch (Exception e)
\r
483 if (e.getMessage().indexOf("Exception") > -1)
\r
485 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
486 wsInfo.setProgressText(j.jobnum,
\r
487 "Failed to submit the prediction. (Just close the window)\n"
\r
489 "It is most likely that there is a problem with the server.\n");
\r
490 System.err.println(
\r
491 "JPredWS Client: Failed to submit the prediction. Quite possibly because of a server error - see below)\n" +
\r
492 e.getMessage() + "\n");
\r
494 jalview.bin.Cache.log.warn("Server Exception", e);
\r
498 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
\r
499 // JBPNote - this could be a popup informing the user of the problem.
\r
500 wsInfo.appendProgressText(j.jobnum,
\r
501 "Failed to submit the prediction:\n"
\r
503 wsInfo.getProgressText());
\r
505 jalview.bin.Cache.log.debug("Failed Submission of job " + j.jobnum, e);
\r
508 j.allowedServerExceptions = -1;
\r
509 j.subjobComplete = true;
\r
513 /* private void addFloatAnnotations(Alignment al, int[] gapmap,
\r
514 Vector values, String Symname,
\r
515 String Visname, float min,
\r
516 float max, int winLength)
\r
518 Annotation[] annotations = new Annotation[al.getWidth()];
\r
520 for (int j = 0; j < values.size(); j++)
\r
522 float value = Float.parseFloat(values.get(j).toString());
\r
523 annotations[gapmap[j]] = new Annotation("", value + "", ' ',
\r
527 al.addAnnotation(new AlignmentAnnotation(Symname, Visname,
\r
528 annotations, min, max, winLength));
\r
533 int results = 0; // number of result sets received
\r
534 JobStateSummary finalState = new JobStateSummary();
\r
537 for (int j = 0; j < jobs.length; j++)
\r
539 finalState.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
\r
540 if (jobs[j].submitted && jobs[j].subjobComplete && jobs[j].hasResults())
\r
546 catch (Exception ex)
\r
549 Cache.log.error("Unexpected exception when processing results for " +
\r
551 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
555 wsInfo.showResultsNewFrame
\r
556 .addActionListener(new java.awt.event.ActionListener()
\r
558 public void actionPerformed(
\r
559 java.awt.event.ActionEvent evt)
\r
561 displayResults(true);
\r
564 wsInfo.mergeResults
\r
565 .addActionListener(new java.awt.event.ActionListener()
\r
567 public void actionPerformed(
\r
568 java.awt.event.ActionEvent evt)
\r
570 displayResults(false);
\r
573 wsInfo.setResultsReady();
\r
577 wsInfo.setFinishedNoResults();
\r
581 void displayResults(boolean newWindow)
\r
585 Alignment res = null;
\r
586 for (int jn = 0; jn < jobs.length; jn++)
\r
588 Alignment jobres = null;
\r
589 JPredJob j = (JPredJob) jobs[jn];
\r
591 if (j.hasResults())
\r
595 jalview.bin.Cache.log.debug("Parsing output of job " + jn);
\r
596 jobres = j.getResultSet();
\r
597 jalview.bin.Cache.log.debug("Finished parsing output.");
\r
598 if (jobs.length==1)
\r
601 // do merge with other job results
\r
604 catch (Exception e)
\r
606 jalview.bin.Cache.log.error(
\r
607 "JNet Client: JPred Annotation Parse Error",
\r
609 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
\r
610 wsInfo.appendProgressText(j.jobnum,
\r
611 OutputHeader + "\n" +
\r
612 j.result.getStatus() +
\r
613 "\nInvalid JNet job result data!\n" +
\r
615 j.result.setBroken(true);
\r
624 AlignFrame af = new AlignFrame(res);
\r
625 Desktop.addInternalFrame(af, altitle,
\r
626 AlignFrame.NEW_WINDOW_WIDTH,
\r
627 AlignFrame.NEW_WINDOW_HEIGHT);
\r
631 Cache.log.info("Append results onto existing alignment.");
\r
637 void pollJob(WSJob job)
\r
640 job.result = server.getresult(job.jobId);
\r
642 public boolean isCancellable()
\r
647 public void cancelJob()
\r
649 throw new Error("Implementation error!");
\r