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
29 import org.apache.axis.client.*;
\r
35 import javax.swing.*;
\r
37 import javax.xml.namespace.QName;
\r
40 public class JPredClient extends WSClient {
\r
41 ext.vamsas.JPredWS server;
\r
42 String altitle = "";
\r
43 java.util.Hashtable SequenceInfo = null;
\r
45 public JPredClient(String title, SequenceI[] msf) {
\r
46 wsInfo = setWebService();
\r
48 SequenceI seq = msf[0];
\r
49 altitle = "JNet prediction on " + seq.getName() +
\r
50 " using alignment from " + title;
\r
52 wsInfo.setProgressText("Job details for MSA based prediction (" +
\r
53 title + ") on sequence :\n>" + seq.getName() + "\n" +
\r
54 AlignSeq.extractGaps("-. ", seq.getSequence()) + "\n");
\r
56 SequenceInfo = jalview.analysis.SeqsetUtils.SeqCharacterHash(seq);
\r
58 if (!locateWebService()) {
\r
62 JPredThread jthread = new JPredThread(msf);
\r
66 public JPredClient(String title, SequenceI seq) {
\r
67 wsInfo = setWebService();
\r
68 wsInfo.setProgressText("Job details for prediction on sequence :\n>" +
\r
69 seq.getName() + "\n" +
\r
70 AlignSeq.extractGaps("-. ", seq.getSequence()) + "\n");
\r
71 altitle = "JNet prediction for sequence " + seq.getName() + " from " +
\r
74 SequenceInfo = jalview.analysis.SeqsetUtils.SeqCharacterHash(seq);
\r
76 if (!locateWebService()) {
\r
80 JPredThread jthread = new JPredThread(seq);
\r
84 private WebserviceInfo setWebService() {
\r
85 WebServiceName = "JNetWS";
\r
86 WebServiceJobTitle = "JNet secondary structure prediction";
\r
87 WebServiceReference = "\"Cuff J. A and Barton G.J (1999) Application of enhanced " +
\r
88 "multiple sequence alignment profiles to improve protein secondary structure prediction, " +
\r
89 "Proteins 40:502-511\".";
\r
90 WsURL = "http://www.compbio.dundee.ac.uk/JalviewWS/services/jpred";
\r
92 WebserviceInfo wsInfo = new WebserviceInfo(WebServiceJobTitle,
\r
93 WebServiceReference);
\r
98 private boolean locateWebService() {
\r
99 JPredWSServiceLocator loc = new JPredWSServiceLocator(); // Default
\r
102 this.server = loc.getjpred(new java.net.URL(WsURL)); // JBPNote will be set from properties
\r
103 ((JpredSoapBindingStub) this.server).setTimeout(60000); // one minute stub
\r
104 } catch (Exception ex) {
\r
105 JOptionPane.showMessageDialog(Desktop.desktop,
\r
106 "The Secondary Structure Prediction Service named " +
\r
107 WebServiceName + " at " + WsURL + " couldn't be located.",
\r
108 "Internal Jalview Error", JOptionPane.WARNING_MESSAGE);
\r
109 wsInfo.setProgressText("Serious! " + WebServiceName +
\r
110 " Service location failed\nfor URL :" + WsURL + "\n" +
\r
112 wsInfo.setStatus(wsInfo.STATE_STOPPED_SERVERERROR);
\r
120 class JPredThread extends Thread {
\r
121 String OutputHeader;
\r
122 ext.vamsas.JpredResult result;
\r
123 ext.vamsas.Sequence sequence;
\r
124 ext.vamsas.Msfalignment msa;
\r
126 boolean jobComplete = false;
\r
127 int allowedServerExceptions = 3; // thread dies if too many exceptions.
\r
129 JPredThread(SequenceI seq) {
\r
130 OutputHeader = wsInfo.getProgressText();
\r
131 this.sequence = new ext.vamsas.Sequence();
\r
132 this.sequence.setId(seq.getName());
\r
133 this.sequence.setSeq(AlignSeq.extractGaps("-. ", seq.getSequence()));
\r
136 JPredThread(SequenceI[] msf) {
\r
137 OutputHeader = wsInfo.getProgressText();
\r
138 this.sequence = new ext.vamsas.Sequence();
\r
139 this.sequence.setId(msf[0].getName());
\r
140 this.sequence.setSeq(AlignSeq.extractGaps("-. ",
\r
141 msf[0].getSequence()));
\r
143 jalview.io.PileUpfile mwrite = new jalview.io.PileUpfile();
\r
144 this.msa = new ext.vamsas.Msfalignment();
\r
145 msa.setMsf(mwrite.print(msf));
\r
148 public void run() {
\r
151 while (!jobComplete && (allowedServerExceptions > 0)) {
\r
153 if ((result = server.getresult(jobId)) == null) {
\r
154 throw (new Exception(
\r
155 "Timed out when communicating with server\nTry again later.\n"));
\r
158 if (result.isRunning()) {
\r
159 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
\r
160 } else if (result.isQueued()) {
\r
161 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
\r
164 if (result.isFinished()) {
\r
166 jobComplete = true;
\r
169 wsInfo.setProgressText(OutputHeader + "\n" +
\r
170 result.getStatus());
\r
172 if (!(result.isJobFailed() || result.isServerError())) {
\r
174 Thread.sleep(5000);
\r
175 } catch (InterruptedException ex1) {
\r
178 // System.out.println("I'm alive "+seqid+" "+jobid);
\r
180 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
183 } catch (Exception ex) {
\r
184 allowedServerExceptions--;
\r
185 wsInfo.appendProgressText("\nJPredWS Server exception!\n" +
\r
189 if (allowedServerExceptions > 0) {
\r
190 Thread.sleep(5000);
\r
192 } catch (InterruptedException ex1) {
\r
197 if (!(result.isJobFailed() || result.isServerError())) {
\r
198 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
\r
200 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
207 jobId = server.predictOnMsa(msa);
\r
209 jobId = server.predict(sequence);
\r
212 if (jobId != null) {
\r
213 if (jobId.startsWith("Broken")) {
\r
214 throw new Exception("Submission " + jobId);
\r
216 System.out.println(WsURL + " Job Id '" + jobId + "'");
\r
219 throw new Exception("Server timed out - try again later\n");
\r
221 } catch (Exception e) {
\r
222 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
223 allowedServerExceptions = 0;
\r
224 jobComplete = false;
\r
225 wsInfo.appendProgressText("Failed to submit the prediction: " +
\r
226 e.toString() + "\nJust close the window\n");
\r
227 System.err.println(
\r
228 "JPredWS Client: Failed to submit the prediction\n" +
\r
229 e.toString() + "\n");
\r
231 // e.printStackTrace(); TODO: JBPNote DEBUG
\r
235 private void addFloatAnnotations(Alignment al, int[] gapmap,
\r
236 Vector values, String Symname, String Visname, float min,
\r
237 float max, int winLength) {
\r
238 Annotation[] annotations = new Annotation[al.getWidth()];
\r
240 for (int j = 0; j < values.size(); j++) {
\r
241 float value = Float.parseFloat(values.get(j).toString());
\r
242 annotations[gapmap[j]] = new Annotation("", value + "", ' ',
\r
246 al.addAnnotation(new AlignmentAnnotation(Symname, Visname,
\r
247 annotations, min, max, winLength));
\r
250 void parseResult() {
\r
251 // OutputHeader = output.getText();
\r
252 if (result.isFailed()) {
\r
253 OutputHeader += "Job failed.\n";
\r
256 if (result.getStatus() != null) {
\r
257 OutputHeader += ("\n" + result.getStatus());
\r
260 if (result.getPredfile() != null) {
\r
261 OutputHeader += ("\n" + result.getPredfile());
\r
263 // 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
266 if (result.getAligfile() != null) {
\r
267 OutputHeader += ("\n" + result.getAligfile());
\r
270 wsInfo.setProgressText(OutputHeader);
\r
273 // JPredFile prediction = new JPredFile("C:/JalviewX/files/jpred.txt", "File");
\r
274 jalview.io.JPredFile prediction = new jalview.io.JPredFile(result.getPredfile(),
\r
276 SequenceI[] preds = prediction.getSeqsAsArray();
\r
278 int FirstSeq; // the position of the query sequence in Alignment al
\r
279 boolean noMsa = true; // set if no MSA has been returned by JPred
\r
281 if ((this.msa != null) && (result.getAligfile() != null)) {
\r
282 // we ignore the returned alignment if we only predicted on a single sequence
\r
283 String format = jalview.io.IdentifyFile.Identify(result.getAligfile(),
\r
286 if (jalview.io.FormatAdapter.formats.contains(format)) {
\r
287 al = new Alignment(jalview.io.FormatAdapter.readFile(
\r
288 result.getAligfile(), "Paste", format));
\r
292 throw (new Exception(
\r
293 "Unknown format 'format' for file : \n" +
\r
294 result.getAligfile()));
\r
297 al = new Alignment(preds);
\r
298 FirstSeq = prediction.getQuerySeqPosition();
\r
301 if (!jalview.analysis.SeqsetUtils.SeqCharacterUnhash(
\r
302 al.getSequenceAt(FirstSeq), SequenceInfo)) {
\r
303 throw (new Exception(
\r
304 "Couldn't recover sequence properties for JNet Query sequence!"));
\r
307 AlignmentAnnotation annot;
\r
308 Annotation[] annotations = null;
\r
310 int width = preds[0].getSequence().length();
\r
312 int[] gapmap = al.getSequenceAt(FirstSeq).gapMap();
\r
314 if (gapmap.length != width) {
\r
315 throw (new Exception(
\r
316 "Jnet Client Error\nNumber of residues in supposed query sequence :\n" +
\r
317 al.getSequenceAt(FirstSeq).getName() + "\n" +
\r
318 al.getSequenceAt(FirstSeq).getSequence() +
\r
319 "\nDiffer from number of prediction sites in \n" +
\r
320 result.getPredfile() + "\n"));
\r
323 // JBPNote Should also rename the query sequence sometime...
\r
326 while (i < preds.length) {
\r
327 String id = preds[i].getName().toUpperCase();
\r
329 if (id.startsWith("LUPAS") || id.startsWith("JNET") ||
\r
330 id.startsWith("JPRED")) {
\r
331 annotations = new Annotation[al.getWidth()];
\r
333 if (id.equals("JNETPRED") || id.equals("JNETPSSM") ||
\r
334 id.equals("JNETFREQ") || id.equals("JNETHMM") ||
\r
335 id.equals("JNETALIGN") || id.equals("JPRED")) {
\r
336 for (int j = 0; j < width; j++) {
\r
337 annotations[gapmap[j]] = new Annotation("", "",
\r
338 preds[i].getCharAt(j), 0);
\r
340 } else if (id.equals("JNETCONF")) {
\r
341 for (int j = 0; j < width; j++) {
\r
342 float value = Float.parseFloat(preds[i].getCharAt(
\r
344 annotations[gapmap[j]] = new Annotation(preds[i].getCharAt(
\r
345 j) + "", "", preds[i].getCharAt(j),
\r
349 for (int j = 0; j < width; j++) {
\r
350 annotations[gapmap[j]] = new Annotation(preds[i].getCharAt(
\r
351 j) + "", "", ' ', 0);
\r
355 if (id.equals("JNETCONF")) {
\r
356 annot = new AlignmentAnnotation(preds[i].getName(),
\r
357 "JNet Output", annotations, 0f, 10f, 1);
\r
360 annot = new AlignmentAnnotation(preds[i].getName(),
\r
361 "JNet Output", annotations);
\r
364 al.addAnnotation(annot);
\r
367 al.deleteSequence(preds[i]);
\r
374 Hashtable scores = prediction.getScores();
\r
376 /* addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPH"),
\r
377 "JnetpropH", "Jnet Helix Propensity", 0f,1f,1);
\r
379 addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPB"),
\r
380 "JnetpropB", "Jnet Beta Sheet Propensity", 0f,1f,1);
\r
382 addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPC"),
\r
383 "JnetpropC", "Jnet Coil Propensity", 0f,1f,1);
\r
385 AlignFrame af = new AlignFrame(al);
\r
387 Desktop.addInternalFrame(af, altitle,
\r
388 AlignFrame.NEW_WINDOW_WIDTH, AlignFrame.NEW_WINDOW_HEIGHT);
\r
389 } catch (Exception ex) {
\r
390 ex.printStackTrace();
\r