2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.ws.jws1;
22 import jalview.analysis.*;
24 import jalview.datamodel.*;
27 import jalview.util.*;
28 import jalview.ws.AWsJob;
29 import jalview.ws.JobStateSummary;
30 import jalview.ws.WSClientI;
31 import vamsas.objects.simple.JpredResult;
33 class JPredThread extends JWS1Thread implements WSClientI
35 // TODO: put mapping between JPredJob input and input data here -
36 // JNetAnnotation adding is done after result parsing.
37 class JPredJob extends WSJob
39 // TODO: make JPredJob deal only with what was sent to and received from a
41 int[] predMap = null; // mapping from sequence(i) to the original
43 // sequence(predMap[i]) being predicted on
45 vamsas.objects.simple.Sequence sequence;
47 vamsas.objects.simple.Msfalignment msa;
49 java.util.Hashtable SequenceInfo = null;
51 int msaIndex = 0; // the position of the original sequence in the array of
53 // Sequences in the input object that this job holds a
58 * @return true if getResultSet will return a valid alignment and prediction
61 public boolean hasResults()
63 if (subjobComplete && result != null && result.isFinished()
64 && ((JpredResult) result).getPredfile() != null
65 && ((JpredResult) result).getAligfile() != null)
72 public boolean hasValidInput()
83 * @return null or Object[] { annotated alignment for this prediction,
84 * ColumnSelection for this prediction} or null if no results
88 public Object[] getResultSet() throws Exception
90 if (result == null || !result.isFinished())
95 ColumnSelection alcsel = null;
96 int FirstSeq = -1; // the position of the query sequence in Alignment al
98 JpredResult result = (JpredResult) this.result;
100 jalview.bin.Cache.log.debug("Parsing output from JNet job.");
101 // JPredFile prediction = new JPredFile("C:/JalviewX/files/jpred.txt",
103 jalview.io.JPredFile prediction = new jalview.io.JPredFile(
104 result.getPredfile(), "Paste");
105 SequenceI[] preds = prediction.getSeqsAsArray();
106 jalview.bin.Cache.log.debug("Got prediction profile.");
108 if ((this.msa != null) && (result.getAligfile() != null))
110 jalview.bin.Cache.log.debug("Getting associated alignment.");
111 // we ignore the returned alignment if we only predicted on a single
113 String format = new jalview.io.IdentifyFile().Identify(
114 result.getAligfile(), "Paste");
116 if (jalview.io.FormatAdapter.isValidFormat(format))
121 Object[] alandcolsel = input
122 .getAlignmentAndColumnSelection(getGapChar());
123 sqs = (SequenceI[]) alandcolsel[0];
124 al = new Alignment(sqs);
125 alcsel = (ColumnSelection) alandcolsel[1];
129 al = new FormatAdapter().readFile(result.getAligfile(),
131 sqs = new SequenceI[al.getHeight()];
133 for (int i = 0, j = al.getHeight(); i < j; i++)
135 sqs[i] = al.getSequenceAt(i);
137 if (!jalview.analysis.SeqsetUtils.deuniquify(
138 (Hashtable) SequenceInfo, sqs))
140 throw (new Exception(
141 "Couldn't recover sequence properties for alignment."));
147 jalview.io.JnetAnnotationMaker.add_annotation(prediction, al,
148 FirstSeq, false, predMap);
153 throw (new Exception("Unknown format " + format
154 + " for file : \n" + result.getAligfile()));
159 al = new Alignment(preds);
160 FirstSeq = prediction.getQuerySeqPosition();
163 char gc = getGapChar();
164 SequenceI[] sqs = (SequenceI[]) ((java.lang.Object[]) input
165 .getAlignmentAndColumnSelection(gc))[0];
166 if (this.msaIndex >= sqs.length)
169 "Implementation Error! Invalid msaIndex for JPredJob on parent MSA input object!");
173 // Uses RemoveGapsCommand
175 new jalview.commands.RemoveGapsCommand("Remove Gaps",
177 { sqs[msaIndex] }, currentView);
179 SequenceI profileseq = al.getSequenceAt(FirstSeq);
180 profileseq.setSequence(sqs[msaIndex].getSequenceAsString());
183 if (!jalview.analysis.SeqsetUtils.SeqCharacterUnhash(
184 al.getSequenceAt(FirstSeq), SequenceInfo))
186 throw (new Exception(
187 "Couldn't recover sequence properties for JNet Query sequence!"));
192 jalview.io.JnetAnnotationMaker.add_annotation(prediction, al,
193 FirstSeq, true, predMap);
194 SequenceI profileseq = al.getSequenceAt(0); // this includes any gaps.
195 alignToProfileSeq(al, profileseq);
198 // Adjust input view for gaps
199 // propagate insertions into profile
200 alcsel = ColumnSelection.propagateInsertions(profileseq, al, input);
205 { al, alcsel }; // , FirstSeq, noMsa};
209 * Given an alignment where all other sequences except profileseq are
210 * aligned to the ungapped profileseq, insert gaps in the other sequences to
211 * realign them with the residues in profileseq
216 private void alignToProfileSeq(Alignment al, SequenceI profileseq)
218 char gc = al.getGapCharacter();
219 int[] gapMap = profileseq.gapMap();
220 // insert gaps into profile
221 for (int lp = 0, r = 0; r < gapMap.length; r++)
223 if (gapMap[r] - lp > 1)
225 StringBuffer sb = new StringBuffer();
226 for (int s = 0, ns = gapMap[r] - lp; s < ns; s++)
230 for (int s = 1, ns = al.getHeight(); s < ns; s++)
232 String sq = al.getSequenceAt(s).getSequenceAsString();
233 int diff = gapMap[r] - sq.length();
238 while ((diff = gapMap[r] - sq.length()) > 0)
241 + ((diff >= sb.length()) ? sb.toString() : sb
242 .substring(0, diff));
244 al.getSequenceAt(s).setSequence(sq);
248 al.getSequenceAt(s).setSequence(
249 sq.substring(0, gapMap[r]) + sb.toString()
250 + sq.substring(gapMap[r]));
259 public JPredJob(Hashtable SequenceInfo, SequenceI seq, int[] delMap)
262 this.predMap = delMap;
263 String sq = AlignSeq.extractGaps(Comparison.GapChars,
264 seq.getSequenceAsString());
265 if (sq.length() >= 20)
267 this.SequenceInfo = SequenceInfo;
268 sequence = new vamsas.objects.simple.Sequence();
269 sequence.setId(seq.getName());
274 public JPredJob(Hashtable SequenceInfo, SequenceI[] msf, int[] delMap)
276 this(SequenceInfo, msf[0], delMap);
277 if (sequence != null)
281 msa = new vamsas.objects.simple.Msfalignment();
282 jalview.io.PileUpfile pileup = new jalview.io.PileUpfile();
283 msa.setMsf(pileup.print(msf));
289 ext.vamsas.Jpred server;
293 JPredThread(WebserviceInfo wsinfo, String altitle,
294 ext.vamsas.Jpred server, String wsurl, AlignmentView alview,
297 super(alframe, wsinfo, alview, wsurl);
298 this.altitle = altitle;
299 this.server = server;
302 JPredThread(WebserviceInfo wsinfo, String altitle,
303 ext.vamsas.Jpred server, String wsurl, Hashtable SequenceInfo,
304 SequenceI seq, int[] delMap, AlignmentView alview,
307 this(wsinfo, altitle, server, wsurl, alview, alframe);
308 JPredJob job = new JPredJob(SequenceInfo, seq, delMap);
309 if (job.hasValidInput())
311 OutputHeader = wsInfo.getProgressText();
318 JPredThread(WebserviceInfo wsinfo, String altitle,
319 ext.vamsas.Jpred server, Hashtable SequenceInfo, SequenceI[] msf,
320 int[] delMap, AlignmentView alview, AlignFrame alframe,
323 this(wsinfo, altitle, server, wsurl, alview, alframe);
324 JPredJob job = new JPredJob(SequenceInfo, msf, delMap);
325 if (job.hasValidInput())
329 OutputHeader = wsInfo.getProgressText();
334 public void StartJob(AWsJob j)
336 if (!(j instanceof JPredJob))
339 "Implementation error - StartJob(JpredJob) called on "
344 JPredJob job = (JPredJob) j;
347 job.setJobId(server.predictOnMsa(job.msa));
349 else if (job.sequence != null)
351 job.setJobId(server.predict(job.sequence)); // debug like : job.jobId =
352 // "/jobs/www-jpred/jp_Yatat29";//
355 if (job.getJobId() != null)
357 if (job.getJobId().startsWith("Broken"))
359 job.result = (vamsas.objects.simple.Result) new JpredResult();
360 job.result.setInvalid(true);
361 job.result.setStatus("Submission " + job.getJobId());
362 throw new Exception(job.getJobId());
366 job.setSubmitted(true);
367 job.setSubjobComplete(false);
368 Cache.log.info(WsUrl + " Job Id '" + job.getJobId() + "'");
373 throw new Exception("Server timed out - try again later\n");
375 } catch (Exception e)
377 // kill the whole job.
378 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
379 if (e.getMessage().indexOf("Exception") > -1)
381 wsInfo.setStatus(j.getJobnum(),
382 WebserviceInfo.STATE_STOPPED_SERVERERROR);
383 wsInfo.setProgressText(
385 "Failed to submit the prediction. (Just close the window)\n"
386 + "It is most likely that there is a problem with the server.\n");
388 .println("JPredWS Client: Failed to submit the prediction. Quite possibly because of a server error - see below)\n"
389 + e.getMessage() + "\n");
391 jalview.bin.Cache.log.warn("Server Exception", e);
395 wsInfo.setStatus(j.getJobnum(), WebserviceInfo.STATE_STOPPED_ERROR);
396 // JBPNote - this could be a popup informing the user of the problem.
397 wsInfo.appendProgressText(j.getJobnum(),
398 "Failed to submit the prediction:\n" + e.getMessage()
399 + wsInfo.getProgressText());
401 jalview.bin.Cache.log.debug(
402 "Failed Submission of job " + j.getJobnum(), e);
405 j.setAllowedServerExceptions(-1);
406 j.setSubjobComplete(true);
410 public void parseResult()
412 int results = 0; // number of result sets received
413 JobStateSummary finalState = new JobStateSummary();
416 for (int j = 0; j < jobs.length; j++)
418 finalState.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
419 if (jobs[j].isSubmitted() && jobs[j].isSubjobComplete()
420 && jobs[j].hasResults())
425 } catch (Exception ex)
428 Cache.log.error("Unexpected exception when processing results for "
430 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
434 wsInfo.showResultsNewFrame
435 .addActionListener(new java.awt.event.ActionListener()
437 public void actionPerformed(java.awt.event.ActionEvent evt)
439 displayResults(true);
443 .addActionListener(new java.awt.event.ActionListener()
445 public void actionPerformed(java.awt.event.ActionEvent evt)
447 displayResults(false);
450 wsInfo.setResultsReady();
454 wsInfo.setFinishedNoResults();
458 void displayResults(boolean newWindow)
460 // TODO: cope with multiple subjobs.
465 for (int jn = 0; jn < jobs.length; jn++)
467 Object[] jobres = null;
468 JPredJob j = (JPredJob) jobs[jn];
472 // hack - we only deal with all single seuqence predictions or all
473 // profile predictions
474 msa = (j.msa != null) ? true : msa;
477 jalview.bin.Cache.log.debug("Parsing output of job " + jn);
478 jobres = j.getResultSet();
479 jalview.bin.Cache.log.debug("Finished parsing output.");
480 if (jobs.length == 1)
486 // do merge with other job results
488 "Multiple JNet subjob merging not yet implemented.");
490 } catch (Exception e)
492 jalview.bin.Cache.log.error(
493 "JNet Client: JPred Annotation Parse Error", e);
494 wsInfo.setStatus(j.getJobnum(),
495 WebserviceInfo.STATE_STOPPED_ERROR);
496 wsInfo.appendProgressText(j.getJobnum(), OutputHeader + "\n"
497 + j.result.getStatus()
498 + "\nInvalid JNet job result data!\n" + e.getMessage());
499 j.result.setBroken(true);
513 af = new AlignFrame((Alignment) res[0],
514 (ColumnSelection) res[1], AlignFrame.DEFAULT_WIDTH,
515 AlignFrame.DEFAULT_HEIGHT);
519 af = new AlignFrame((Alignment) res[0],
520 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
526 * java.lang.Object[] alandcolsel =
527 * input.getAlignmentAndColumnSelection
528 * (alignFrame.getViewport().getGapCharacter()); if
529 * (((SequenceI[])alandcolsel[0])[0].getLength()!=res.getWidth()) {
530 * if (msa) { throw new Error("Implementation Error! ColumnSelection
531 * from input alignment will not map to result alignment!"); } } if
532 * (!msa) { // update hidden regions to account for loss of gaps in
533 * profile. - if any // gapMap returns insert list, interpreted as
534 * delete list by pruneDeletions //((ColumnSelection)
535 * alandcolsel[1]).pruneDeletions(ShiftList.parseMap(((SequenceI[])
536 * alandcolsel[0])[0].gapMap())); }
539 af = new AlignFrame((Alignment) res[0],
540 (ColumnSelection) res[1], AlignFrame.DEFAULT_WIDTH,
541 AlignFrame.DEFAULT_HEIGHT);
543 Desktop.addInternalFrame(af, altitle, AlignFrame.DEFAULT_WIDTH,
544 AlignFrame.DEFAULT_HEIGHT);
548 Cache.log.info("Append results onto existing alignment.");
554 public void pollJob(AWsJob job) throws Exception
556 ((JPredJob) job).result = server.getresult(job.getJobId());
559 public boolean isCancellable()
564 public void cancelJob()
566 throw new Error("Implementation error!");
569 public boolean canMergeResults()