{"id":6219,"date":"2020-11-09T20:44:27","date_gmt":"2020-11-09T20:44:27","guid":{"rendered":"https:\/\/www.blopig.com\/blog\/?p=6219"},"modified":"2020-12-09T13:18:28","modified_gmt":"2020-12-09T13:18:28","slug":"using-python-in-pymol","status":"publish","type":"post","link":"https:\/\/www.blopig.com\/blog\/2020\/11\/using-python-in-pymol\/","title":{"rendered":"Using Python in PyMOL"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Decades later, we owe Warren DeLano and his commitment to open source a great debt. Warren wrote <a href=\"https:\/\/github.com\/schrodinger\/pymol-open-source\">PyMOL<\/a>, an amazingly powerful and popular molecular visualization tool, but it has many hidden talents. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Perhaps its greatest strength is the use of the open source language, Python, as its control language.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\">Before we go any further, you might need to install PyMOL first. The best way to do this is to install the open source version of PyMOL\u2014not the &#8220;Education&#8221; version from Schr\u00f6dinger. The primary reason is the ability to install and use <a href=\"https:\/\/pymolwiki.org\/index.php\/Plugins\">plugins<\/a> is absent in the &#8220;incentive&#8221; version that Schr\u00f6dinger distributes. Check out the wonderful PyMOL Wiki for instructions on how to install PyMOL in <a href=\"https:\/\/pymolwiki.org\/index.php\/Linux_Install\">Linux<\/a>, <a href=\"https:\/\/pymolwiki.org\/index.php\/MAC_Install\">macOS<\/a>, and <a href=\"https:\/\/pymolwiki.org\/index.php\/Windows_Install\">Windows<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s not unusual to have dozens of molecular objects in a PyMOL session. To do something to all or a subset of them, we need a Python list of all the objects we want to work on. To do this, we can use the &#8216;cmd.get_object_list()&#8217; method:<\/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 import cmd\nallobjs = cmd.get_object_list('all')<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">While much functionality in PyMOL is accessible using the GUI, we can also access these and many more commands from the &#8216;pymol&#8217; Python package, in particular, &#8216;pymol.cmd&#8217;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once we have a list of all the objects, we can do things like print the names of the objects, disable (effectively turn them off), or align them onto a reference object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example 1: Print the names of all the selected objects<\/h2>\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 import cmd\n\nseleobjs = cmd.get_object_list('(sele)')\nfor o in seleobjs: \\\n    print o<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example 2: Turn off all the selected objects<\/h2>\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 import cmd\n\nseleobjs = cmd.get_object_list('(sele)')\nfor o in seleobjs: \\\n    cmd.disable(o)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example 3: Align multiple selected molecules to a reference molecule<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s say we have an object called &#8216;TARGET&#8217; that we want to align all the selected objects onto. We can use the align method (see the <a href=\"https:\/\/pymolwiki.org\/index.php\/Align\">&#8220;align&#8221; page on PyMOL Wiki<\/a> for more information). <\/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 import cmd\n\nseleobjs = cmd.get_object_list('(sele)')\nfor o in seleobjs:\n    cmd.align(o, 'TARGET')<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">There are better algorithms for aligning proteins, though. PyMOL comes with &#8216;<a href=\"https:\/\/pymolwiki.org\/index.php\/Super\">super<\/a>&#8216; and &#8216;<a href=\"https:\/\/pymolwiki.org\/index.php\/Cealign\">cealign<\/a>&#8216;. It is also possible to install and use <a href=\"https:\/\/pymolwiki.org\/index.php\/TMalign\">TMalign in PyMOL<\/a>, too; the corresponding TMscore will be reported in the PyMOL message window.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Decades later, we owe Warren DeLano and his commitment to open source a great debt. Warren wrote PyMOL, an amazingly powerful and popular molecular visualization tool, but it has many hidden talents. Perhaps its greatest strength is the use of the open source language, Python, as its control language.<\/p>\n","protected":false},"author":35,"featured_media":0,"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":[29,296,14,351,202,221,227],"tags":[],"ppma_author":[488],"class_list":["post-6219","post","type-post","status-publish","format-standard","hentry","category-code","category-hints-and-tips","category-howto","category-molecular-visualization","category-proteins","category-python","category-python-code"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"authors":[{"term_id":488,"user_id":35,"is_guest":0,"slug":"garrett","display_name":"Garrett","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/df625261419c37dd5c5937e37f17a732626acd6eea1e6fabd03d935c25b453bf?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/6219","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\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/comments?post=6219"}],"version-history":[{"count":1,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/6219\/revisions"}],"predecessor-version":[{"id":6220,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/6219\/revisions\/6220"}],"wp:attachment":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/media?parent=6219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/categories?post=6219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/tags?post=6219"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=6219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}