JAL-1620 version bump and release notes
[jalview.git] / src / jalview / ext / rbvi / chimera / JalviewChimeraBinding.java
index 3f2d5e4..9b9c239 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
  * Copyright (C) 2014 The Jalview Authors
  * 
  * This file is part of Jalview.
@@ -74,13 +74,14 @@ public abstract class JalviewChimeraBinding extends
   private boolean loadingFromArchive = false;
 
   /**
-   * second flag to indicate if the jmol viewer should ignore sequence colouring
-   * events from the structure manager because the GUI is still setting up
+   * second flag to indicate if the Chimera viewer should ignore sequence
+   * colouring events from the structure manager because the GUI is still
+   * setting up
    */
   private boolean loadingFinished = true;
 
   /**
-   * state flag used to check if the Jmol viewer's paint method can be called
+   * state flag used to check if the Chimera viewer's paint method can be called
    */
   private boolean finishedInit = false;
 
@@ -173,7 +174,7 @@ public abstract class JalviewChimeraBinding extends
         } catch (Error e)
         {
         }
-        // Explicitly map to the filename used by Jmol ;
+        // Explicitly map to the filename used by Chimera ;
         // pdbentry[pe].getFile(), protocol);
 
         if (ssm != null)
@@ -236,13 +237,6 @@ public abstract class JalviewChimeraBinding extends
     }
     viewer = new ChimeraManager(
             csm = new ext.edu.ucsf.rbvi.strucviz2.StructureManager(true));
-    /*
-     * viewer = JmolViewer.allocateViewer(renderPanel, new SmarterJmolAdapter(),
-     * "jalviewJmol", ap.av.applet .getDocumentBase(),
-     * ap.av.applet.getCodeBase(), "", this);
-     * 
-     * jmolpopup = JmolPopup.newJmolPopup(viewer, true, "Jmol", true);
-     */
   }
 
   public JalviewChimeraBinding(StructureSelectionManager ssm,
@@ -254,12 +248,13 @@ public abstract class JalviewChimeraBinding extends
   }
 
   /**
-   * construct a title string for the viewer window based on the data jalview
+   * Construct a title string for the viewer window based on the data Jalview
    * knows about
    * 
+   * @param verbose
    * @return
    */
-  public String getViewerTitle()
+  public String getViewerTitle(boolean verbose)
   {
     if (sequence == null || pdbentry == null || sequence.length < 1
             || pdbentry.length < 1 || sequence[0].length < 1)
@@ -268,20 +263,24 @@ public abstract class JalviewChimeraBinding extends
     }
     // TODO: give a more informative title when multiple structures are
     // displayed.
-    StringBuffer title = new StringBuffer("Chimera view for "
-            + sequence[0][0].getName() + ":" + pdbentry[0].getId());
+    StringBuilder title = new StringBuilder(64);
+    title.append("Chimera view for " + sequence[0][0].getName() + ":"
+            + pdbentry[0].getId());
 
-    if (pdbentry[0].getProperty() != null)
+    if (verbose)
     {
-      if (pdbentry[0].getProperty().get("method") != null)
-      {
-        title.append(" Method: ");
-        title.append(pdbentry[0].getProperty().get("method"));
-      }
-      if (pdbentry[0].getProperty().get("chains") != null)
+      if (pdbentry[0].getProperty() != null)
       {
-        title.append(" Chain:");
-        title.append(pdbentry[0].getProperty().get("chains"));
+        if (pdbentry[0].getProperty().get("method") != null)
+        {
+          title.append(" Method: ");
+          title.append(pdbentry[0].getProperty().get("method"));
+        }
+        if (pdbentry[0].getProperty().get("chains") != null)
+        {
+          title.append(" Chain:");
+          title.append(pdbentry[0].getProperty().get("chains"));
+        }
       }
     }
     return title.toString();
@@ -319,20 +318,24 @@ public abstract class JalviewChimeraBinding extends
             + ";focus " + cmdstring, false);
   }
 
-  public void closeViewer()
+  /**
+   * Close down the Jalview viewer, and (optionally) the associate Chimera
+   * window.
+   */
+  public void closeViewer(boolean closeChimera)
   {
     ssm.removeStructureViewerListener(this, this.getPdbFile());
-    // and shut down Chimera
-    viewer.exitChimera();
-    // viewer.evalStringQuiet("zap");
-    // viewer.setJmolStatusListener(null);
+    if (closeChimera)
+    {
+      viewer.exitChimera();
+    }
     lastCommand = null;
     viewer = null;
     releaseUIResources();
   }
 
   /**
-   * called by JalviewJmolbinding after closeViewer is called - release any
+   * called by JalviewChimerabinding after closeViewer is called - release any
    * resources and references so they can be garbage collected.
    */
   protected abstract void releaseUIResources();
@@ -340,17 +343,15 @@ public abstract class JalviewChimeraBinding extends
   public void colourByChain()
   {
     colourBySequence = false;
-    // TODO: colour by chain should colour each chain distinctly across all
-    // visible models
-    // TODO: http://issues.jalview.org/browse/JAL-628
-    evalStateCommand("select *;color chain",false);
+    evalStateCommand("rainbow chain", false);
   }
 
   public void colourByCharge()
   {
     colourBySequence = false;
-    evalStateCommand("colour *;color white;select ASP,GLU;color red;"
-            + "select LYS,ARG;color blue;select CYS;color yellow", false);
+    evalStateCommand(
+            "color white;color red ::ASP;color red ::GLU;color blue ::LYS;color blue ::ARG;color yellow ::CYS",
+            false);
   }
 
   /**
@@ -400,7 +401,7 @@ public abstract class JalviewChimeraBinding extends
     assert (_alignment.length == _refStructure.length && _alignment.length != _hiddenCols.length);
     StringBuilder allComs = new StringBuilder(128); // Chimera superposition cmd
     String[] files = getPdbFile();
-    // check to see if we are still waiting for Jmol files
+    // check to see if we are still waiting for Chimera files
     long starttime = System.currentTimeMillis();
     boolean waiting = true;
     do
@@ -670,7 +671,7 @@ public abstract class JalviewChimeraBinding extends
       }
       System.out.println("Select regions:\n" + selectioncom.toString());
       allComs.append("; ~display all; chain @CA|P; ribbon "
-              + selectioncom.toString() + "");
+              + selectioncom.toString() + "; focus");
       // evalStateCommand("select *; backbone; select "+selcom.toString()+"; cartoons; center "+selcom.toString());
       evalStateCommand(allComs.toString(), true /* false */);
     }
@@ -689,7 +690,24 @@ public abstract class JalviewChimeraBinding extends
     }
   }
 
-  public void evalStateCommand(final String command, boolean resp)
+  /**
+   * Answers true if the Chimera process is still running, false if ended or not
+   * started.
+   * 
+   * @return
+   */
+  public boolean isChimeraRunning()
+  {
+    return viewer.isChimeraLaunched();
+  }
+
+  /**
+   * Send a command to Chimera, and optionally log any responses.
+   * 
+   * @param command
+   * @param logResponse
+   */
+  public void evalStateCommand(final String command, boolean logResponse)
   {
     viewerCommandHistory(false);
     checkLaunched();
@@ -701,8 +719,8 @@ public abstract class JalviewChimeraBinding extends
 //        public void run()
 //        {
       // trim command or it may never find a match in the replyLog!!
-      lastReply = viewer.sendChimeraCommand(command.trim(), resp);
-      if (debug && resp)
+      lastReply = viewer.sendChimeraCommand(command.trim(), logResponse);
+      if (debug && logResponse)
           {
             log("Response from command ('" + command + "') was:\n"
                     + lastReply);
@@ -764,7 +782,7 @@ public abstract class JalviewChimeraBinding extends
 
   private void waitForChimera()
   {
-    while (viewer.isBusy())
+    while (viewer != null && viewer.isBusy())
     {
       try {
         Thread.sleep(15);
@@ -936,7 +954,7 @@ public abstract class JalviewChimeraBinding extends
     }
   }
 
-  boolean debug = true;
+  boolean debug = false;
 
   private void log(String message)
   {
@@ -945,8 +963,8 @@ public abstract class JalviewChimeraBinding extends
 
   private void viewerCommandHistory(boolean enable)
   {
-    log("(Not yet implemented) History "
-            + ((debug || enable) ? "on" : "off"));
+    // log("(Not yet implemented) History "
+    // + ((debug || enable) ? "on" : "off"));
   }
 
   public void loadInline(String string)
@@ -1197,10 +1215,13 @@ public abstract class JalviewChimeraBinding extends
     String res;
     int index;
     Color col;
+    // Chimera expects RBG values in the range 0-1
+    final double normalise = 255D;
     viewerCommandHistory(false);
     // TODO: Switch between nucleotide or aa selection expressions
     Enumeration en = ResidueProperties.aa3Hash.keys();
-    StringBuffer command = new StringBuffer("select *;color white;");
+    StringBuilder command = new StringBuilder(128);
+    command.append("color white;");
     while (en.hasMoreElements())
     {
       res = en.nextElement().toString();
@@ -1211,32 +1232,19 @@ public abstract class JalviewChimeraBinding extends
       }
 
       col = cs.findColour(ResidueProperties.aa[index].charAt(0));
-      // TODO: need colour string function and res selection here
-      command.append("select " + res + ";color[" + col.getRed() + ","
-              + col.getGreen() + "," + col.getBlue() + "];");
+      command.append("color " + col.getRed() / normalise + ","
+              + col.getGreen() / normalise + "," + col.getBlue()
+              / normalise + " ::" + res + ";");
     }
 
     evalStateCommand(command.toString(),false);
     viewerCommandHistory(true);
   }
 
-  public void showHelp()
-  {
-    // chimera help
-    showUrl("http://jmol.sourceforge.net/docs/JmolUserGuide/", "jmolHelp");
-  }
-
   /**
-   * open the URL somehow
-   * 
-   * @param target
-   */
-  public abstract void showUrl(String url, String target);
-
-  /**
-   * called when the binding thinks the UI needs to be refreshed after a Jmol
+   * called when the binding thinks the UI needs to be refreshed after a Chimera
    * state change. this could be because structures were loaded, or because an
-   * error has occured.
+   * error has occurred.
    */
   public abstract void refreshGUI();
 
@@ -1265,8 +1273,8 @@ public abstract class JalviewChimeraBinding extends
 
   /**
    * 
-   * @return true if Jmol is still restoring state or loading is still going on
-   *         (see setFinsihedLoadingFromArchive)
+   * @return true if Chimeral is still restoring state or loading is still going
+   *         on (see setFinsihedLoadingFromArchive)
    */
   public boolean isLoadingFromArchive()
   {
@@ -1284,13 +1292,22 @@ public abstract class JalviewChimeraBinding extends
     loadingFinished = finishedLoading;
   }
 
-  public void setBackgroundColour(java.awt.Color col)
+  /**
+   * Send the Chimera 'background solid <color>" command.
+   * 
+   * @see https
+   *      ://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/background
+   *      .html
+   * @param col
+   */
+  public void setBackgroundColour(Color col)
   {
     viewerCommandHistory(false);
-    // todo set background colour
-    viewer.sendChimeraCommand(
-            "background [" + col.getRed() + "," + col.getGreen() + ","
-                    + col.getBlue() + "];", false);
+    double normalise = 255D;
+    final String command = "background solid " + col.getRed() / normalise + ","
+            + col.getGreen() / normalise + "," + col.getBlue()
+            / normalise + ";";
+    viewer.sendChimeraCommand(command, false);
     viewerCommandHistory(true);
   }