# getting KeyError on certain residues in BioPython: ATOM vs. HETATM lines

When I want to load atoms for residuum (' ', 85, ' ') in chain A for protein 3tmm with structure[0][chain_id][residue], but I'm getting KeyError exception:

Traceback (most recent call last):
[...]
File "/home/pok/Checkouts/bilkoviny/loader.py", line 118, in _get_residue_atoms
return list(self.structure[0][chain_id][residue])
File "/usr/lib64/python2.7/site-packages/Bio/PDB/Chain.py", line 74, in __getitem__
return Entity.__getitem__(self, id)
File "/usr/lib64/python2.7/site-packages/Bio/PDB/Entity.py", line 40, in __getitem__
return self.child_dict[id]
KeyError: (' ', 85, ' ')


I assume this is because this residuum do not have "ATOM" lines in PDB file:

[...]
ATOM    704  CD1 ILE A  84      19.348  -4.839  18.138  1.00 47.08           C
ANISOU  704  CD1 ILE A  84     5862   6108   5917   -216  -1513    609       C
HETATM  705  N   MSE A  85      21.619  -0.130  16.236  1.00 31.89           N
ANISOU  705  N   MSE A  85     5143   3072   3900   -766  -1011    962       N
HETATM  706  CA  MSE A  85      22.277   0.675  15.226  1.00 40.76           C
ANISOU  706  CA  MSE A  85     6708   3869   4911   -972   -729   1162       C
HETATM  707  C   MSE A  85      23.146   1.786  15.818  1.00 44.98           C
ANISOU  707  C   MSE A  85     7191   4141   5757  -1268   -558   1109       C
HETATM  708  O   MSE A  85      24.257   2.045  15.338  1.00 43.69           O
ANISOU  708  O   MSE A  85     7055   3855   5692  -1611   -151   1153       O
HETATM  709  CB  MSE A  85      21.244   1.265  14.269  1.00 46.50           C
ANISOU  709  CB  MSE A  85     8043   4309   5316   -709   -948   1345       C
HETATM  710  CG  MSE A  85      21.849   1.954  13.052  1.00 54.03           C
ANISOU  710  CG  MSE A  85     9604   4929   5995   -907   -623   1638       C
HETATM  711 SE   MSE A  85      20.503   2.755  11.877  1.00 84.35          SE
ANISOU  711 SE   MSE A  85    14348   8336   9366   -486  -1038   1884      SE
HETATM  712  CE  MSE A  85      21.690   3.538  10.518  1.00 83.79           C
ANISOU  712  CE  MSE A  85    15063   7865   8907   -915   -414   2326       C
ATOM    713  N   SER A  86      22.636   2.453  16.846  1.00 43.76           N
ANISOU  713  N   SER A  86     5234   6599   4794  -1778   -946    195       N
[...]


I have been told "MSE is same as MET, it is just with selenium instead of sulphur" and that I probably need to load these atoms (I'm approximating secondary structure element position, so I want individual atom coordinates).

Is there some way how to get atoms for this residue in this protein via BioPython?

You can find the answer in The Biopython Structural Bioinformatics FAQ:

What is a residue id?

This is a bit more complicated, due to the clumsy PDB format. A residue id is a tuple with three elements:

• The hetero-flag: this is 'H_' plus the name of the hetero-residue (e.g. 'H_GLC' in the case of a glucose molecule), or 'W' in the case of a water molecule.
• The sequence identifier in the chain, e.g. 100
• The insertion code, e.g. 'A'. The insertion code is sometimes used to preserve a certain desirable residue numbering scheme. A Ser 80 insertion mutant (inserted e.g. between a Thr 80 and an Asn 81 residue) could e.g. have sequence identifiers and insertion codes as follows: Thr 80 A, Ser 80 B, Asn 81. In this way the residue numbering scheme stays in tune with that of the wild type structure.

The id of the above glucose residue would thus be ('H_GLC', 100, 'A').

So in your case it would be ('H_MSE', 85, ' ').

• Thank you! Please do you know if there is a way how to get something like (*, 85, *) - I mean, if I do not care about hetero-flag and insertion code at all. Jan 6 '18 at 20:12
• @jhutar ignoring biopython hetero-flag would make sense, as it is biopython-specific workaround for incorrect PDB files; I don't know if it's possible. Do not ignore insertion codes, they are part of the sequence number. Jan 7 '18 at 16:30