{"id":4887,"date":"2019-08-05T13:10:18","date_gmt":"2019-08-05T12:10:18","guid":{"rendered":"https:\/\/www.blopig.com\/blog\/?p=4887"},"modified":"2022-08-25T11:08:20","modified_gmt":"2022-08-25T10:08:20","slug":"how-to-iterate-in-pymol","status":"publish","type":"post","link":"https:\/\/www.blopig.com\/blog\/2019\/08\/how-to-iterate-in-pymol\/","title":{"rendered":"How to Iterate in PyMOL"},"content":{"rendered":"\n<p>Sometimes pointing-and-clicking just doesn&#8217;t cut it. With PyMOL&#8217;s built-in Python interpreter, repetitive actions are made simple.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Example 1: Print the names of all the selected objects<\/h2>\n\n\n\n<p>You&#8217;ve selected several objects in PyMOL, and you want to see their names. By default, a new selection object called &#8220;(sele)&#8221; is created. We can create a Python variable called seleobjs, say, and then loop over them:<\/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=\"\">seleobjs = cmd.get_object_list('(sele)')\nfor obj in seleobjs: print obj<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example 2: Disable all the selected objects<\/h2>\n\n\n\n<p>If you still have your selection,  &#8220;(sele)&#8221;, you can disable them (effectively turn them off on the right-hand sidebar):<\/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=\"\">seleobjs = cmd.get_object_list('(sele)')\nfor obj in seleobjs: cmd.disable(obj)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example 3: Delete all the selected objects<\/h2>\n\n\n\n<p>You&#8217;re done with the objects in your selection,  &#8220;(sele)&#8221;: you might be able to delete them using &#8220;delete &lt;wildcard&gt;&#8221;, but if you can&#8217;t write a wildcard that covers all the names of the objects, you can iterate over a selection and delete them:<\/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=\"\">seleobjs = cmd.get_object_list('(sele)')\nfor obj in seleobjs: cmd.delete(obj)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example 4: How to renumber residues with insertion codes in PyMOL<\/h2>\n\n\n\n<p>Here&#8217;s a more complicated example of iterating in PyMOL. Say we want to renumber the PyMOL object called &#8220;MOL_NAME&#8221;, which has residues with insertion codes to be renumbered, then, <em>e.g<\/em>.:<\/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=\"\">offset = 1000 # For proteins with fewer than 1000 residues, to avoid overwriting\n\n# Remember to edit MOL_NAME in this code!\n\n# Iterate over the \u03b1-carbons in chain B\nresnums=[] ; iterate MOL_NAME and chain B and n. CA, resnums.append(resi)\nfor i,r in reversed(list(enumerate(resnums))): cmd.alter('\/MOL_NAME\/\/B\/\\%s\/'  %  r, 'resi=\"%d\"' % (i + offset))\n\n# Iterate over the \u03b1-carbons in chain D\nresnums=[] ; iterate MOL_NAME and chain D and n. CA, resnums.append(resi)\nfor i,r in reversed(list(enumerate(resnums))): cmd.alter('\/MOL_NAME\/\/D\/\\%s\/'  %  r, 'resi=\"%d\"' % (i + offset))\n\n# Tell PyMOL to sort the atoms; this is necessary after altering\nsort<\/pre>\n\n\n\n<p>Note that these for-loops are written on one line; you could also use the backslash &#8216;\\&#8217; to break the command over multiple lines.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes pointing-and-clicking just doesn&#8217;t cut it. With PyMOL&#8217;s built-in Python interpreter, repetitive actions are made simple.<\/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,14,228,202,227,221,15],"tags":[252,132],"ppma_author":[488],"class_list":["post-4887","post","type-post","status-publish","format-standard","hentry","category-code","category-howto","category-protein-structure","category-proteins","category-python-code","category-python","category-technical","tag-command-line","tag-pymol"],"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\/4887","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=4887"}],"version-history":[{"count":5,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/4887\/revisions"}],"predecessor-version":[{"id":8492,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/4887\/revisions\/8492"}],"wp:attachment":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/media?parent=4887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/categories?post=4887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/tags?post=4887"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=4887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}