JAL-3551 working proof of concept of Jalview driving PyMOL
[jalview.git] / src / jalview / ext / rbvi / chimera / ChimeraCommands.java
index 1b1dd35..7e04f39 100644 (file)
@@ -29,17 +29,17 @@ import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.gui.Desktop;
 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.ColorUtils;
-import jalview.util.IntRangeComparator;
 
 import java.awt.Color;
 import java.util.ArrayList;
-import java.util.Collections;
+import java.util.Arrays;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -52,21 +52,25 @@ import java.util.Map;
  */
 public class ChimeraCommands extends StructureCommandsBase
 {
+  private static final StructureCommand SHOW_BACKBONE = new StructureCommand("~display all;chain @CA|P");
+
   public static final String NAMESPACE_PREFIX = "jv_";
 
-  private static final String CMD_COLOUR_BY_CHARGE = "color white;color red ::ASP;color red ::GLU;color blue ::LYS;color blue ::ARG;color yellow ::CYS";
+  private static final StructureCommandI COLOUR_BY_CHARGE = new StructureCommand(
+          "color white;color red ::ASP;color red ::GLU;color blue ::LYS;color blue ::ARG;color yellow ::CYS");
 
-  private static final String CMD_COLOUR_BY_CHAIN = "rainbow chain";
+  private static final StructureCommandI COLOUR_BY_CHAIN = new StructureCommand(
+          "rainbow chain");
 
   // Chimera clause to exclude alternate locations in atom selection
   private static final String NO_ALTLOCS = "&~@.B-Z&~@.2-9";
 
   @Override
-  public String getColourCommand(String atomSpec, Color colour)
+  public StructureCommandI getColourCommand(String atomSpec, Color colour)
   {
     // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/color.html
     String colourCode = getColourString(colour);
-    return "color " + colourCode + " " + atomSpec;
+    return new StructureCommand("color " + colourCode + " " + atomSpec);
   }
 
   /**
@@ -92,7 +96,7 @@ public class ChimeraCommands extends StructureCommandsBase
    * @return
    */
   @Override
-  public String[] setAttributesForFeatures(
+  public List<StructureCommandI> setAttributesForFeatures(
           StructureSelectionManager ssm, String[] files, SequenceI[][] seqs,
           AlignmentViewPanel viewPanel)
   {
@@ -155,6 +159,7 @@ public class ChimeraCommands extends StructureCommandsBase
     for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
     {
       final int modelNumber = pdbfnum + getModelStartNo();
+      String modelId = String.valueOf(modelNumber);
       StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
 
       if (mapping == null || mapping.length < 1)
@@ -178,12 +183,12 @@ public class ChimeraCommands extends StructureCommandsBase
             if (!visibleFeatures.isEmpty())
             {
               scanSequenceFeatures(visibleFeatures, structureMapping, seq,
-                      theMap, modelNumber);
+                      theMap, modelId);
             }
             if (showLinkedFeatures)
             {
               scanComplementFeatures(complementRenderer, structureMapping,
-                      seq, theMap, modelNumber);
+                      seq, theMap, modelId);
             }
           }
         }
@@ -205,7 +210,8 @@ public class ChimeraCommands extends StructureCommandsBase
   protected static void scanComplementFeatures(
           FeatureRenderer complementRenderer,
           StructureMapping structureMapping, SequenceI seq,
-          Map<String, Map<Object, AtomSpecModel>> theMap, int modelNumber)
+          Map<String, Map<Object, AtomSpecModel>> theMap,
+          String modelNumber)
   {
     /*
      * for each sequence residue mapped to a structure position...
@@ -269,19 +275,19 @@ public class ChimeraCommands extends StructureCommandsBase
   }
 
   /**
-   * Inspect features on the sequence; for each feature that is visible, determine
-   * its mapped ranges in the structure (if any) according to the given mapping,
-   * and add them to the map.
+   * Inspect features on the sequence; for each feature that is visible,
+   * determine its mapped ranges in the structure (if any) according to the
+   * given mapping, and add them to the map.
    * 
    * @param visibleFeatures
    * @param mapping
    * @param seq
    * @param theMap
-   * @param modelNumber
+   * @param modelId
    */
   protected static void scanSequenceFeatures(List<String> visibleFeatures,
           StructureMapping mapping, SequenceI seq,
-          Map<String, Map<Object, AtomSpecModel>> theMap, int modelNumber)
+          Map<String, Map<Object, AtomSpecModel>> theMap, String modelId)
   {
     List<SequenceFeature> sfs = seq.getFeatures().getPositionalFeatures(
             visibleFeatures.toArray(new String[visibleFeatures.size()]));
@@ -321,7 +327,7 @@ public class ChimeraCommands extends StructureCommandsBase
         }
         for (int[] range : mappedRanges)
         {
-          addAtomSpecRange(featureValues, value, modelNumber, range[0],
+          addAtomSpecRange(featureValues, value, modelId, range[0],
                   range[1], mapping.getChain());
         }
       }
@@ -343,10 +349,10 @@ public class ChimeraCommands extends StructureCommandsBase
    * @param featureMap
    * @return
    */
-  protected String[] setAttributes(
+  protected List<StructureCommandI> setAttributes(
           Map<String, Map<Object, AtomSpecModel>> featureMap)
   {
-    List<String> commands = new ArrayList<>();
+    List<StructureCommandI> commands = new ArrayList<>();
     for (String featureType : featureMap.keySet())
     {
       String attributeName = makeAttributeName(featureType);
@@ -368,13 +374,13 @@ public class ChimeraCommands extends StructureCommandsBase
         AtomSpecModel atomSpecModel = values.get(value);
         String featureValue = value.toString();
         featureValue = featureValue.replaceAll("\\'", "&#39;");
-        String cmd = setAttribute(attributeName, featureValue,
+        StructureCommandI cmd = setAttribute(attributeName, featureValue,
                 atomSpecModel);
         commands.add(cmd);
       }
     }
 
-    return commands.toArray(new String[commands.size()]);
+    return commands;
   }
 
   /**
@@ -390,7 +396,7 @@ public class ChimeraCommands extends StructureCommandsBase
    * @param atomSpecModel
    * @return
    */
-  protected String setAttribute(String attributeName,
+  protected StructureCommandI setAttribute(String attributeName,
           String attributeValue,
           AtomSpecModel atomSpecModel)
   {
@@ -398,7 +404,7 @@ public class ChimeraCommands extends StructureCommandsBase
     sb.append("setattr res ").append(attributeName).append(" '")
             .append(attributeValue).append("' ");
     sb.append(getAtomSpec(atomSpecModel, false));
-    return sb.toString();
+    return new StructureCommand(sb.toString());
   }
 
   /**
@@ -435,15 +441,15 @@ public class ChimeraCommands extends StructureCommandsBase
   }
 
   @Override
-  public String colourByChain()
+  public StructureCommandI colourByChain()
   {
-    return CMD_COLOUR_BY_CHAIN;
+    return COLOUR_BY_CHAIN;
   }
 
   @Override
-  public String colourByCharge()
+  public List<StructureCommandI> colourByCharge()
   {
-    return CMD_COLOUR_BY_CHARGE;
+    return Arrays.asList(COLOUR_BY_CHARGE);
   }
 
   @Override
@@ -453,21 +459,21 @@ public class ChimeraCommands extends StructureCommandsBase
   }
 
   @Override
-  public String setBackgroundColour(Color col)
+  public StructureCommandI setBackgroundColour(Color col)
   {
     // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/set.html#bgcolor
-    return "set bgColor " + ColorUtils.toTkCode(col);
+    return new StructureCommand("set bgColor " + ColorUtils.toTkCode(col));
   }
 
   @Override
-  public String focusView()
+  public StructureCommandI focusView()
   {
     // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/focus.html
-    return "focus";
+    return new StructureCommand("focus");
   }
 
   @Override
-  public String showChains(List<String> toShow)
+  public List<StructureCommandI> showChains(List<String> toShow)
   {
     /*
      * Construct a chimera command like
@@ -498,11 +504,12 @@ public class ChimeraCommands extends StructureCommandsBase
      */
     final String command = "~display #*; ~ribbon #*; ribbon :"
             + cmd.toString();
-    return command;
+    return Arrays.asList(new StructureCommand(command));
   }
 
   @Override
-  public String superposeStructures(AtomSpecModel spec, AtomSpecModel ref)
+  public List<StructureCommandI> superposeStructures(AtomSpecModel spec,
+          AtomSpecModel ref)
   {
     /*
      * Form Chimera match command to match spec to ref
@@ -518,28 +525,26 @@ public class ChimeraCommands extends StructureCommandsBase
     cmd.append("match ").append(atomSpec).append(" ").append(refSpec);
 
     /*
-     * show superposed residues as ribbon, others as chain
+     * show superposed residues as ribbon
      */
-    // fixme this should precede the loop over all alignments/structures
-    cmd.append(";~display all; chain @CA|P");
     cmd.append("; ribbon ");
     cmd.append(atomSpec).append("|").append(refSpec).append("; focus");
 
-    return cmd.toString();
+    return Arrays.asList(new StructureCommand(cmd.toString()));
   }
 
   @Override
-  public String openCommandFile(String path)
+  public StructureCommandI openCommandFile(String path)
   {
     // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/filetypes.html
-    return "open cmd:" + path;
+    return new StructureCommand("open cmd:" + path);
   }
 
   @Override
-  public String saveSession(String filepath)
+  public StructureCommandI saveSession(String filepath)
   {
     // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/save.html
-    return "save " + filepath;
+    return new StructureCommand("save " + filepath);
   }
 
   /**
@@ -571,7 +576,7 @@ public class ChimeraCommands extends StructureCommandsBase
   {
     StringBuilder sb = new StringBuilder(128);
     boolean firstModel = true;
-    for (Integer model : atomSpec.getModels())
+    for (String model : atomSpec.getModels())
     {
       if (!firstModel)
       {
@@ -591,7 +596,7 @@ public class ChimeraCommands extends StructureCommandsBase
    * @param atomSpec
    * @param alphaOnly
    */
-  protected void appendModel(StringBuilder sb, Integer model,
+  protected void appendModel(StringBuilder sb, String model,
           AtomSpecModel atomSpec, boolean alphaOnly)
   {
     sb.append("#").append(model).append(":");
@@ -603,46 +608,10 @@ public class ChimeraCommands extends StructureCommandsBase
       chain = " ".equals(chain) ? chain : chain.trim();
 
       List<int[]> rangeList = atomSpec.getRanges(model, chain);
-
-      /*
-       * sort ranges into ascending start position order
-       */
-      Collections.sort(rangeList, IntRangeComparator.ASCENDING);
-
-      int start = rangeList.isEmpty() ? 0 : rangeList.get(0)[0];
-      int end = rangeList.isEmpty() ? 0 : rangeList.get(0)[1];
-
-      Iterator<int[]> iterator = rangeList.iterator();
-      while (iterator.hasNext())
+      for (int[] range : rangeList)
       {
-        int[] range = iterator.next();
-        if (range[0] <= end + 1)
-        {
-          /*
-           * range overlaps or is contiguous with the last one
-           * - so just extend the end position, and carry on
-           * (unless this is the last in the list)
-           */
-          end = Math.max(end, range[1]);
-        }
-        else
-        {
-          /*
-           * we have a break so append the last range
-           */
-          appendRange(sb, start, end, chain, firstPositionForModel, false);
-          firstPositionForModel = false;
-          start = range[0];
-          end = range[1];
-        }
-      }
-
-      /*
-       * and append the last range
-       */
-      if (!rangeList.isEmpty())
-      {
-        appendRange(sb, start, end, chain, firstPositionForModel, false);
+        appendRange(sb, range[0], range[1], chain, firstPositionForModel,
+                false);
         firstPositionForModel = false;
       }
     }
@@ -657,9 +626,15 @@ public class ChimeraCommands extends StructureCommandsBase
   }
 
   @Override
-  public String showBackbone()
+  public List<StructureCommandI> showBackbone()
+  {
+    return Arrays.asList(SHOW_BACKBONE);
+  }
+
+  @Override
+  public StructureCommandI loadFile(String file)
   {
-    return "~display all;chain @CA|P";
+    return new StructureCommand("open " + file);
   }
 
 }