The heteroatoms shouldn't be part of the chain. But you can know if a residue is a heteroatom with:
pdb = PDBParser().get_structure("1C4R", "1C4R.pdb")
for residue in pdb.get_residues():
tags = residue.get_full_id()
# tags contains a tuple with (Structure ID, Model ID, Chain ID, (Residue ID))
# Residue ID is a tuple with (*Hetero Field*, Residue ID, Insertion Code)
# Thus you're interested in the Hetero Field, that is empty if the residue
# is not a hetero atom or have some flag if it is (W for waters, H, etc.)
if tags[3][0] != " ":
# The residue is a heteroatom
else:
# It is not
You can also get the id of the residue (without the three first fields) with:
tags = residue.id
# or het_flag,_ ,_ = residue.id
if tags[0] != " ":
# The residue is a heteroatom
else:
# It is not
I'm adding a link to the relevant documentation: http://biopython.org/DIST/docs/cookbook/biopdb_faq.pdf
The subject is in the page 8, "What is a residue id?". Quoting:
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 (eg. ’H_GLC’
in the case of a glucose molecule), or ’W’ in the case of a water molecule.
To add comments in and resume:
from Bio.PDB import PDBParser, PDBIO, Select
class NonHetSelect(Select):
def accept_residue(self, residue):
return 1 if residue.id[0] == " " else 0
pdb = PDBParser().get_structure("1C4R", "1C4R.pdb")
io = PDBIO()
io.set_structure(pdb)
io.save("non_het.pdb", NonHetSelect())