2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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 = 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 * Add gaps into the sequences aligned to profileseq under the given
266 private ColumnSelection propagateInsertions(SequenceI profileseq,
267 Alignment al, AlignmentView input)
269 char gc = al.getGapCharacter();
270 Object[] alandcolsel = input.getAlignmentAndColumnSelection(gc);
271 ColumnSelection nview = (ColumnSelection) alandcolsel[1];
273 nview.pruneDeletions(ShiftList
274 .parseMap((origseq = ((SequenceI[]) alandcolsel[0])[0])
275 .gapMap())); // recover original prediction sequence's
277 int[] viscontigs = nview.getVisibleContigs(0, profileseq.getLength());
280 // input.pruneDeletions(ShiftList.parseMap(((SequenceI[])
281 // alandcolsel[0])[0].gapMap()))
282 // add profile to visible contigs
283 for (int v = 0; v < viscontigs.length; v += 2)
285 if (viscontigs[v] > spos)
287 StringBuffer sb = new StringBuffer();
288 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
292 for (int s = 0, ns = al.getHeight(); s < ns; s++)
294 SequenceI sqobj = al.getSequenceAt(s);
295 if (sqobj != profileseq)
297 String sq = al.getSequenceAt(s).getSequenceAsString();
298 if (sq.length() <= spos + offset)
301 int diff = spos + offset - sq.length() - 1;
306 while ((diff = spos + offset - sq.length() - 1) > 0)
309 + ((diff >= sb.length()) ? sb.toString() : sb
310 .substring(0, diff));
317 al.getSequenceAt(s).setSequence(
318 sq.substring(0, spos + offset) + sb.toString()
319 + sq.substring(spos + offset));
323 // offset+=sb.length();
325 spos = viscontigs[v + 1] + 1;
327 if ((offset + spos) < profileseq.getLength())
329 StringBuffer sb = new StringBuffer();
330 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
334 for (int s = 1, ns = al.getHeight(); s < ns; s++)
336 String sq = al.getSequenceAt(s).getSequenceAsString();
338 int diff = origseq.getLength() - sq.length();
342 + ((diff >= sb.length()) ? sb.toString() : sb
343 .substring(0, diff));
344 diff = origseq.getLength() - sq.length();
351 public JPredJob(Hashtable SequenceInfo, SequenceI seq, int[] delMap)
354 this.predMap = delMap;
355 String sq = AlignSeq.extractGaps(Comparison.GapChars,
356 seq.getSequenceAsString());
357 if (sq.length() >= 20)
359 this.SequenceInfo = SequenceInfo;
360 sequence = new vamsas.objects.simple.Sequence();
361 sequence.setId(seq.getName());
366 public JPredJob(Hashtable SequenceInfo, SequenceI[] msf, int[] delMap)
368 this(SequenceInfo, msf[0], delMap);
369 if (sequence != null)
373 msa = new vamsas.objects.simple.Msfalignment();
374 jalview.io.PileUpfile pileup = new jalview.io.PileUpfile();
375 msa.setMsf(pileup.print(msf));
381 ext.vamsas.Jpred server;
385 JPredThread(WebserviceInfo wsinfo, String altitle,
386 ext.vamsas.Jpred server, String wsurl, AlignmentView alview,
389 super(alframe, wsinfo, alview, wsurl);
390 this.altitle = altitle;
391 this.server = server;
394 JPredThread(WebserviceInfo wsinfo, String altitle,
395 ext.vamsas.Jpred server, String wsurl, Hashtable SequenceInfo,
396 SequenceI seq, int[] delMap, AlignmentView alview,
399 this(wsinfo, altitle, server, wsurl, alview, alframe);
400 JPredJob job = new JPredJob(SequenceInfo, seq, delMap);
401 if (job.hasValidInput())
403 OutputHeader = wsInfo.getProgressText();
410 JPredThread(WebserviceInfo wsinfo, String altitle,
411 ext.vamsas.Jpred server, Hashtable SequenceInfo, SequenceI[] msf,
412 int[] delMap, AlignmentView alview, AlignFrame alframe,
415 this(wsinfo, altitle, server, wsurl, alview, alframe);
416 JPredJob job = new JPredJob(SequenceInfo, msf, delMap);
417 if (job.hasValidInput())
421 OutputHeader = wsInfo.getProgressText();
426 public void StartJob(AWsJob j)
428 if (!(j instanceof JPredJob))
431 "Implementation error - StartJob(JpredJob) called on "
436 JPredJob job = (JPredJob) j;
439 job.setJobId(server.predictOnMsa(job.msa));
441 else if (job.sequence != null)
443 job.setJobId(server.predict(job.sequence)); // debug like : job.jobId =
444 // "/jobs/www-jpred/jp_Yatat29";//
447 if (job.getJobId() != null)
449 if (job.getJobId().startsWith("Broken"))
451 job.result = (vamsas.objects.simple.Result) new JpredResult();
452 job.result.setInvalid(true);
453 job.result.setStatus("Submission " + job.getJobId());
454 throw new Exception(job.getJobId());
458 job.setSubmitted(true);
459 job.setSubjobComplete(false);
460 Cache.log.info(WsUrl + " Job Id '" + job.getJobId() + "'");
465 throw new Exception("Server timed out - try again later\n");
467 } catch (Exception e)
469 // kill the whole job.
470 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
471 if (e.getMessage().indexOf("Exception") > -1)
473 wsInfo.setStatus(j.getJobnum(),
474 WebserviceInfo.STATE_STOPPED_SERVERERROR);
475 wsInfo.setProgressText(
477 "Failed to submit the prediction. (Just close the window)\n"
478 + "It is most likely that there is a problem with the server.\n");
480 .println("JPredWS Client: Failed to submit the prediction. Quite possibly because of a server error - see below)\n"
481 + e.getMessage() + "\n");
483 jalview.bin.Cache.log.warn("Server Exception", e);
487 wsInfo.setStatus(j.getJobnum(), WebserviceInfo.STATE_STOPPED_ERROR);
488 // JBPNote - this could be a popup informing the user of the problem.
489 wsInfo.appendProgressText(j.getJobnum(),
490 "Failed to submit the prediction:\n" + e.getMessage()
491 + wsInfo.getProgressText());
493 jalview.bin.Cache.log.debug(
494 "Failed Submission of job " + j.getJobnum(), e);
497 j.setAllowedServerExceptions(-1);
498 j.setSubjobComplete(true);
502 public void parseResult()
504 int results = 0; // number of result sets received
505 JobStateSummary finalState = new JobStateSummary();
508 for (int j = 0; j < jobs.length; j++)
510 finalState.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
511 if (jobs[j].isSubmitted() && jobs[j].isSubjobComplete()
512 && jobs[j].hasResults())
517 } catch (Exception ex)
520 Cache.log.error("Unexpected exception when processing results for "
522 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
526 wsInfo.showResultsNewFrame
527 .addActionListener(new java.awt.event.ActionListener()
529 public void actionPerformed(java.awt.event.ActionEvent evt)
531 displayResults(true);
535 .addActionListener(new java.awt.event.ActionListener()
537 public void actionPerformed(java.awt.event.ActionEvent evt)
539 displayResults(false);
542 wsInfo.setResultsReady();
546 wsInfo.setFinishedNoResults();
550 void displayResults(boolean newWindow)
552 // TODO: cope with multiple subjobs.
557 for (int jn = 0; jn < jobs.length; jn++)
559 Object[] jobres = null;
560 JPredJob j = (JPredJob) jobs[jn];
564 // hack - we only deal with all single seuqence predictions or all
565 // profile predictions
566 msa = (j.msa != null) ? true : msa;
569 jalview.bin.Cache.log.debug("Parsing output of job " + jn);
570 jobres = j.getResultSet();
571 jalview.bin.Cache.log.debug("Finished parsing output.");
572 if (jobs.length == 1)
578 // do merge with other job results
580 "Multiple JNet subjob merging not yet implemented.");
582 } catch (Exception e)
584 jalview.bin.Cache.log.error(
585 "JNet Client: JPred Annotation Parse Error", e);
586 wsInfo.setStatus(j.getJobnum(),
587 WebserviceInfo.STATE_STOPPED_ERROR);
588 wsInfo.appendProgressText(j.getJobnum(), OutputHeader + "\n"
589 + j.result.getStatus()
590 + "\nInvalid JNet job result data!\n" + e.getMessage());
591 j.result.setBroken(true);
605 af = new AlignFrame((Alignment) res[0],
606 (ColumnSelection) res[1], AlignFrame.DEFAULT_WIDTH,
607 AlignFrame.DEFAULT_HEIGHT);
611 af = new AlignFrame((Alignment) res[0],
612 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
618 * java.lang.Object[] alandcolsel =
619 * input.getAlignmentAndColumnSelection
620 * (alignFrame.getViewport().getGapCharacter()); if
621 * (((SequenceI[])alandcolsel[0])[0].getLength()!=res.getWidth()) {
622 * if (msa) { throw new Error("Implementation Error! ColumnSelection
623 * from input alignment will not map to result alignment!"); } } if
624 * (!msa) { // update hidden regions to account for loss of gaps in
625 * profile. - if any // gapMap returns insert list, interpreted as
626 * delete list by pruneDeletions //((ColumnSelection)
627 * alandcolsel[1]).pruneDeletions(ShiftList.parseMap(((SequenceI[])
628 * alandcolsel[0])[0].gapMap())); }
631 af = new AlignFrame((Alignment) res[0],
632 (ColumnSelection) res[1], AlignFrame.DEFAULT_WIDTH,
633 AlignFrame.DEFAULT_HEIGHT);
635 Desktop.addInternalFrame(af, altitle, AlignFrame.DEFAULT_WIDTH,
636 AlignFrame.DEFAULT_HEIGHT);
640 Cache.log.info("Append results onto existing alignment.");
646 public void pollJob(AWsJob job) throws Exception
648 ((JPredJob) job).result = server.getresult(job.getJobId());
651 public boolean isCancellable()
656 public void cancelJob()
658 throw new Error("Implementation error!");
661 public boolean canMergeResults()