<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.pymol.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=GeoffreyWoollard</id>
	<title>PyMOL Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.pymol.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=GeoffreyWoollard"/>
	<link rel="alternate" type="text/html" href="https://wiki.pymol.org/index.php/Special:Contributions/GeoffreyWoollard"/>
	<updated>2026-04-04T04:41:13Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.1</generator>
	<entry>
		<id>https://wiki.pymol.org/index.php?title=Iterate&amp;diff=12059</id>
		<title>Iterate</title>
		<link rel="alternate" type="text/html" href="https://wiki.pymol.org/index.php?title=Iterate&amp;diff=12059"/>
		<updated>2013-07-16T19:39:12Z</updated>

		<summary type="html">&lt;p&gt;GeoffreyWoollard: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''iterate''' iterates over an expression with a separate name space for each atom.  However, unlike the &amp;quot;alter&amp;quot; command, atomic properties can not be altered.  Thus, '''iterate''' is more efficient than '''alter'''.&lt;br /&gt;
&lt;br /&gt;
= Details = &lt;br /&gt;
Iterate is a powerful tool that can be used to perform operations and aggregations using atomic selections, and store the results in any global object, such as the predefined '''stored''' object (see [[#PYMOL API]] below for usage of local objects).  For example, one could iterate over all of the alpha carbons and record their B Factors, or print out all of the secondary structure classifications of all the residues.&lt;br /&gt;
&lt;br /&gt;
The '''iterate''' command only has access to certain properties.  Those properties are:&lt;br /&gt;
*'''model''': the pymol object label (appearing in the selection panel on the right)&lt;br /&gt;
*'''name''': the atom name&lt;br /&gt;
*'''resn''': the residue name&lt;br /&gt;
*'''resi''': the residue identifier (residue number)&lt;br /&gt;
*'''chain''': the chain name&lt;br /&gt;
*'''alt'''&lt;br /&gt;
*'''elem''': the chemical element&lt;br /&gt;
*'''q'''&lt;br /&gt;
*'''b''': the B Factor&lt;br /&gt;
*'''segi'''&lt;br /&gt;
*'''type''' (ATOM,HETATM): the atom type&lt;br /&gt;
*'''formal_charge''': the formal charge of the atom&lt;br /&gt;
*'''partial_charge''': the partial charge of the atom&lt;br /&gt;
*'''numeric_type'''&lt;br /&gt;
*'''text_type '''&lt;br /&gt;
*'''ID'''&lt;br /&gt;
*'''vdw'''&lt;br /&gt;
&lt;br /&gt;
All strings in the expression must be explicitly quoted.  This operation typically takes a second per thousand atoms.&lt;br /&gt;
&lt;br /&gt;
===Note about Atom Coordinates===&lt;br /&gt;
&lt;br /&gt;
The coordinates of the atom are not accessible via '''iterate'''. To inspect the coordinates of the atoms, see [[Iterate_State]].&lt;br /&gt;
&lt;br /&gt;
===USAGE===&lt;br /&gt;
 iterate (selection),expression&lt;br /&gt;
&lt;br /&gt;
===EXAMPLES===&lt;br /&gt;
====Example====&lt;br /&gt;
*The following example calculates the net charge on an object.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# create the global variable between Python and PyMOL&lt;br /&gt;
stored.net_charge = 0&lt;br /&gt;
# sum up the stored charges of all the atoms&lt;br /&gt;
iterate (all),stored.net_charge = stored.net_charge + partial_charge&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
*The following example fills an array, '''stored.names''' with the names of all the residues.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# create the global variable between Python and PyMOL&lt;br /&gt;
stored.names = []&lt;br /&gt;
# get all of the names&lt;br /&gt;
iterate (all),stored.names.append(name)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
*The following prints the b-factors for all atoms around residue #1.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
iterate resi 1, print round(b,2)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
====Example - b and resi ====&lt;br /&gt;
Just copy and paste this directly into command line&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
show_as cartoon ;&lt;br /&gt;
cmd.spectrum(&amp;quot;b&amp;quot;, &amp;quot;blue_white_red&amp;quot;, &amp;quot;n. CA&amp;quot;) ;&lt;br /&gt;
m1 = [] ;&lt;br /&gt;
iterate (all and n. CA),m1.append((b,resi,chain)) ;&lt;br /&gt;
zero = [] ;&lt;br /&gt;
[zero.append(i) for i,val in enumerate(m1) if val[0]==0] ;&lt;br /&gt;
m2 = [i for j,i in enumerate(m1) if j not in zero] ;&lt;br /&gt;
for j,i,k in m2: print j,i,k&lt;br /&gt;
bmax,resimax,chainmax =  max(m2, key=lambda p: p[0]) ;&lt;br /&gt;
bmin,resimin,chainmin =  min(m2, key=lambda p: p[0]) ;&lt;br /&gt;
cmd.ramp_new(&amp;quot;R2&amp;quot;, &amp;quot;* n. CA&amp;quot;, range=[0, bmin, bmax], color=&amp;quot;[blue, white, red ]&amp;quot;) ;&lt;br /&gt;
for b,resi,chain in m2: cmd.label(&amp;quot;chain %s and resi %s and n. CA&amp;quot;%(chain,resi), &amp;quot;b=%3.2f&amp;quot;%b)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
*The following example shows a common task in PyMOL, changing the B-factor column to display some other property.  Let's assume you have a file with a list of numbers, one per line, for each alpha carbon in the protein.  Furthermore, assume you want to map these values to the alpha carbons in the protein.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# set ALL b-factors to 0.&lt;br /&gt;
alter protName, b=0&lt;br /&gt;
&lt;br /&gt;
# read the new bfactors from disk&lt;br /&gt;
f = open('fileName','r').readlines()&lt;br /&gt;
&lt;br /&gt;
# set the alpha carbon bfactors.&lt;br /&gt;
alter protName and n. CA, b=f.pop(0)&lt;br /&gt;
&lt;br /&gt;
# Spectrum color bfactors by alpha carbons.&lt;br /&gt;
spectrum b, selection=(protName and n. CA)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== PYMOL API ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
cmd.iterate(string selection, string expression, int quiet=1, dict space=None)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When calling iterate, iterate_state, alter or alter_state from a python script, you can use the 'space' argument to pass local objects into the expression namespace.&lt;br /&gt;
&lt;br /&gt;
The second example from above but without the global pymol.stored variable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
myspace = {'names': []}&lt;br /&gt;
cmd.iterate('(all)', 'names.append(name)', space=myspace)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
User defined functions can also be included in the namespace:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def myfunc(resi,resn,name):&lt;br /&gt;
    print '%s`%s/%s' % (resn ,resi, name)&lt;br /&gt;
&lt;br /&gt;
myspace = {'myfunc': myfunc}&lt;br /&gt;
cmd.iterate('(all)', 'myfunc(resi,resn,name)', space=myspace)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===SEE ALSO===&lt;br /&gt;
[[Iterate_State]], [[Alter]], [[Alter_State]], [[Color]], [[Color#Color_by_Spectrum_Example|Coloring with different color sprectrums]].&lt;br /&gt;
&lt;br /&gt;
[[Category:Commands|Iterate]]&lt;/div&gt;</summary>
		<author><name>GeoffreyWoollard</name></author>
	</entry>
</feed>