JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / ws / jws2 / MsaWSThread.java
index d2bb897..40e6de7 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
+ * Copyright (C) 2015 The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -23,6 +23,7 @@ package jalview.ws.jws2;
 import jalview.analysis.AlignSeq;
 import jalview.bin.Cache;
 import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AlignmentOrder;
 import jalview.datamodel.AlignmentView;
 import jalview.datamodel.ColumnSelection;
@@ -30,6 +31,7 @@ import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
 import jalview.gui.Desktop;
+import jalview.gui.SplitFrame;
 import jalview.gui.WebserviceInfo;
 import jalview.util.MessageManager;
 import jalview.ws.AWsJob;
@@ -44,6 +46,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Vector;
 
+import javax.swing.JInternalFrame;
+
 import compbio.data.msa.MsaWS;
 import compbio.metadata.Argument;
 import compbio.metadata.ChunkHolder;
@@ -121,7 +125,9 @@ class MsaWSThread extends AWS2Thread implements WSClientI
       int nseqs = 0;
       if (minlen < 0)
       {
-        throw new Error(MessageManager.getString("error.implementation_error_minlen_must_be_greater_zero"));
+        throw new Error(
+                MessageManager
+                        .getString("error.implementation_error_minlen_must_be_greater_zero"));
       }
       for (int i = 0; i < seqs.length; i++)
       {
@@ -160,8 +166,7 @@ class MsaWSThread extends AWS2Thread implements WSClientI
                     .extractGaps(jalview.util.Comparison.GapChars,
                             seqs[i].getSequenceAsString());
           }
-          emptySeqs.add(new String[]
-          { newname, empty });
+          emptySeqs.add(new String[] { newname, empty });
         }
       }
       return valid;
@@ -292,8 +297,7 @@ class MsaWSThread extends AWS2Thread implements WSClientI
         jalview.analysis.AlignmentSorter.recoverOrder(alseqs);
         // account for any missing sequences
         jalview.analysis.SeqsetUtils.deuniquify(SeqNames, alseqs);
-        return new Object[]
-        { alseqs, msaorder };
+        return new Object[] { alseqs, msaorder };
       }
       return null;
     }
@@ -447,7 +451,7 @@ class MsaWSThread extends AWS2Thread implements WSClientI
    * @param presorder
    *          boolean
    */
-  MsaWSThread(MsaWS server, String wsUrl, WebserviceInfo wsinfo,
+  private MsaWSThread(MsaWS server, String wsUrl, WebserviceInfo wsinfo,
           jalview.gui.AlignFrame alFrame, AlignmentView alview,
           String wsname, boolean subgaps, boolean presorder)
   {
@@ -525,6 +529,7 @@ class MsaWSThread extends AWS2Thread implements WSClientI
   {
     return validInput;
   }
+
   public boolean isCancellable()
   {
     return true;
@@ -651,7 +656,9 @@ class MsaWSThread extends AWS2Thread implements WSClientI
     // boiler plate template
     if (!(job instanceof MsaWSJob))
     {
-      throw new Error(MessageManager.formatMessage("error.implementation_error_msawbjob_called", new String[]{job.getClass().toString()}));
+      throw new Error(MessageManager.formatMessage(
+              "error.implementation_error_msawbjob_called",
+              new String[] { job.getClass().toString() }));
     }
     MsaWSJob j = (MsaWSJob) job;
     if (j.isSubmitted())
@@ -697,28 +704,39 @@ class MsaWSThread extends AWS2Thread implements WSClientI
       }
       else
       {
-        throw new Exception(MessageManager.formatMessage("exception.web_service_returned_null_try_later", new String[]{WsUrl}));
+        throw new Exception(MessageManager.formatMessage(
+                "exception.web_service_returned_null_try_later",
+                new String[] { WsUrl }));
       }
     } catch (compbio.metadata.UnsupportedRuntimeException _lex)
     {
       lex = _lex;
-      wsInfo.appendProgressText(MessageManager.formatMessage("info.job_couldnt_be_run_server_doesnt_support_program", new String[]{_lex.getMessage()}));
-      wsInfo.warnUser(_lex.getMessage(), MessageManager.getString("warn.service_not_supported"));
+      wsInfo.appendProgressText(MessageManager.formatMessage(
+              "info.job_couldnt_be_run_server_doesnt_support_program",
+              new String[] { _lex.getMessage() }));
+      wsInfo.warnUser(_lex.getMessage(),
+              MessageManager.getString("warn.service_not_supported"));
       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
       wsInfo.setStatus(j.getJobnum(),
               WebserviceInfo.STATE_STOPPED_SERVERERROR);
     } catch (compbio.metadata.LimitExceededException _lex)
     {
       lex = _lex;
-      wsInfo.appendProgressText(MessageManager.formatMessage("info.job_couldnt_be_run_exceeded_hard_limit", new String[]{_lex.getMessage()}));
-      wsInfo.warnUser(_lex.getMessage(), MessageManager.getString("warn.input_is_too_big"));
+      wsInfo.appendProgressText(MessageManager.formatMessage(
+              "info.job_couldnt_be_run_exceeded_hard_limit",
+              new String[] { _lex.getMessage() }));
+      wsInfo.warnUser(_lex.getMessage(),
+              MessageManager.getString("warn.input_is_too_big"));
       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
       wsInfo.setStatus(j.getJobnum(), WebserviceInfo.STATE_STOPPED_ERROR);
     } catch (compbio.metadata.WrongParameterException _lex)
     {
       lex = _lex;
-      wsInfo.warnUser(_lex.getMessage(), MessageManager.getString("warn.invalid_job_param_set"));
-      wsInfo.appendProgressText(MessageManager.formatMessage("info.job_couldnt_be_run_incorrect_param_setting", new String[]{_lex.getMessage()}));
+      wsInfo.warnUser(_lex.getMessage(),
+              MessageManager.getString("warn.invalid_job_param_set"));
+      wsInfo.appendProgressText(MessageManager.formatMessage(
+              "info.job_couldnt_be_run_incorrect_param_setting",
+              new String[] { _lex.getMessage() }));
       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
       wsInfo.setStatus(j.getJobnum(), WebserviceInfo.STATE_STOPPED_ERROR);
     } catch (Error e)
@@ -751,8 +769,8 @@ class MsaWSThread extends AWS2Thread implements WSClientI
         // TODO: JBPNote catch timeout or other fault types explicitly
 
         j.setAllowedServerExceptions(0);
-        wsInfo.appendProgressText(j.getJobnum(),
-                MessageManager.getString("info.failed_to_submit_sequences_for_alignment"));
+        wsInfo.appendProgressText(j.getJobnum(), MessageManager
+                .getString("info.failed_to_submit_sequences_for_alignment"));
       }
     }
   }
@@ -760,7 +778,9 @@ class MsaWSThread extends AWS2Thread implements WSClientI
   public void parseResult()
   {
     long progbar = System.currentTimeMillis();
-    wsInfo.setProgressBar(MessageManager.getString("status.collecting_job_results"), progbar);
+    wsInfo.setProgressBar(
+            MessageManager.getString("status.collecting_job_results"),
+            progbar);
     int results = 0; // number of result sets received
     JobStateSummary finalState = new JobStateSummary();
     try
@@ -892,10 +912,16 @@ class MsaWSThread extends AWS2Thread implements WSClientI
     wsInfo.setProgressBar(null, progbar);
   }
 
+  /**
+   * Display alignment results in a new frame (or - not currently supported -
+   * added to an existing alignment).
+   * 
+   * @param newFrame
+   */
   void displayResults(boolean newFrame)
   {
     // view input or result data for each block
-    Vector alorders = new Vector();
+    List<AlignmentOrder> alorders = new ArrayList<AlignmentOrder>();
     SequenceI[][] results = new SequenceI[jobs.length][];
     AlignmentOrder[] orders = new AlignmentOrder[jobs.length];
     String lastProgram = null;
@@ -907,7 +933,7 @@ class MsaWSThread extends AWS2Thread implements WSClientI
         msjob = (MsaWSJob) jobs[j];
         Object[] res = msjob.getAlignment();
         lastProgram = msjob.getAlignmentProgram();
-        alorders.add(res[1]);
+        alorders.add((AlignmentOrder) res[1]);
         results[j] = (SequenceI[]) res[0];
         orders[j] = (AlignmentOrder) res[1];
 
@@ -944,71 +970,134 @@ class MsaWSThread extends AWS2Thread implements WSClientI
 
     if (newFrame)
     {
-      AlignFrame af = new AlignFrame(al, columnselection,
-              AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
+      displayInNewFrame(al, alorders, columnselection);
 
-      // initialise with same renderer settings as in parent alignframe.
-      af.getFeatureRenderer().transferSettings(this.featureSettings);
-      // update orders
-      if (alorders.size() > 0)
-      {
-        if (alorders.size() == 1)
-        {
-          af.addSortByOrderMenuItem(WebServiceName + " Ordering",
-                  (AlignmentOrder) alorders.get(0));
-        }
-        else
-        {
-          // construct a non-redundant ordering set
-          Vector names = new Vector();
-          for (int i = 0, l = alorders.size(); i < l; i++)
-          {
-            String orderName = new String(" Region " + i);
-            int j = i + 1;
+    }
+    else
+    {
+      // TODO 2.9.x feature
+      System.out.println("MERGE WITH OLD FRAME");
+      // TODO: modify alignment in original frame, replacing old for new
+      // alignment using the commands.EditCommand model to ensure the update can
+      // be undone
+    }
+  }
 
-            while (j < l)
-            {
-              if (((AlignmentOrder) alorders.get(i))
-                      .equals(((AlignmentOrder) alorders.get(j))))
-              {
-                alorders.remove(j);
-                l--;
-                orderName += "," + j;
-              }
-              else
-              {
-                j++;
-              }
-            }
+  /**
+   * Display the alignment result in a new frame.
+   * 
+   * @param al
+   * @param alorders
+   * @param columnselection
+   */
+  protected void displayInNewFrame(AlignmentI al,
+          List<AlignmentOrder> alorders, ColumnSelection columnselection)
+  {
+    AlignFrame af = new AlignFrame(al, columnselection,
+            AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
 
-            if (i == 0 && j == 1)
-            {
-              names.add(new String(""));
-            }
-            else
-            {
-              names.add(orderName);
-            }
-          }
-          for (int i = 0, l = alorders.size(); i < l; i++)
-          {
-            af.addSortByOrderMenuItem(
-                    WebServiceName + ((String) names.get(i)) + " Ordering",
-                    (AlignmentOrder) alorders.get(i));
-          }
-        }
+    // initialise with same renderer settings as in parent alignframe.
+    af.getFeatureRenderer().transferSettings(this.featureSettings);
+
+    if (alorders.size() > 0)
+    {
+      addSortByMenuItems(af, alorders);
+    }
+
+    // TODO: refactor retrieve and show as new splitFrame as Desktop method
+
+    /*
+     * If alignment was requested from one half of a SplitFrame, show in a
+     * SplitFrame with the other pane similarly aligned.
+     */
+    AlignFrame requestedBy = getRequestingAlignFrame();
+    if (requestedBy != null
+            && requestedBy.getSplitViewContainer() != null
+            && requestedBy.getSplitViewContainer().getComplement(
+                    requestedBy) != null)
+    {
+      AlignmentI complement = requestedBy.getSplitViewContainer()
+              .getComplement(requestedBy);
+      String complementTitle = requestedBy.getSplitViewContainer()
+              .getComplementTitle(requestedBy);
+      // becomes null if the alignment window was closed before the alignment
+      // job finished.
+      AlignmentI copyComplement = new Alignment(complement);
+      copyComplement.alignAs(al);
+      if (copyComplement.getHeight() > 0)
+      {
+        af.setTitle(alTitle);
+        AlignFrame af2 = new AlignFrame(copyComplement,
+                AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
+        af2.setTitle(complementTitle);
+        String linkedTitle = MessageManager
+                .getString("label.linked_view_title");
+        JInternalFrame splitFrame = new SplitFrame(al.isNucleotide() ? af
+                : af2, al.isNucleotide() ? af2 : af);
+        Desktop.addInternalFrame(splitFrame, linkedTitle, -1, -1);
+        return;
       }
+    }
 
-      Desktop.addInternalFrame(af, alTitle, AlignFrame.DEFAULT_WIDTH,
-              AlignFrame.DEFAULT_HEIGHT);
+    /*
+     * Not from SplitFrame, or failed to created a complementary alignment
+     */
+    Desktop.addInternalFrame(af, alTitle, AlignFrame.DEFAULT_WIDTH,
+            AlignFrame.DEFAULT_HEIGHT);
+  }
 
+  /**
+   * Add sort order options to the AlignFrame menus.
+   * 
+   * @param af
+   * @param alorders
+   */
+  protected void addSortByMenuItems(AlignFrame af,
+          List<AlignmentOrder> alorders)
+  {
+    // update orders
+    if (alorders.size() == 1)
+    {
+      af.addSortByOrderMenuItem(WebServiceName + " Ordering",
+              alorders.get(0));
     }
     else
     {
-      System.out.println("MERGE WITH OLD FRAME");
-      // TODO: modify alignment in original frame, replacing old for new
-      // alignment using the commands.EditCommand model to ensure the update can
-      // be undone
+      // construct a non-redundant ordering set
+      List<String> names = new ArrayList<String>();
+      for (int i = 0, l = alorders.size(); i < l; i++)
+      {
+        String orderName = " Region " + i;
+        int j = i + 1;
+
+        while (j < l)
+        {
+          if (alorders.get(i).equals(alorders.get(j)))
+          {
+            alorders.remove(j);
+            l--;
+            orderName += "," + j;
+          }
+          else
+          {
+            j++;
+          }
+        }
+
+        if (i == 0 && j == 1)
+        {
+          names.add("");
+        }
+        else
+        {
+          names.add(orderName);
+        }
+      }
+      for (int i = 0, l = alorders.size(); i < l; i++)
+      {
+        af.addSortByOrderMenuItem(WebServiceName + (names.get(i))
+                + " Ordering", alorders.get(i));
+      }
     }
   }