JAL-1761 need to pass molecule type when generating superposition commands
[jalview.git] / src / jalview / ext / jmol / JmolCommands.java
index b38979b..3a66349 100644 (file)
-/*******************************************************************************
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
- * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
- *
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
  * This file is part of Jalview.
- *
+ * 
  * Jalview is free software: you can redistribute it and/or
  * modify it under the terms of the GNU General Public License 
- * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
- *
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
  * Jalview is distributed in the hope that it will be useful, but 
  * WITHOUT ANY WARRANTY; without even the implied warranty 
  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
  * PURPOSE.  See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
- *******************************************************************************/
-/**\r
- * \r
- */\r
-package jalview.ext.jmol;\r
-\r
-import jalview.api.FeatureRenderer;\r
-import jalview.api.SequenceRenderer;\r
-import jalview.datamodel.AlignmentI;\r
-import jalview.datamodel.SequenceI;\r
-import jalview.structure.StructureMapping;\r
-import jalview.structure.StructureMappingcommandSet;\r
-import jalview.structure.StructureSelectionManager;\r
-import jalview.util.Comparison;\r
-\r
-import java.awt.Color;\r
-import java.util.ArrayList;\r
-/**\r
- * Routines for generating Jmol commands for Jalview/Jmol binding\r
- * another cruisecontrol test.\r
- * \r
- * @author JimP\r
- *\r
- */\r
-public class JmolCommands\r
-{\r
-\r
-  /**\r
-   * Jmol utility which constructs the commands to colour chains by the given alignment\r
-   * @returns Object[] { Object[] { <model being coloured>, \r
-   * \r
-   */\r
-  public static StructureMappingcommandSet[] getColourBySequenceCommand(StructureSelectionManager ssm, String[] files, SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr, AlignmentI alignment)\r
-  {\r
-    \r
-    ArrayList<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();\r
-    \r
-    for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)\r
-    {\r
-      StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);\r
-      StringBuffer command = new StringBuffer();\r
-      StructureMappingcommandSet smc;\r
-      ArrayList<String> str = new ArrayList<String>();\r
-  \r
-      if (mapping == null || mapping.length < 1)\r
-        continue;\r
-      \r
-      int lastPos = -1;\r
-      for (int s = 0; s < sequence[pdbfnum].length; s++)\r
-      {\r
-        for (int sp, m = 0; m < mapping.length; m++)\r
-        {\r
-          if (mapping[m].getSequence() == sequence[pdbfnum][s]\r
-                  && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)\r
-          {\r
-            SequenceI asp = alignment.getSequenceAt(sp);\r
-            for (int r = 0; r < asp.getLength(); r++)\r
-            {\r
-              // no mapping to gaps in sequence\r
-              if (jalview.util.Comparison.isGap(asp.getCharAt(r)))\r
-              {\r
-                continue;\r
-              }\r
-              int pos = mapping[m].getPDBResNum(asp.findPosition(r));\r
-  \r
-              if (pos < 1 || pos == lastPos)\r
-                continue;\r
-  \r
-              lastPos = pos;\r
-  \r
-              Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);\r
-  \r
-              if (fr != null)\r
-                col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);\r
-              String newSelcom = (mapping[m].getChain() != " " ? ":"\r
-                      + mapping[m].getChain() : "")\r
-                      + "/"\r
-                      + (pdbfnum + 1)\r
-                      + ".1"\r
-                      + ";color["\r
-                      + col.getRed()\r
-                      + ","\r
-                      + col.getGreen()\r
-                      + ","\r
-                      + col.getBlue() + "]";\r
-              if (command.length()>newSelcom.length() && command.substring(command.length()-newSelcom.length()).equals(newSelcom))\r
-              {\r
-                command = JmolCommands.condenseCommand(command, pos);\r
-                continue;\r
-              }\r
-              // TODO: deal with case when buffer is too large for Jmol to parse\r
-              // - execute command and flush\r
-  \r
-              command.append(";");\r
-              if (command.length()>51200)\r
-              {\r
-                // add another chunk\r
-                str.add(command.toString());\r
-                command.setLength(0);\r
-              }\r
-              command.append("select " + pos);\r
-              command.append(newSelcom);\r
-            }\r
-            break;\r
-          }\r
-        }\r
-      }\r
-      {\r
-          // add final chunk\r
-          str.add(command.toString());\r
-          command.setLength(0);\r
-        }\r
-        // Finally, add the command set ready to be returned.\r
-        cset.add(new StructureMappingcommandSet(JmolCommands.class,files[pdbfnum], str.toArray(new String[str.size()])));\r
-\r
-    }\r
-    return cset.toArray(new StructureMappingcommandSet[cset.size()]);\r
-  }\r
-\r
-  public static StringBuffer condenseCommand(StringBuffer command, int pos)\r
-  {\r
-  \r
-    // work back to last 'select'\r
-    int p=command.length(),q=p;\r
-    do {\r
-      p-=6;\r
-      if (p<1) { p=0; };\r
-    } while ((q=command.indexOf("select",p))==-1 && p>0);\r
-    \r
-    StringBuffer sb = new StringBuffer(command.substring(0,q+7));\r
-  \r
-    command =  command.delete(0,q+7);\r
-  \r
-    String start;\r
-  \r
-    if (command.indexOf("-") > -1)\r
-    {\r
-      start = command.substring(0, command.indexOf("-"));\r
-    }\r
-    else\r
-    {\r
-      start = command.substring(0, command.indexOf(":"));\r
-    }\r
-  \r
-    sb.append(start + "-" + pos + command.substring(command.indexOf(":")));\r
-  \r
-    return sb;\r
-  }\r
-\r
-}\r
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' 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;
+import jalview.api.SequenceRenderer;
+import jalview.datamodel.AlignmentI;
+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.util.Comparison;
+import jalview.util.Platform;
+
+/**
+ * Routines for generating Jmol commands for Jalview/Jmol binding
+ * 
+ * @author JimP
+ * 
+ */
+public class JmolCommands extends StructureCommandsBase
+{
+  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 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 = "|";
+
+  private static final String HYPHEN = "-";
+
+  private static final String COLON = ":";
+
+  private static final String SLASH = "/";
+
+  /**
+   * {@inheritDoc}
+   * 
+   * @return
+   */
+  @Override
+  public int getModelStartNo()
+  {
+    return 1;
+  }
+
+  /**
+   * 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
+            : String.format("[%d,%d,%d]", c.getRed(), c.getGreen(),
+                    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 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 List<StructureCommandI> superposeStructures(AtomSpecModel refAtoms,
+          AtomSpecModel atomSpec, boolean isNucleotide)
+  {
+    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, 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 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, boolean alphaOnly)
+  {
+    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()
+  {
+    return Arrays.asList(SHOW_BACKBONE);
+  }
+
+  @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,
+          AlignmentViewPanel viewPanel)
+  {
+    // TODO delete method
+
+    FeatureRenderer fr = viewPanel.getFeatureRenderer();
+    FeatureColourFinder finder = new FeatureColourFinder(fr);
+    AlignViewportI viewport = viewPanel.getAlignViewport();
+    HiddenColumns cs = viewport.getAlignment().getHiddenColumns();
+    AlignmentI al = viewport.getAlignment();
+    List<String> cset = new ArrayList<>();
+
+    for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
+    {
+      StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
+      StringBuilder command = new StringBuilder(128);
+      List<String> str = new ArrayList<>();
+
+      if (mapping == null || mapping.length < 1)
+      {
+        continue;
+      }
+
+      for (int s = 0; s < sequence[pdbfnum].length; s++)
+      {
+        for (int sp, m = 0; m < mapping.length; m++)
+        {
+          if (mapping[m].getSequence() == sequence[pdbfnum][s]
+                  && (sp = al.findIndex(sequence[pdbfnum][s])) > -1)
+          {
+            int lastPos = StructureMapping.UNASSIGNED_VALUE;
+            SequenceI asp = al.getSequenceAt(sp);
+            for (int r = 0; r < asp.getLength(); r++)
+            {
+              // no mapping to gaps in sequence
+              if (Comparison.isGap(asp.getCharAt(r)))
+              {
+                continue;
+              }
+              int pos = mapping[m].getPDBResNum(asp.findPosition(r));
+
+              if (pos == lastPos)
+              {
+                continue;
+              }
+              if (pos == StructureMapping.UNASSIGNED_VALUE)
+              {
+                // terminate current colour op
+                if (command.length() > 0
+                        && command.charAt(command.length() - 1) != ';')
+                {
+                  command.append(";");
+                }
+                // reset lastPos
+                lastPos = StructureMapping.UNASSIGNED_VALUE;
+                continue;
+              }
+
+              lastPos = pos;
+
+              Color col = sr.getResidueColour(sequence[pdbfnum][s], r,
+                      finder);
+
+              /*
+               * shade hidden regions darker
+               */
+              if (!cs.isVisible(r))
+              {
+                col = Color.GRAY;
+              }
+
+              String newSelcom = (mapping[m].getChain() != " "
+                      ? ":" + mapping[m].getChain()
+                      : "") + "/" + (pdbfnum + 1) + ".1" + ";color"
+                      + getColourString(col);
+              if (command.length() > newSelcom.length() && command
+                      .substring(command.length() - newSelcom.length())
+                      .equals(newSelcom))
+              {
+                command = JmolCommands.condenseCommand(command, pos);
+                continue;
+              }
+              // TODO: deal with case when buffer is too large for Jmol to parse
+              // - execute command and flush
+
+              if (command.length() > 0
+                      && command.charAt(command.length() - 1) != ';')
+              {
+                command.append(";");
+              }
+
+              if (command.length() > 51200)
+              {
+                // add another chunk
+                str.add(command.toString());
+                command.setLength(0);
+              }
+              command.append("select " + pos);
+              command.append(newSelcom);
+            }
+            // break;
+          }
+        }
+      }
+      {
+        // add final chunk
+        str.add(command.toString());
+        command.setLength(0);
+      }
+      cset.addAll(str);
+
+    }
+    return cset.toArray(new String[cset.size()]);
+  }
+
+  /**
+   * Helper method
+   * 
+   * @param command
+   * @param pos
+   * @return
+   */
+  @Deprecated
+  private static StringBuilder condenseCommand(
+          StringBuilder command,
+          int pos)
+  {
+
+    // work back to last 'select'
+    int p = command.length(), q = p;
+    do
+    {
+      p -= 6;
+      if (p < 1)
+      {
+        p = 0;
+      }
+      ;
+    } while ((q = command.indexOf("select", p)) == -1 && p > 0);
+
+    StringBuilder sb = new StringBuilder(command.substring(0, q + 7));
+
+    command = command.delete(0, q + 7);
+
+    String start;
+
+    if (command.indexOf("-") > -1)
+    {
+      start = command.substring(0, command.indexOf("-"));
+    }
+    else
+    {
+      start = command.substring(0, command.indexOf(":"));
+    }
+
+    sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
+
+    return sb;
+  }
+
+  @Override
+  public StructureCommandI openSession(String filepath)
+  {
+    return loadFile(filepath);
+  }
+
+  @Override
+  public StructureCommandI closeViewer()
+  {
+    return null; // not an external viewer
+  }
+}