JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / ws / jws2 / MsaWSThread.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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(
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
144         String newname = jalview.analysis.SeqsetUtils.unique_name(i); // same
145         // for
146         // any
147         // subjob
148         SeqNames.put(newname,
149                 jalview.analysis.SeqsetUtils.SeqCharacterHash(seqs[i]));
150         if (valid && seqs[i].getEnd() - seqs[i].getStart() > minlen - 1)
151         {
152           // make new input sequence with or without gaps
153           seq = new compbio.data.sequence.FastaSequence(newname,
154                   (submitGaps) ? seqs[i].getSequenceAsString()
155                           : AlignSeq.extractGaps(
156                                   jalview.util.Comparison.GapChars,
157                                   seqs[i].getSequenceAsString()));
158           this.seqs.add(seq);
159         }
160         else
161         {
162           String empty = null;
163           if (seqs[i].getEnd() >= seqs[i].getStart())
164           {
165             empty = (submitGaps) ? seqs[i].getSequenceAsString() : AlignSeq
166                     .extractGaps(jalview.util.Comparison.GapChars,
167                             seqs[i].getSequenceAsString());
168           }
169           emptySeqs.add(new String[] { newname, empty });
170         }
171       }
172       return valid;
173     }
174
175     /**
176      * 
177      * @return true if getAlignment will return a valid alignment result.
178      */
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     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
533   public boolean isCancellable()
534   {
535     return true;
536   }
537
538   public void cancelJob()
539   {
540     if (!jobComplete && jobs != null)
541     {
542       boolean cancelled = true;
543       for (int job = 0; job < jobs.length; job++)
544       {
545         if (jobs[job].isSubmitted() && !jobs[job].isSubjobComplete())
546         {
547           String cancelledMessage = "";
548           try
549           {
550             boolean cancelledJob = server.cancelJob(jobs[job].getJobId());
551             if (true) // cancelledJob || true)
552             {
553               // CANCELLED_JOB
554               // if the Jaba server indicates the job can't be cancelled, its
555               // because its running on the server's local execution engine
556               // so we just close the window anyway.
557               cancelledMessage = "Job cancelled.";
558               ((MsaWSJob) jobs[job]).cancel(); // TODO: refactor to avoid this
559                                                // ugliness -
560               wsInfo.setStatus(jobs[job].getJobnum(),
561                       WebserviceInfo.STATE_CANCELLED_OK);
562             }
563             else
564             {
565               // VALID UNSTOPPABLE JOB
566               cancelledMessage += "Server cannot cancel this job. just close the window.\n";
567               cancelled = false;
568               // wsInfo.setStatus(jobs[job].jobnum,
569               // WebserviceInfo.STATE_RUNNING);
570             }
571           } catch (Exception exc)
572           {
573             cancelledMessage += ("\nProblems cancelling the job : Exception received...\n"
574                     + exc + "\n");
575             Cache.log.warn(
576                     "Exception whilst cancelling " + jobs[job].getJobId(),
577                     exc);
578           }
579           wsInfo.setProgressText(jobs[job].getJobnum(), OutputHeader
580                   + cancelledMessage + "\n");
581         }
582         else
583         {
584           // if we hadn't submitted then just mark the job as cancelled.
585           jobs[job].setSubjobComplete(true);
586           wsInfo.setStatus(jobs[job].getJobnum(),
587                   WebserviceInfo.STATE_CANCELLED_OK);
588
589         }
590       }
591       if (cancelled)
592       {
593         wsInfo.setStatus(WebserviceInfo.STATE_CANCELLED_OK);
594         jobComplete = true;
595       }
596       this.interrupt(); // kick thread to update job states.
597     }
598     else
599     {
600       if (!jobComplete)
601       {
602         wsInfo.setProgressText(OutputHeader
603                 + "Server cannot cancel this job because it has not been submitted properly. just close the window.\n");
604       }
605     }
606   }
607
608   public void pollJob(AWsJob job) throws Exception
609   {
610     // TODO: investigate if we still need to cast here in J1.6
611     MsaWSJob j = ((MsaWSJob) job);
612     // this is standard code, but since the interface doesn't comprise of a
613     // basic one that implements (getJobStatus, pullExecStatistics) we have to
614     // repeat the code for all jw2s services.
615     j.setjobStatus(server.getJobStatus(job.getJobId()));
616     updateJobProgress(j);
617   }
618
619   /**
620    * 
621    * @param j
622    * @return true if more job progress data was available
623    * @throws Exception
624    */
625   protected boolean updateJobProgress(MsaWSJob j) throws Exception
626   {
627     StringBuffer response = j.jobProgress;
628     long lastchunk = j.getLastChunk();
629     boolean changed = false;
630     do
631     {
632       j.setLastChunk(lastchunk);
633       ChunkHolder chunk = server
634               .pullExecStatistics(j.getJobId(), lastchunk);
635       if (chunk != null)
636       {
637         changed |= chunk.getChunk().length() > 0;
638         response.append(chunk.getChunk());
639         lastchunk = chunk.getNextPosition();
640         try
641         {
642           Thread.sleep(50);
643         } catch (InterruptedException x)
644         {
645         }
646         ;
647       }
648       ;
649     } while (lastchunk >= 0 && j.getLastChunk() != lastchunk);
650     return changed;
651   }
652
653   public void StartJob(AWsJob job)
654   {
655     Exception lex = null;
656     // boiler plate template
657     if (!(job instanceof MsaWSJob))
658     {
659       throw new Error(MessageManager.formatMessage(
660               "error.implementation_error_msawbjob_called",
661               new String[] { job.getClass().toString() }));
662     }
663     MsaWSJob j = (MsaWSJob) job;
664     if (j.isSubmitted())
665     {
666       if (Cache.log.isDebugEnabled())
667       {
668         Cache.log.debug("Tried to submit an already submitted job "
669                 + j.getJobId());
670       }
671       return;
672     }
673     // end boilerplate
674
675     if (j.seqs == null || j.seqs.size() == 0)
676     {
677       // special case - selection consisted entirely of empty sequences...
678       j.setjobStatus(JobStatus.FINISHED);
679       j.setStatus(MessageManager.getString("label.empty_alignment_job"));
680     }
681     try
682     {
683       j.addInitialStatus(); // list the presets/parameters used for the job in
684                             // status
685       if (j.isPresetJob())
686       {
687         j.setJobId(server.presetAlign(j.seqs, j.getServerPreset()));
688       }
689       else if (j.hasArguments())
690       {
691         j.setJobId(server.customAlign(j.seqs, j.getJabaArguments()));
692       }
693       else
694       {
695         j.setJobId(server.align(j.seqs));
696       }
697
698       if (j.getJobId() != null)
699       {
700         j.setSubmitted(true);
701         j.setSubjobComplete(false);
702         // System.out.println(WsURL + " Job Id '" + jobId + "'");
703         return;
704       }
705       else
706       {
707         throw new Exception(MessageManager.formatMessage(
708                 "exception.web_service_returned_null_try_later",
709                 new String[] { WsUrl }));
710       }
711     } catch (compbio.metadata.UnsupportedRuntimeException _lex)
712     {
713       lex = _lex;
714       wsInfo.appendProgressText(MessageManager.formatMessage(
715               "info.job_couldnt_be_run_server_doesnt_support_program",
716               new String[] { _lex.getMessage() }));
717       wsInfo.warnUser(_lex.getMessage(),
718               MessageManager.getString("warn.service_not_supported"));
719       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
720       wsInfo.setStatus(j.getJobnum(),
721               WebserviceInfo.STATE_STOPPED_SERVERERROR);
722     } catch (compbio.metadata.LimitExceededException _lex)
723     {
724       lex = _lex;
725       wsInfo.appendProgressText(MessageManager.formatMessage(
726               "info.job_couldnt_be_run_exceeded_hard_limit",
727               new String[] { _lex.getMessage() }));
728       wsInfo.warnUser(_lex.getMessage(),
729               MessageManager.getString("warn.input_is_too_big"));
730       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
731       wsInfo.setStatus(j.getJobnum(), WebserviceInfo.STATE_STOPPED_ERROR);
732     } catch (compbio.metadata.WrongParameterException _lex)
733     {
734       lex = _lex;
735       wsInfo.warnUser(_lex.getMessage(),
736               MessageManager.getString("warn.invalid_job_param_set"));
737       wsInfo.appendProgressText(MessageManager.formatMessage(
738               "info.job_couldnt_be_run_incorrect_param_setting",
739               new String[] { _lex.getMessage() }));
740       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
741       wsInfo.setStatus(j.getJobnum(), WebserviceInfo.STATE_STOPPED_ERROR);
742     } catch (Error e)
743     {
744       // For unexpected errors
745       System.err
746               .println(WebServiceName
747                       + "Client: Failed to submit the sequences for alignment (probably a server side problem)\n"
748                       + "When contacting Server:" + WsUrl + "\n");
749       e.printStackTrace(System.err);
750       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
751       wsInfo.setStatus(j.getJobnum(),
752               WebserviceInfo.STATE_STOPPED_SERVERERROR);
753     } catch (Exception e)
754     {
755       // For unexpected errors
756       System.err
757               .println(WebServiceName
758                       + "Client: Failed to submit the sequences for alignment (probably a server side problem)\n"
759                       + "When contacting Server:" + WsUrl + "\n");
760       e.printStackTrace(System.err);
761       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
762       wsInfo.setStatus(j.getJobnum(),
763               WebserviceInfo.STATE_STOPPED_SERVERERROR);
764     } finally
765     {
766       if (!j.isSubmitted())
767       {
768         // Boilerplate code here
769         // TODO: JBPNote catch timeout or other fault types explicitly
770
771         j.setAllowedServerExceptions(0);
772         wsInfo.appendProgressText(j.getJobnum(), MessageManager
773                 .getString("info.failed_to_submit_sequences_for_alignment"));
774       }
775     }
776   }
777
778   public void parseResult()
779   {
780     long progbar = System.currentTimeMillis();
781     wsInfo.setProgressBar(
782             MessageManager.getString("status.collecting_job_results"),
783             progbar);
784     int results = 0; // number of result sets received
785     JobStateSummary finalState = new JobStateSummary();
786     try
787     {
788       for (int j = 0; j < jobs.length; j++)
789       {
790         MsaWSJob msjob = ((MsaWSJob) jobs[j]);
791         if (jobs[j].isFinished() && msjob.alignment == null)
792         {
793           int nunchanged = 3, nexcept = 3;
794           boolean jpchanged = false, jpex = false;
795           do
796           {
797             try
798             {
799               jpchanged = updateJobProgress(msjob);
800               jpex = false;
801               if (jpchanged)
802               {
803                 nexcept = 3;
804               }
805             } catch (Exception e)
806             {
807
808               Cache.log
809                       .warn("Exception when retrieving remaining Job progress data for job "
810                               + msjob.getJobId() + " on server " + WsUrl);
811               e.printStackTrace();
812               nexcept--;
813               nunchanged = 3;
814               // set flag remember that we've had an exception.
815               jpex = true;
816               jpchanged = false;
817             }
818             if (!jpchanged)
819             {
820               try
821               {
822                 Thread.sleep(jpex ? 2400 : 1200); // wait a bit longer if we
823                                                   // experienced an exception.
824               } catch (Exception ex)
825               {
826               }
827               ;
828               nunchanged--;
829             }
830           } while (nunchanged > 0 && nexcept > 0);
831
832           if (Cache.log.isDebugEnabled())
833           {
834             System.out.println("Job Execution file for job: "
835                     + msjob.getJobId() + " on server " + WsUrl);
836             System.out.println(msjob.getStatus());
837             System.out.println("*** End of status");
838
839           }
840           try
841           {
842             msjob.alignment = server.getResult(msjob.getJobId());
843           } catch (compbio.metadata.ResultNotAvailableException e)
844           {
845             // job has failed for some reason - probably due to invalid
846             // parameters
847             Cache.log
848                     .debug("Results not available for finished job - marking as broken job.",
849                             e);
850             msjob.jobProgress
851                     .append("\nResult not available. Probably due to invalid input or parameter settings. Server error message below:\n\n"
852                             + e.getLocalizedMessage());
853             msjob.setjobStatus(JobStatus.FAILED);
854           } catch (Exception e)
855           {
856             Cache.log.error("Couldn't get Alignment for job.", e);
857             // TODO: Increment count and retry ?
858             msjob.setjobStatus(JobStatus.UNDEFINED);
859           }
860         }
861         finalState.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
862         if (jobs[j].isSubmitted() && jobs[j].isSubjobComplete()
863                 && jobs[j].hasResults())
864         {
865           results++;
866           compbio.data.sequence.Alignment alignment = ((MsaWSJob) jobs[j]).alignment;
867           if (alignment != null)
868           {
869             // server.close(jobs[j].getJobnum());
870             // wsInfo.appendProgressText(jobs[j].getJobnum(),
871             // "\nAlignment Object Method Notes\n");
872             // wsInfo.appendProgressText(jobs[j].getJobnum(),
873             // "Calculated with "+alignment.getMetadata().getProgram().toString());
874             // JBPNote The returned files from a webservice could be
875             // hidden behind icons in the monitor window that,
876             // when clicked, pop up their corresponding data
877           }
878         }
879       }
880     } catch (Exception ex)
881     {
882
883       Cache.log.error("Unexpected exception when processing results for "
884               + alTitle, ex);
885       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
886     }
887     if (results > 0)
888     {
889       wsInfo.showResultsNewFrame
890               .addActionListener(new java.awt.event.ActionListener()
891               {
892                 public void actionPerformed(java.awt.event.ActionEvent evt)
893                 {
894                   displayResults(true);
895                 }
896               });
897       wsInfo.mergeResults
898               .addActionListener(new java.awt.event.ActionListener()
899               {
900                 public void actionPerformed(java.awt.event.ActionEvent evt)
901                 {
902                   displayResults(false);
903                 }
904               });
905       wsInfo.setResultsReady();
906     }
907     else
908     {
909       wsInfo.setFinishedNoResults();
910     }
911     updateGlobalStatus(finalState);
912     wsInfo.setProgressBar(null, progbar);
913   }
914
915   /**
916    * Display alignment results in a new frame (or - not currently supported -
917    * added to an existing alignment).
918    * 
919    * @param newFrame
920    */
921   void displayResults(boolean newFrame)
922   {
923     // view input or result data for each block
924     List<AlignmentOrder> alorders = new ArrayList<AlignmentOrder>();
925     SequenceI[][] results = new SequenceI[jobs.length][];
926     AlignmentOrder[] orders = new AlignmentOrder[jobs.length];
927     String lastProgram = null;
928     MsaWSJob msjob;
929     for (int j = 0; j < jobs.length; j++)
930     {
931       if (jobs[j].hasResults())
932       {
933         msjob = (MsaWSJob) jobs[j];
934         Object[] res = msjob.getAlignment();
935         lastProgram = msjob.getAlignmentProgram();
936         alorders.add((AlignmentOrder) res[1]);
937         results[j] = (SequenceI[]) res[0];
938         orders[j] = (AlignmentOrder) res[1];
939
940         // SequenceI[] alignment = input.getUpdated
941       }
942       else
943       {
944         results[j] = null;
945       }
946     }
947     Object[] newview = input.getUpdatedView(results, orders, getGapChar());
948     // trash references to original result data
949     for (int j = 0; j < jobs.length; j++)
950     {
951       results[j] = null;
952       orders[j] = null;
953     }
954     SequenceI[] alignment = (SequenceI[]) newview[0];
955     ColumnSelection columnselection = (ColumnSelection) newview[1];
956     Alignment al = new Alignment(alignment);
957     // TODO: add 'provenance' property to alignment from the method notes
958     if (lastProgram != null)
959     {
960       al.setProperty("Alignment Program", lastProgram);
961     }
962     // accompanying each subjob
963     if (dataset != null)
964     {
965       al.setDataset(dataset);
966     }
967
968     propagateDatasetMappings(al);
969     // JBNote- TODO: warn user if a block is input rather than aligned data ?
970
971     if (newFrame)
972     {
973       displayInNewFrame(al, alorders, columnselection);
974
975     }
976     else
977     {
978       // TODO 2.9.x feature
979       System.out.println("MERGE WITH OLD FRAME");
980       // TODO: modify alignment in original frame, replacing old for new
981       // alignment using the commands.EditCommand model to ensure the update can
982       // be undone
983     }
984   }
985
986   /**
987    * Display the alignment result in a new frame.
988    * 
989    * @param al
990    * @param alorders
991    * @param columnselection
992    */
993   protected void displayInNewFrame(AlignmentI al,
994           List<AlignmentOrder> alorders, ColumnSelection columnselection)
995   {
996     AlignFrame af = new AlignFrame(al, columnselection,
997             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
998
999     // initialise with same renderer settings as in parent alignframe.
1000     af.getFeatureRenderer().transferSettings(this.featureSettings);
1001
1002     if (alorders.size() > 0)
1003     {
1004       addSortByMenuItems(af, alorders);
1005     }
1006
1007     // TODO: refactor retrieve and show as new splitFrame as Desktop method
1008
1009     /*
1010      * If alignment was requested from one half of a SplitFrame, show in a
1011      * SplitFrame with the other pane similarly aligned.
1012      */
1013     AlignFrame requestedBy = getRequestingAlignFrame();
1014     if (requestedBy != null
1015             && requestedBy.getSplitViewContainer() != null
1016             && requestedBy.getSplitViewContainer().getComplement(
1017                     requestedBy) != null)
1018     {
1019       AlignmentI complement = requestedBy.getSplitViewContainer()
1020               .getComplement(requestedBy);
1021       String complementTitle = requestedBy.getSplitViewContainer()
1022               .getComplementTitle(requestedBy);
1023       // becomes null if the alignment window was closed before the alignment
1024       // job finished.
1025       AlignmentI copyComplement = new Alignment(complement);
1026       copyComplement.alignAs(al);
1027       if (copyComplement.getHeight() > 0)
1028       {
1029         af.setTitle(alTitle);
1030         AlignFrame af2 = new AlignFrame(copyComplement,
1031                 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
1032         af2.setTitle(complementTitle);
1033         String linkedTitle = MessageManager
1034                 .getString("label.linked_view_title");
1035         JInternalFrame splitFrame = new SplitFrame(al.isNucleotide() ? af
1036                 : af2, al.isNucleotide() ? af2 : af);
1037         Desktop.addInternalFrame(splitFrame, linkedTitle, -1, -1);
1038         return;
1039       }
1040     }
1041
1042     /*
1043      * Not from SplitFrame, or failed to created a complementary alignment
1044      */
1045     Desktop.addInternalFrame(af, alTitle, AlignFrame.DEFAULT_WIDTH,
1046             AlignFrame.DEFAULT_HEIGHT);
1047   }
1048
1049   /**
1050    * Add sort order options to the AlignFrame menus.
1051    * 
1052    * @param af
1053    * @param alorders
1054    */
1055   protected void addSortByMenuItems(AlignFrame af,
1056           List<AlignmentOrder> alorders)
1057   {
1058     // update orders
1059     if (alorders.size() == 1)
1060     {
1061       af.addSortByOrderMenuItem(WebServiceName + " Ordering",
1062               alorders.get(0));
1063     }
1064     else
1065     {
1066       // construct a non-redundant ordering set
1067       List<String> names = new ArrayList<String>();
1068       for (int i = 0, l = alorders.size(); i < l; i++)
1069       {
1070         String orderName = " Region " + i;
1071         int j = i + 1;
1072
1073         while (j < l)
1074         {
1075           if (alorders.get(i).equals(alorders.get(j)))
1076           {
1077             alorders.remove(j);
1078             l--;
1079             orderName += "," + j;
1080           }
1081           else
1082           {
1083             j++;
1084           }
1085         }
1086
1087         if (i == 0 && j == 1)
1088         {
1089           names.add("");
1090         }
1091         else
1092         {
1093           names.add(orderName);
1094         }
1095       }
1096       for (int i = 0, l = alorders.size(); i < l; i++)
1097       {
1098         af.addSortByOrderMenuItem(WebServiceName + (names.get(i))
1099                 + " Ordering", alorders.get(i));
1100       }
1101     }
1102   }
1103
1104   public boolean canMergeResults()
1105   {
1106     return false;
1107   }
1108 }