Merge branch 'develop' into improvement/JAL-3830_install4j9_macos_setup_application
[jalview.git] / src / jalview / ext / jmol / JmolCommands.java
index 7dd5c0b..8b67ad9 100644 (file)
  */
 package jalview.ext.jmol;
 
+import java.awt.Color;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
 import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
 import jalview.api.FeatureRenderer;
@@ -29,15 +35,14 @@ import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.SequenceI;
 import jalview.renderer.seqfeatures.FeatureColourFinder;
 import jalview.structure.AtomSpecModel;
+import jalview.structure.StructureCommand;
+import jalview.structure.StructureCommandI;
 import jalview.structure.StructureCommandsBase;
 import jalview.structure.StructureMapping;
 import jalview.structure.StructureSelectionManager;
+import jalview.structure.StructureCommandsI.AtomSpecType;
 import jalview.util.Comparison;
-
-import java.awt.Color;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
+import jalview.util.Platform;
 
 /**
  * Routines for generating Jmol commands for Jalview/Jmol binding
@@ -47,10 +52,20 @@ import java.util.Map;
  */
 public class JmolCommands extends StructureCommandsBase
 {
-  private static final String CMD_COLOUR_BY_CHARGE = "select *;color white;select ASP,GLU;color red;"
-          + "select LYS,ARG;color blue;select CYS;color yellow";
+  private static final StructureCommand SHOW_BACKBONE = new StructureCommand(
+          "select *; cartoons off; backbone");
+
+  private static final StructureCommand FOCUS_VIEW = new StructureCommand("zoom 0");
 
-  private static final String CMD_COLOUR_BY_CHAIN = "select *;color chain";
+  private static final StructureCommand COLOUR_ALL_WHITE = new StructureCommand(
+          "select *;color white;");
+
+  private static final StructureCommandI COLOUR_BY_CHARGE = new StructureCommand(
+          "select *;color white;select ASP,GLU;color red;"
+                  + "select LYS,ARG;color blue;select CYS;color yellow");
+
+  private static final StructureCommandI COLOUR_BY_CHAIN = new StructureCommand(
+          "select *;color chain");
 
   private static final String PIPE = "|";
 
@@ -71,7 +86,13 @@ public class JmolCommands extends StructureCommandsBase
     return 1;
   }
 
-  @Override
+  /**
+   * Returns a string representation of the given colour suitable for inclusion
+   * in Jmol commands
+   * 
+   * @param c
+   * @return
+   */
   protected String getColourString(Color c)
   {
     return c == null ? null
@@ -79,27 +100,221 @@ public class JmolCommands extends StructureCommandsBase
                     c.getBlue());
   }
 
+  @Override
+  public StructureCommandI colourByChain()
+  {
+    return COLOUR_BY_CHAIN;
+  }
+
+  @Override
+  public List<StructureCommandI> colourByCharge()
+  {
+    return Arrays.asList(COLOUR_BY_CHARGE);
+  }
+
+  @Override
+  public List<StructureCommandI> colourByResidues(Map<String, Color> colours)
+  {
+    List<StructureCommandI> cmds = super.colourByResidues(colours);
+    cmds.add(0, COLOUR_ALL_WHITE);
+    return cmds;
+  }
+
+  @Override
+  public StructureCommandI setBackgroundColour(Color col)
+  {
+    return new StructureCommand("background " + getColourString(col));
+  }
+
+  @Override
+  public StructureCommandI focusView()
+  {
+    return FOCUS_VIEW;
+  }
+
+  @Override
+  public List<StructureCommandI> showChains(List<String> toShow)
+  {
+    StringBuilder atomSpec = new StringBuilder(128);
+    boolean first = true;
+    for (String chain : toShow)
+    {
+      String[] tokens = chain.split(":");
+      if (tokens.length == 2)
+      {
+        if (!first)
+        {
+          atomSpec.append(" or ");
+        }
+        first = false;
+        atomSpec.append(":").append(tokens[1]).append(" /").append(tokens[0]);
+      }
+    }
+
+    String spec = atomSpec.toString();
+    String command = "select *;restrict " + spec + ";cartoon;center "
+            + spec;
+    return Arrays.asList(new StructureCommand(command));
+  }
+
   /**
-   * Returns commands (one per colour key in the map) like
+   * Returns a command to superpose atoms in {@code atomSpec} to those in
+   * {@code refAtoms}, restricted to alpha carbons only (Phosphorous for rna).
+   * For example
    * 
    * <pre>
-   *   select 2:A/1.1|3-27:B/1.1|9-12:A/2.1;color[173,0,82]
+   * compare {2.1} {1.1} SUBSET {(*.CA | *.P) and conformation=1} 
+   *         ATOMS {1-87:A}{2-54:A|61-94:A} ROTATE TRANSLATE 1.0;
    * </pre>
+   * 
+   * where {@code conformation=1} excludes ALTLOC atom locations, and 1.0 is the
+   * time in seconds to animate the action. For this example, atoms in model 2
+   * are moved towards atoms in model 1.
+   * <p>
+   * The two atomspecs should each be for one model only, but may have more than
+   * one chain. The number of atoms specified should be the same for both
+   * models, though if not, Jmol may make a 'best effort' at superposition.
+   * 
+   * @see https://chemapps.stolaf.edu/jmol/docs/#compare
    */
   @Override
-  public String[] colourBySequence(Map<Object, AtomSpecModel> colourMap)
+  public List<StructureCommandI> superposeStructures(AtomSpecModel refAtoms,
+          AtomSpecModel atomSpec, AtomSpecType backbone)
+  {
+    StringBuilder sb = new StringBuilder(64);
+    String refModel = refAtoms.getModels().iterator().next();
+    String model2 = atomSpec.getModels().iterator().next();
+    sb.append(String.format("compare {%s.1} {%s.1}", model2, refModel));
+    sb.append(" SUBSET {(*.CA | *.P) and conformation=1} ATOMS {");
+
+    /*
+     * command examples don't include modelspec with atoms, getAtomSpec does;
+     * it works, so leave it as it is for simplicity
+     */
+    sb.append(getAtomSpec(atomSpec, backbone)).append("}{");
+    sb.append(getAtomSpec(refAtoms, backbone)).append("}");
+    sb.append(" ROTATE TRANSLATE ");
+    sb.append(getCommandSeparator());
+
+    /*
+     * show residues used for superposition as ribbon
+     */
+    sb.append("select ").append(getAtomSpec(atomSpec, AtomSpecType.RESIDUE_ONLY)).append("|");
+    sb.append(getAtomSpec(refAtoms, AtomSpecType.RESIDUE_ONLY)).append(getCommandSeparator())
+            .append("cartoons");
+
+    return Arrays.asList(new StructureCommand(sb.toString()));
+  }
+
+  @Override
+  public StructureCommandI openCommandFile(String path)
+  {
+    /*
+     * https://chemapps.stolaf.edu/jmol/docs/#script
+     * not currently used in Jalview
+     */
+    return new StructureCommand("script " + path);
+  }
+
+  @Override
+  public StructureCommandI saveSession(String filepath)
+  {
+    /*
+     * https://chemapps.stolaf.edu/jmol/docs/#writemodel
+     */
+    return new StructureCommand("write STATE \"" + filepath + "\"");
+  }
+
+  @Override
+  protected StructureCommandI colourResidues(String atomSpec, Color colour)
+  {
+    StringBuilder sb = new StringBuilder(atomSpec.length()+20);
+    sb.append("select ").append(atomSpec).append(getCommandSeparator())
+            .append("color").append(getColourString(colour));
+    return new StructureCommand(sb.toString());
+  }
+
+  @Override
+  protected String getResidueSpec(String residue)
+  {
+    return residue;
+  }
+
+  /**
+   * Generates a Jmol atomspec string like
+   * 
+   * <pre>
+   * 2-5:A/1.1,8:A/1.1,5-10:B/2.1
+   * </pre>
+   * 
+   * Parameter {@code alphaOnly} is not used here - this restriction is made by
+   * a separate clause in the {@code compare} (superposition) command.
+   */
+  @Override
+  public String getAtomSpec(AtomSpecModel model, AtomSpecType specType)
+  {
+    StringBuilder sb = new StringBuilder(128);
+
+    boolean first = true;
+    for (String modelNo : model.getModels())
+    {
+      for (String chain : model.getChains(modelNo))
+      {
+        for (int[] range : model.getRanges(modelNo, chain))
+        {
+          if (!first)
+          {
+            sb.append(PIPE);
+          }
+          first = false;
+          if (range[0] == range[1])
+          {
+            sb.append(range[0]);
+          }
+          else
+          {
+            sb.append(range[0]).append(HYPHEN).append(range[1]);
+          }
+          sb.append(COLON).append(chain.trim()).append(SLASH);
+          sb.append(String.valueOf(modelNo)).append(".1");
+        }
+      }
+    }
+
+    return sb.toString();
+  }
+
+  @Override
+  public List<StructureCommandI> showBackbone()
   {
-    List<String> colourCommands = buildColourCommands(colourMap);
+    return Arrays.asList(SHOW_BACKBONE);
+  }
 
-    return colourCommands.toArray(new String[colourCommands.size()]);
+  @Override
+  public StructureCommandI loadFile(String file)
+  {
+    // https://chemapps.stolaf.edu/jmol/docs/#loadfiles
+    return new StructureCommand("load FILES \"" + 
+            Platform.escapeBackslashes(file) + "\"");
   }
 
+  /**
+   * Obsolete method, only referenced from
+   * jalview.javascript.MouseOverStructureListener
+   * 
+   * @param ssm
+   * @param files
+   * @param sequence
+   * @param sr
+   * @param viewPanel
+   * @return
+   */
+  @Deprecated
   public String[] colourBySequence(StructureSelectionManager ssm,
-          String[] files,
-          SequenceI[][] sequence, SequenceRenderer sr,
+          String[] files, SequenceI[][] sequence, SequenceRenderer sr,
           AlignmentViewPanel viewPanel)
   {
-    // TODO refactor to call buildColoursMap() first...
+    // TODO delete method
 
     FeatureRenderer fr = viewPanel.getFeatureRenderer();
     FeatureColourFinder finder = new FeatureColourFinder(fr);
@@ -211,7 +426,16 @@ public class JmolCommands extends StructureCommandsBase
     return cset.toArray(new String[cset.size()]);
   }
 
-  public static StringBuilder condenseCommand(StringBuilder command,
+  /**
+   * Helper method
+   * 
+   * @param command
+   * @param pos
+   * @return
+   */
+  @Deprecated
+  private static StringBuilder condenseCommand(
+          StringBuilder command,
           int pos)
   {
 
@@ -248,195 +472,14 @@ public class JmolCommands extends StructureCommandsBase
   }
 
   @Override
-  public String colourByChain()
-  {
-    return CMD_COLOUR_BY_CHAIN;
-  }
-
-  @Override
-  public String colourByCharge()
-  {
-    return CMD_COLOUR_BY_CHARGE;
-  }
-
-  @Override
-  public String colourByResidues(Map<String, Color> colours)
-  {
-    StringBuilder cmd = new StringBuilder(128);
-    cmd.append("select *;color white;");
-    cmd.append(super.colourByResidues(colours));
-
-    return cmd.toString();
-  }
-
-  @Override
-  public String setBackgroundColour(Color col)
-  {
-    return "background " + getColourString(col);
-  }
-
-  @Override
-  public String focusView()
-  {
-    return "zoom 0";
-  }
-
-  @Override
-  public String showChains(List<String> toShow)
-  {
-    StringBuilder atomSpec = new StringBuilder(128);
-    boolean first = true;
-    for (String chain : toShow)
-    {
-      String[] tokens = chain.split(":");
-      if (tokens.length == 2)
-      {
-        if (!first)
-        {
-          atomSpec.append(" or ");
-        }
-        first = false;
-        atomSpec.append(":").append(tokens[1]).append(" /").append(tokens[0]);
-      }
-    }
-
-    String spec = atomSpec.toString();
-    String command = "select *;restrict " + spec + ";cartoon;center "
-            + spec;
-    return command;
-  }
-
-  /**
-   * Returns a command to superpose atoms in {@code atomSpec} to those in
-   * {@code refAtoms}, restricted to alpha carbons only (Phosphorous for rna).
-   * For example
-   * 
-   * <pre>
-   * compare {2.1} {1.1} SUBSET {(*.CA | *.P) and conformation=1} 
-   *         ATOMS {1-87:A}{2-54:A|61-94:A} ROTATE TRANSLATE 1.0;
-   * </pre>
-   * 
-   * where {@code conformation=1} excludes ALTLOC atom locations, and 1.0 is the
-   * time in seconds to animate the action. For this example, atoms in model 2
-   * are moved towards atoms in model 1.
-   * <p>
-   * The two atomspecs should each be for one model only, but may have more than
-   * one chain. The number of atoms specified should be the same for both
-   * models, though if not, Jmol may make a 'best effort' at superposition.
-   * 
-   * @see https://chemapps.stolaf.edu/jmol/docs/#compare
-   */
-  @Override
-  public String superposeStructures(AtomSpecModel refAtoms,
-          AtomSpecModel atomSpec)
+  public StructureCommandI openSession(String filepath)
   {
-    StringBuilder sb = new StringBuilder(64);
-    int refModel = refAtoms.getModels().iterator().next();
-    int model2 = atomSpec.getModels().iterator().next();
-    sb.append(String.format("compare {%d.1} {%d.1}", model2, refModel));
-    sb.append(" SUBSET {(*.CA | *.P) and conformation=1} ATOMS {");
-
-    /*
-     * command examples don't include modelspec with atoms, getAtomSpec does;
-     * it works, so leave it as it is for simplicity
-     */
-    sb.append(getAtomSpec(atomSpec, true)).append("}{");
-    sb.append(getAtomSpec(refAtoms, true)).append("}");
-    sb.append(" ROTATE TRANSLATE ");
-    sb.append(getCommandSeparator());
-
-    /*
-     * show residues used for superposition as ribbon
-     */
-    sb.append("select ").append(getAtomSpec(atomSpec, false)).append("|");
-    sb.append(getAtomSpec(refAtoms, false)).append(getCommandSeparator())
-            .append("cartoons");
-
-    return sb.toString();
-  }
-
-  @Override
-  public String openCommandFile(String path)
-  {
-    /*
-     * https://chemapps.stolaf.edu/jmol/docs/#script
-     * not currently used in Jalview
-     */
-    return "script " + path;
-  }
-
-  @Override
-  public String saveSession(String filepath)
-  {
-    /*
-     * https://chemapps.stolaf.edu/jmol/docs/#write
-     * not currently used in Jalview
-     */
-    return "write \"" + filepath + "\"";
-  }
-
-  @Override
-  protected String getColourCommand(String atomSpec, Color colour)
-  {
-    StringBuilder sb = new StringBuilder(atomSpec.length()+20);
-    sb.append("select ").append(atomSpec).append(getCommandSeparator())
-            .append("color").append(getColourString(colour));
-    return sb.toString();
-  }
-
-  @Override
-  protected String getResidueSpec(String residue)
-  {
-    return residue;
-  }
-
-  /**
-   * Generates a Jmol atomspec string like
-   * 
-   * <pre>
-   * 2-5:A/1.1,8:A/1.1,5-10:B/2.1
-   * </pre>
-   * 
-   * Parameter {@code alphaOnly} is not used here - this restriction is made by
-   * a separate clause in the {@code compare} (superposition) command.
-   */
-  @Override
-  public String getAtomSpec(AtomSpecModel model, boolean alphaOnly)
-  {
-    StringBuilder sb = new StringBuilder(128);
-
-    boolean first = true;
-    for (int modelNo : model.getModels())
-    {
-      for (String chain : model.getChains(modelNo))
-      {
-        for (int[] range : model.getRanges(modelNo, chain))
-        {
-          if (!first)
-          {
-            sb.append(PIPE);
-          }
-          first = false;
-          if (range[0] == range[1])
-          {
-            sb.append(range[0]);
-          }
-          else
-          {
-            sb.append(range[0]).append(HYPHEN).append(range[1]);
-          }
-          sb.append(COLON).append(chain.trim()).append(SLASH);
-          sb.append(String.valueOf(modelNo)).append(".1");
-        }
-      }
-    }
-
-    return sb.toString();
+    return loadFile(filepath);
   }
 
   @Override
-  public String showBackbone()
+  public StructureCommandI closeViewer()
   {
-    return "select *; cartoons off; backbone";
+    return null; // not an external viewer
   }
 }