2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
\r
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
\r
5 * This file is part of Jalview.
\r
7 * Jalview is free software: you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
\r
11 * Jalview is distributed in the hope that it will be useful, but
\r
12 * WITHOUT ANY WARRANTY; without even the implied warranty
\r
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
14 * PURPOSE. See the GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
\r
22 import jalview.analysis.*;
\r
23 import jalview.bin.*;
\r
24 import jalview.datamodel.*;
\r
25 import jalview.datamodel.Alignment;
\r
26 import jalview.gui.*;
\r
27 import jalview.io.*;
\r
28 import jalview.util.*;
\r
29 import vamsas.objects.simple.JpredResult;
\r
31 class JPredThread extends WSThread implements WSClientI
\r
33 // TODO: put mapping between JPredJob input and input data here -
\r
34 // JNetAnnotation adding is done after result parsing.
\r
35 class JPredJob extends WSThread.WSJob
\r
37 // TODO: make JPredJob deal only with what was sent to and received from a
\r
39 int[] predMap = null; // mapping from sequence(i) to the original
\r
41 // sequence(predMap[i]) being predicted on
\r
43 vamsas.objects.simple.Sequence sequence;
\r
45 vamsas.objects.simple.Msfalignment msa;
\r
47 java.util.Hashtable SequenceInfo = null;
\r
49 int msaIndex = 0; // the position of the original sequence in the array of
\r
51 // Sequences in the input object that this job holds a
\r
56 * @return true if getResultSet will return a valid alignment and prediction
\r
59 public boolean hasResults()
\r
61 if (subjobComplete && result != null && result.isFinished()
\r
62 && ((JpredResult) result).getPredfile() != null
\r
63 && ((JpredResult) result).getAligfile() != null)
\r
70 boolean hasValidInput()
\r
72 if (sequence != null)
\r
81 * @return null or Object[] { annotated alignment for this prediction,
\r
82 * ColumnSelection for this prediction} or null if no results
\r
86 public Object[] getResultSet() throws Exception
\r
88 if (result == null || !result.isFinished())
\r
92 Alignment al = null;
\r
93 ColumnSelection alcsel = null;
\r
94 int FirstSeq = -1; // the position of the query sequence in Alignment al
\r
96 JpredResult result = (JpredResult) this.result;
\r
98 jalview.bin.Cache.log.debug("Parsing output from JNet job.");
\r
99 // JPredFile prediction = new JPredFile("C:/JalviewX/files/jpred.txt",
\r
101 jalview.io.JPredFile prediction = new jalview.io.JPredFile(result
\r
102 .getPredfile(), "Paste");
\r
103 SequenceI[] preds = prediction.getSeqsAsArray();
\r
104 jalview.bin.Cache.log.debug("Got prediction profile.");
\r
106 if ((this.msa != null) && (result.getAligfile() != null))
\r
108 jalview.bin.Cache.log.debug("Getting associated alignment.");
\r
109 // we ignore the returned alignment if we only predicted on a single
\r
111 String format = new jalview.io.IdentifyFile().Identify(result
\r
112 .getAligfile(), "Paste");
\r
114 if (jalview.io.FormatAdapter.isValidFormat(format))
\r
117 if (predMap != null)
\r
119 Object[] alandcolsel = input
\r
120 .getAlignmentAndColumnSelection(getGapChar());
\r
121 sqs = (SequenceI[]) alandcolsel[0];
\r
122 al = new Alignment(sqs);
\r
123 alcsel = (ColumnSelection) alandcolsel[1];
\r
127 al = new FormatAdapter().readFile(result.getAligfile(),
\r
129 sqs = new SequenceI[al.getHeight()];
\r
131 for (int i = 0, j = al.getHeight(); i < j; i++)
\r
133 sqs[i] = al.getSequenceAt(i);
\r
135 if (!jalview.analysis.SeqsetUtils.deuniquify(
\r
136 (Hashtable) SequenceInfo, sqs))
\r
138 throw (new Exception(
\r
139 "Couldn't recover sequence properties for alignment."));
\r
143 al.setDataset(null);
\r
145 jalview.io.JnetAnnotationMaker.add_annotation(prediction, al,
\r
146 FirstSeq, false, predMap);
\r
151 throw (new Exception("Unknown format " + format
\r
152 + " for file : \n" + result.getAligfile()));
\r
157 al = new Alignment(preds);
\r
158 FirstSeq = prediction.getQuerySeqPosition();
\r
159 if (predMap != null)
\r
161 char gc = getGapChar();
\r
162 SequenceI[] sqs = (SequenceI[]) ((java.lang.Object[]) input
\r
163 .getAlignmentAndColumnSelection(gc))[0];
\r
164 if (this.msaIndex >= sqs.length)
\r
167 "Implementation Error! Invalid msaIndex for JPredJob on parent MSA input object!");
\r
171 // Uses RemoveGapsCommand
\r
173 new jalview.commands.RemoveGapsCommand("Remove Gaps",
\r
175 { sqs[msaIndex] }, currentView);
\r
177 SequenceI profileseq = al.getSequenceAt(FirstSeq);
\r
178 profileseq.setSequence(sqs[msaIndex].getSequenceAsString());
\r
181 if (!jalview.analysis.SeqsetUtils.SeqCharacterUnhash(al
\r
182 .getSequenceAt(FirstSeq), SequenceInfo))
\r
184 throw (new Exception(
\r
185 "Couldn't recover sequence properties for JNet Query sequence!"));
\r
189 al.setDataset(null);
\r
190 jalview.io.JnetAnnotationMaker.add_annotation(prediction, al,
\r
191 FirstSeq, true, predMap);
\r
192 SequenceI profileseq = al.getSequenceAt(0); // this includes any gaps.
\r
193 alignToProfileSeq(al, profileseq);
\r
194 if (predMap != null)
\r
196 // Adjust input view for gaps
\r
197 // propagate insertions into profile
\r
198 alcsel = propagateInsertions(profileseq, al, input);
\r
202 return new Object[]
\r
203 { al, alcsel }; // , FirstSeq, noMsa};
\r
207 * Given an alignment where all other sequences except profileseq are
\r
208 * aligned to the ungapped profileseq, insert gaps in the other sequences to
\r
209 * realign them with the residues in profileseq
\r
212 * @param profileseq
\r
214 private void alignToProfileSeq(Alignment al, SequenceI profileseq)
\r
216 char gc = al.getGapCharacter();
\r
217 int[] gapMap = profileseq.gapMap();
\r
218 // insert gaps into profile
\r
219 for (int lp = 0, r = 0; r < gapMap.length; r++)
\r
221 if (gapMap[r] - lp > 1)
\r
223 StringBuffer sb = new StringBuffer();
\r
224 for (int s = 0, ns = gapMap[r] - lp; s < ns; s++)
\r
228 for (int s = 1, ns = al.getHeight(); s < ns; s++)
\r
230 String sq = al.getSequenceAt(s).getSequenceAsString();
\r
231 int diff = gapMap[r] - sq.length();
\r
236 while ((diff = gapMap[r] - sq.length()) > 0)
\r
239 + ((diff >= sb.length()) ? sb.toString() : sb
\r
240 .substring(0, diff));
\r
242 al.getSequenceAt(s).setSequence(sq);
\r
246 al.getSequenceAt(s).setSequence(
\r
247 sq.substring(0, gapMap[r]) + sb.toString()
\r
248 + sq.substring(gapMap[r]));
\r
257 * Add gaps into the sequences aligned to profileseq under the given
\r
260 * @param profileseq
\r
264 private ColumnSelection propagateInsertions(SequenceI profileseq,
\r
265 Alignment al, AlignmentView input)
\r
267 char gc = al.getGapCharacter();
\r
268 Object[] alandcolsel = input.getAlignmentAndColumnSelection(gc);
\r
269 ColumnSelection nview = (ColumnSelection) alandcolsel[1];
\r
271 nview.pruneDeletions(ShiftList
\r
272 .parseMap((origseq = ((SequenceI[]) alandcolsel[0])[0])
\r
273 .gapMap())); // recover original prediction sequence's
\r
274 // mapping to view.
\r
275 int[] viscontigs = nview.getVisibleContigs(0, profileseq.getLength());
\r
278 // input.pruneDeletions(ShiftList.parseMap(((SequenceI[])
\r
279 // alandcolsel[0])[0].gapMap()))
\r
280 // add profile to visible contigs
\r
281 for (int v = 0; v < viscontigs.length; v += 2)
\r
283 if (viscontigs[v] > spos)
\r
285 StringBuffer sb = new StringBuffer();
\r
286 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
\r
290 for (int s = 0, ns = al.getHeight(); s < ns; s++)
\r
292 SequenceI sqobj = al.getSequenceAt(s);
\r
293 if (sqobj != profileseq)
\r
295 String sq = al.getSequenceAt(s).getSequenceAsString();
\r
296 if (sq.length() <= spos + offset)
\r
299 int diff = spos + offset - sq.length() - 1;
\r
304 while ((diff = spos + offset - sq.length() - 1) > 0)
\r
307 + ((diff >= sb.length()) ? sb.toString() : sb
\r
308 .substring(0, diff));
\r
311 sq += sb.toString();
\r
315 al.getSequenceAt(s).setSequence(
\r
316 sq.substring(0, spos + offset) + sb.toString()
\r
317 + sq.substring(spos + offset));
\r
321 // offset+=sb.length();
\r
323 spos = viscontigs[v + 1] + 1;
\r
325 if ((offset + spos) < profileseq.getLength())
\r
327 StringBuffer sb = new StringBuffer();
\r
328 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
\r
332 for (int s = 1, ns = al.getHeight(); s < ns; s++)
\r
334 String sq = al.getSequenceAt(s).getSequenceAsString();
\r
336 int diff = origseq.getLength() - sq.length();
\r
340 + ((diff >= sb.length()) ? sb.toString() : sb
\r
341 .substring(0, diff));
\r
342 diff = origseq.getLength() - sq.length();
\r
349 public JPredJob(Hashtable SequenceInfo, SequenceI seq, int[] delMap)
\r
352 this.predMap = delMap;
\r
353 String sq = AlignSeq.extractGaps(Comparison.GapChars, seq
\r
354 .getSequenceAsString());
\r
355 if (sq.length() >= 20)
\r
357 this.SequenceInfo = SequenceInfo;
\r
358 sequence = new vamsas.objects.simple.Sequence();
\r
359 sequence.setId(seq.getName());
\r
360 sequence.setSeq(sq);
\r
364 public JPredJob(Hashtable SequenceInfo, SequenceI[] msf, int[] delMap)
\r
366 this(SequenceInfo, msf[0], delMap);
\r
367 if (sequence != null)
\r
369 if (msf.length > 1)
\r
371 msa = new vamsas.objects.simple.Msfalignment();
\r
372 jalview.io.PileUpfile pileup = new jalview.io.PileUpfile();
\r
373 msa.setMsf(pileup.print(msf));
\r
379 ext.vamsas.Jpred server;
\r
381 String altitle = "";
\r
383 JPredThread(WebserviceInfo wsinfo, String altitle,
\r
384 ext.vamsas.Jpred server, String wsurl, AlignmentView alview,
\r
385 AlignFrame alframe)
\r
387 super(alframe, wsinfo, alview, wsurl);
\r
388 this.altitle = altitle;
\r
389 this.server = server;
\r
392 JPredThread(WebserviceInfo wsinfo, String altitle,
\r
393 ext.vamsas.Jpred server, String wsurl, Hashtable SequenceInfo,
\r
394 SequenceI seq, int[] delMap, AlignmentView alview,
\r
395 AlignFrame alframe)
\r
397 this(wsinfo, altitle, server, wsurl, alview, alframe);
\r
398 JPredJob job = new JPredJob(SequenceInfo, seq, delMap);
\r
399 if (job.hasValidInput())
\r
401 OutputHeader = wsInfo.getProgressText();
\r
408 JPredThread(WebserviceInfo wsinfo, String altitle,
\r
409 ext.vamsas.Jpred server, Hashtable SequenceInfo, SequenceI[] msf,
\r
410 int[] delMap, AlignmentView alview, AlignFrame alframe,
\r
413 this(wsinfo, altitle, server, wsurl, alview, alframe);
\r
414 JPredJob job = new JPredJob(SequenceInfo, msf, delMap);
\r
415 if (job.hasValidInput())
\r
419 OutputHeader = wsInfo.getProgressText();
\r
424 void StartJob(WSJob j)
\r
426 if (!(j instanceof JPredJob))
\r
429 "Implementation error - StartJob(JpredJob) called on "
\r
434 JPredJob job = (JPredJob) j;
\r
435 if (job.msa != null)
\r
437 job.jobId = server.predictOnMsa(job.msa);
\r
439 else if (job.sequence != null)
\r
441 job.jobId = server.predict(job.sequence); // debug like : job.jobId =
\r
442 // "/jobs/www-jpred/jp_Yatat29";//
\r
445 if (job.jobId != null)
\r
447 if (job.jobId.startsWith("Broken"))
\r
449 job.result = (vamsas.objects.simple.Result) new JpredResult();
\r
450 job.result.setInvalid(true);
\r
451 job.result.setStatus("Submission " + job.jobId);
\r
452 throw new Exception(job.jobId);
\r
456 job.submitted = true;
\r
457 job.subjobComplete = false;
\r
458 Cache.log.info(WsUrl + " Job Id '" + job.jobId + "'");
\r
463 throw new Exception("Server timed out - try again later\n");
\r
465 } catch (Exception e)
\r
467 // kill the whole job.
\r
468 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
469 if (e.getMessage().indexOf("Exception") > -1)
\r
472 .setStatus(j.jobnum,
\r
473 WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
477 "Failed to submit the prediction. (Just close the window)\n"
\r
478 + "It is most likely that there is a problem with the server.\n");
\r
480 .println("JPredWS Client: Failed to submit the prediction. Quite possibly because of a server error - see below)\n"
\r
481 + e.getMessage() + "\n");
\r
483 jalview.bin.Cache.log.warn("Server Exception", e);
\r
487 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
\r
488 // JBPNote - this could be a popup informing the user of the problem.
\r
489 wsInfo.appendProgressText(j.jobnum,
\r
490 "Failed to submit the prediction:\n" + e.getMessage()
\r
491 + wsInfo.getProgressText());
\r
493 jalview.bin.Cache.log.debug("Failed Submission of job " + j.jobnum,
\r
497 j.allowedServerExceptions = -1;
\r
498 j.subjobComplete = true;
\r
504 int results = 0; // number of result sets received
\r
505 JobStateSummary finalState = new JobStateSummary();
\r
508 for (int j = 0; j < jobs.length; j++)
\r
510 finalState.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
\r
511 if (jobs[j].submitted && jobs[j].subjobComplete
\r
512 && jobs[j].hasResults())
\r
517 } catch (Exception ex)
\r
520 Cache.log.error("Unexpected exception when processing results for "
\r
522 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
526 wsInfo.showResultsNewFrame
\r
527 .addActionListener(new java.awt.event.ActionListener()
\r
529 public void actionPerformed(java.awt.event.ActionEvent evt)
\r
531 displayResults(true);
\r
534 wsInfo.mergeResults
\r
535 .addActionListener(new java.awt.event.ActionListener()
\r
537 public void actionPerformed(java.awt.event.ActionEvent evt)
\r
539 displayResults(false);
\r
542 wsInfo.setResultsReady();
\r
546 wsInfo.setFinishedNoResults();
\r
550 void displayResults(boolean newWindow)
\r
552 // TODO: cope with multiple subjobs.
\r
555 Object[] res = null;
\r
556 boolean msa = false;
\r
557 for (int jn = 0; jn < jobs.length; jn++)
\r
559 Object[] jobres = null;
\r
560 JPredJob j = (JPredJob) jobs[jn];
\r
562 if (j.hasResults())
\r
564 // hack - we only deal with all single seuqence predictions or all
\r
565 // profile predictions
\r
566 msa = (j.msa != null) ? true : msa;
\r
569 jalview.bin.Cache.log.debug("Parsing output of job " + jn);
\r
570 jobres = j.getResultSet();
\r
571 jalview.bin.Cache.log.debug("Finished parsing output.");
\r
572 if (jobs.length == 1)
\r
578 // do merge with other job results
\r
580 "Multiple JNet subjob merging not yet implemented.");
\r
582 } catch (Exception e)
\r
584 jalview.bin.Cache.log.error(
\r
585 "JNet Client: JPred Annotation Parse Error", e);
\r
586 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
\r
587 wsInfo.appendProgressText(j.jobnum, OutputHeader + "\n"
\r
588 + j.result.getStatus()
\r
589 + "\nInvalid JNet job result data!\n" + e.getMessage());
\r
590 j.result.setBroken(true);
\r
602 if (res[1] != null)
\r
604 af = new AlignFrame((Alignment) res[0],
\r
605 (ColumnSelection) res[1], AlignFrame.DEFAULT_WIDTH,
\r
606 AlignFrame.DEFAULT_HEIGHT);
\r
610 af = new AlignFrame((Alignment) res[0],
\r
611 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
\r
617 * java.lang.Object[] alandcolsel =
\r
618 * input.getAlignmentAndColumnSelection
\r
619 * (alignFrame.getViewport().getGapCharacter()); if
\r
620 * (((SequenceI[])alandcolsel[0])[0].getLength()!=res.getWidth()) {
\r
621 * if (msa) { throw new Error("Implementation Error! ColumnSelection
\r
622 * from input alignment will not map to result alignment!"); } } if
\r
623 * (!msa) { // update hidden regions to account for loss of gaps in
\r
624 * profile. - if any // gapMap returns insert list, interpreted as
\r
625 * delete list by pruneDeletions //((ColumnSelection)
\r
626 * alandcolsel[1]).pruneDeletions(ShiftList.parseMap(((SequenceI[])
\r
627 * alandcolsel[0])[0].gapMap())); }
\r
630 af = new AlignFrame((Alignment) res[0],
\r
631 (ColumnSelection) res[1], AlignFrame.DEFAULT_WIDTH,
\r
632 AlignFrame.DEFAULT_HEIGHT);
\r
634 Desktop.addInternalFrame(af, altitle, AlignFrame.DEFAULT_WIDTH,
\r
635 AlignFrame.DEFAULT_HEIGHT);
\r
639 Cache.log.info("Append results onto existing alignment.");
\r
645 void pollJob(WSJob job) throws Exception
\r
647 job.result = server.getresult(job.jobId);
\r
650 public boolean isCancellable()
\r
655 public void cancelJob()
\r
657 throw new Error("Implementation error!");
\r
660 public boolean canMergeResults()
\r