Executing a script in chimerax python shell for an interactive session

Hi all, I hope you are doing well. I have a python script that allows the user to visualize specific residue pairs. Here is an overview of how the script functions: * Ask the user to input the path of a .cxs file (created previously) and a text file containing the pairs to be visualized * Print the names of the pairs along a uniquely assigned number * Ask the user to input a number in order to visualize the pair assigned to it To run the script currently, I have to do the following: i)Open chimerax ii)Show the shell iii) Call the script from the shell Now, is it possible to include some codes in the script that would allow me to just open chimerax, click on File -> Open -> this script, and then the shell would show up and the execution would continue there? Right now, if I ran the script using File -> Open -> this script, I would get this error: Traceback (most recent call last): File "C:/Users/Shubham/Desktop/this_script.py", line 8, in session_path = input('Enter file (.cxs) to be visualized with path: \n') RuntimeError: input(): lost sys.stdin Regards, Shubham Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows

Hi Shubham, I’d recommend converting your script to a ChimeraX command. Put your script under a function, say “visualize_pairs(live_session, saved_session, pair_input, pairs)” and then, at the bottom of your script, include some boilerplate to register the command along the lines of this example: https://rbvi.github.io/chimerax-recipes/copycolors/copycolors.html <https://rbvi.github.io/chimerax-recipes/copycolors/copycolors.html> Then, when you launch ChimeraX you need only open your Python script to register the command. Please look at the linked page and let me know if you have any questions for adapting your Python script into a ChimeraX command. — Zach
On May 22, 2022, at 22:51, Shubham Devesh Ramgoolam via ChimeraX-users <chimerax-users@cgl.ucsf.edu> wrote:
Hi all, I hope you are doing well. I have a python script that allows the user to visualize specific residue pairs. Here is an overview of how the script functions: Ask the user to input the path of a .cxs file (created previously) and a text file containing the pairs to be visualized Print the names of the pairs along a uniquely assigned number Ask the user to input a number in order to visualize the pair assigned to it
To run the script currently, I have to do the following: i)Open chimerax ii)Show the shell iii) Call the script from the shell
Now, is it possible to include some codes in the script that would allow me to just open chimerax, click on File -> Open -> this script, and then the shell would show up and the execution would continue there?
Right now, if I ran the script using File -> Open -> this script, I would get this error: Traceback (most recent call last): File "C:/Users/Shubham/Desktop/this_script.py", line 8, in session_path = input('Enter file (.cxs) to be visualized with path: \n') RuntimeError: input(): lost sys.stdin
Regards, Shubham
Sent from Mail <https://go.microsoft.com/fwlink/?LinkId=550986> for Windows
_______________________________________________ ChimeraX-users mailing list ChimeraX-users@cgl.ucsf.edu <mailto:ChimeraX-users@cgl.ucsf.edu> Manage subscription: https://www.rbvi.ucsf.edu/mailman/listinfo/chimerax-users <https://www.rbvi.ucsf.edu/mailman/listinfo/chimerax-users>

Hi Shubham, As far as I know, there is no way to get the Shell tool to start up with a script executing in it. I think what you need to do is have the script do all the heavy lifting. For one thing, it can get the path of the session file with code like this: from chimera.ui.open_save import OpenDialogWithMessage dialog = OpenDialogWithMessage(message="Choose the session file containing the structure to be analyzed", caption="Open Session") session_format = session.data_formats["ChimeraX session"] dialog.setNameFilter(session.data_formats.qt_file_filter(session_format)) paths = dialog.selectedFiles() if not paths: # user cancelled dialog from chimerax.core.errors import CancelOperation raise CancelOperation("script cancelled by user") session_path = paths[0] # probably use an "open" command to open the session You would do a similar thing for the residues text file, except you probably would skip the setNameFilter() call since there is no particular suffix you are looking for. So the tricky part is getting residue-pair choices from the user interactively. In an ideal world you would use Qt to create a dialog with a list of residue pairs in it, and clicking on an item in the list would visualize the pair. That's the approach I would use since I am familiar with Qt, but if you aren't then that's a pretty big learning curve for what you want to accomplish. Nonetheless, if that's what you'd like to do, just let me know and I'll provide some pointers. Alternatively, a simpler approach is to leverage the fact that ChimeraX supports special HTML links that execute commands. So you would write out an HTML file with a link for every residue pair and the user could click on links to visualize the corresponding pair. The file would have a bunch of lines like: <a href="cxcmd:sel :14,27; view sel">Residues 14 and 27</a> or whatever commands you wanted to execute when the link is clicked, plus any other HTML you wanted to put in it. You would then just use the "open" command to open the HTML file and it will show up in a window. --Eric Eric Pettersen UCSF Computer Graphics Lab
On May 22, 2022, at 10:51 PM, Shubham Devesh Ramgoolam via ChimeraX-users <chimerax-users@cgl.ucsf.edu> wrote:
Hi all, I hope you are doing well. I have a python script that allows the user to visualize specific residue pairs. Here is an overview of how the script functions: Ask the user to input the path of a .cxs file (created previously) and a text file containing the pairs to be visualized Print the names of the pairs along a uniquely assigned number Ask the user to input a number in order to visualize the pair assigned to it
To run the script currently, I have to do the following: i)Open chimerax ii)Show the shell iii) Call the script from the shell
Now, is it possible to include some codes in the script that would allow me to just open chimerax, click on File -> Open -> this script, and then the shell would show up and the execution would continue there?
Right now, if I ran the script using File -> Open -> this script, I would get this error: Traceback (most recent call last): File "C:/Users/Shubham/Desktop/this_script.py", line 8, in session_path = input('Enter file (.cxs) to be visualized with path: \n') RuntimeError: input(): lost sys.stdin
Regards, Shubham
Sent from Mail <https://go.microsoft.com/fwlink/?LinkId=550986> for Windows
_______________________________________________ ChimeraX-users mailing list ChimeraX-users@cgl.ucsf.edu <mailto:ChimeraX-users@cgl.ucsf.edu> Manage subscription: https://www.rbvi.ucsf.edu/mailman/listinfo/chimerax-users <https://www.rbvi.ucsf.edu/mailman/listinfo/chimerax-users>

Thank you all for the input. I will use HTML file to address this issue (thanks a lot Eric!). Regards, Shubham Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows ________________________________ From: Eric Pettersen <pett@cgl.ucsf.edu> Sent: Tuesday, May 24, 2022 8:55:09 PM To: Shubham Devesh Ramgoolam <sdramgoolam@uwaterloo.ca> Cc: chimerax-users@cgl.ucsf.edu <chimerax-users@cgl.ucsf.edu> Subject: Re: [chimerax-users] Executing a script in chimerax python shell for an interactive session Hi Shubham, As far as I know, there is no way to get the Shell tool to start up with a script executing in it. I think what you need to do is have the script do all the heavy lifting. For one thing, it can get the path of the session file with code like this: from chimera.ui.open_save import OpenDialogWithMessage dialog = OpenDialogWithMessage(message="Choose the session file containing the structure to be analyzed", caption="Open Session") session_format = session.data_formats["ChimeraX session"] dialog.setNameFilter(session.data_formats.qt_file_filter(session_format)) paths = dialog.selectedFiles() if not paths: # user cancelled dialog from chimerax.core.errors import CancelOperation raise CancelOperation("script cancelled by user") session_path = paths[0] # probably use an "open" command to open the session You would do a similar thing for the residues text file, except you probably would skip the setNameFilter() call since there is no particular suffix you are looking for. So the tricky part is getting residue-pair choices from the user interactively. In an ideal world you would use Qt to create a dialog with a list of residue pairs in it, and clicking on an item in the list would visualize the pair. That's the approach I would use since I am familiar with Qt, but if you aren't then that's a pretty big learning curve for what you want to accomplish. Nonetheless, if that's what you'd like to do, just let me know and I'll provide some pointers. Alternatively, a simpler approach is to leverage the fact that ChimeraX supports special HTML links that execute commands. So you would write out an HTML file with a link for every residue pair and the user could click on links to visualize the corresponding pair. The file would have a bunch of lines like: <a href="cxcmd:sel :14,27; view sel">Residues 14 and 27</a> or whatever commands you wanted to execute when the link is clicked, plus any other HTML you wanted to put in it. You would then just use the "open" command to open the HTML file and it will show up in a window. --Eric Eric Pettersen UCSF Computer Graphics Lab On May 22, 2022, at 10:51 PM, Shubham Devesh Ramgoolam via ChimeraX-users <chimerax-users@cgl.ucsf.edu<mailto:chimerax-users@cgl.ucsf.edu>> wrote: Hi all, I hope you are doing well. I have a python script that allows the user to visualize specific residue pairs. Here is an overview of how the script functions: * Ask the user to input the path of a .cxs file (created previously) and a text file containing the pairs to be visualized * Print the names of the pairs along a uniquely assigned number * Ask the user to input a number in order to visualize the pair assigned to it To run the script currently, I have to do the following: i)Open chimerax ii)Show the shell iii) Call the script from the shell Now, is it possible to include some codes in the script that would allow me to just open chimerax, click on File -> Open -> this script, and then the shell would show up and the execution would continue there? Right now, if I ran the script using File -> Open -> this script, I would get this error: Traceback (most recent call last): File "C:/Users/Shubham/Desktop/this_script.py", line 8, in session_path = input('Enter file (.cxs) to be visualized with path: \n') RuntimeError: input(): lost sys.stdin Regards, Shubham Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows _______________________________________________ ChimeraX-users mailing list ChimeraX-users@cgl.ucsf.edu<mailto:ChimeraX-users@cgl.ucsf.edu> Manage subscription: https://www.rbvi.ucsf.edu/mailman/listinfo/chimerax-users
participants (3)
-
Eric Pettersen
-
Shubham Devesh Ramgoolam
-
Zach Pearson