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