JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / src / jalview / ws / jws1 / SeqSearchWSThread.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.ws.jws1;
22
23 import jalview.analysis.AlignSeq;
24 import jalview.api.FeatureColourI;
25 import jalview.bin.Cache;
26 import jalview.datamodel.Alignment;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.AlignmentView;
29 import jalview.datamodel.SequenceI;
30 import jalview.gui.AlignFrame;
31 import jalview.gui.Desktop;
32 import jalview.gui.WebserviceInfo;
33 import jalview.io.NewickFile;
34 import jalview.util.MessageManager;
35 import jalview.ws.AWsJob;
36 import jalview.ws.JobStateSummary;
37 import jalview.ws.WSClientI;
38
39 import java.util.HashMap;
40 import java.util.Hashtable;
41 import java.util.Map;
42 import java.util.Vector;
43
44 import vamsas.objects.simple.MsaResult;
45 import vamsas.objects.simple.SeqSearchResult;
46
47 class SeqSearchWSThread extends JWS1Thread implements WSClientI
48 {
49   String dbs = null;
50
51   boolean profile = false;
52
53   class SeqSearchWSJob extends WSJob
54   {
55     // hold special input for this
56     vamsas.objects.simple.SequenceSet seqs = new vamsas.objects.simple.SequenceSet();
57
58     /**
59      * MsaWSJob
60      * 
61      * @param jobNum
62      *          int
63      * @param jobId
64      *          String
65      */
66     public SeqSearchWSJob(int jobNum, SequenceI[] inSeqs)
67     {
68       this.jobnum = jobNum;
69       if (!prepareInput(inSeqs, 2))
70       {
71         submitted = true;
72         subjobComplete = true;
73         result = new MsaResult();
74         result.setFinished(true);
75         result.setStatus(MessageManager.getString("label.job_never_ran"));
76       }
77
78     }
79
80     Hashtable SeqNames = new Hashtable();
81
82     Vector emptySeqs = new Vector();
83
84     /**
85      * prepare input sequences for service
86      * 
87      * @param seqs
88      *          jalview sequences to be prepared
89      * @param minlen
90      *          minimum number of residues required for this MsaWS service
91      * @return true if seqs contains sequences to be submitted to service.
92      */
93     private boolean prepareInput(SequenceI[] seqs, int minlen)
94     {
95       int nseqs = 0;
96       if (minlen < 0)
97       {
98         throw new Error(
99                 MessageManager
100                         .getString("error.implementation_error_minlen_must_be_greater_zero"));
101       }
102       for (int i = 0; i < seqs.length; i++)
103       {
104         if (seqs[i].getEnd() - seqs[i].getStart() > minlen - 1)
105         {
106           nseqs++;
107         }
108       }
109       boolean valid = nseqs >= 1; // need at least one sequence for valid input
110       // TODO: generalise
111       vamsas.objects.simple.Sequence[] seqarray = (valid) ? new vamsas.objects.simple.Sequence[nseqs]
112               : null;
113       boolean submitGaps = (nseqs == 1) ? false : true; // profile is submitted
114       // with gaps
115       for (int i = 0, n = 0; i < seqs.length; i++)
116       {
117
118         String newname = jalview.analysis.SeqsetUtils.unique_name(i); // same
119         // for
120         // any
121         // subjob
122         SeqNames.put(newname,
123                 jalview.analysis.SeqsetUtils.SeqCharacterHash(seqs[i]));
124         if (valid && seqs[i].getEnd() - seqs[i].getStart() > minlen - 1)
125         {
126           seqarray[n] = new vamsas.objects.simple.Sequence();
127           seqarray[n].setId(newname);
128           seqarray[n++].setSeq((submitGaps) ? seqs[i].getSequenceAsString()
129                   : AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
130                           seqs[i].getSequenceAsString()));
131         }
132         else
133         {
134           String empty = null;
135           if (seqs[i].getEnd() >= seqs[i].getStart())
136           {
137             empty = (submitGaps) ? seqs[i].getSequenceAsString() : AlignSeq
138                     .extractGaps(jalview.util.Comparison.GapChars,
139                             seqs[i].getSequenceAsString());
140           }
141           emptySeqs.add(new String[] { newname, empty });
142         }
143       }
144       if (submitGaps)
145       {
146         // almost certainly have to remove gapped columns here
147       }
148       this.seqs = new vamsas.objects.simple.SequenceSet();
149       this.seqs.setSeqs(seqarray);
150       return valid;
151     }
152
153     /**
154      * 
155      * @return true if getAlignment will return a valid alignment result.
156      */
157     @Override
158     public boolean hasResults()
159     {
160       if (subjobComplete
161               && result != null
162               && result.isFinished()
163               && ((SeqSearchResult) result).getAlignment() != null
164               && ((SeqSearchResult) result).getAlignment().getSeqs() != null)
165       {
166         return true;
167       }
168       return false;
169     }
170
171     /**
172      * return sequence search results for display
173      * 
174      * @return null or { Alignment(+features and annotation), NewickFile)}
175      */
176     public Object[] getAlignment(AlignmentI dataset,
177             Map<String, FeatureColourI> featureColours)
178     {
179
180       if (result != null && result.isFinished())
181       {
182         SequenceI[] alseqs = null;
183         // char alseq_gapchar = '-';
184         // int alseq_l = 0;
185         if (((SeqSearchResult) result).getAlignment() != null)
186         {
187           alseqs = getVamsasAlignment(((SeqSearchResult) result)
188                   .getAlignment());
189           // alseq_gapchar = ( (SeqSearchResult)
190           // result).getAlignment().getGapchar().charAt(0);
191           // alseq_l = alseqs.length;
192         }
193         /**
194          * what has to be done. 1 - annotate returned alignment with annotation
195          * file and sequence features file, and associate any tree-nodes. 2.
196          * connect alignment back to any associated dataset: 2.a. deuniquify
197          * recovers sequence information - but additionally, relocations must be
198          * made from the returned aligned sequence back to the dataset.
199          */
200         // construct annotated alignment as it would be done by the jalview
201         // applet
202         jalview.datamodel.Alignment al = new Alignment(alseqs);
203         // al.setDataset(dataset);
204         // make dataset
205         String inFile = null;
206         try
207         {
208           inFile = ((SeqSearchResult) result).getAnnotation();
209           if (inFile != null && inFile.length() > 0)
210           {
211             new jalview.io.AnnotationFile().readAnnotationFile(al, inFile,
212                     jalview.io.AppletFormatAdapter.PASTE);
213           }
214         } catch (Exception e)
215         {
216           System.err
217                   .println("Failed to parse the annotation file associated with the alignment.");
218           System.err.println(">>>EOF" + inFile + "\n<<<EOF\n");
219           e.printStackTrace(System.err);
220         }
221
222         try
223         {
224           inFile = ((SeqSearchResult) result).getFeatures();
225           if (inFile != null && inFile.length() > 0)
226           {
227             jalview.io.FeaturesFile ff = new jalview.io.FeaturesFile(
228                     inFile, jalview.io.AppletFormatAdapter.PASTE);
229             ff.parse(al, featureColours, false);
230           }
231         } catch (Exception e)
232         {
233           System.err
234                   .println("Failed to parse the Features file associated with the alignment.");
235           System.err.println(">>>EOF" + inFile + "\n<<<EOF\n");
236           e.printStackTrace(System.err);
237         }
238         jalview.io.NewickFile nf = null;
239         try
240         {
241           inFile = ((SeqSearchResult) result).getNewickTree();
242           if (inFile != null && inFile.length() > 0)
243           {
244             nf = new jalview.io.NewickFile(inFile,
245                     jalview.io.AppletFormatAdapter.PASTE);
246             if (!nf.isValid())
247             {
248               nf.close();
249               nf = null;
250             }
251           }
252         } catch (Exception e)
253         {
254           System.err
255                   .println("Failed to parse the treeFile associated with the alignment.");
256           System.err.println(">>>EOF" + inFile + "\n<<<EOF\n");
257           e.printStackTrace(System.err);
258         }
259
260         /*
261          * TODO: housekeeping w.r.t. recovery of dataset and annotation
262          * references for input sequences, and then dataset sequence creation
263          * for new sequences retrieved from service // finally, attempt to
264          * de-uniquify to recover input sequence identity, and try to map back
265          * onto dataset Note: this
266          * jalview.analysis.SeqsetUtils.deuniquify(SeqNames, alseqs, true); will
267          * NOT WORK - the returned alignment may contain multiple versions of
268          * the input sequence, each being a subsequence of the original.
269          * deuniquify also removes existing annotation and features added in the
270          * previous step... al.setDataset(dataset); // add in new sequences
271          * retrieved from sequence search which are not already in dataset. //
272          * trigger a 'fetchDBids' to annotate sequences with database ids...
273          */
274
275         return new Object[] { al, nf };
276       }
277       return null;
278     }
279
280     /**
281      * mark subjob as cancelled and set result object appropriatly
282      */
283     void cancel()
284     {
285       cancelled = true;
286       subjobComplete = true;
287       result = null;
288     }
289
290     /**
291      * 
292      * @return boolean true if job can be submitted.
293      */
294     @Override
295     public boolean hasValidInput()
296     {
297       if (seqs.getSeqs() != null)
298       {
299         return true;
300       }
301       return false;
302     }
303   }
304
305   String alTitle; // name which will be used to form new alignment window.
306
307   AlignmentI dataset; // dataset to which the new alignment will be
308
309   // associated.
310
311   ext.vamsas.SeqSearchI server = null;
312
313   private String dbArg;
314
315   /**
316    * set basic options for this (group) of Msa jobs
317    * 
318    * @param subgaps
319    *          boolean
320    * @param presorder
321    *          boolean
322    */
323   SeqSearchWSThread(ext.vamsas.SeqSearchI server, String wsUrl,
324           WebserviceInfo wsinfo, jalview.gui.AlignFrame alFrame,
325           AlignmentView alview, String wsname, String db)
326   {
327     super(alFrame, wsinfo, alview, wsname, wsUrl);
328     this.server = server;
329     this.dbArg = db;
330   }
331
332   /**
333    * create one or more Msa jobs to align visible seuqences in _msa
334    * 
335    * @param title
336    *          String
337    * @param _msa
338    *          AlignmentView
339    * @param subgaps
340    *          boolean
341    * @param presorder
342    *          boolean
343    * @param seqset
344    *          Alignment
345    */
346   SeqSearchWSThread(ext.vamsas.SeqSearchI server, String wsUrl,
347           WebserviceInfo wsinfo, jalview.gui.AlignFrame alFrame,
348           String wsname, String title, AlignmentView _msa, String db,
349           AlignmentI seqset)
350   {
351     this(server, wsUrl, wsinfo, alFrame, _msa, wsname, db);
352     OutputHeader = wsInfo.getProgressText();
353     alTitle = title;
354     dataset = seqset;
355
356     SequenceI[][] conmsa = _msa.getVisibleContigs('-');
357     if (conmsa != null)
358     {
359       int njobs = conmsa.length;
360       jobs = new SeqSearchWSJob[njobs];
361       for (int j = 0; j < njobs; j++)
362       {
363         if (j != 0)
364         {
365           jobs[j] = new SeqSearchWSJob(wsinfo.addJobPane(), conmsa[j]);
366         }
367         else
368         {
369           jobs[j] = new SeqSearchWSJob(0, conmsa[j]);
370         }
371         if (njobs > 0)
372         {
373           wsinfo.setProgressName("region " + jobs[j].getJobnum(),
374                   jobs[j].getJobnum());
375         }
376         wsinfo.setProgressText(jobs[j].getJobnum(), OutputHeader);
377       }
378     }
379   }
380
381   @Override
382   public boolean isCancellable()
383   {
384     return true;
385   }
386
387   @Override
388   public void cancelJob()
389   {
390     if (!jobComplete && jobs != null)
391     {
392       boolean cancelled = true;
393       for (int job = 0; job < jobs.length; job++)
394       {
395         if (jobs[job].isSubmitted() && !jobs[job].isSubjobComplete())
396         {
397           String cancelledMessage = "";
398           try
399           {
400             vamsas.objects.simple.WsJobId cancelledJob = server
401                     .cancel(jobs[job].getJobId());
402             if (cancelledJob.getStatus() == 2)
403             {
404               // CANCELLED_JOB
405               cancelledMessage = "Job cancelled.";
406               ((SeqSearchWSJob) jobs[job]).cancel();
407               wsInfo.setStatus(jobs[job].getJobnum(),
408                       WebserviceInfo.STATE_CANCELLED_OK);
409             }
410             else if (cancelledJob.getStatus() == 3)
411             {
412               // VALID UNSTOPPABLE JOB
413               cancelledMessage += "Server cannot cancel this job. just close the window.\n";
414               cancelled = false;
415               // wsInfo.setStatus(jobs[job].jobnum,
416               // WebserviceInfo.STATE_RUNNING);
417             }
418
419             if (cancelledJob.getJobId() != null)
420             {
421               cancelledMessage += ("[" + cancelledJob.getJobId() + "]");
422             }
423
424             cancelledMessage += "\n";
425           } catch (Exception exc)
426           {
427             cancelledMessage += ("\nProblems cancelling the job : Exception received...\n"
428                     + exc + "\n");
429             Cache.log.warn(
430                     "Exception whilst cancelling " + jobs[job].getJobId(),
431                     exc);
432           }
433           wsInfo.setProgressText(jobs[job].getJobnum(), OutputHeader
434                   + cancelledMessage + "\n");
435         }
436       }
437       if (cancelled)
438       {
439         wsInfo.setStatus(WebserviceInfo.STATE_CANCELLED_OK);
440         jobComplete = true;
441       }
442       this.interrupt(); // kick thread to update job states.
443     }
444     else
445     {
446       if (!jobComplete)
447       {
448         wsInfo.setProgressText(OutputHeader
449                 + "Server cannot cancel this job because it has not been submitted properly. just close the window.\n");
450       }
451     }
452   }
453
454   @Override
455   public void pollJob(AWsJob job) throws Exception
456   {
457     ((SeqSearchWSJob) job).result = server.getResult(((SeqSearchWSJob) job)
458             .getJobId());
459   }
460
461   @Override
462   public void StartJob(AWsJob job)
463   {
464     if (!(job instanceof SeqSearchWSJob))
465     {
466       throw new Error(MessageManager.formatMessage(
467               "error.implementation_error_msawbjob_called",
468               new String[] { job.getClass().toString() }));
469     }
470     SeqSearchWSJob j = (SeqSearchWSJob) job;
471     if (j.isSubmitted())
472     {
473       if (Cache.log.isDebugEnabled())
474       {
475         Cache.log.debug("Tried to submit an already submitted job "
476                 + j.getJobId());
477       }
478       return;
479     }
480     if (j.seqs.getSeqs() == null)
481     {
482       // special case - selection consisted entirely of empty sequences...
483       j.setSubmitted(true);
484       j.result = new MsaResult();
485       j.result.setFinished(true);
486       j.result.setStatus(MessageManager
487               .getString("label.empty_alignment_job"));
488       ((MsaResult) j.result).setMsa(null);
489     }
490     try
491     {
492       vamsas.objects.simple.WsJobId jobsubmit = server.search(
493               j.seqs.getSeqs()[0], dbArg);
494
495       if ((jobsubmit != null) && (jobsubmit.getStatus() == 1))
496       {
497         j.setJobId(jobsubmit.getJobId());
498         j.setSubmitted(true);
499         j.setSubjobComplete(false);
500         // System.out.println(WsURL + " Job Id '" + jobId + "'");
501       }
502       else
503       {
504         if (jobsubmit == null)
505         {
506           throw new Exception(MessageManager.formatMessage(
507                   "exception.web_service_returned_null_try_later",
508                   new String[] { WsUrl }));
509         }
510
511         throw new Exception(jobsubmit.getJobId());
512       }
513     } catch (Exception e)
514     {
515       // TODO: JBPNote catch timeout or other fault types explicitly
516       // For unexpected errors
517       System.err
518               .println(WebServiceName
519                       + "Client: Failed to submit the sequences for alignment (probably a server side problem)\n"
520                       + "When contacting Server:" + WsUrl + "\n"
521                       + e.toString() + "\n");
522       j.setAllowedServerExceptions(0);
523       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
524       wsInfo.setStatus(j.getJobnum(),
525               WebserviceInfo.STATE_STOPPED_SERVERERROR);
526       wsInfo.appendProgressText(j.getJobnum(), MessageManager
527               .getString("info.failed_to_submit_sequences_for_alignment"));
528
529       // e.printStackTrace(); // TODO: JBPNote DEBUG
530     }
531   }
532
533   private jalview.datamodel.Sequence[] getVamsasAlignment(
534           vamsas.objects.simple.Alignment valign)
535   {
536     vamsas.objects.simple.Sequence[] seqs = valign.getSeqs().getSeqs();
537     jalview.datamodel.Sequence[] msa = new jalview.datamodel.Sequence[seqs.length];
538
539     for (int i = 0, j = seqs.length; i < j; i++)
540     {
541       msa[i] = new jalview.datamodel.Sequence(seqs[i].getId(),
542               seqs[i].getSeq());
543     }
544
545     return msa;
546   }
547
548   @Override
549   public void parseResult()
550   {
551     int results = 0; // number of result sets received
552     JobStateSummary finalState = new JobStateSummary();
553     try
554     {
555       for (int j = 0; j < jobs.length; j++)
556       {
557         finalState.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
558         if (jobs[j].isSubmitted() && jobs[j].isSubjobComplete()
559                 && jobs[j].hasResults())
560         {
561           results++;
562           vamsas.objects.simple.Alignment valign = ((SeqSearchResult) ((SeqSearchWSJob) jobs[j]).result)
563                   .getAlignment();
564           if (valign != null)
565           {
566             wsInfo.appendProgressText(jobs[j].getJobnum(), MessageManager
567                     .getString("info.alignment_object_method_notes"));
568             String[] lines = valign.getMethod();
569             for (int line = 0; line < lines.length; line++)
570             {
571               wsInfo.appendProgressText(jobs[j].getJobnum(), lines[line]
572                       + "\n");
573             }
574             // JBPNote The returned files from a webservice could be
575             // hidden behind icons in the monitor window that,
576             // when clicked, pop up their corresponding data
577           }
578         }
579       }
580     } catch (Exception ex)
581     {
582
583       Cache.log.error("Unexpected exception when processing results for "
584               + alTitle, ex);
585       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
586     }
587     if (results > 0)
588     {
589       wsInfo.showResultsNewFrame
590               .addActionListener(new java.awt.event.ActionListener()
591               {
592                 @Override
593                 public void actionPerformed(java.awt.event.ActionEvent evt)
594                 {
595                   displayResults(true);
596                 }
597               });
598       wsInfo.mergeResults
599               .addActionListener(new java.awt.event.ActionListener()
600               {
601                 @Override
602                 public void actionPerformed(java.awt.event.ActionEvent evt)
603                 {
604                   displayResults(false);
605                 }
606               });
607       wsInfo.setResultsReady();
608     }
609     else
610     {
611       wsInfo.setFinishedNoResults();
612     }
613   }
614
615   void displayResults(boolean newFrame)
616   {
617     if (!newFrame)
618     {
619       System.err.println("MERGE WITH OLD FRAME NOT IMPLEMENTED");
620       return;
621     }
622     // each subjob is an independent alignment for the moment
623     // Alignment al[] = new Alignment[jobs.length];
624     // NewickFile nf[] = new NewickFile[jobs.length];
625     for (int j = 0; j < jobs.length; j++)
626     {
627       Map<String, FeatureColourI> featureColours = new HashMap<String, FeatureColourI>();
628       Alignment al = null;
629       NewickFile nf = null;
630       if (jobs[j].hasResults())
631       {
632         Object[] res = ((SeqSearchWSJob) jobs[j]).getAlignment(dataset,
633                 featureColours);
634         if (res == null)
635         {
636           continue;
637         }
638         ;
639         al = (Alignment) res[0];
640         nf = (NewickFile) res[1];
641       }
642       else
643       {
644         al = null;
645         nf = null;
646         continue;
647       }
648       /*
649        * We can't map new alignment back with insertions from input's hidden
650        * regions until dataset mapping is sorted out... but basically it goes
651        * like this: 1. Merge each domain hit back onto the visible segments in
652        * the same way as a Jnet prediction is mapped back
653        * 
654        * Object[] newview = input.getUpdatedView(results, orders, getGapChar());
655        * // trash references to original result data for (int j = 0; j <
656        * jobs.length; j++) { results[j] = null; orders[j] = null; } SequenceI[]
657        * alignment = (SequenceI[]) newview[0]; ColumnSelection columnselection =
658        * (ColumnSelection) newview[1]; Alignment al = new Alignment(alignment);
659        * 
660        * if (dataset != null) { al.setDataset(dataset); }
661        * 
662        * propagateDatasetMappings(al); }
663        */
664
665       AlignFrame af = new AlignFrame(al,// columnselection,
666               AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
667       if (nf != null)
668       {
669         af.ShowNewickTree(nf, MessageManager.formatMessage(
670                 "label.tree_from", new String[] { this.alTitle }));
671       }
672       // initialise with same renderer settings as in parent alignframe.
673       af.getFeatureRenderer().transferSettings(this.featureSettings);
674       Desktop.addInternalFrame(af, alTitle, AlignFrame.DEFAULT_WIDTH,
675               AlignFrame.DEFAULT_HEIGHT);
676     }
677   }
678
679   @Override
680   public boolean canMergeResults()
681   {
682     return false;
683   }
684 }