JAL-2422 basic proof of concept of ChimeraX opened/coloured by Jalview
[jalview.git] / src / ext / edu / ucsf / rbvi / strucviz2 / ChimeraManager.java
index a910a5a..a322f0b 100644 (file)
@@ -254,7 +254,7 @@ public class ChimeraManager
     for (ChimeraModel chimeraModel : modelList)
     {
       // get model color
-      Color modelColor = getModelColor(chimeraModel);
+      Color modelColor = isChimeraX() ? null : getModelColor(chimeraModel);
       if (modelColor != null)
       {
         chimeraModel.setModelColor(modelColor);
@@ -265,7 +265,7 @@ public class ChimeraManager
       // chimeraSend("repr stick "+newModel.toSpec());
 
       // Create the information we need for the navigator
-      if (type != ModelType.SMILES)
+      if (type != ModelType.SMILES && !isChimeraX())
       {
         addResidues(chimeraModel);
       }
@@ -473,17 +473,19 @@ public class ChimeraManager
   public List<ChimeraModel> getModelList()
   {
     List<ChimeraModel> modelList = new ArrayList<>();
-    List<String> list = sendChimeraCommand("list models type molecule",
-            true);
-    if (list != null)
-    {
-      for (String modelLine : list)
-      {
-        ChimeraModel chimeraModel = new ChimeraModel(modelLine);
-        modelList.add(chimeraModel);
-      }
-    }
-    return modelList;
+    modelList.add(new ChimeraModel("4zhp", ModelType.PDB_MODEL, 1, 0));
+    return modelList; // ChimeraX doesn't have 'list models' command
+    // List<String> list = sendChimeraCommand("list models type molecule",
+    // true);
+    // if (list != null)
+    // {
+    // for (String modelLine : list)
+    // {
+    // ChimeraModel chimeraModel = new ChimeraModel(modelLine);
+    // modelList.add(chimeraModel);
+    // }
+    // }
+    // return modelList;
   }
 
   /**
@@ -555,6 +557,7 @@ public class ChimeraManager
       {
         // ensure symbolic links are resolved
         chimeraPath = Paths.get(chimeraPath).toRealPath().toString();
+        isChimeraX = chimeraPath.toLowerCase().contains("chimerax");
         File path = new File(chimeraPath);
         // uncomment the next line to simulate Chimera not installed
         // path = new File(chimeraPath + "x");
@@ -567,8 +570,16 @@ public class ChimeraManager
         args.add(chimeraPath);
         // shows Chimera output window but suppresses REST responses:
         // args.add("--debug");
-        args.add("--start");
-        args.add("RESTServer");
+        if (isChimeraX())
+        {
+          args.add("--cmd");
+          args.add("remote rest start");
+        }
+        else
+        {
+          args.add("--start");
+          args.add("RESTServer");
+        }
         ProcessBuilder pb = new ProcessBuilder(args);
         chimera = pb.start();
         error = "";
@@ -616,15 +627,23 @@ public class ChimeraManager
       {
         responses.append("\n" + response);
         // expect: REST server on host 127.0.0.1 port port_number
+        // ChimeraX is the same except "REST server started on host..."
         if (response.startsWith("REST server"))
         {
           String[] tokens = response.split(" ");
-          if (tokens.length == 7 && "port".equals(tokens[5]))
+          for (int i = 0; i < tokens.length - 1; i++)
           {
-            port = Integer.parseInt(tokens[6]);
-            break;
+            if ("port".equals(tokens[i]))
+            {
+              port = Integer.parseInt(tokens[i + 1]);
+              break;
+            }
           }
         }
+        if (port > 0)
+        {
+          break; // hack for hanging readLine()
+        }
         response = lineReader.readLine();
       }
     } catch (Exception e)
@@ -762,6 +781,8 @@ public class ChimeraManager
 
   private volatile boolean busy = false;
 
+  private boolean isChimeraX;
+
   /**
    * Send a command to Chimera.
    * 
@@ -775,7 +796,7 @@ public class ChimeraManager
    */
   public List<String> sendChimeraCommand(String command, boolean reply)
   {
-   // System.out.println("chimeradebug>> " + command);
+    System.out.println("chimeradebug>> " + command);
     if (!isChimeraLaunched() || command == null
             || "".equals(command.trim()))
     {
@@ -822,14 +843,24 @@ public class ChimeraManager
   {
     String restUrl = "http://127.0.0.1:" + this.chimeraRestPort + "/run";
     List<NameValuePair> commands = new ArrayList<>(1);
-    commands.add(new BasicNameValuePair("command", command));
+    String encoded = command.replace(" ", "+").replace("#", "%23")
+            .replace("|", "%7C").replace(";", "%3B");
+    commands.add(new BasicNameValuePair("command", encoded));
 
     List<String> reply = new ArrayList<>();
     BufferedReader response = null;
     try
     {
-      response = HttpClientUtils.doHttpUrlPost(restUrl, commands, CONNECTION_TIMEOUT_MS,
-              REST_REPLY_TIMEOUT_MS);
+      if (isChimeraX())
+      {
+        response = HttpClientUtils.doHttpGet(restUrl, commands,
+                CONNECTION_TIMEOUT_MS, REST_REPLY_TIMEOUT_MS);
+      }
+      else
+      {
+        response = HttpClientUtils.doHttpUrlPost(restUrl, commands,
+                CONNECTION_TIMEOUT_MS, REST_REPLY_TIMEOUT_MS);
+      }
       String line = "";
       while ((line = response.readLine()) != null)
       {
@@ -901,4 +932,9 @@ public class ChimeraManager
   {
     return chimera;
   }
+
+  public boolean isChimeraX()
+  {
+    return isChimeraX;
+  }
 }