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