2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ext.pymol;
23 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.io.PrintWriter;
29 import java.net.HttpURLConnection;
30 import java.net.SocketException;
32 import java.nio.file.Paths;
33 import java.util.ArrayList;
34 import java.util.List;
36 import jalview.bin.Cache;
37 import jalview.bin.Console;
38 import jalview.gui.Preferences;
39 import jalview.structure.StructureCommand;
40 import jalview.structure.StructureCommandI;
42 public class PymolManager
44 private static final int RPC_REPLY_TIMEOUT_MS = 15000;
46 private static final int CONNECTION_TIMEOUT_MS = 100;
48 private static final String POST1 = "<methodCall><methodName>";
50 private static final String POST2 = "</methodName><params>";
52 private static final String POST3 = "</params></methodCall>";
54 private Process pymolProcess;
56 private int pymolXmlRpcPort;
59 * Returns a list of paths to try for the PyMOL executable. Any user
60 * preference is placed first, otherwise 'standard' paths depending on the
65 public static List<String> getPymolPaths()
67 return getPymolPaths(System.getProperty("os.name"));
71 * Returns a list of paths to try for the PyMOL executable. Any user
72 * preference is placed first, otherwise 'standard' paths depending on the
76 * operating system as reported by environment variable
80 protected static List<String> getPymolPaths(String os)
82 List<String> pathList = new ArrayList<>();
84 String userPath = Cache.getDefault(Preferences.PYMOL_PATH, null);
87 pathList.add(userPath);
91 * add default installation paths
93 String pymol = "PyMOL";
94 if (os.startsWith("Linux"))
96 pathList.add("/usr/local/pymol/bin/" + pymol);
97 pathList.add("/usr/local/bin/" + pymol);
98 pathList.add("/usr/bin/" + pymol);
99 pathList.add(System.getProperty("user.home") + "/opt/bin/" + pymol);
101 else if (os.startsWith("Windows"))
103 // todo Windows installation path(s)
104 for (String root : new String[]
106 String.format("%s\\AppData\\Local", System.getProperty("user.home")),
107 "\\Program Files", "C:\\Program Files", "\\Program Files (x86)",
108 "C:\\Program Files (x86)" })
110 for (String binary : new String [] {"PyMOLWinWithConsole.bat", "PyMOLWin.exe"})
112 pathList.add(String.format("%s\\Schrodinger\\PyMOL2\\%s", root, binary));
117 else if (os.startsWith("Mac"))
119 pathList.add("/Applications/PyMOL.app/Contents/MacOS/" + pymol);
124 public boolean isPymolLaunched()
126 // TODO pull up generic methods for external viewer processes
127 boolean launched = false;
128 if (pymolProcess != null)
132 pymolProcess.exitValue();
133 // if we get here, process has ended
134 } catch (IllegalThreadStateException e)
136 // ok - not yet terminated
144 * Sends the command to Pymol; if requested, tries to get and return any
145 * replies, else returns null
151 public List<String> sendCommand(StructureCommandI command,
154 String postBody = getPostRequest(command);
155 // System.out.println(postBody);// debug
156 String rpcUrl = "http://127.0.0.1:" + this.pymolXmlRpcPort;
157 PrintWriter out = null;
158 BufferedReader in = null;
159 List<String> result = getReply ? new ArrayList<>() : null;
163 URL realUrl = new URL(rpcUrl);
164 HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
165 conn.setRequestProperty("accept", "*/*");
166 conn.setRequestProperty("content-type", "text/xml");
167 conn.setDoOutput(true);
168 conn.setDoInput(true);
169 out = new PrintWriter(conn.getOutputStream());
172 int rc = conn.getResponseCode();
173 if (rc != HttpURLConnection.HTTP_OK)
176 String.format("Error status from %s: %d", rpcUrl, rc));
180 InputStream inputStream = conn.getInputStream();
183 in = new BufferedReader(new InputStreamReader(inputStream));
185 while ((line = in.readLine()) != null)
190 } catch (SocketException e)
192 // thrown when 'quit' command is sent to PyMol
193 Console.warn(String.format("Request to %s returned %s", rpcUrl,
195 } catch (Exception e)
204 if (Console.isTraceEnabled())
206 Console.trace("Sent: " + command.toString());
209 Console.trace("Received: " + result);
217 * Builds the body of the XML-RPC format POST request to execute the command
222 static String getPostRequest(StructureCommandI command)
224 StringBuilder sb = new StringBuilder(64);
225 sb.append(POST1).append(command.getCommand()).append(POST2);
226 if (command.hasParameters())
228 for (String p : command.getParameters())
231 * for now assuming all are string - <string> element is optional
232 * refactor in future if other data types needed
233 * https://www.tutorialspoint.com/xml-rpc/xml_rpc_data_model.htm
235 sb.append("<parameter><value>").append(p)
236 .append("</value></parameter>");
240 return sb.toString();
243 public Process launchPymol()
245 // todo pull up much of this
246 // Do nothing if already launched
247 if (isPymolLaunched())
252 String error = "Error message: ";
253 for (String pymolPath : getPymolPaths())
257 // ensure symbolic links are resolved
258 pymolPath = Paths.get(pymolPath).toRealPath().toString();
259 File path = new File(pymolPath);
260 // uncomment the next line to simulate Pymol not installed
261 // path = new File(pymolPath + "x");
262 if (!path.canExecute())
264 error += "File '" + path + "' does not exist.\n";
267 List<String> args = new ArrayList<>();
269 args.add("-R"); // https://pymolwiki.org/index.php/RPC
270 ProcessBuilder pb = new ProcessBuilder(args);
271 pymolProcess = pb.start();
274 } catch (Exception e)
276 // Pymol could not be started using this path
277 error += e.getMessage();
281 if (pymolProcess != null)
283 this.pymolXmlRpcPort = getPortNumber();
284 if (pymolXmlRpcPort > 0)
286 Console.info("PyMOL XMLRPC started on port " + pymolXmlRpcPort);
290 error += "Failed to read PyMOL XMLRPC port number";
291 Console.error(error);
292 pymolProcess.destroy();
300 private int getPortNumber()
302 // TODO pull up most of this!
304 InputStream readChan = pymolProcess.getInputStream();
305 BufferedReader lineReader = new BufferedReader(
306 new InputStreamReader(readChan));
307 StringBuilder responses = new StringBuilder();
310 String response = lineReader.readLine();
311 while (response != null)
313 responses.append("\n" + response);
314 // expect: xml-rpc server running on host localhost, port 9123
315 if (response.contains("xml-rpc"))
317 String[] tokens = response.split(" ");
318 for (int i = 0; i < tokens.length - 1; i++)
320 if ("port".equals(tokens[i]))
322 port = Integer.parseInt(tokens[i + 1]);
329 break; // hack for hanging readLine()
331 response = lineReader.readLine();
333 } catch (Exception e)
335 Console.error("Failed to get REST port number from " + responses
336 + ": " + e.getMessage());
337 // logger.error("Failed to get REST port number from " + responses + ": "
338 // + e.getMessage());
344 } catch (IOException e2)
350 Console.error("Failed to start PyMOL with XMLRPC, response was: "
353 Console.info("PyMOL started with XMLRPC on port " + port);