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.analysis.SeqsetUtils.SequenceInfo;
26 import jalview.bin.Cache;
27 import jalview.datamodel.Alignment;
28 import jalview.datamodel.AlignmentAnnotation;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.AlignmentView;
31 import jalview.datamodel.HiddenColumns;
32 import jalview.datamodel.SequenceI;
33 import jalview.gui.AlignFrame;
34 import jalview.gui.Desktop;
35 import jalview.gui.WebserviceInfo;
36 import jalview.io.DataSourceType;
37 import jalview.io.FileFormatI;
38 import jalview.io.FormatAdapter;
39 import jalview.io.IdentifyFile;
40 import jalview.io.JPredFile;
41 import jalview.io.JnetAnnotationMaker;
42 import jalview.io.PileUpfile;
43 import jalview.util.Comparison;
44 import jalview.util.MessageManager;
45 import jalview.ws.AWsJob;
46 import jalview.ws.JobStateSummary;
47 import jalview.ws.WSClientI;
49 import java.util.Hashtable;
50 import java.util.List;
53 import vamsas.objects.simple.JpredResult;
55 class JPredThread extends JWS1Thread implements WSClientI
57 // TODO: put mapping between JPredJob input and input data here -
58 // JNetAnnotation adding is done after result parsing.
59 class JPredJob extends WSJob
61 // TODO: make JPredJob deal only with what was sent to and received from a
63 int[] predMap = null; // mapping from sequence(i) to the original
65 // sequence(predMap[i]) being predicted on
67 vamsas.objects.simple.Sequence sequence;
69 vamsas.objects.simple.Msfalignment msa;
71 Object SequenceInfo = null;
73 int msaIndex = 0; // the position of the original sequence in the array of
75 // Sequences in the input object that this job holds a
80 * @return true if getResultSet will return a valid alignment and prediction
84 public boolean hasResults()
86 if (subjobComplete && result != null && result.isFinished()
87 && ((JpredResult) result).getPredfile() != null
88 && ((JpredResult) result).getAligfile() != null)
96 public boolean hasValidInput()
107 * @return null or Object[] { annotated alignment for this prediction,
108 * ColumnSelection for this prediction} or null if no results
112 public Object[] getResultSet() throws Exception
114 if (result == null || !result.isFinished())
118 AlignmentI al = null;
119 HiddenColumns alhidden = null;
120 int FirstSeq = -1; // the position of the query sequence in Alignment al
122 JpredResult result = (JpredResult) this.result;
124 Cache.log.debug("Parsing output from JNet job.");
125 // JPredFile prediction = new JPredFile("C:/JalviewX/files/jpred.txt",
127 JPredFile prediction = new JPredFile(result.getPredfile(),
128 DataSourceType.PASTE);
129 SequenceI[] preds = prediction.getSeqsAsArray();
130 Cache.log.debug("Got prediction profile.");
132 if ((this.msa != null) && (result.getAligfile() != null))
134 Cache.log.debug("Getting associated alignment.");
135 // we ignore the returned alignment if we only predicted on a single
137 FileFormatI format = new IdentifyFile()
138 .identify(result.getAligfile(), DataSourceType.PASTE);
145 Object[] alandcolsel = input
146 .getAlignmentAndHiddenColumns(getGapChar());
147 sqs = (SequenceI[]) alandcolsel[0];
148 al = new Alignment(sqs);
149 alhidden = (HiddenColumns) alandcolsel[1];
153 al = new FormatAdapter().readFile(result.getAligfile(),
154 DataSourceType.PASTE, format);
155 sqs = new SequenceI[al.getHeight()];
157 for (int i = 0, j = al.getHeight(); i < j; i++)
159 sqs[i] = al.getSequenceAt(i);
161 if (!SeqsetUtils.deuniquify((Map<String, SequenceInfo>)SequenceInfo, sqs))
163 throw (new Exception(MessageManager.getString(
164 "exception.couldnt_recover_sequence_properties_for_alignment")));
168 if (currentView.getDataset() != null)
170 al.setDataset(currentView.getDataset());
177 JnetAnnotationMaker.add_annotation(prediction, al, FirstSeq,
183 throw (new Exception(MessageManager.formatMessage(
184 "exception.unknown_format_for_file", new String[]
185 { "", result.getAligfile() })));
190 al = new Alignment(preds);
191 FirstSeq = prediction.getQuerySeqPosition();
194 char gc = getGapChar();
195 SequenceI[] sqs = (SequenceI[]) input
196 .getAlignmentAndHiddenColumns(gc)[0];
197 if (this.msaIndex >= sqs.length)
199 throw new Error(MessageManager.getString(
200 "error.implementation_error_invalid_msa_index_for_job"));
204 // Uses RemoveGapsCommand
206 new jalview.commands.RemoveGapsCommand(
207 MessageManager.getString("label.remove_gaps"),
209 { sqs[msaIndex] }, currentView);
211 SequenceI profileseq = al.getSequenceAt(FirstSeq);
212 profileseq.setSequence(sqs[msaIndex].getSequenceAsString());
215 if (!jalview.analysis.SeqsetUtils.SeqCharacterUnhash(
216 al.getSequenceAt(FirstSeq), (SequenceInfo)SequenceInfo))
218 throw (new Exception(MessageManager.getString(
219 "exception.couldnt_recover_sequence_props_for_jnet_query")));
223 if (currentView.getDataset() != null)
225 al.setDataset(currentView.getDataset());
232 jalview.io.JnetAnnotationMaker.add_annotation(prediction, al,
233 FirstSeq, true, predMap);
234 SequenceI profileseq = al.getSequenceAt(0); // this includes any gaps.
235 alignToProfileSeq(al, profileseq);
238 // Adjust input view for gaps
239 // propagate insertions into profile
240 alhidden = al.propagateInsertions(profileseq, input);
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, alhidden }; // , 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(calcId,
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)
321 sq = sq + ((diff >= sb.length()) ? sb.toString()
322 : sb.substring(0, diff));
324 al.getSequenceAt(s).setSequence(sq);
328 al.getSequenceAt(s).setSequence(sq.substring(0, gapMap[r])
329 + sb.toString() + sq.substring(gapMap[r]));
337 public JPredJob(Object SequenceInfo, SequenceI seq, int[] delMap)
340 this.predMap = delMap;
341 String sq = AlignSeq.extractGaps(Comparison.GapChars,
342 seq.getSequenceAsString());
343 if (sq.length() >= 20)
345 this.SequenceInfo = SequenceInfo;
346 sequence = new vamsas.objects.simple.Sequence();
347 sequence.setId(seq.getName());
352 errorMessage = "Sequence is too short to predict with JPred - need at least 20 amino acids.";
356 public JPredJob(Object SequenceInfo, SequenceI[] msf, int[] delMap)
358 this(SequenceInfo, msf[0], delMap);
359 if (sequence != null)
363 msa = new vamsas.objects.simple.Msfalignment();
364 PileUpfile pileup = new PileUpfile();
365 msa.setMsf(pileup.print(msf, true));
370 String errorMessage = "";
372 public String getValidationMessages()
374 return errorMessage + "\n";
378 ext.vamsas.Jpred server;
382 JPredThread(WebserviceInfo wsinfo, String altitle,
383 ext.vamsas.Jpred server, String wsurl, AlignmentView alview,
386 super(alframe, wsinfo, alview, wsurl);
387 this.altitle = altitle;
388 this.server = server;
391 JPredThread(WebserviceInfo wsinfo, String altitle,
392 ext.vamsas.Jpred server, String wsurl, SequenceInfo SequenceInfo,
393 SequenceI seq, int[] delMap, AlignmentView alview,
396 this(wsinfo, altitle, server, wsurl, alview, alframe);
397 JPredJob job = new JPredJob(SequenceInfo, seq, delMap);
398 if (job.hasValidInput())
400 OutputHeader = wsInfo.getProgressText();
401 jobs = new WSJob[] { job };
406 wsInfo.appendProgressText(job.getValidationMessages());
410 JPredThread(WebserviceInfo wsinfo, String altitle,
411 ext.vamsas.Jpred server, Map<String, SequenceInfo> SequenceInfo,
412 SequenceI[] msf, int[] delMap, AlignmentView alview,
413 AlignFrame alframe, String wsurl)
415 this(wsinfo, altitle, server, wsurl, alview, alframe);
416 JPredJob job = new JPredJob(SequenceInfo, msf, delMap);
417 if (job.hasValidInput())
419 jobs = new WSJob[] { job };
420 OutputHeader = wsInfo.getProgressText();
425 wsInfo.appendProgressText(job.getValidationMessages());
430 public void StartJob(AWsJob j)
432 if (!(j instanceof JPredJob))
434 throw new Error(MessageManager.formatMessage(
435 "error.implementation_error_startjob_called", new String[]
436 { j.getClass().toString() }));
440 JPredJob job = (JPredJob) j;
443 job.setJobId(server.predictOnMsa(job.msa));
445 else if (job.sequence != null)
447 job.setJobId(server.predict(job.sequence)); // debug like : job.jobId =
448 // "/jobs/www-jpred/jp_Yatat29";//
451 if (job.getJobId() != null)
453 if (job.getJobId().startsWith("Broken"))
455 job.result = new JpredResult();
456 job.result.setInvalid(true);
457 job.result.setStatus(MessageManager
458 .formatMessage("label.submission_params", new String[]
459 { job.getJobId().toString() }));
460 throw new Exception(job.getJobId());
464 job.setSubmitted(true);
465 job.setSubjobComplete(false);
466 Cache.log.info(WsUrl + " Job Id '" + job.getJobId() + "'");
471 throw new Exception(MessageManager
472 .getString("exception.server_timeout_try_later"));
474 } catch (Exception e)
476 // kill the whole job.
477 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
478 if (e.getMessage().indexOf("Exception") > -1)
480 wsInfo.setStatus(j.getJobnum(),
481 WebserviceInfo.STATE_STOPPED_SERVERERROR);
482 wsInfo.setProgressText(j.getJobnum(),
483 "Failed to submit the prediction. (Just close the window)\n"
484 + "It is most likely that there is a problem with the server.\n");
486 "JPredWS Client: Failed to submit the prediction. Quite possibly because of a server error - see below)\n"
487 + e.getMessage() + "\n");
489 jalview.bin.Cache.log.warn("Server Exception", e);
493 wsInfo.setStatus(j.getJobnum(), WebserviceInfo.STATE_STOPPED_ERROR);
494 // JBPNote - this could be a popup informing the user of the problem.
495 wsInfo.appendProgressText(j.getJobnum(),
496 MessageManager.formatMessage(
497 "info.failed_to_submit_prediction", new String[]
498 { e.getMessage(), wsInfo.getProgressText() }));
500 jalview.bin.Cache.log
501 .debug("Failed Submission of job " + j.getJobnum(), e);
504 j.setAllowedServerExceptions(-1);
505 j.setSubjobComplete(true);
510 public void parseResult()
512 int results = 0; // number of result sets received
513 JobStateSummary finalState = new JobStateSummary();
516 for (int j = 0; j < jobs.length; j++)
518 finalState.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
519 if (jobs[j].isSubmitted() && jobs[j].isSubjobComplete()
520 && jobs[j].hasResults())
525 } catch (Exception ex)
529 "Unexpected exception when processing results for " + altitle,
531 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
535 wsInfo.showResultsNewFrame
536 .addActionListener(new java.awt.event.ActionListener()
539 public void actionPerformed(java.awt.event.ActionEvent evt)
541 displayResults(true);
545 .addActionListener(new java.awt.event.ActionListener()
548 public void actionPerformed(java.awt.event.ActionEvent evt)
550 displayResults(false);
553 wsInfo.setResultsReady();
557 wsInfo.setStatus(wsInfo.STATE_STOPPED_ERROR);
558 wsInfo.appendInfoText("No jobs ran.");
559 wsInfo.setFinishedNoResults();
563 void displayResults(boolean newWindow)
565 // TODO: cope with multiple subjobs.
570 for (int jn = 0; jn < jobs.length; jn++)
572 Object[] jobres = null;
573 JPredJob j = (JPredJob) jobs[jn];
577 // hack - we only deal with all single seuqence predictions or all
578 // profile predictions
579 msa = (j.msa != null) ? true : msa;
582 jalview.bin.Cache.log.debug("Parsing output of job " + jn);
583 jobres = j.getResultSet();
584 jalview.bin.Cache.log.debug("Finished parsing output.");
585 if (jobs.length == 1)
591 // do merge with other job results
592 throw new Error(MessageManager.getString(
593 "error.multiple_jnet_subjob_merge_not_implemented"));
595 } catch (Exception e)
597 jalview.bin.Cache.log
598 .error("JNet Client: JPred Annotation Parse Error", e);
599 wsInfo.setStatus(j.getJobnum(),
600 WebserviceInfo.STATE_STOPPED_ERROR);
601 wsInfo.appendProgressText(j.getJobnum(),
602 MessageManager.formatMessage(
603 "info.invalid_jnet_job_result_data",
605 { OutputHeader.toString(), j.result.getStatus(),
607 j.result.setBroken(true);
617 ((AlignmentI) res[0])
618 .setSeqrep(((AlignmentI) res[0]).getSequenceAt(0));
623 af = new AlignFrame((Alignment) res[0],
624 (HiddenColumns) res[1], AlignFrame.DEFAULT_WIDTH,
625 AlignFrame.DEFAULT_HEIGHT);
629 af = new AlignFrame((Alignment) res[0],
630 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
636 * java.lang.Object[] alandcolsel =
637 * input.getAlignmentAndColumnSelection
638 * (alignFrame.getViewport().getGapCharacter()); if
639 * (((SequenceI[])alandcolsel[0])[0].getLength()!=res.getWidth()) {
640 * if (msa) { throw new Error("Implementation Error! ColumnSelection
641 * from input alignment will not map to result alignment!"); } } if
642 * (!msa) { // update hidden regions to account for loss of gaps in
643 * profile. - if any // gapMap returns insert list, interpreted as
644 * delete list by pruneDeletions //((ColumnSelection)
645 * alandcolsel[1]).pruneDeletions(ShiftList.parseMap(((SequenceI[])
646 * alandcolsel[0])[0].gapMap())); }
649 af = new AlignFrame((Alignment) res[0], (HiddenColumns) res[1],
650 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
652 Desktop.addInternalFrame(af, altitle, AlignFrame.DEFAULT_WIDTH,
653 AlignFrame.DEFAULT_HEIGHT);
657 Cache.log.info("Append results onto existing alignment.");
664 public void pollJob(AWsJob job) throws Exception
666 ((JPredJob) job).result = server.getresult(job.getJobId());
670 public boolean isCancellable()
676 public void cancelJob()
678 throw new Error(MessageManager.getString("error.implementation_error"));
682 public boolean canMergeResults()