JAL-1742 don't offer to align another chain to the same structure
[jalview.git] / src / jalview / gui / ChimeraViewFrame.java
index bc985df..4372617 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  */
 package jalview.gui;
 
-import jalview.bin.Cache;
-import jalview.datamodel.Alignment;
-import jalview.datamodel.AlignmentI;
-import jalview.datamodel.ColumnSelection;
-import jalview.datamodel.PDBEntry;
-import jalview.datamodel.SequenceI;
-import jalview.ext.rbvi.chimera.JalviewChimeraBinding;
-import jalview.io.AppletFormatAdapter;
-import jalview.io.JalviewFileChooser;
-import jalview.io.JalviewFileView;
-import jalview.schemes.BuriedColourScheme;
-import jalview.schemes.ColourSchemeI;
-import jalview.schemes.HelixColourScheme;
-import jalview.schemes.HydrophobicColourScheme;
-import jalview.schemes.PurinePyrimidineColourScheme;
-import jalview.schemes.StrandColourScheme;
-import jalview.schemes.TaylorColourScheme;
-import jalview.schemes.TurnColourScheme;
-import jalview.schemes.ZappoColourScheme;
-import jalview.structures.models.AAStructureBindingModel;
-import jalview.util.MessageManager;
-import jalview.util.Platform;
-import jalview.ws.dbsources.Pdb;
-
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
@@ -69,6 +45,30 @@ import javax.swing.event.InternalFrameEvent;
 import javax.swing.event.MenuEvent;
 import javax.swing.event.MenuListener;
 
+import jalview.bin.Cache;
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.PDBEntry;
+import jalview.datamodel.SequenceI;
+import jalview.ext.rbvi.chimera.JalviewChimeraBinding;
+import jalview.io.AppletFormatAdapter;
+import jalview.io.JalviewFileChooser;
+import jalview.io.JalviewFileView;
+import jalview.schemes.BuriedColourScheme;
+import jalview.schemes.ColourSchemeI;
+import jalview.schemes.HelixColourScheme;
+import jalview.schemes.HydrophobicColourScheme;
+import jalview.schemes.PurinePyrimidineColourScheme;
+import jalview.schemes.StrandColourScheme;
+import jalview.schemes.TaylorColourScheme;
+import jalview.schemes.TurnColourScheme;
+import jalview.schemes.ZappoColourScheme;
+import jalview.structures.models.AAStructureBindingModel;
+import jalview.util.MessageManager;
+import jalview.util.Platform;
+import jalview.ws.dbsources.Pdb;
+
 /**
  * GUI elements for handlnig an external chimera display
  * 
@@ -97,6 +97,13 @@ public class ChimeraViewFrame extends StructureViewerBase
    */
   private Thread worker = null;
 
+  /*
+   * Path to Chimera session file. This is set when an open Jalview/Chimera
+   * session is saved, or on restore from a Jalview project (if it holds the
+   * filename of any saved Chimera sessions).
+   */
+  private String chimeraSessionFile = null;
+
   /**
    * Initialise menu options.
    */
@@ -265,6 +272,14 @@ public class ChimeraViewFrame extends StructureViewerBase
     for (ChimeraViewFrame topView : existingViews)
     {
       // TODO: highlight topView in view somehow
+      /*
+       * JAL-1742 exclude view with this structure already mapped (don't offer
+       * to align chain B to chain A of the same structure)
+       */
+      if (topView.hasPdbId(pdbentry.getId()))
+      {
+        continue;
+      }
       int option = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
               MessageManager.formatMessage("label.add_pdbentry_to_view",
                       new Object[]
@@ -289,6 +304,11 @@ public class ChimeraViewFrame extends StructureViewerBase
     { seq });
   }
 
+  protected boolean hasPdbId(String pdbId)
+  {
+    return jmb.hasPdbId(pdbId);
+  }
+
   private void openNewChimera(AlignmentPanel ap, PDBEntry[] pdbentrys,
           SequenceI[][] seqs)
   {
@@ -305,17 +325,16 @@ public class ChimeraViewFrame extends StructureViewerBase
     jmb.setColourBySequence(true);
     setSize(400, 400); // probably should be a configurable/dynamic default here
     initMenus();
-    worker = null;
-    {
-      addingStructures = false;
-      worker = new Thread(this);
-      worker.start();
-    }
+
+    addingStructures = false;
+    worker = new Thread(this);
+    worker.start();
+
     this.addInternalFrameListener(new InternalFrameAdapter()
     {
       public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
       {
-        closeViewer();
+        closeViewer(false);
       }
     });
 
@@ -336,114 +355,37 @@ public class ChimeraViewFrame extends StructureViewerBase
     openNewChimera(ap, pe, seqs);
   }
 
-  public AlignmentPanel[] getAllAlignmentPanels()
-  {
-    AlignmentPanel[] t, list = new AlignmentPanel[0];
-    for (String setid : _aps)
-    {
-      AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
-      if (panels != null)
-      {
-        t = new AlignmentPanel[list.length + panels.length];
-        System.arraycopy(list, 0, t, 0, list.length);
-        System.arraycopy(panels, 0, t, list.length, panels.length);
-        list = t;
-      }
-    }
-
-    return list;
-  }
-
   /**
-   * set the primary alignmentPanel reference and add another alignPanel to the
-   * list of ones to use for colouring and aligning
+   * Create a new viewer from saved session state data including Chimera session
+   * file.
    * 
-   * @param nap
-   */
-  public void addAlignmentPanel(AlignmentPanel nap)
-  {
-    if (getAlignmentPanel() == null)
-    {
-      setAlignmentPanel(nap);
-    }
-    if (!_aps.contains(nap.av.getSequenceSetId()))
-    {
-      _aps.add(nap.av.getSequenceSetId());
-    }
-  }
-
-  /**
-   * remove any references held to the given alignment panel
+   * @param chimeraSession
    * 
-   * @param nap
+   * @param alignPanel
+   * @param pdbArray
+   * @param seqsArray
+   * @param colourByChimera
+   * @param colourBySequence
    */
-  public void removeAlignmentPanel(AlignmentPanel nap)
-  {
-    try
-    {
-      _alignwith.remove(nap);
-      _colourwith.remove(nap);
-      if (getAlignmentPanel() == nap)
-      {
-        setAlignmentPanel(null);
-        for (AlignmentPanel aps : getAllAlignmentPanels())
-        {
-          if (aps != nap)
-          {
-            setAlignmentPanel(aps);
-            break;
-          }
-        }
-      }
-    } catch (Exception ex)
-    {
-    }
-    if (getAlignmentPanel() != null)
-    {
-      buildActionMenu();
-    }
-  }
-
-  public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
-  {
-    addAlignmentPanel(nap);
-    if (!_alignwith.contains(nap))
-    {
-      _alignwith.add(nap);
-    }
-  }
-
-  public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
+  public ChimeraViewFrame(String chimeraSession, AlignmentPanel alignPanel,
+          PDBEntry[] pdbArray,
+          SequenceI[][] seqsArray, boolean colourByChimera,
+          boolean colourBySequence)
   {
-    if (_alignwith.contains(nap))
-    {
-      _alignwith.remove(nap);
-    }
-  }
-
-  public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
-          boolean enableColourBySeq)
-  {
-    useAlignmentPanelForColourbyseq(nap);
-    getBinding().setColourBySequence(enableColourBySeq);
-    seqColour.setSelected(enableColourBySeq);
-    viewerColour.setSelected(!enableColourBySeq);
-  }
-
-  public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
-  {
-    addAlignmentPanel(nap);
-    if (!_colourwith.contains(nap))
+    super();
+    this.chimeraSessionFile = chimeraSession;
+    openNewChimera(alignPanel, pdbArray, seqsArray);
+    if (colourByChimera)
     {
-      _colourwith.add(nap);
+      jmb.setColourBySequence(false);
+      seqColour.setSelected(false);
+      viewerColour.setSelected(true);
     }
-  }
-
-  public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
-  {
-    if (_colourwith.contains(nap))
+    else if (colourBySequence)
     {
-      _colourwith.remove(nap);
+      jmb.setColourBySequence(true);
+      seqColour.setSelected(true);
+      viewerColour.setSelected(false);
     }
   }
 
@@ -521,19 +463,34 @@ public class ChimeraViewFrame extends StructureViewerBase
     return result;
   }
 
-  void initChimera(String command)
+  /**
+   * Launch Chimera. If we have a chimera session file name, send Chimera the
+   * command to open its saved session file.
+   */
+  void initChimera()
   {
     jmb.setFinishedInit(false);
-    // TODO: consider waiting until the structure/view is fully loaded before
-    // displaying
     jalview.gui.Desktop.addInternalFrame(this, jmb.getViewerTitle("Chimera", true),
             getBounds().width, getBounds().height);
-    if (command == null)
+
+    /*
+     * Pass an empty 'command' to launch Chimera
+     */
+    jmb.evalStateCommand("", false);
+
+    if (this.chimeraSessionFile != null)
     {
-      command = "";
+      boolean opened = jmb.openSession(chimeraSessionFile);
+      if (!opened)
+      {
+        System.err
+                .println("An error occurred opening Chimera session file "
+                        + chimeraSessionFile);
+      }
     }
-    jmb.evalStateCommand(command, false);
     jmb.setFinishedInit(true);
+
+    jmb.startChimeraListener();
   }
 
   void setChainMenuItems(List<String> chainNames)
@@ -603,19 +560,26 @@ public class ChimeraViewFrame extends StructureViewerBase
    * Close down this instance of Jalview's Chimera viewer, giving the user the
    * option to close the associated Chimera window (process). They may wish to
    * keep it open until they have had an opportunity to save any work.
+   * 
+   * @param closeChimera
+   *          if true, close any linked Chimera process; if false, prompt first
    */
-  public void closeViewer()
+  public void closeViewer(boolean closeChimera)
   {
     if (jmb.isChimeraRunning())
     {
-      String prompt = MessageManager
-              .formatMessage("label.confirm_close_chimera", new Object[]
-              { jmb.getViewerTitle("Chimera", false) });
-      prompt = JvSwingUtils.wrapTooltip(true, prompt);
-      int confirm = JOptionPane.showConfirmDialog(this, prompt,
-              MessageManager.getString("label.close_viewer"),
-              JOptionPane.YES_NO_OPTION);
-      jmb.closeViewer(confirm == JOptionPane.YES_OPTION);
+      if (!closeChimera)
+      {
+        String prompt = MessageManager.formatMessage(
+                "label.confirm_close_chimera", new Object[]
+                { jmb.getViewerTitle("Chimera", false) });
+        prompt = JvSwingUtils.wrapTooltip(true, prompt);
+        int confirm = JOptionPane.showConfirmDialog(this, prompt,
+                MessageManager.getString("label.close_viewer"),
+                JOptionPane.YES_NO_OPTION);
+        closeChimera = confirm == JOptionPane.YES_OPTION;
+      }
+      jmb.closeViewer(closeChimera);
     }
     setAlignmentPanel(null);
     _aps.clear();
@@ -713,7 +677,7 @@ public class ChimeraViewFrame extends StructureViewerBase
       {
         try
         {
-          initChimera("");
+          initChimera();
         } catch (Exception ex)
         {
           Cache.log.error("Couldn't open Chimera viewer!", ex);
@@ -834,8 +798,8 @@ public class ChimeraViewFrame extends StructureViewerBase
     if (pdbseq != null && pdbseq.getHeight() > 0)
     {
       // just use the file name from the first sequence's first PDBEntry
-      filePath = new File(((PDBEntry) pdbseq.getSequenceAt(0).getPDBId()
-              .elementAt(0)).getFile()).getAbsolutePath();
+      filePath = new File(pdbseq.getSequenceAt(0).getPDBId()
+              .elementAt(0).getFile()).getAbsolutePath();
       processingEntry.setFile(filePath);
     }
     return filePath;
@@ -962,7 +926,7 @@ public class ChimeraViewFrame extends StructureViewerBase
       // Set the colour using the current view for the associated alignframe
       for (AlignmentPanel ap : _colourwith)
       {
-        jmb.colourBySequence(ap.av.showSequenceFeatures, ap);
+        jmb.colourBySequence(ap.av.isShowSequenceFeatures(), ap);
       }
     }
   }
@@ -1074,7 +1038,7 @@ public class ChimeraViewFrame extends StructureViewerBase
       repaint();
       return;
     }
-    setChainMenuItems(jmb.chainNames);
+    setChainMenuItems(jmb.getChainNames());
 
     this.setTitle(jmb.getViewerTitle("Chimera", true));
     if (jmb.getPdbFile().length > 1 && jmb.getSequence().length > 1)
@@ -1175,9 +1139,26 @@ public class ChimeraViewFrame extends StructureViewerBase
    * if successful, else false.
    * 
    * @param filepath
+   * @see getStateInfo
    */
   public boolean saveSession(String filepath)
   {
-    return jmb.saveSession(filepath);
+    boolean result = jmb.saveSession(filepath);
+    if (result)
+    {
+      this.chimeraSessionFile = filepath;
+    }
+    return result;
+  }
+
+  /**
+   * Returns the file path of the Chimera session file the last time it was
+   * saved. If it was never saved, returns an empty string. There is no
+   * guarantee that the Chimera session has not changed since it was saved.
+   */
+  @Override
+  public String getStateInfo()
+  {
+    return this.chimeraSessionFile;
   }
 }