b9d38fe0be9517ad43f94b2c41e44dd383bb5699
[jalview.git] / src / jalview / ws / jws1 / SeqSearchWSThread.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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.ws.AWsJob;
31 import jalview.ws.JobStateSummary;
32 import jalview.ws.WSClientI;
33 import vamsas.objects.simple.MsaResult;
34 import vamsas.objects.simple.SeqSearchResult;
35
36 class SeqSearchWSThread extends JWS1Thread implements WSClientI
37 {
38   String dbs = null;
39
40   boolean profile = false;
41
42   class SeqSearchWSJob extends WSJob
43   {
44     // hold special input for this
45     vamsas.objects.simple.SequenceSet seqs = new vamsas.objects.simple.SequenceSet();
46
47     /**
48      * MsaWSJob
49      * 
50      * @param jobNum
51      *          int
52      * @param jobId
53      *          String
54      */
55     public SeqSearchWSJob(int jobNum, SequenceI[] inSeqs)
56     {
57       this.jobnum = jobNum;
58       if (!prepareInput(inSeqs, 2))
59       {
60         submitted = true;
61         subjobComplete = true;
62         result = new MsaResult();
63         result.setFinished(true);
64         result.setStatus("Job never ran - input returned to user.");
65       }
66
67     }
68
69     Hashtable SeqNames = new Hashtable();
70
71     Vector emptySeqs = new Vector();
72
73     /**
74      * prepare input sequences for service
75      * 
76      * @param seqs
77      *          jalview sequences to be prepared
78      * @param minlen
79      *          minimum number of residues required for this MsaWS service
80      * @return true if seqs contains sequences to be submitted to service.
81      */
82     private boolean prepareInput(SequenceI[] seqs, int minlen)
83     {
84       int nseqs = 0;
85       if (minlen < 0)
86       {
87         throw new Error(
88                 "Implementation error: minlen must be zero or more.");
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("StartJob(MsaWSJob) called on a WSJobInstance "
450               + job.getClass());
451     }
452     SeqSearchWSJob j = (SeqSearchWSJob) job;
453     if (j.isSubmitted())
454     {
455       if (Cache.log.isDebugEnabled())
456       {
457         Cache.log.debug("Tried to submit an already submitted job "
458                 + j.getJobId());
459       }
460       return;
461     }
462     if (j.seqs.getSeqs() == null)
463     {
464       // special case - selection consisted entirely of empty sequences...
465       j.setSubmitted(true);
466       j.result = new MsaResult();
467       j.result.setFinished(true);
468       j.result.setStatus("Empty Alignment Job");
469       ((MsaResult) j.result).setMsa(null);
470     }
471     try
472     {
473       vamsas.objects.simple.WsJobId jobsubmit = server.search(
474               j.seqs.getSeqs()[0], dbArg);
475
476       if ((jobsubmit != null) && (jobsubmit.getStatus() == 1))
477       {
478         j.setJobId(jobsubmit.getJobId());
479         j.setSubmitted(true);
480         j.setSubjobComplete(false);
481         // System.out.println(WsURL + " Job Id '" + jobId + "'");
482       }
483       else
484       {
485         if (jobsubmit == null)
486         {
487           throw new Exception(
488                   "Server at "
489                           + WsUrl
490                           + " returned null object, it probably cannot be contacted. Try again later ?");
491         }
492
493         throw new Exception(jobsubmit.getJobId());
494       }
495     } catch (Exception e)
496     {
497       // TODO: JBPNote catch timeout or other fault types explicitly
498       // For unexpected errors
499       System.err
500               .println(WebServiceName
501                       + "Client: Failed to submit the sequences for alignment (probably a server side problem)\n"
502                       + "When contacting Server:" + WsUrl + "\n"
503                       + e.toString() + "\n");
504       j.setAllowedServerExceptions(0);
505       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
506       wsInfo.setStatus(j.getJobnum(),
507               WebserviceInfo.STATE_STOPPED_SERVERERROR);
508       wsInfo.appendProgressText(
509               j.getJobnum(),
510               "Failed to submit sequences for alignment.\n"
511                       + "It is most likely that there is a problem with the server.\n"
512                       + "Just close the window\n");
513
514       // e.printStackTrace(); // TODO: JBPNote DEBUG
515     }
516   }
517
518   private jalview.datamodel.Sequence[] getVamsasAlignment(
519           vamsas.objects.simple.Alignment valign)
520   {
521     vamsas.objects.simple.Sequence[] seqs = valign.getSeqs().getSeqs();
522     jalview.datamodel.Sequence[] msa = new jalview.datamodel.Sequence[seqs.length];
523
524     for (int i = 0, j = seqs.length; i < j; i++)
525     {
526       msa[i] = new jalview.datamodel.Sequence(seqs[i].getId(),
527               seqs[i].getSeq());
528     }
529
530     return msa;
531   }
532
533   public void parseResult()
534   {
535     int results = 0; // number of result sets received
536     JobStateSummary finalState = new JobStateSummary();
537     try
538     {
539       for (int j = 0; j < jobs.length; j++)
540       {
541         finalState.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
542         if (jobs[j].isSubmitted() && jobs[j].isSubjobComplete()
543                 && jobs[j].hasResults())
544         {
545           results++;
546           vamsas.objects.simple.Alignment valign = ((SeqSearchResult) ((SeqSearchWSJob) jobs[j]).result)
547                   .getAlignment();
548           if (valign != null)
549           {
550             wsInfo.appendProgressText(jobs[j].getJobnum(),
551                     "\nAlignment Object Method Notes\n");
552             String[] lines = valign.getMethod();
553             for (int line = 0; line < lines.length; line++)
554             {
555               wsInfo.appendProgressText(jobs[j].getJobnum(), lines[line]
556                       + "\n");
557             }
558             // JBPNote The returned files from a webservice could be
559             // hidden behind icons in the monitor window that,
560             // when clicked, pop up their corresponding data
561           }
562         }
563       }
564     } catch (Exception ex)
565     {
566
567       Cache.log.error("Unexpected exception when processing results for "
568               + alTitle, ex);
569       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
570     }
571     if (results > 0)
572     {
573       wsInfo.showResultsNewFrame
574               .addActionListener(new java.awt.event.ActionListener()
575               {
576                 public void actionPerformed(java.awt.event.ActionEvent evt)
577                 {
578                   displayResults(true);
579                 }
580               });
581       wsInfo.mergeResults
582               .addActionListener(new java.awt.event.ActionListener()
583               {
584                 public void actionPerformed(java.awt.event.ActionEvent evt)
585                 {
586                   displayResults(false);
587                 }
588               });
589       wsInfo.setResultsReady();
590     }
591     else
592     {
593       wsInfo.setFinishedNoResults();
594     }
595   }
596
597   void displayResults(boolean newFrame)
598   {
599     if (!newFrame)
600     {
601       System.err.println("MERGE WITH OLD FRAME NOT IMPLEMENTED");
602       return;
603     }
604     // each subjob is an independent alignment for the moment
605     // Alignment al[] = new Alignment[jobs.length];
606     // NewickFile nf[] = new NewickFile[jobs.length];
607     for (int j = 0; j < jobs.length; j++)
608     {
609       Hashtable featureColours = new Hashtable();
610       Alignment al = null;
611       NewickFile nf = null;
612       if (jobs[j].hasResults())
613       {
614         Object[] res = ((SeqSearchWSJob) jobs[j]).getAlignment(dataset,
615                 featureColours);
616         if (res == null)
617         {
618           continue;
619         }
620         ;
621         al = (Alignment) res[0];
622         nf = (NewickFile) res[1];
623       }
624       else
625       {
626         al = null;
627         nf = null;
628         continue;
629       }
630       /*
631        * We can't map new alignment back with insertions from input's hidden
632        * regions until dataset mapping is sorted out... but basically it goes
633        * like this: 1. Merge each domain hit back onto the visible segments in
634        * the same way as a Jnet prediction is mapped back
635        * 
636        * Object[] newview = input.getUpdatedView(results, orders, getGapChar());
637        * // trash references to original result data for (int j = 0; j <
638        * jobs.length; j++) { results[j] = null; orders[j] = null; } SequenceI[]
639        * alignment = (SequenceI[]) newview[0]; ColumnSelection columnselection =
640        * (ColumnSelection) newview[1]; Alignment al = new Alignment(alignment);
641        * 
642        * if (dataset != null) { al.setDataset(dataset); }
643        * 
644        * propagateDatasetMappings(al); }
645        */
646
647       AlignFrame af = new AlignFrame(al,// columnselection,
648               AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
649       if (nf != null)
650       {
651         af.ShowNewickTree(nf, "Tree from " + this.alTitle);
652       }
653       // initialise with same renderer settings as in parent alignframe.
654       af.getFeatureRenderer().transferSettings(this.featureSettings);
655       Desktop.addInternalFrame(af, alTitle, AlignFrame.DEFAULT_WIDTH,
656               AlignFrame.DEFAULT_HEIGHT);
657     }
658   }
659
660   public boolean canMergeResults()
661   {
662     return false;
663   }
664 }