2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.jws1;
23 import jalview.analysis.AlignSeq;
24 import jalview.analysis.SeqsetUtils;
25 import jalview.bin.Cache;
26 import jalview.datamodel.Alignment;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.AlignmentView;
30 import jalview.datamodel.ColumnSelection;
31 import jalview.datamodel.SequenceI;
32 import jalview.gui.AlignFrame;
33 import jalview.gui.Desktop;
34 import jalview.gui.WebserviceInfo;
35 import jalview.io.DataSourceType;
36 import jalview.io.FileFormatI;
37 import jalview.io.FormatAdapter;
38 import jalview.io.IdentifyFile;
39 import jalview.io.JPredFile;
40 import jalview.io.JnetAnnotationMaker;
41 import jalview.util.Comparison;
42 import jalview.util.MessageManager;
43 import jalview.ws.AWsJob;
44 import jalview.ws.JobStateSummary;
45 import jalview.ws.WSClientI;
47 import java.util.Hashtable;
48 import java.util.List;
50 import vamsas.objects.simple.JpredResult;
52 class JPredThread extends JWS1Thread implements WSClientI
54 // TODO: put mapping between JPredJob input and input data here -
55 // JNetAnnotation adding is done after result parsing.
56 class JPredJob extends WSJob
58 // TODO: make JPredJob deal only with what was sent to and received from a
60 int[] predMap = null; // mapping from sequence(i) to the original
62 // sequence(predMap[i]) being predicted on
64 vamsas.objects.simple.Sequence sequence;
66 vamsas.objects.simple.Msfalignment msa;
68 java.util.Hashtable SequenceInfo = null;
70 int msaIndex = 0; // the position of the original sequence in the array of
72 // Sequences in the input object that this job holds a
77 * @return true if getResultSet will return a valid alignment and prediction
81 public boolean hasResults()
83 if (subjobComplete && result != null && result.isFinished()
84 && ((JpredResult) result).getPredfile() != null
85 && ((JpredResult) result).getAligfile() != null)
93 public boolean hasValidInput()
104 * @return null or Object[] { annotated alignment for this prediction,
105 * ColumnSelection for this prediction} or null if no results
109 public Object[] getResultSet() throws Exception
111 if (result == null || !result.isFinished())
115 AlignmentI al = null;
116 ColumnSelection alcsel = null;
117 int FirstSeq = -1; // the position of the query sequence in Alignment al
119 JpredResult result = (JpredResult) this.result;
121 Cache.log.debug("Parsing output from JNet job.");
122 // JPredFile prediction = new JPredFile("C:/JalviewX/files/jpred.txt",
124 JPredFile prediction = new JPredFile(
125 result.getPredfile(), DataSourceType.PASTE);
126 SequenceI[] preds = prediction.getSeqsAsArray();
127 Cache.log.debug("Got prediction profile.");
129 if ((this.msa != null) && (result.getAligfile() != null))
131 Cache.log.debug("Getting associated alignment.");
132 // we ignore the returned alignment if we only predicted on a single
134 FileFormatI format = new IdentifyFile().identify(
135 result.getAligfile(), DataSourceType.PASTE);
142 Object[] alandcolsel = input
143 .getAlignmentAndColumnSelection(getGapChar());
144 sqs = (SequenceI[]) alandcolsel[0];
145 al = new Alignment(sqs);
146 alcsel = (ColumnSelection) alandcolsel[1];
150 al = new FormatAdapter().readFile(result.getAligfile(),
151 DataSourceType.PASTE, format);
152 sqs = new SequenceI[al.getHeight()];
154 for (int i = 0, j = al.getHeight(); i < j; i++)
156 sqs[i] = al.getSequenceAt(i);
158 if (!SeqsetUtils.deuniquify(SequenceInfo, sqs))
160 throw (new Exception(
162 .getString("exception.couldnt_recover_sequence_properties_for_alignment")));
166 if (currentView.getDataset() != null)
168 al.setDataset(currentView.getDataset());
175 JnetAnnotationMaker.add_annotation(prediction, al,
176 FirstSeq, false, predMap);
181 throw (new Exception(MessageManager.formatMessage(
182 "exception.unknown_format_for_file", new String[] {
183 format.toString(), result.getAligfile() })));
188 al = new Alignment(preds);
189 FirstSeq = prediction.getQuerySeqPosition();
192 char gc = getGapChar();
193 SequenceI[] sqs = (SequenceI[]) input
194 .getAlignmentAndColumnSelection(gc)[0];
195 if (this.msaIndex >= sqs.length)
199 .getString("error.implementation_error_invalid_msa_index_for_job"));
203 // Uses RemoveGapsCommand
205 new jalview.commands.RemoveGapsCommand(
206 MessageManager.getString("label.remove_gaps"),
207 new SequenceI[] { sqs[msaIndex] }, currentView);
209 SequenceI profileseq = al.getSequenceAt(FirstSeq);
210 profileseq.setSequence(sqs[msaIndex].getSequenceAsString());
213 if (!jalview.analysis.SeqsetUtils.SeqCharacterUnhash(
214 al.getSequenceAt(FirstSeq), SequenceInfo))
216 throw (new Exception(
218 .getString("exception.couldnt_recover_sequence_props_for_jnet_query")));
222 if (currentView.getDataset() != null)
224 al.setDataset(currentView.getDataset());
231 jalview.io.JnetAnnotationMaker.add_annotation(prediction, al,
232 FirstSeq, true, predMap);
233 SequenceI profileseq = al.getSequenceAt(0); // this includes any gaps.
234 alignToProfileSeq(al, profileseq);
237 // Adjust input view for gaps
238 // propagate insertions into profile
239 alcsel = ColumnSelection.propagateInsertions(profileseq, al,
244 // transfer to dataset
245 for (AlignmentAnnotation alant : al.getAlignmentAnnotation())
247 if (alant.sequenceRef != null)
249 replaceAnnotationOnAlignmentWith(alant, alant.label,
250 "jalview.jws1.Jpred" + (this.msa == null ? "" : "MSA"),
254 return new Object[] { al, alcsel }; // , FirstSeq, noMsa};
258 * copied from JabawsCalcWorker
265 protected void replaceAnnotationOnAlignmentWith(
266 AlignmentAnnotation newAnnot, String typeName, String calcId,
269 SequenceI dsseq = aSeq.getDatasetSequence();
270 while (dsseq.getDatasetSequence() != null)
272 dsseq = dsseq.getDatasetSequence();
274 // look for same annotation on dataset and lift this one over
275 List<AlignmentAnnotation> dsan = dsseq.getAlignmentAnnotations(
277 if (dsan != null && dsan.size() > 0)
279 for (AlignmentAnnotation dssan : dsan)
281 dsseq.removeAlignmentAnnotation(dssan);
284 AlignmentAnnotation dssan = new AlignmentAnnotation(newAnnot);
285 dsseq.addAlignmentAnnotation(dssan);
286 dssan.adjustForAlignment();
290 * Given an alignment where all other sequences except profileseq are
291 * aligned to the ungapped profileseq, insert gaps in the other sequences to
292 * realign them with the residues in profileseq
297 private void alignToProfileSeq(AlignmentI al, SequenceI profileseq)
299 char gc = al.getGapCharacter();
300 int[] gapMap = profileseq.gapMap();
301 // insert gaps into profile
302 for (int lp = 0, r = 0; r < gapMap.length; r++)
304 if (gapMap[r] - lp > 1)
306 StringBuffer sb = new StringBuffer();
307 for (int s = 0, ns = gapMap[r] - lp; s < ns; s++)
311 for (int s = 1, ns = al.getHeight(); s < ns; s++)
313 String sq = al.getSequenceAt(s).getSequenceAsString();
314 int diff = gapMap[r] - sq.length();
319 while ((diff = gapMap[r] - sq.length()) > 0)
322 + ((diff >= sb.length()) ? sb.toString() : sb
323 .substring(0, diff));
325 al.getSequenceAt(s).setSequence(sq);
329 al.getSequenceAt(s).setSequence(
330 sq.substring(0, gapMap[r]) + sb.toString()
331 + sq.substring(gapMap[r]));
339 public JPredJob(Hashtable SequenceInfo, SequenceI seq, int[] delMap)
342 this.predMap = delMap;
343 String sq = AlignSeq.extractGaps(Comparison.GapChars,
344 seq.getSequenceAsString());
345 if (sq.length() >= 20)
347 this.SequenceInfo = SequenceInfo;
348 sequence = new vamsas.objects.simple.Sequence();
349 sequence.setId(seq.getName());
354 errorMessage = "Sequence is too short to predict with JPred - need at least 20 amino acids.";
358 public JPredJob(Hashtable SequenceInfo, SequenceI[] msf, int[] delMap)
360 this(SequenceInfo, msf[0], delMap);
361 if (sequence != null)
365 msa = new vamsas.objects.simple.Msfalignment();
366 jalview.io.PileUpfile pileup = new jalview.io.PileUpfile();
367 msa.setMsf(pileup.print(msf));
372 String errorMessage = "";
374 public String getValidationMessages()
376 return errorMessage + "\n";
380 ext.vamsas.Jpred server;
384 JPredThread(WebserviceInfo wsinfo, String altitle,
385 ext.vamsas.Jpred server, String wsurl, AlignmentView alview,
388 super(alframe, wsinfo, alview, wsurl);
389 this.altitle = altitle;
390 this.server = server;
393 JPredThread(WebserviceInfo wsinfo, String altitle,
394 ext.vamsas.Jpred server, String wsurl, Hashtable SequenceInfo,
395 SequenceI seq, int[] delMap, AlignmentView alview,
398 this(wsinfo, altitle, server, wsurl, alview, alframe);
399 JPredJob job = new JPredJob(SequenceInfo, seq, delMap);
400 if (job.hasValidInput())
402 OutputHeader = wsInfo.getProgressText();
403 jobs = new WSJob[] { job };
408 wsInfo.appendProgressText(job.getValidationMessages());
412 JPredThread(WebserviceInfo wsinfo, String altitle,
413 ext.vamsas.Jpred server, Hashtable SequenceInfo, SequenceI[] msf,
414 int[] delMap, AlignmentView alview, AlignFrame alframe,
417 this(wsinfo, altitle, server, wsurl, alview, alframe);
418 JPredJob job = new JPredJob(SequenceInfo, msf, delMap);
419 if (job.hasValidInput())
421 jobs = new WSJob[] { job };
422 OutputHeader = wsInfo.getProgressText();
427 wsInfo.appendProgressText(job.getValidationMessages());
432 public void StartJob(AWsJob j)
434 if (!(j instanceof JPredJob))
436 throw new Error(MessageManager.formatMessage(
437 "error.implementation_error_startjob_called",
438 new String[] { j.getClass().toString() }));
442 JPredJob job = (JPredJob) j;
445 job.setJobId(server.predictOnMsa(job.msa));
447 else if (job.sequence != null)
449 job.setJobId(server.predict(job.sequence)); // debug like : job.jobId =
450 // "/jobs/www-jpred/jp_Yatat29";//
453 if (job.getJobId() != null)
455 if (job.getJobId().startsWith("Broken"))
457 job.result = new JpredResult();
458 job.result.setInvalid(true);
459 job.result.setStatus(MessageManager.formatMessage(
460 "label.submission_params", new String[] { job.getJobId()
462 throw new Exception(job.getJobId());
466 job.setSubmitted(true);
467 job.setSubjobComplete(false);
468 Cache.log.info(WsUrl + " Job Id '" + job.getJobId() + "'");
475 .getString("exception.server_timeout_try_later"));
477 } catch (Exception e)
479 // kill the whole job.
480 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
481 if (e.getMessage().indexOf("Exception") > -1)
483 wsInfo.setStatus(j.getJobnum(),
484 WebserviceInfo.STATE_STOPPED_SERVERERROR);
485 wsInfo.setProgressText(
487 "Failed to submit the prediction. (Just close the window)\n"
488 + "It is most likely that there is a problem with the server.\n");
490 .println("JPredWS Client: Failed to submit the prediction. Quite possibly because of a server error - see below)\n"
491 + e.getMessage() + "\n");
493 jalview.bin.Cache.log.warn("Server Exception", e);
497 wsInfo.setStatus(j.getJobnum(), WebserviceInfo.STATE_STOPPED_ERROR);
498 // JBPNote - this could be a popup informing the user of the problem.
499 wsInfo.appendProgressText(j.getJobnum(), MessageManager
501 "info.failed_to_submit_prediction",
502 new String[] { e.getMessage(),
503 wsInfo.getProgressText() }));
505 jalview.bin.Cache.log.debug(
506 "Failed Submission of job " + j.getJobnum(), e);
509 j.setAllowedServerExceptions(-1);
510 j.setSubjobComplete(true);
515 public void parseResult()
517 int results = 0; // number of result sets received
518 JobStateSummary finalState = new JobStateSummary();
521 for (int j = 0; j < jobs.length; j++)
523 finalState.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
524 if (jobs[j].isSubmitted() && jobs[j].isSubjobComplete()
525 && jobs[j].hasResults())
530 } catch (Exception ex)
533 Cache.log.error("Unexpected exception when processing results for "
535 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
539 wsInfo.showResultsNewFrame
540 .addActionListener(new java.awt.event.ActionListener()
543 public void actionPerformed(java.awt.event.ActionEvent evt)
545 displayResults(true);
549 .addActionListener(new java.awt.event.ActionListener()
552 public void actionPerformed(java.awt.event.ActionEvent evt)
554 displayResults(false);
557 wsInfo.setResultsReady();
561 wsInfo.setStatus(wsInfo.STATE_STOPPED_ERROR);
562 wsInfo.appendInfoText("No jobs ran.");
563 wsInfo.setFinishedNoResults();
567 void displayResults(boolean newWindow)
569 // TODO: cope with multiple subjobs.
574 for (int jn = 0; jn < jobs.length; jn++)
576 Object[] jobres = null;
577 JPredJob j = (JPredJob) jobs[jn];
581 // hack - we only deal with all single seuqence predictions or all
582 // profile predictions
583 msa = (j.msa != null) ? true : msa;
586 jalview.bin.Cache.log.debug("Parsing output of job " + jn);
587 jobres = j.getResultSet();
588 jalview.bin.Cache.log.debug("Finished parsing output.");
589 if (jobs.length == 1)
595 // do merge with other job results
598 .getString("error.multiple_jnet_subjob_merge_not_implemented"));
600 } catch (Exception e)
602 jalview.bin.Cache.log.error(
603 "JNet Client: JPred Annotation Parse Error", e);
604 wsInfo.setStatus(j.getJobnum(),
605 WebserviceInfo.STATE_STOPPED_ERROR);
606 wsInfo.appendProgressText(j.getJobnum(), MessageManager
607 .formatMessage("info.invalid_jnet_job_result_data",
608 new String[] { OutputHeader.toString(),
609 j.result.getStatus(), e.getMessage() }));
610 j.result.setBroken(true);
620 ((AlignmentI) res[0]).setSeqrep(((AlignmentI) res[0])
626 af = new AlignFrame((Alignment) res[0],
627 (ColumnSelection) res[1], AlignFrame.DEFAULT_WIDTH,
628 AlignFrame.DEFAULT_HEIGHT);
632 af = new AlignFrame((Alignment) res[0],
633 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
639 * java.lang.Object[] alandcolsel =
640 * input.getAlignmentAndColumnSelection
641 * (alignFrame.getViewport().getGapCharacter()); if
642 * (((SequenceI[])alandcolsel[0])[0].getLength()!=res.getWidth()) {
643 * if (msa) { throw new Error("Implementation Error! ColumnSelection
644 * from input alignment will not map to result alignment!"); } } if
645 * (!msa) { // update hidden regions to account for loss of gaps in
646 * profile. - if any // gapMap returns insert list, interpreted as
647 * delete list by pruneDeletions //((ColumnSelection)
648 * alandcolsel[1]).pruneDeletions(ShiftList.parseMap(((SequenceI[])
649 * alandcolsel[0])[0].gapMap())); }
652 af = new AlignFrame((Alignment) res[0],
653 (ColumnSelection) res[1], AlignFrame.DEFAULT_WIDTH,
654 AlignFrame.DEFAULT_HEIGHT);
656 Desktop.addInternalFrame(af, altitle, AlignFrame.DEFAULT_WIDTH,
657 AlignFrame.DEFAULT_HEIGHT);
661 Cache.log.info("Append results onto existing alignment.");
668 public void pollJob(AWsJob job) throws Exception
670 ((JPredJob) job).result = server.getresult(job.getJobId());
674 public boolean isCancellable()
680 public void cancelJob()
682 throw new Error(MessageManager.getString("error.implementation_error"));
686 public boolean canMergeResults()