JAL-2629 hmmalign now correctly creates a new frame
[jalview.git] / src / jalview / ws / jws2 / MsaWSThread.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.jws2;
22
23 import jalview.analysis.AlignSeq;
24 import jalview.bin.Cache;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.AlignmentOrder;
28 import jalview.datamodel.AlignmentView;
29 import jalview.datamodel.HiddenColumns;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceI;
32 import jalview.gui.AlignFrame;
33 import jalview.gui.Desktop;
34 import jalview.gui.SplitFrame;
35 import jalview.gui.WebserviceInfo;
36 import jalview.util.MessageManager;
37 import jalview.ws.AWsJob;
38 import jalview.ws.JobStateSummary;
39 import jalview.ws.WSClientI;
40 import jalview.ws.jws2.dm.JabaWsParamSet;
41 import jalview.ws.params.WsParamSetI;
42
43 import java.util.ArrayList;
44 import java.util.Hashtable;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.Vector;
48
49 import javax.swing.JInternalFrame;
50
51 import compbio.data.msa.MsaWS;
52 import compbio.metadata.Argument;
53 import compbio.metadata.ChunkHolder;
54 import compbio.metadata.JobStatus;
55 import compbio.metadata.Preset;
56
57 class MsaWSThread extends AWS2Thread implements WSClientI
58 {
59   boolean submitGaps = false; // pass sequences including gaps to alignment
60
61   // service
62
63   boolean preserveOrder = true; // and always store and recover sequence
64
65   // order
66
67   class MsaWSJob extends JWs2Job
68   {
69     long lastChunk = 0;
70
71     WsParamSetI preset = null;
72
73     List<Argument> arguments = null;
74
75     /**
76      * input
77      */
78     ArrayList<compbio.data.sequence.FastaSequence> seqs = new ArrayList<>();
79
80     /**
81      * output
82      */
83     compbio.data.sequence.Alignment alignment;
84
85     // set if the job didn't get run - then the input is simply returned to the
86     // user
87     private boolean returnInput = false;
88
89     /**
90      * MsaWSJob
91      * 
92      * @param jobNum
93      *          int
94      * @param jobId
95      *          String
96      */
97     public MsaWSJob(int jobNum, SequenceI[] inSeqs)
98     {
99       this.jobnum = jobNum;
100       if (!prepareInput(inSeqs, 2))
101       {
102         submitted = true;
103         subjobComplete = true;
104         returnInput = true;
105       }
106
107     }
108
109     Hashtable<String, Map> SeqNames = new Hashtable();
110
111     Vector<String[]> emptySeqs = new Vector();
112
113     /**
114      * prepare input sequences for MsaWS service
115      * 
116      * @param seqs
117      *          jalview sequences to be prepared
118      * @param minlen
119      *          minimum number of residues required for this MsaWS service
120      * @return true if seqs contains sequences to be submitted to service.
121      */
122     // TODO: return compbio.seqs list or nothing to indicate validity.
123     private boolean prepareInput(SequenceI[] seqs, int minlen)
124     {
125       int nseqs = 0;
126       if (minlen < 0)
127       {
128         throw new Error(
129                 MessageManager
130                         .getString("error.implementation_error_minlen_must_be_greater_zero"));
131       }
132       for (int i = 0; i < seqs.length; i++)
133       {
134         if (seqs[i].getEnd() - seqs[i].getStart() > minlen - 1)
135         {
136           nseqs++;
137         }
138       }
139       boolean valid = nseqs > 1; // need at least two seqs
140       compbio.data.sequence.FastaSequence seq;
141       for (int i = 0, n = 0; i < seqs.length; i++)
142       {
143         String newname = jalview.analysis.SeqsetUtils.unique_name(i); // same
144         // for
145         // any
146         // subjob
147         SeqNames.put(newname,
148                 jalview.analysis.SeqsetUtils.SeqCharacterHash(seqs[i]));
149         if (valid && seqs[i].getEnd() - seqs[i].getStart() > minlen - 1)
150         {
151           // make new input sequence with or without gaps
152           seq = new compbio.data.sequence.FastaSequence(newname,
153                   (submitGaps) ? seqs[i].getSequenceAsString()
154                           : AlignSeq.extractGaps(
155                                   jalview.util.Comparison.GapChars,
156                                   seqs[i].getSequenceAsString()));
157           this.seqs.add(seq);
158         }
159         else
160         {
161           String empty = null;
162           if (seqs[i].getEnd() >= seqs[i].getStart())
163           {
164             empty = (submitGaps) ? seqs[i].getSequenceAsString() : AlignSeq
165                     .extractGaps(jalview.util.Comparison.GapChars,
166                             seqs[i].getSequenceAsString());
167           }
168           emptySeqs.add(new String[] { newname, empty });
169         }
170       }
171       return valid;
172     }
173
174     /**
175      * 
176      * @return true if getAlignment will return a valid alignment result.
177      */
178     @Override
179     public boolean hasResults()
180     {
181       if (subjobComplete
182               && isFinished()
183               && (alignment != null || (emptySeqs != null && emptySeqs
184                       .size() > 0)))
185       {
186         return true;
187       }
188       return false;
189     }
190
191     /**
192      * 
193      * get the alignment including any empty sequences in the original order
194      * with original ids. Caller must access the alignment.getMetadata() object
195      * to annotate the final result passsed to the user.
196      * 
197      * @return { SequenceI[], AlignmentOrder }
198      */
199     public Object[] getAlignment()
200     {
201       // is this a generic subjob or a Jws2 specific Object[] return signature
202       if (hasResults())
203       {
204         SequenceI[] alseqs = null;
205         char alseq_gapchar = '-';
206         int alseq_l = 0;
207         if (alignment.getSequences().size() > 0)
208         {
209           alseqs = new SequenceI[alignment.getSequences().size()];
210           for (compbio.data.sequence.FastaSequence seq : alignment
211                   .getSequences())
212           {
213             alseqs[alseq_l++] = new Sequence(seq.getId(), seq.getSequence());
214           }
215           alseq_gapchar = alignment.getMetadata().getGapchar();
216
217         }
218         // add in the empty seqs.
219         if (emptySeqs.size() > 0)
220         {
221           SequenceI[] t_alseqs = new SequenceI[alseq_l + emptySeqs.size()];
222           // get width
223           int i, w = 0;
224           if (alseq_l > 0)
225           {
226             for (i = 0, w = alseqs[0].getLength(); i < alseq_l; i++)
227             {
228               if (w < alseqs[i].getLength())
229               {
230                 w = alseqs[i].getLength();
231               }
232               t_alseqs[i] = alseqs[i];
233               alseqs[i] = null;
234             }
235           }
236           // check that aligned width is at least as wide as emptySeqs width.
237           int ow = w, nw = w;
238           for (i = 0, w = emptySeqs.size(); i < w; i++)
239           {
240             String[] es = emptySeqs.get(i);
241             if (es != null && es[1] != null)
242             {
243               int sw = es[1].length();
244               if (nw < sw)
245               {
246                 nw = sw;
247               }
248             }
249           }
250           // make a gapped string.
251           StringBuffer insbuff = new StringBuffer(w);
252           for (i = 0; i < nw; i++)
253           {
254             insbuff.append(alseq_gapchar);
255           }
256           if (ow < nw)
257           {
258             for (i = 0; i < alseq_l; i++)
259             {
260               int sw = t_alseqs[i].getLength();
261               if (nw > sw)
262               {
263                 // pad at end
264                 alseqs[i].setSequence(t_alseqs[i].getSequenceAsString()
265                         + insbuff.substring(0, sw - nw));
266               }
267             }
268           }
269           for (i = 0, w = emptySeqs.size(); i < w; i++)
270           {
271             String[] es = emptySeqs.get(i);
272             if (es[1] == null)
273             {
274               t_alseqs[i + alseq_l] = new jalview.datamodel.Sequence(es[0],
275                       insbuff.toString(), 1, 0);
276             }
277             else
278             {
279               if (es[1].length() < nw)
280               {
281                 t_alseqs[i + alseq_l] = new jalview.datamodel.Sequence(
282                         es[0],
283                         es[1] + insbuff.substring(0, nw - es[1].length()),
284                         1, 1 + es[1].length());
285               }
286               else
287               {
288                 t_alseqs[i + alseq_l] = new jalview.datamodel.Sequence(
289                         es[0], es[1]);
290               }
291             }
292           }
293           alseqs = t_alseqs;
294         }
295         AlignmentOrder msaorder = new AlignmentOrder(alseqs);
296         // always recover the order - makes parseResult()'s life easier.
297         jalview.analysis.AlignmentSorter.recoverOrder(alseqs);
298         // account for any missing sequences
299         jalview.analysis.SeqsetUtils.deuniquify(SeqNames, alseqs);
300         return new Object[] { alseqs, msaorder };
301       }
302       return null;
303     }
304
305     /**
306      * mark subjob as cancelled and set result object appropriatly
307      */
308     void cancel()
309     {
310       cancelled = true;
311       subjobComplete = true;
312       alignment = null;
313     }
314
315     /**
316      * 
317      * @return boolean true if job can be submitted.
318      */
319     @Override
320     public boolean hasValidInput()
321     {
322       // TODO: get attributes for this MsaWS instance to check if it can do two
323       // sequence alignment.
324       if (seqs != null && seqs.size() >= 2) // two or more sequences is valid ?
325       {
326         return true;
327       }
328       return false;
329     }
330
331     StringBuffer jobProgress = new StringBuffer();
332
333     public void setStatus(String string)
334     {
335       jobProgress.setLength(0);
336       jobProgress.append(string);
337     }
338
339     @Override
340     public String getStatus()
341     {
342       return jobProgress.toString();
343     }
344
345     @Override
346     public boolean hasStatus()
347     {
348       return jobProgress != null;
349     }
350
351     /**
352      * @return the lastChunk
353      */
354     public long getLastChunk()
355     {
356       return lastChunk;
357     }
358
359     /**
360      * @param lastChunk
361      *          the lastChunk to set
362      */
363     public void setLastChunk(long lastChunk)
364     {
365       this.lastChunk = lastChunk;
366     }
367
368     String alignmentProgram = null;
369
370     public String getAlignmentProgram()
371     {
372       return alignmentProgram;
373     }
374
375     public boolean hasArguments()
376     {
377       return (arguments != null && arguments.size() > 0)
378               || (preset != null && preset instanceof JabaWsParamSet);
379     }
380
381     public List<Argument> getJabaArguments()
382     {
383       List<Argument> newargs = new ArrayList<>();
384       if (preset != null && preset instanceof JabaWsParamSet)
385       {
386         newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
387       }
388       if (arguments != null && arguments.size() > 0)
389       {
390         newargs.addAll(arguments);
391       }
392       return newargs;
393     }
394
395     /**
396      * add a progess header to status string containing presets/args used
397      */
398     public void addInitialStatus()
399     {
400       if (preset != null)
401       {
402         jobProgress.append("Using "
403                 + (preset instanceof JabaPreset ? "Server" : "User")
404                 + "Preset: " + preset.getName());
405         if (preset instanceof JabaWsParamSet)
406         {
407           for (Argument opt : ((JabaWsParamSet) preset).getjabaArguments())
408           {
409             jobProgress.append(opt.getName() + " " + opt.getDefaultValue()
410                     + "\n");
411           }
412         }
413       }
414       if (arguments != null && arguments.size() > 0)
415       {
416         jobProgress.append("With custom parameters : \n");
417         // merge arguments with preset's own arguments.
418         for (Argument opt : arguments)
419         {
420           jobProgress.append(opt.getName() + " " + opt.getDefaultValue()
421                   + "\n");
422         }
423       }
424       jobProgress.append("\nJob Output:\n");
425     }
426
427     public boolean isPresetJob()
428     {
429       return preset != null && preset instanceof JabaPreset;
430     }
431
432     public Preset getServerPreset()
433     {
434       return (isPresetJob()) ? ((JabaPreset) preset).p : null;
435     }
436   }
437
438   String alTitle; // name which will be used to form new alignment window.
439
440   AlignmentI dataset; // dataset to which the new alignment will be
441
442   // associated.
443
444   @SuppressWarnings("unchecked")
445   MsaWS server = null;
446
447   /**
448    * set basic options for this (group) of Msa jobs
449    * 
450    * @param subgaps
451    *          boolean
452    * @param presorder
453    *          boolean
454    */
455   private MsaWSThread(MsaWS server, String wsUrl, WebserviceInfo wsinfo,
456           jalview.gui.AlignFrame alFrame, AlignmentView alview,
457           String wsname, boolean subgaps, boolean presorder)
458   {
459     super(alFrame, wsinfo, alview, wsname, wsUrl);
460     this.server = server;
461     this.submitGaps = subgaps;
462     this.preserveOrder = presorder;
463   }
464
465   /**
466    * create one or more Msa jobs to align visible seuqences in _msa
467    * 
468    * @param title
469    *          String
470    * @param _msa
471    *          AlignmentView
472    * @param subgaps
473    *          boolean
474    * @param presorder
475    *          boolean
476    * @param seqset
477    *          Alignment
478    */
479   MsaWSThread(MsaWS server2, WsParamSetI preset, List<Argument> paramset,
480           String wsUrl, WebserviceInfo wsinfo,
481           jalview.gui.AlignFrame alFrame, String wsname, String title,
482           AlignmentView _msa, boolean subgaps, boolean presorder,
483           AlignmentI seqset)
484   {
485     this(server2, wsUrl, wsinfo, alFrame, _msa, wsname, subgaps, presorder);
486     OutputHeader = wsInfo.getProgressText();
487     alTitle = title;
488     dataset = seqset;
489
490     SequenceI[][] conmsa = _msa.getVisibleContigs('-');
491     if (conmsa != null)
492     {
493       int nvalid = 0, njobs = conmsa.length;
494       jobs = new MsaWSJob[njobs];
495       for (int j = 0; j < njobs; j++)
496       {
497         if (j != 0)
498         {
499           jobs[j] = new MsaWSJob(wsinfo.addJobPane(), conmsa[j]);
500         }
501         else
502         {
503           jobs[j] = new MsaWSJob(0, conmsa[j]);
504         }
505         if (((MsaWSJob) jobs[j]).hasValidInput())
506         {
507           nvalid++;
508         }
509         ((MsaWSJob) jobs[j]).preset = preset;
510         ((MsaWSJob) jobs[j]).arguments = paramset;
511         ((MsaWSJob) jobs[j]).alignmentProgram = wsname;
512         if (njobs > 0)
513         {
514           wsinfo.setProgressName("region " + jobs[j].getJobnum(),
515                   jobs[j].getJobnum());
516         }
517         wsinfo.setProgressText(jobs[j].getJobnum(), OutputHeader);
518       }
519       validInput = nvalid > 0;
520     }
521   }
522
523   boolean validInput = false;
524
525   /**
526    * 
527    * @return true if the thread will perform a calculation
528    */
529   public boolean hasValidInput()
530   {
531     return validInput;
532   }
533
534   @Override
535   public boolean isCancellable()
536   {
537     return true;
538   }
539
540   @Override
541   public void cancelJob()
542   {
543     if (!jobComplete && jobs != null)
544     {
545       boolean cancelled = true;
546       for (int job = 0; job < jobs.length; job++)
547       {
548         if (jobs[job].isSubmitted() && !jobs[job].isSubjobComplete())
549         {
550           String cancelledMessage = "";
551           try
552           {
553             boolean cancelledJob = server.cancelJob(jobs[job].getJobId());
554             if (true) // cancelledJob || true)
555             {
556               // CANCELLED_JOB
557               // if the Jaba server indicates the job can't be cancelled, its
558               // because its running on the server's local execution engine
559               // so we just close the window anyway.
560               cancelledMessage = "Job cancelled.";
561               ((MsaWSJob) jobs[job]).cancel(); // TODO: refactor to avoid this
562                                                // ugliness -
563               wsInfo.setStatus(jobs[job].getJobnum(),
564                       WebserviceInfo.STATE_CANCELLED_OK);
565             }
566             else
567             {
568               // VALID UNSTOPPABLE JOB
569               cancelledMessage += "Server cannot cancel this job. just close the window.\n";
570               cancelled = false;
571               // wsInfo.setStatus(jobs[job].jobnum,
572               // WebserviceInfo.STATE_RUNNING);
573             }
574           } catch (Exception exc)
575           {
576             cancelledMessage += ("\nProblems cancelling the job : Exception received...\n"
577                     + exc + "\n");
578             Cache.log.warn(
579                     "Exception whilst cancelling " + jobs[job].getJobId(),
580                     exc);
581           }
582           wsInfo.setProgressText(jobs[job].getJobnum(), OutputHeader
583                   + cancelledMessage + "\n");
584         }
585         else
586         {
587           // if we hadn't submitted then just mark the job as cancelled.
588           jobs[job].setSubjobComplete(true);
589           wsInfo.setStatus(jobs[job].getJobnum(),
590                   WebserviceInfo.STATE_CANCELLED_OK);
591
592         }
593       }
594       if (cancelled)
595       {
596         wsInfo.setStatus(WebserviceInfo.STATE_CANCELLED_OK);
597         jobComplete = true;
598       }
599       this.interrupt(); // kick thread to update job states.
600     }
601     else
602     {
603       if (!jobComplete)
604       {
605         wsInfo.setProgressText(OutputHeader
606                 + "Server cannot cancel this job because it has not been submitted properly. just close the window.\n");
607       }
608     }
609   }
610
611   @Override
612   public void pollJob(AWsJob job) throws Exception
613   {
614     // TODO: investigate if we still need to cast here in J1.6
615     MsaWSJob j = ((MsaWSJob) job);
616     // this is standard code, but since the interface doesn't comprise of a
617     // basic one that implements (getJobStatus, pullExecStatistics) we have to
618     // repeat the code for all jw2s services.
619     j.setjobStatus(server.getJobStatus(job.getJobId()));
620     updateJobProgress(j);
621   }
622
623   /**
624    * 
625    * @param j
626    * @return true if more job progress data was available
627    * @throws Exception
628    */
629   protected boolean updateJobProgress(MsaWSJob j) throws Exception
630   {
631     StringBuffer response = j.jobProgress;
632     long lastchunk = j.getLastChunk();
633     boolean changed = false;
634     do
635     {
636       j.setLastChunk(lastchunk);
637       ChunkHolder chunk = server
638               .pullExecStatistics(j.getJobId(), lastchunk);
639       if (chunk != null)
640       {
641         changed |= chunk.getChunk().length() > 0;
642         response.append(chunk.getChunk());
643         lastchunk = chunk.getNextPosition();
644         try
645         {
646           Thread.sleep(50);
647         } catch (InterruptedException x)
648         {
649         }
650         ;
651       }
652       ;
653     } while (lastchunk >= 0 && j.getLastChunk() != lastchunk);
654     return changed;
655   }
656
657   @Override
658   public void StartJob(AWsJob job)
659   {
660     Exception lex = null;
661     // boiler plate template
662     if (!(job instanceof MsaWSJob))
663     {
664       throw new Error(MessageManager.formatMessage(
665               "error.implementation_error_msawbjob_called",
666               new String[] { job.getClass().toString() }));
667     }
668     MsaWSJob j = (MsaWSJob) job;
669     if (j.isSubmitted())
670     {
671       if (Cache.log.isDebugEnabled())
672       {
673         Cache.log.debug("Tried to submit an already submitted job "
674                 + j.getJobId());
675       }
676       return;
677     }
678     // end boilerplate
679
680     if (j.seqs == null || j.seqs.size() == 0)
681     {
682       // special case - selection consisted entirely of empty sequences...
683       j.setjobStatus(JobStatus.FINISHED);
684       j.setStatus(MessageManager.getString("label.empty_alignment_job"));
685     }
686     try
687     {
688       j.addInitialStatus(); // list the presets/parameters used for the job in
689                             // status
690       if (j.isPresetJob())
691       {
692         j.setJobId(server.presetAlign(j.seqs, j.getServerPreset()));
693       }
694       else if (j.hasArguments())
695       {
696         j.setJobId(server.customAlign(j.seqs, j.getJabaArguments()));
697       }
698       else
699       {
700         j.setJobId(server.align(j.seqs));
701       }
702
703       if (j.getJobId() != null)
704       {
705         j.setSubmitted(true);
706         j.setSubjobComplete(false);
707         // System.out.println(WsURL + " Job Id '" + jobId + "'");
708         return;
709       }
710       else
711       {
712         throw new Exception(MessageManager.formatMessage(
713                 "exception.web_service_returned_null_try_later",
714                 new String[] { WsUrl }));
715       }
716     } catch (compbio.metadata.UnsupportedRuntimeException _lex)
717     {
718       lex = _lex;
719       wsInfo.appendProgressText(MessageManager.formatMessage(
720               "info.job_couldnt_be_run_server_doesnt_support_program",
721               new String[] { _lex.getMessage() }));
722       wsInfo.warnUser(_lex.getMessage(),
723               MessageManager.getString("warn.service_not_supported"));
724       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
725       wsInfo.setStatus(j.getJobnum(),
726               WebserviceInfo.STATE_STOPPED_SERVERERROR);
727     } catch (compbio.metadata.LimitExceededException _lex)
728     {
729       lex = _lex;
730       wsInfo.appendProgressText(MessageManager.formatMessage(
731               "info.job_couldnt_be_run_exceeded_hard_limit",
732               new String[] { _lex.getMessage() }));
733       wsInfo.warnUser(_lex.getMessage(),
734               MessageManager.getString("warn.input_is_too_big"));
735       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
736       wsInfo.setStatus(j.getJobnum(), WebserviceInfo.STATE_STOPPED_ERROR);
737     } catch (compbio.metadata.WrongParameterException _lex)
738     {
739       lex = _lex;
740       wsInfo.warnUser(_lex.getMessage(),
741               MessageManager.getString("warn.invalid_job_param_set"));
742       wsInfo.appendProgressText(MessageManager.formatMessage(
743               "info.job_couldnt_be_run_incorrect_param_setting",
744               new String[] { _lex.getMessage() }));
745       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
746       wsInfo.setStatus(j.getJobnum(), WebserviceInfo.STATE_STOPPED_ERROR);
747     } catch (Error e)
748     {
749       // For unexpected errors
750       System.err
751               .println(WebServiceName
752                       + "Client: Failed to submit the sequences for alignment (probably a server side problem)\n"
753                       + "When contacting Server:" + WsUrl + "\n");
754       e.printStackTrace(System.err);
755       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
756       wsInfo.setStatus(j.getJobnum(),
757               WebserviceInfo.STATE_STOPPED_SERVERERROR);
758     } catch (Exception e)
759     {
760       // For unexpected errors
761       System.err
762               .println(WebServiceName
763                       + "Client: Failed to submit the sequences for alignment (probably a server side problem)\n"
764                       + "When contacting Server:" + WsUrl + "\n");
765       e.printStackTrace(System.err);
766       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
767       wsInfo.setStatus(j.getJobnum(),
768               WebserviceInfo.STATE_STOPPED_SERVERERROR);
769     } finally
770     {
771       if (!j.isSubmitted())
772       {
773         // Boilerplate code here
774         // TODO: JBPNote catch timeout or other fault types explicitly
775
776         j.setAllowedServerExceptions(0);
777         wsInfo.appendProgressText(j.getJobnum(), MessageManager
778                 .getString("info.failed_to_submit_sequences_for_alignment"));
779       }
780     }
781   }
782
783   @Override
784   public void parseResult()
785   {
786     long progbar = System.currentTimeMillis();
787     wsInfo.setProgressBar(
788             MessageManager.getString("status.collecting_job_results"),
789             progbar);
790     int results = 0; // number of result sets received
791     JobStateSummary finalState = new JobStateSummary();
792     try
793     {
794       for (int j = 0; j < jobs.length; j++)
795       {
796         MsaWSJob msjob = ((MsaWSJob) jobs[j]);
797         if (jobs[j].isFinished() && msjob.alignment == null)
798         {
799           int nunchanged = 3, nexcept = 3;
800           boolean jpchanged = false, jpex = false;
801           do
802           {
803             try
804             {
805               jpchanged = updateJobProgress(msjob);
806               jpex = false;
807               if (jpchanged)
808               {
809                 nexcept = 3;
810               }
811             } catch (Exception e)
812             {
813
814               Cache.log
815                       .warn("Exception when retrieving remaining Job progress data for job "
816                               + msjob.getJobId() + " on server " + WsUrl);
817               e.printStackTrace();
818               nexcept--;
819               nunchanged = 3;
820               // set flag remember that we've had an exception.
821               jpex = true;
822               jpchanged = false;
823             }
824             if (!jpchanged)
825             {
826               try
827               {
828                 Thread.sleep(jpex ? 2400 : 1200); // wait a bit longer if we
829                                                   // experienced an exception.
830               } catch (Exception ex)
831               {
832               }
833               ;
834               nunchanged--;
835             }
836           } while (nunchanged > 0 && nexcept > 0);
837
838           if (Cache.log.isDebugEnabled())
839           {
840             System.out.println("Job Execution file for job: "
841                     + msjob.getJobId() + " on server " + WsUrl);
842             System.out.println(msjob.getStatus());
843             System.out.println("*** End of status");
844
845           }
846           try
847           {
848             msjob.alignment = server.getResult(msjob.getJobId());
849           } catch (compbio.metadata.ResultNotAvailableException e)
850           {
851             // job has failed for some reason - probably due to invalid
852             // parameters
853             Cache.log
854                     .debug("Results not available for finished job - marking as broken job.",
855                             e);
856             msjob.jobProgress
857                     .append("\nResult not available. Probably due to invalid input or parameter settings. Server error message below:\n\n"
858                             + e.getLocalizedMessage());
859             msjob.setjobStatus(JobStatus.FAILED);
860           } catch (Exception e)
861           {
862             Cache.log.error("Couldn't get Alignment for job.", e);
863             // TODO: Increment count and retry ?
864             msjob.setjobStatus(JobStatus.UNDEFINED);
865           }
866         }
867         finalState.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
868         if (jobs[j].isSubmitted() && jobs[j].isSubjobComplete()
869                 && jobs[j].hasResults())
870         {
871           results++;
872           compbio.data.sequence.Alignment alignment = ((MsaWSJob) jobs[j]).alignment;
873           if (alignment != null)
874           {
875             // server.close(jobs[j].getJobnum());
876             // wsInfo.appendProgressText(jobs[j].getJobnum(),
877             // "\nAlignment Object Method Notes\n");
878             // wsInfo.appendProgressText(jobs[j].getJobnum(),
879             // "Calculated with "+alignment.getMetadata().getProgram().toString());
880             // JBPNote The returned files from a webservice could be
881             // hidden behind icons in the monitor window that,
882             // when clicked, pop up their corresponding data
883           }
884         }
885       }
886     } catch (Exception ex)
887     {
888
889       Cache.log.error("Unexpected exception when processing results for "
890               + alTitle, ex);
891       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
892     }
893     if (results > 0)
894     {
895       wsInfo.showResultsNewFrame
896               .addActionListener(new java.awt.event.ActionListener()
897               {
898                 @Override
899                 public void actionPerformed(java.awt.event.ActionEvent evt)
900                 {
901                   displayResults(true);
902                 }
903               });
904       wsInfo.mergeResults
905               .addActionListener(new java.awt.event.ActionListener()
906               {
907                 @Override
908                 public void actionPerformed(java.awt.event.ActionEvent evt)
909                 {
910                   displayResults(false);
911                 }
912               });
913       wsInfo.setResultsReady();
914     }
915     else
916     {
917       wsInfo.setFinishedNoResults();
918     }
919     updateGlobalStatus(finalState);
920     wsInfo.setProgressBar(null, progbar);
921   }
922
923   /**
924    * Display alignment results in a new frame (or - not currently supported -
925    * added to an existing alignment).
926    * 
927    * @param newFrame
928    */
929   void displayResults(boolean newFrame)
930   {
931     // view input or result data for each block
932     List<AlignmentOrder> alorders = new ArrayList<>();
933     SequenceI[][] results = new SequenceI[jobs.length][];
934     AlignmentOrder[] orders = new AlignmentOrder[jobs.length];
935     String lastProgram = null;
936     MsaWSJob msjob;
937     for (int j = 0; j < jobs.length; j++)
938     {
939       if (jobs[j].hasResults())
940       {
941         msjob = (MsaWSJob) jobs[j];
942         Object[] res = msjob.getAlignment();
943         lastProgram = msjob.getAlignmentProgram();
944         alorders.add((AlignmentOrder) res[1]);
945         results[j] = (SequenceI[]) res[0];
946         orders[j] = (AlignmentOrder) res[1];
947
948         // SequenceI[] alignment = input.getUpdated
949       }
950       else
951       {
952         results[j] = null;
953       }
954     }
955     Object[] newview = input.getUpdatedView(results, orders, getGapChar());
956     // trash references to original result data
957     for (int j = 0; j < jobs.length; j++)
958     {
959       results[j] = null;
960       orders[j] = null;
961     }
962     SequenceI[] alignment = (SequenceI[]) newview[0];
963     HiddenColumns hidden = (HiddenColumns) newview[1];
964     Alignment al = new Alignment(alignment);
965     // TODO: add 'provenance' property to alignment from the method notes
966     if (lastProgram != null)
967     {
968       al.setProperty("Alignment Program", lastProgram);
969     }
970     // accompanying each subjob
971     if (dataset != null)
972     {
973       al.setDataset(dataset);
974     }
975
976     propagateDatasetMappings(al);
977     // JBNote- TODO: warn user if a block is input rather than aligned data ?
978
979     if (newFrame)
980     {
981       displayInNewFrame(al, alorders, hidden);
982     }
983     else
984     {
985       // TODO 2.9.x feature
986       System.out.println("MERGE WITH OLD FRAME");
987       // TODO: modify alignment in original frame, replacing old for new
988       // alignment using the commands.EditCommand model to ensure the update can
989       // be undone
990     }
991   }
992
993   /**
994    * Display the alignment result in a new frame.
995    * 
996    * @param al
997    * @param alorders
998    * @param columnselection
999    */
1000   protected void displayInNewFrame(AlignmentI al,
1001           List<AlignmentOrder> alorders, HiddenColumns hidden)
1002   {
1003     AlignFrame af = new AlignFrame(al, hidden,
1004             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
1005
1006     // initialise with same renderer settings as in parent alignframe.
1007     af.getFeatureRenderer().transferSettings(this.featureSettings);
1008
1009     if (alorders.size() > 0)
1010     {
1011       addSortByMenuItems(af, alorders);
1012     }
1013
1014     // TODO: refactor retrieve and show as new splitFrame as Desktop method
1015
1016     /*
1017      * If alignment was requested from one half of a SplitFrame, show in a
1018      * SplitFrame with the other pane similarly aligned.
1019      */
1020     AlignFrame requestedBy = getRequestingAlignFrame();
1021     if (requestedBy != null
1022             && requestedBy.getSplitViewContainer() != null
1023             && requestedBy.getSplitViewContainer().getComplement(
1024                     requestedBy) != null)
1025     {
1026       AlignmentI complement = requestedBy.getSplitViewContainer()
1027               .getComplement(requestedBy);
1028       String complementTitle = requestedBy.getSplitViewContainer()
1029               .getComplementTitle(requestedBy);
1030       // becomes null if the alignment window was closed before the alignment
1031       // job finished.
1032       AlignmentI copyComplement = new Alignment(complement);
1033       // todo should this be done by copy constructor?
1034       copyComplement.setGapCharacter(complement.getGapCharacter());
1035       // share the same dataset (and the mappings it holds)
1036       copyComplement.setDataset(complement.getDataset());
1037       copyComplement.alignAs(al);
1038       if (copyComplement.getHeight() > 0)
1039       {
1040         af.setTitle(alTitle);
1041         AlignFrame af2 = new AlignFrame(copyComplement,
1042                 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
1043         af2.setTitle(complementTitle);
1044         String linkedTitle = MessageManager
1045                 .getString("label.linked_view_title");
1046         JInternalFrame splitFrame = new SplitFrame(al.isNucleotide() ? af
1047                 : af2, al.isNucleotide() ? af2 : af);
1048         Desktop.addInternalFrame(splitFrame, linkedTitle, -1, -1);
1049         return;
1050       }
1051     }
1052
1053     /*
1054      * Not from SplitFrame, or failed to created a complementary alignment
1055      */
1056     Desktop.addInternalFrame(af, alTitle, AlignFrame.DEFAULT_WIDTH,
1057             AlignFrame.DEFAULT_HEIGHT);
1058   }
1059
1060   /**
1061    * Add sort order options to the AlignFrame menus.
1062    * 
1063    * @param af
1064    * @param alorders
1065    */
1066   protected void addSortByMenuItems(AlignFrame af,
1067           List<AlignmentOrder> alorders)
1068   {
1069     // update orders
1070     if (alorders.size() == 1)
1071     {
1072       af.addSortByOrderMenuItem(WebServiceName + " Ordering",
1073               alorders.get(0));
1074     }
1075     else
1076     {
1077       // construct a non-redundant ordering set
1078       List<String> names = new ArrayList<>();
1079       for (int i = 0, l = alorders.size(); i < l; i++)
1080       {
1081         String orderName = " Region " + i;
1082         int j = i + 1;
1083
1084         while (j < l)
1085         {
1086           if (alorders.get(i).equals(alorders.get(j)))
1087           {
1088             alorders.remove(j);
1089             l--;
1090             orderName += "," + j;
1091           }
1092           else
1093           {
1094             j++;
1095           }
1096         }
1097
1098         if (i == 0 && j == 1)
1099         {
1100           names.add("");
1101         }
1102         else
1103         {
1104           names.add(orderName);
1105         }
1106       }
1107       for (int i = 0, l = alorders.size(); i < l; i++)
1108       {
1109         af.addSortByOrderMenuItem(WebServiceName + (names.get(i))
1110                 + " Ordering", alorders.get(i));
1111       }
1112     }
1113   }
1114
1115   @Override
1116   public boolean canMergeResults()
1117   {
1118     return false;
1119   }
1120 }