{"id":11981,"date":"2024-11-29T09:15:35","date_gmt":"2024-11-29T09:15:35","guid":{"rendered":"https:\/\/www.blopig.com\/blog\/?p=11981"},"modified":"2024-11-29T09:15:37","modified_gmt":"2024-11-29T09:15:37","slug":"controlling-pymol-from-afar","status":"publish","type":"post","link":"https:\/\/www.blopig.com\/blog\/2024\/11\/controlling-pymol-from-afar\/","title":{"rendered":"Controlling PyMol from afar"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Do you keep downloading <code>.pdb<\/code> and <code>.sdf<\/code> files and loading them into PyMol repeatedly?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If yes, then <a href=\"https:\/\/github.com\/Croydon-Brixton\/pymol-remote\">PyMol remote<\/a> might be just for you. With PyMol remote, you can control a PyMol session running on your laptop from any other machine. For example, from a Jupyter Notebook running on your HPC cluster.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h3 class=\"wp-block-heading\">Setup<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You need to have <code>pymol_remote<\/code> installed on both the local and remote machine. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install pymol_remote<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<h5 class=\"wp-block-heading\">Start PyMol server on laptop<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">First, start a PyMol server using <code>pymol_remote<\/code>. By default, it listens on port <code>9123<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pymol_remote<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Create tunnel <\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Next, forward the remote port. To do this manually, choose a port to forward to, say <code>9123<\/code> again, and create an <code>ssh<\/code> tunnel for example.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ssh -R 9123:localhost:9123 yourservername -N -f<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Optionally:<\/em> Since I also use <code>ssh<\/code> to connect to the server, I added a <code>RemoteForward<\/code> entry to my <code>~\/.ssh\/config<\/code> which will automatically do the port forwarding for me. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Host yourservername\n\tHostName yourservername.stats.ox.ac.uk\n\tRemoteForward 9123 localhost:9123<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Setup PyMol session in notebook<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Now, open a Jupyter Notebook on the remote server. In the notebook, setup the remote PyMol session using <code>localhost<\/code> as the IP address and the port chosen in the previous step. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from pymol_remote.client import PymolSession\n\ncmd = PymolSession(hostname=\"127.0.0.1\", port=9123)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Use PyMol remotely<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s it! We can now control the remote PyMol session like we would a local session. For example, we can fetch a structure from the PDB and generate the protein&#8217;s surface. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># reset PyMol session\ncmd.reinitialize()\n\n# load entry 8TZX from the PDB\ncmd.fetch(\"8TZX\")\n\n# show the protein with a white surface\ncmd.hide(\"cartoon\")\ncmd.show(\"surface\")\ncmd.color(\"white\", \"polymer\")<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, let&#8217;s add a prediction stored on the machine running the Notebook:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">with open(\".\/model_output\/top_prediction.sdf\") as file:\n  \n    # read local file into buffer\n    buffer = file.read()\n\n    # load file into the remote PyMol session\n    cmd.set_state(buffer, format=\"sdf\", object=\"prediction\")<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can manipulate the PyMol session via the user interface, the PyMol command line, or the Python API in the notebook. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once we are happy, we can save the image:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2024\/11\/image-2.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"625\" height=\"352\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2024\/11\/image-2.png?resize=625%2C352&#038;ssl=1\" alt=\"\" class=\"wp-image-12010\" srcset=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2024\/11\/image-2.png?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2024\/11\/image-2.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2024\/11\/image-2.png?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2024\/11\/image-2.png?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2024\/11\/image-2.png?resize=624%2C351&amp;ssl=1 624w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2024\/11\/image-2.png?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2024\/11\/image-2.png?w=1250&amp;ssl=1 1250w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Do you keep downloading .pdb and .sdf files and loading them into PyMol repeatedly? If yes, then PyMol remote might be just for you. With PyMol remote, you can control a PyMol session running on your laptop from any other machine. For example, from a Jupyter Notebook running on your HPC cluster.<\/p>\n","protected":false},"author":92,"featured_media":12010,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","wikipediapreview_detectlinks":true,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"ngg_post_thumbnail":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[187,29,621,296,14,351,221,227],"tags":[132,152,823,768],"ppma_author":[487],"class_list":["post-11981","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cheminformatics","category-code","category-data-visualization","category-hints-and-tips","category-howto","category-molecular-visualization","category-python","category-python-code","tag-pymol","tag-python","tag-remote","tag-ssh"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2024\/11\/image-2.png?fit=1920%2C1080&ssl=1","jetpack_sharing_enabled":true,"authors":[{"term_id":487,"user_id":92,"is_guest":0,"slug":"martin","display_name":"Martin Buttenschoen","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/766a8e998df1df02635f3d2411a8526569f394d114b2fc9ebb896d84bb37484f?s=96&d=mm&r=g","author_category":"","user_url":"","last_name":"Buttenschoen","first_name":"Martin","job_title":"","description":""}],"_links":{"self":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/11981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/users\/92"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/comments?post=11981"}],"version-history":[{"count":5,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/11981\/revisions"}],"predecessor-version":[{"id":12012,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/11981\/revisions\/12012"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/media\/12010"}],"wp:attachment":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/media?parent=11981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/categories?post=11981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/tags?post=11981"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=11981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}