Hello,

I am working on a Python project that uses ChimeraX (currently running 1.5 to interactively view dynamics data by mapping it onto a molecule. All the figures/movies in this paper, https://www.nature.com/articles/s41467-021-27417-y, were made possible by Chimera! Also thanks to Tom Goddard for explaining to me how some of those figures could be made awhile ago (https://rbvi.github.io/chimerax-recipes/spherical_harmonics/spherical_harmonics.html ). 

So we’re controlling ChimeraX remotely; we launch a session and then can send commands from Python over to Chimera and edit images from within our python code (while still allowing us to interact via the ChimeraX interface). The basic way this works is as follows

launch chimeraX from our code, including a short python script
python script imports our libraries, launches “cmxr” (chimeraX remote)
cmxr uses multiprocessing.connection.Listener to listen for commands/data coming from Client in our python code
!!!The Listener runs in a thread (threading.Thread) so that we continue to be able to interact normally with ChimeraX (launching the Listener without using a thread freezes ChimeraX while waiting for commands, rendering it unusable)
Then, we just tell it to run some function(s) that we’ve imported into ChimeraX, and provide some data, atom ids, etc. over the Client/Listener connection

There are a few things that I can’t figure how to make work via this process. We’ve had a few long-standing workarounds, but I’ve always been a little disturbed that we can’t get this all to work from python alone. At the moment, when we launch ChimeraX, we also enable remote control via curl, and some commands are then sent that way (remotecontrol rest start port XXXX)

In particular, if I want to send commands that would normally be executed at ChimeraX’s command line, in principle I should be able to use chimerax.core.commands.run. If I execute “run” from a simple python script, everything works, but if I run it from within a thread (as required by our Listener setup), then ChimeraX freezes. So, we send these commands via curl on the remotecontrol instead.

Second, is there a right way to open models/surfaces from the ChimeraX python shell? At the moment, we also open pdbs via curl commands. For pdbs, maybe I just don’t know the right python command. However, I am re-implementing spherical harmonics referenced above. Once I have prepared the surface (“s”), if I add it to session.models, it crashes chimeraX if it is being added within the listener thread, and if there are no models already open. If another model is already open, then it works just fine, and if I load the same modules/execute the same commands from within the python shell provided in ChimeraX, it also works (regardless of other models being open). It seems like maybe ChimeraX isn’t able to fully initialize the session from within a thread. 

I’ve grouped these problems together because I guess mainly they have to do with being unable to do some actions from within threads, and if there’s an explanation/workaround for dealing with that.

Thanks for your help!

Cheers,
Albert



–––––––––––––––––––––––––––––––––––
Albert Smith-Penzel, Ph.D.
DFG Project Leader
Institute for Medical Physics and Biophysics
Leipzig University
04107 Leipzig, Germany
E-mail: albert.smith-penzel@uni-leipzig.de