JAL-2422 pull-up refactoring of structure commands (continued)
[jalview.git] / src / jalview / ext / jmol / JmolCommands.java
index 7a52ba9..c8a54cd 100644 (file)
@@ -28,49 +28,56 @@ import jalview.datamodel.AlignmentI;
 import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.SequenceI;
 import jalview.renderer.seqfeatures.FeatureColourFinder;
+import jalview.structure.StructureCommandsBase;
 import jalview.structure.StructureMapping;
-import jalview.structure.StructureMappingcommandSet;
 import jalview.structure.StructureSelectionManager;
+import jalview.util.Comparison;
 
 import java.awt.Color;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 
 /**
- * Routines for generating Jmol commands for Jalview/Jmol binding another
- * cruisecontrol test.
+ * Routines for generating Jmol commands for Jalview/Jmol binding
  * 
  * @author JimP
  * 
  */
-public class JmolCommands
+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";
 
-  /**
-   * Jmol utility which constructs the commands to colour chains by the given
-   * alignment
-   * 
-   * @returns Object[] { Object[] { <model being coloured>,
-   * 
-   */
-  public static StructureMappingcommandSet[] getColourBySequenceCommand(
+  private static final String CMD_COLOUR_BY_CHAIN = "select *;color chain";
+
+  private static String formatRGB(Color c) {
+    return c == null ? null
+            : String.format("[%d,%d,%d]", c.getRed(), c.getGreen(),
+                    c.getBlue());
+  }
+
+  @Override
+  public String[] colourBySequence(
           StructureSelectionManager ssm, String[] files,
           SequenceI[][] sequence, SequenceRenderer sr,
           AlignmentViewPanel viewPanel)
   {
+    // TODO refactor to call buildColoursMap() first...
+
     FeatureRenderer fr = viewPanel.getFeatureRenderer();
     FeatureColourFinder finder = new FeatureColourFinder(fr);
     AlignViewportI viewport = viewPanel.getAlignViewport();
     HiddenColumns cs = viewport.getAlignment().getHiddenColumns();
     AlignmentI al = viewport.getAlignment();
-    List<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();
+    List<String> cset = new ArrayList<>();
 
     for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
     {
       StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
-      StringBuffer command = new StringBuffer();
-      StructureMappingcommandSet smc;
-      ArrayList<String> str = new ArrayList<String>();
+      StringBuilder command = new StringBuilder(128);
+      List<String> str = new ArrayList<>();
 
       if (mapping == null || mapping.length < 1)
       {
@@ -89,7 +96,7 @@ public class JmolCommands
             for (int r = 0; r < asp.getLength(); r++)
             {
               // no mapping to gaps in sequence
-              if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
+              if (Comparison.isGap(asp.getCharAt(r)))
               {
                 continue;
               }
@@ -141,7 +148,7 @@ public class JmolCommands
               // - execute command and flush
 
               if (command.length() > 0
-                      && command.charAt(command.length() - 1) == ';')
+                      && command.charAt(command.length() - 1) != ';')
               {
                 command.append(";");
               }
@@ -164,15 +171,14 @@ public class JmolCommands
         str.add(command.toString());
         command.setLength(0);
       }
-      // Finally, add the command set ready to be returned.
-      cset.add(new StructureMappingcommandSet(JmolCommands.class,
-              files[pdbfnum], str.toArray(new String[str.size()])));
+      cset.addAll(str);
 
     }
-    return cset.toArray(new StructureMappingcommandSet[cset.size()]);
+    return cset.toArray(new String[cset.size()]);
   }
 
-  public static StringBuffer condenseCommand(StringBuffer command, int pos)
+  public static StringBuilder condenseCommand(StringBuilder command,
+          int pos)
   {
 
     // work back to last 'select'
@@ -187,7 +193,7 @@ public class JmolCommands
       ;
     } while ((q = command.indexOf("select", p)) == -1 && p > 0);
 
-    StringBuffer sb = new StringBuffer(command.substring(0, q + 7));
+    StringBuilder sb = new StringBuilder(command.substring(0, q + 7));
 
     command = command.delete(0, q + 7);
 
@@ -207,4 +213,76 @@ public class JmolCommands
     return sb;
   }
 
+  @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;");
+
+    /*
+     * concatenate commands like
+     * select VAL;color[100,215,55]
+     */
+    for (Entry<String, Color> entry : colours.entrySet())
+    {
+      Color col = entry.getValue();
+      String resCode = entry.getKey();
+      cmd.append("select ").append(resCode).append(";");
+      cmd.append("color[");
+      cmd.append(formatRGB(col));
+      cmd.append("];");
+    }
+
+    return cmd.toString();
+  }
+
+  @Override
+  public String setBackgroundColour(Color col)
+  {
+    return "background " + formatRGB(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;
+  }
+
 }