<p>I am guessing you used the command <code>pymol.cmd.centerofmass</code>? If so the selector is the ticket to doing it by selecting everything of the residue bar the backbone atoms, e.g. f'resi {r} not name N+CA+C+O+H'</p>
<p>Parenthetically, the <code>cmd.iterate</code> command is really ugly and counter-intuitive —it's heavily used because it is older. The command <code>get_model(selection)</code> in pymol2 module is so much nicer.</p>
<pre><code>for atom in pymol.cmd.get_model('name CA').atom:
     atom.resi #note it is type str due to insertion codes!
     atom.resn
     atom.coords # List[float]
</code></pre>
<p>Second parenthesis. A <code>and not element H</code> is not needed or beneficial to <code>pymol.cmd.centerofmass</code> as this takes account of mass and occupancy. There is no need to select only heavy atoms as a proton weighs nothing. The barycentre and centroid are the same in geometry where there is no mass to the points, but here mass is present.</p>

CC BY-SA 4.0
2021-12-09T14:09:43.770Z
That is good advice.  Thank you.  I am also looking at Thomas Holder's code (https://pymolwiki.org/index.php/Sidechaincenters) as a comparative approach.
gwot
CC BY-SA 4.0
2022-01-07T14:59:39.430Z
@gwot Upvoting and checking 'answer resolved' is a good way to say thanks
M__
CC BY-SA 4.0
2022-01-08T15:10:15.523Z
Hi Matteo, sorry for the breach of etiquette.  I have accepted you helpful answer, but do not yet have the rep to upvote.
gwot
CC BY-SA 4.0
2021-12-08T13:26:09.923Z
2021-12-08T13:32:01.143Z
2021-12-08T13:32:01.143Z
Matteo Ferla
<p>I trying to write a pythonic PyMol script that loads a PDB, cmd.iterates through the PDB and stores the residues (three letter code) into an array.  I then want to loop through this array and calculate the center of masses (COM) of each aa's sidechains (except gly) and add it into a second array.  I am able to calculate the COM of a residue as a whole, i.e. including backbone atoms.  But what I want to calculate is the COM of the sidechain.  Please point me in the right direction.</p>
<pre><code>path = 'Path to PDB dir'
for filename in glob.glob(os.path.join(path, '*.PDB')):
    cmd.load(filename)  
    stored_residues = []
    cmd.iterate('name ca', 'stored_residues.append(resn)')

    stored_residues_COM = []

    for i in stored_residues:
         stored_residues_sidechain_COM.append(centerOfMass('resn %s' % i))
</code></pre>

<python><pymol>
Pymol python script for selecting a residue's sidechain and calculating its center of mass
# Pymol python script for selecting a residue's sidechain and calculating its center of mass

(Asked by: username_0 on 2021-12-07)

I trying to write a pythonic PyMol script that loads a PDB, cmd.iterates through the PDB and stores the residues (three letter code) into an array.  I then want to loop through this array and calculate the center of masses (COM) of each aa's sidechains (except gly) and add it into a second array.  I am able to calculate the COM of a residue as a whole, i.e. including backbone atoms.  But what I want to calculate is the COM of the sidechain.  Please point me in the right direction.
path = 'Path to PDB dir'
for filename in glob.glob(os.path.join(path, '*.PDB')):
    cmd.load(filename)  
    stored_residues = []
    cmd.iterate('name ca', 'stored_residues.append(resn)')

    stored_residues_COM = []

    for i in stored_residues:
         stored_residues_sidechain_COM.append(centerOfMass('resn %s' % i))

**Tags:** python, pymol

---

## 1 answers

### Answer by username_1 on 2021-12-08. Score: 1 | Accepted answer

I am guessing you used the command pymol.cmd.centerofmass? If so the selector is the ticket to doing it by selecting everything of the residue bar the backbone atoms, e.g. f'resi {r} not name N+CA+C+O+H'
Parenthetically, the cmd.iterate command is really ugly and counter-intuitive —it's heavily used because it is older. The command get_model(selection) in pymol2 module is so much nicer.
for atom in pymol.cmd.get_model('name CA').atom:
     atom.resi #note it is type str due to insertion codes!
     atom.resn
     atom.coords # List[float]

Second parenthesis. A and not element H is not needed or beneficial to pymol.cmd.centerofmass as this takes account of mass and occupancy. There is no need to select only heavy atoms as a proton weighs nothing. The barycentre and centroid are the same in geometry where there is no mass to the points, but here mass is present.

> **Comments:**
> **username_0**: That is good advice.  Thank you.  I am also looking at Thomas Holder's code (https://pymolwiki.org/index.php/Sidechaincenters) as a comparative approach.  
> **username_2**: @gwot Upvoting and checking 'answer resolved' is a good way to say thanks  
> **username_0**: Hi Matteo, sorry for the breach of etiquette.  I have accepted you helpful answer, but do not yet have the rep to upvote.  

---

