JAL-2295 (optionally) return Chimera command reply
[jalview.git] / src / jalview / ext / rbvi / chimera / JalviewChimeraBinding.java
index 7ba9186..3d6e239 100644 (file)
@@ -38,6 +38,10 @@ import jalview.structures.models.AAStructureBindingModel;
 import jalview.util.MessageManager;
 
 import java.awt.Color;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PrintWriter;
 import java.net.BindException;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
@@ -592,31 +596,40 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
   }
 
   /**
-   * Send a command to Chimera, and optionally log any responses.
+   * Send a command to Chimera, and optionally log and return any responses
    * 
    * @param command
-   * @param logResponse
+   * @param getResponse
    */
-  public void sendChimeraCommand(final String command, boolean logResponse)
+  public List<String> sendChimeraCommand(final String command,
+          boolean getResponse)
   {
     if (viewer == null)
     {
       // ? thread running after viewer shut down
-      return;
+      return null;
     }
+    List<String> reply = null;
     viewerCommandHistory(false);
     if (lastCommand == null || !lastCommand.equals(command))
     {
       // trim command or it may never find a match in the replyLog!!
       List<String> lastReply = viewer.sendChimeraCommand(command.trim(),
-              logResponse);
-      if (logResponse && debug)
+              getResponse);
+      if (getResponse)
       {
-        log("Response from command ('" + command + "') was:\n" + lastReply);
+        reply = lastReply;
+        if (debug)
+        {
+          log("Response from command ('" + command + "') was:\n"
+                  + lastReply);
+        }
       }
     }
     viewerCommandHistory(true);
     lastCommand = command;
+
+    return reply;
   }
 
   /**
@@ -656,32 +669,16 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
     }
     AlignmentI alignment = alignmentv.getAlignment();
 
-    for (jalview.structure.StructureMappingcommandSet cpdbbyseq : getColourBySequenceCommands(
-            files, sr, fr, alignment))
+    StructureMappingcommandSet colourBySequenceCommands = ChimeraCommands
+            .getColourBySequenceCommand(getSsm(), files, getSequence(), sr,
+                    fr, alignment);
+    for (String command : colourBySequenceCommands.commands)
     {
-      for (String command : cpdbbyseq.commands)
-      {
-        sendAsynchronousCommand(command, COLOURING_CHIMERA);
-      }
+      sendAsynchronousCommand(command, COLOURING_CHIMERA);
     }
   }
 
   /**
-   * @param files
-   * @param sr
-   * @param fr
-   * @param alignment
-   * @return
-   */
-  protected StructureMappingcommandSet[] getColourBySequenceCommands(
-          String[] files, SequenceRenderer sr, FeatureRenderer fr,
-          AlignmentI alignment)
-  {
-    return ChimeraCommands.getColourBySequenceCommand(getSsm(), files,
-            getSequence(), sr, fr, alignment);
-  }
-
-  /**
    * @param command
    */
   protected void executeWhenReady(String command)
@@ -1125,4 +1122,69 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
       sm.highlightStructure(this, seq, positions);
     }
   }
+
+  /**
+   * Constructs and send commands to Chimera to set attributes on residues for
+   * features visible in Jalview
+   * 
+   * @param avp
+   */
+  public void sendFeaturesToChimera(AlignmentViewPanel avp)
+  {
+    AlignmentI alignment = avp.getAlignment();
+    FeatureRenderer fr = getFeatureRenderer(avp);
+
+    /*
+     * fr is null if feature display is turned off
+     */
+    if (fr == null)
+    {
+      return;
+    }
+
+    String[] files = getPdbFile();
+    if (files == null)
+    {
+      return;
+    }
+
+    StructureMappingcommandSet commandSet = ChimeraCommands
+            .getSetAttributeCommandsForFeatures(getSsm(), files,
+                    getSequence(), fr, alignment);
+    // for (String command : commandSet.commands)
+    // {
+    // sendAsynchronousCommand(command, null);
+    // }
+    sendCommandsByFile(commandSet.commands);
+  }
+
+  /**
+   * Write commands to a temporary file, and send a command to Chimera to open
+   * the file as a commands script. For use when sending a large number of
+   * separate commands would overload the REST interface mechanism.
+   * 
+   * @param commands
+   */
+  protected void sendCommandsByFile(String[] commands)
+  {
+    try
+    {
+      File tmp = File.createTempFile("chim", ".com");
+      tmp.deleteOnExit();
+      PrintWriter out = new PrintWriter(new FileOutputStream(tmp));
+      for (String command : commands)
+      {
+        out.println(command);
+      }
+      out.flush();
+      out.close();
+      String path = tmp.getAbsolutePath();
+      sendAsynchronousCommand("open cmd:" + path, null);
+    } catch (IOException e)
+    {
+      System.err
+              .println("Sending commands to Chimera via file failed with "
+                      + e.getMessage());
+    }
+  }
 }