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.datamodel.*;
\r
28 import jalview.gui.*;
\r
30 public class JPredClient
\r
33 ext.vamsas.Jpred server;
\r
34 String altitle = "";
\r
35 java.util.Hashtable SequenceInfo = null;
\r
36 public JPredClient(ext.vamsas.ServiceHandle sh, String title, SequenceI seq) {
\r
37 wsInfo = setWebService(sh);
\r
38 startJPredClient(title, seq);
\r
40 public JPredClient(ext.vamsas.ServiceHandle sh, String title, SequenceI[] msa) {
\r
41 wsInfo = setWebService(sh);
\r
42 startJPredClient(title, msa);
\r
45 public JPredClient(String title, SequenceI[] msf) {
\r
46 startJPredClient(title, msf);
\r
49 public JPredClient(String title, SequenceI seq) {
\r
50 startJPredClient(title, seq);
\r
53 private void startJPredClient(String title, SequenceI[] msf)
\r
56 wsInfo = setWebService();
\r
58 SequenceI seq = msf[0];
\r
59 altitle = "JNet prediction on " + seq.getName() +
\r
60 " using alignment from " + title;
\r
62 wsInfo.setProgressText("Job details for MSA based prediction (" +
\r
63 title + ") on sequence :\n>" + seq.getName() + "\n" +
\r
64 AlignSeq.extractGaps("-. ", seq.getSequence()) +
\r
66 SequenceI aln[] = new SequenceI[msf.length];
\r
67 for (int i=0,j=msf.length; i<j;i++)
\r
68 aln[i] = new jalview.datamodel.Sequence(msf[i]);
\r
69 SequenceInfo = jalview.analysis.SeqsetUtils.uniquify(aln, true);
\r
71 if (!locateWebService())
\r
76 JPredThread jthread = new JPredThread(aln);
\r
80 public void startJPredClient(String title, SequenceI seq)
\r
83 wsInfo = setWebService();
\r
84 wsInfo.setProgressText("Job details for prediction on sequence :\n>" +
\r
85 seq.getName() + "\n" +
\r
86 AlignSeq.extractGaps("-. ", seq.getSequence()) +
\r
88 altitle = "JNet prediction for sequence " + seq.getName() + " from " +
\r
91 SequenceInfo = jalview.analysis.SeqsetUtils.SeqCharacterHash(seq);
\r
93 if (!locateWebService())
\r
98 JPredThread jthread = new JPredThread(seq);
\r
102 private WebserviceInfo setWebService()
\r
104 WebServiceName = "JNetWS";
\r
105 WebServiceJobTitle = "JNet secondary structure prediction";
\r
106 WebServiceReference =
\r
107 "\"Cuff J. A and Barton G.J (1999) Application of enhanced " +
\r
108 "multiple sequence alignment profiles to improve protein secondary structure prediction, " +
\r
109 "Proteins 40:502-511\".";
\r
110 WsURL = "http://www.compbio.dundee.ac.uk/JalviewWS/services/jpred";
\r
112 WebserviceInfo wsInfo = new WebserviceInfo(WebServiceJobTitle,
\r
113 WebServiceReference);
\r
118 private boolean locateWebService()
\r
120 ext.vamsas.JpredServiceLocator loc = new JpredServiceLocator(); // Default
\r
124 this.server = loc.getjpred(new java.net.URL(WsURL)); // JBPNote will be set from properties
\r
125 ( (JpredSoapBindingStub)this.server).setTimeout(60000); // one minute stub
\r
127 catch (Exception ex)
\r
129 JOptionPane.showMessageDialog(Desktop.desktop,
\r
130 "The Secondary Structure Prediction Service named " +
\r
131 WebServiceName + " at " + WsURL +
\r
132 " couldn't be located.",
\r
133 "Internal Jalview Error",
\r
134 JOptionPane.WARNING_MESSAGE);
\r
135 wsInfo.setProgressText("Serious! " + WebServiceName +
\r
136 " Service location failed\nfor URL :" + WsURL +
\r
139 wsInfo.setStatus(wsInfo.STATE_STOPPED_SERVERERROR);
\r
150 String OutputHeader;
\r
151 vamsas.objects.simple.JpredResult result;
\r
152 vamsas.objects.simple.Sequence sequence;
\r
153 vamsas.objects.simple.Msfalignment msa;
\r
155 boolean jobComplete = false;
\r
156 int allowedServerExceptions = 3; // thread dies if too many exceptions.
\r
158 JPredThread(SequenceI seq)
\r
160 OutputHeader = wsInfo.getProgressText();
\r
161 this.sequence = new vamsas.objects.simple.Sequence();
\r
162 this.sequence.setId(seq.getName());
\r
163 this.sequence.setSeq(AlignSeq.extractGaps("-. ", seq.getSequence()));
\r
166 JPredThread(SequenceI[] msf)
\r
168 OutputHeader = wsInfo.getProgressText();
\r
169 this.sequence = new vamsas.objects.simple.Sequence();
\r
170 this.sequence.setId(msf[0].getName());
\r
171 this.sequence.setSeq(AlignSeq.extractGaps("-. ",
\r
172 msf[0].getSequence()));
\r
174 jalview.io.PileUpfile mwrite = new jalview.io.PileUpfile();
\r
175 this.msa = new vamsas.objects.simple.Msfalignment();
\r
176 msa.setMsf(mwrite.print(msf));
\r
183 while (!jobComplete && (allowedServerExceptions > 0))
\r
187 if ( (result = server.getresult(jobId)) == null)
\r
189 throw (new Exception(
\r
190 "Timed out when communicating with server\nTry again later.\n"));
\r
192 if (result.getState()==0)
\r
193 jalview.bin.Jalview.log.debug("Finished "+jobId);
\r
194 if (result.isRunning())
\r
196 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
\r
198 if (result.isQueued())
\r
200 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
\r
203 wsInfo.setProgressText(OutputHeader + "\n" +
\r
204 result.getStatus());
\r
206 if (result.isFinished())
\r
210 jobComplete = true;
\r
213 // catch exceptions
\r
214 if (! (result.isJobFailed() || result.isServerError()))
\r
218 Thread.sleep(5000);
\r
220 catch (InterruptedException ex1)
\r
224 // System.out.println("I'm alive "+seqid+" "+jobid);
\r
228 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
230 jobComplete = true;
\r
234 catch (Exception ex)
\r
236 allowedServerExceptions--;
\r
237 wsInfo.appendProgressText("\nJPredWS Server exception!\n" +
\r
242 if (allowedServerExceptions > 0)
\r
244 Thread.sleep(5000);
\r
247 catch (InterruptedException ex1)
\r
253 if (! (result.isJobFailed() || result.isServerError()))
\r
255 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
\r
259 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
269 jobId = server.predictOnMsa(msa);
\r
273 jobId = server.predict(sequence);
\r
278 if (jobId.startsWith("Broken"))
\r
280 throw new Exception("Submission " + jobId);
\r
284 System.out.println(WsURL + " Job Id '" + jobId + "'");
\r
289 throw new Exception("Server timed out - try again later\n");
\r
292 catch (Exception e)
\r
294 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
295 allowedServerExceptions = -1;
\r
296 jobComplete = true;
\r
298 wsInfo.setProgressText("Failed to submit the prediction. (Just close the window)\n"
\r
299 + ((e.getMessage().indexOf("Exception")>-1) ? (
\r
300 "It is most likely that there is a problem with the server.\n")
\r
302 +wsInfo.getProgressText());
\r
303 System.err.println(
\r
304 "JPredWS Client: Failed to submit the prediction (Possibly a server error - see below)\n" +
\r
305 e.getMessage() + "\n");
\r
307 jalview.bin.Jalview.log.debug("Failed Submission",e);
\r
313 private void addFloatAnnotations(Alignment al, int[] gapmap,
\r
314 Vector values, String Symname,
\r
315 String Visname, float min,
\r
316 float max, int winLength)
\r
318 Annotation[] annotations = new Annotation[al.getWidth()];
\r
320 for (int j = 0; j < values.size(); j++)
\r
322 float value = Float.parseFloat(values.get(j).toString());
\r
323 annotations[gapmap[j]] = new Annotation("", value + "", ' ',
\r
327 al.addAnnotation(new AlignmentAnnotation(Symname, Visname,
\r
328 annotations, min, max, winLength));
\r
333 // OutputHeader = output.getText();
\r
334 if (result.isFailed())
\r
336 OutputHeader += "Job failed.\n";
\r
339 if (result.getStatus() != null)
\r
341 OutputHeader += ("\n" + result.getStatus());
\r
344 if (result.getPredfile() != null)
\r
346 OutputHeader += ("\n" + result.getPredfile());
\r
348 // 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
351 if (result.getAligfile() != null)
\r
353 OutputHeader += ("\n" + result.getAligfile());
\r
356 wsInfo.setProgressText(OutputHeader+"Parsing...");
\r
360 jalview.bin.Jalview.log.debug("Parsing output from JNet job.");
\r
361 // JPredFile prediction = new JPredFile("C:/JalviewX/files/jpred.txt", "File");
\r
362 jalview.io.JPredFile prediction = new jalview.io.JPredFile(result.
\r
365 SequenceI[] preds = prediction.getSeqsAsArray();
\r
366 jalview.bin.Jalview.log.debug("Got prediction profile.");
\r
368 int FirstSeq; // the position of the query sequence in Alignment al
\r
369 boolean noMsa = true; // set if no MSA has been returned by JPred
\r
371 if ( (this.msa != null) && (result.getAligfile() != null))
\r
373 jalview.bin.Jalview.log.debug("Getting associated alignment.");
\r
374 // we ignore the returned alignment if we only predicted on a single sequence
\r
375 String format = jalview.io.IdentifyFile.Identify(result.getAligfile(),
\r
378 if (jalview.io.FormatAdapter.formats.contains(format))
\r
380 al = new Alignment(jalview.io.FormatAdapter.readFile(
\r
381 result.getAligfile(), "Paste", format));
\r
382 for (int i=0, j=al.getHeight(); i<j; i++) {
\r
383 SequenceI sq = al.getSequenceAt(i);
\r
384 if (!jalview.analysis.SeqsetUtils.SeqCharacterUnhash(
\r
385 sq, (Hashtable) SequenceInfo.get(sq.getName())))
\r
387 throw (new Exception(
\r
388 "Couldn't recover sequence properties for JNet "
\r
389 +((i==0) ? "Query sequence" : "alignment sequence ("+i+")")));
\r
397 throw (new Exception(
\r
398 "Unknown format 'format' for file : \n" +
\r
399 result.getAligfile()));
\r
404 al = new Alignment(preds);
\r
405 FirstSeq = prediction.getQuerySeqPosition();
\r
406 if (!jalview.analysis.SeqsetUtils.SeqCharacterUnhash(
\r
407 al.getSequenceAt(FirstSeq), SequenceInfo))
\r
409 throw (new Exception(
\r
410 "Couldn't recover sequence properties for JNet Query sequence!"));
\r
415 AlignmentAnnotation annot;
\r
416 Annotation[] annotations = null;
\r
418 int width = preds[0].getSequence().length();
\r
420 int[] gapmap = al.getSequenceAt(FirstSeq).gapMap();
\r
422 if (gapmap.length != width)
\r
424 throw (new Exception(
\r
425 "Jnet Client Error\nNumber of residues in supposed query sequence :\n" +
\r
426 al.getSequenceAt(FirstSeq).getName() + "\n" +
\r
427 al.getSequenceAt(FirstSeq).getSequence() +
\r
428 "\nDiffer from number of prediction sites in \n" +
\r
429 result.getPredfile() + "\n"));
\r
432 // JBPNote Should also rename the query sequence sometime...
\r
435 while (i < preds.length)
\r
437 String id = preds[i].getName().toUpperCase();
\r
439 if (id.startsWith("LUPAS") || id.startsWith("JNET") ||
\r
440 id.startsWith("JPRED"))
\r
442 annotations = new Annotation[al.getWidth()];
\r
444 if (id.equals("JNETPRED") || id.equals("JNETPSSM") ||
\r
445 id.equals("JNETFREQ") || id.equals("JNETHMM") ||
\r
446 id.equals("JNETALIGN") || id.equals("JPRED"))
\r
448 for (int j = 0; j < width; j++)
\r
450 annotations[gapmap[j]] = new Annotation("", "",
\r
451 preds[i].getCharAt(j), 0);
\r
454 else if (id.equals("JNETCONF"))
\r
456 for (int j = 0; j < width; j++)
\r
458 float value = Float.parseFloat(preds[i].getCharAt(
\r
460 annotations[gapmap[j]] = new Annotation(preds[i].getCharAt(
\r
461 j) + "", "", preds[i].getCharAt(j),
\r
467 for (int j = 0; j < width; j++)
\r
469 annotations[gapmap[j]] = new Annotation(preds[i].getCharAt(
\r
470 j) + "", "", ' ', 0);
\r
474 if (id.equals("JNETCONF"))
\r
476 annot = new AlignmentAnnotation(preds[i].getName(),
\r
477 "JNet Output", annotations, 0f,
\r
482 annot = new AlignmentAnnotation(preds[i].getName(),
\r
483 "JNet Output", annotations);
\r
486 al.addAnnotation(annot);
\r
490 al.deleteSequence(preds[i]);
\r
497 Hashtable scores = prediction.getScores();
\r
499 /* addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPH"),
\r
500 "JnetpropH", "Jnet Helix Propensity", 0f,1f,1);
\r
502 addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPB"),
\r
503 "JnetpropB", "Jnet Beta Sheet Propensity", 0f,1f,1);
\r
505 addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPC"),
\r
506 "JnetpropC", "Jnet Coil Propensity", 0f,1f,1);
\r
509 wsInfo.setProgressText(OutputHeader);
\r
510 jalview.bin.Jalview.log.debug("Finished parsing output.");
\r
511 AlignFrame af = new AlignFrame(al);
\r
513 Desktop.addInternalFrame(af, altitle,
\r
514 AlignFrame.NEW_WINDOW_WIDTH,
\r
515 AlignFrame.NEW_WINDOW_HEIGHT);
\r
517 catch (Exception ex)
\r
519 jalview.bin.Jalview.log.warn("Exception whilst parsing JNet style secondary structure prediction.");
\r
520 jalview.bin.Jalview.log.debug("Exception: ",ex);
\r