# How to calculate number of each type of atom in a residue in PyMOL?

I have a 3D model of PDB 4UDC (dexamethasone bound to human glucocorticoid receptor), and I'd like to calculate how many atoms of each element are in the dexamethasone molecule (which has the residue name DEX) in this model, just to make sure it is correct (as I have added hydrogen atoms with h_add and if I missed a double bond it will be off). So how do I determine how many atoms of each element are present in this residue?

The sort of output I'd be looking for, from whatever answer you provide, would be something like:

C22H29FO5

if I got the bond order and everything else right. Just so you know, as I would dislike it if you thought I meant if we were to count all the atoms up altogether (which would be $22+29+1+5=57$).

• Why not just look at the pdb in an editor and search for DEX. Then cut this out and paste etc. Commented Jun 18, 2018 at 15:38

Not super elegant, but this should work:

select resn dex and symbol C


Pymol will respond,

Selector: selection "sele" defined with 22 atoms.


Repeat for H, F, O.

• Well it's a nice and simple method, it's just I'd like to be able to write a program that would represent it C[No]H[No]F[No]O[No] (where No is the number of each and square brackets, [], can be omitted). Is that possible or will have I just have to deal with this output format? Commented Jun 18, 2018 at 9:58
• I'm sure it can be done with a few lines of Python, but not by me. Commented Jun 18, 2018 at 10:31

Download the data and using say, Visual Studio Code (which is free) locate DEX in the coordinates by scrolling down. If you open the edit to this post the text is formatted properly)

HETATM 2172  C1  DEX A1778       1.049  40.118  -7.466  1.00 33.40           C
HETATM 2173  C2  DEX A1778       0.936  40.967  -6.455  1.00 33.43           C
HETATM 2174  C3  DEX A1778      -0.392  41.247  -5.876  1.00 35.10           C
HETATM 2175  C4  DEX A1778      -1.589  40.609  -6.463  1.00 32.73           C
HETATM 2176  C5  DEX A1778      -1.450  39.719  -7.454  1.00 34.13           C
HETATM 2177  C6  DEX A1778      -2.723  39.089  -8.030  1.00 33.20           C
HETATM 2178  C7  DEX A1778      -2.784  39.329  -9.551  1.00 34.15           C
HETATM 2179  C8  DEX A1778      -1.457  38.973 -10.271  1.00 33.89           C
HETATM 2180  C9  DEX A1778      -0.254  39.730  -9.617  1.00 34.09           C
HETATM 2181  C10 DEX A1778      -0.096  39.369  -8.094  1.00 32.77           C
HETATM 2182  C11 DEX A1778       1.064  39.565 -10.432  1.00 34.52           C
HETATM 2183  C12 DEX A1778       0.895  39.783 -11.960  1.00 33.48           C
HETATM 2184  C13 DEX A1778      -0.244  38.953 -12.593  1.00 34.24           C
HETATM 2185  C14 DEX A1778      -1.548  39.301 -11.772  1.00 33.89           C
HETATM 2186  C15 DEX A1778      -2.675  38.685 -12.622  1.00 35.44           C
HETATM 2187  C16 DEX A1778      -2.182  38.918 -14.084  1.00 35.12           C
HETATM 2188  C17 DEX A1778      -0.695  39.386 -14.019  1.00 35.80           C
HETATM 2189  C18 DEX A1778       0.119  37.433 -12.584  1.00 32.13           C
HETATM 2190  C19 DEX A1778       0.242  37.886  -7.780  1.00 25.76           C
HETATM 2191  C20 DEX A1778       0.187  38.810 -15.131  1.00 35.76           C
HETATM 2192  C21 DEX A1778       1.535  39.421 -15.434  1.00 35.52           C
HETATM 2193  C22 DEX A1778      -3.098  39.926 -14.771  1.00 34.29           C
HETATM 2194  F1  DEX A1778      -0.577  41.091  -9.664  1.00 36.57           F
HETATM 2195  O1  DEX A1778      -0.478  41.974  -4.905  1.00 39.49           O
HETATM 2196  O2  DEX A1778       1.689  38.298 -10.213  1.00 35.91           O
HETATM 2197  O3  DEX A1778      -0.681  40.815 -14.192  1.00 38.27           O
HETATM 2198  O4  DEX A1778      -0.157  37.869 -15.807  1.00 37.29           O
HETATM 2199  O5  DEX A1778       2.356  38.523 -16.169  1.00 35.99           O

• @pentavalentcarbon Many thanks for edit, I wasn't sure how to format. Commented Jun 19, 2018 at 9:04

Say you have selected the dex residue, and named the selection 'Dex'.
You could've done it using

select Dex, resn dex


Than you can get the count of each atom using this:

from collections import Counter;
counts = Counter([atom.symbol for atom in cmd.get_model('Dex').atom])
print(counts)


This will print

Counter({'C': 22, 'H': 29, 'F': 1, 'O': 5})


to get the format you asked for:

from collections import Counter;
counts = Counter([atom.symbol for atom in cmd.get_model('Dex').atom])
elements = [atom + str(count) for atom, count in counts.iteritems()]
print(''.join(elements))


This will print

C22H29F1O5


Notice that After the F there is a '1' instead of blank.
I could change that if you'd like with a bit more code.