KKULHARIA LABComputational Biology & BioinformaticsProf. Mahesh Kulharia
Central University of Himachal Pradesh
Bioinfo Compass

PDB manipulation with Python

PDB coordinate files use fixed-width columns. A field’s meaning comes from its column position, so splitting each line on spaces is unsafe. This practical reads a few fields, selects atoms, measures a distance and writes a selected coordinate subset.

Training scope: the supplied structure is synthetic. The parser is a teaching example, not a complete PDB/mmCIF implementation. For research data use a maintained structural-biology library, keep the original file and preserve metadata.

1. Inspect an atom record

Download the miniature synthetic PDB file. In an ATOM or HETATM record, atom name is in columns 13–16, residue name 18–20, chain 22, residue number 23–26, coordinates 31–54, occupancy 55–60 and element 77–78 (1-based column numbering). Python slices are 0-based and the right endpoint is excluded.

name = line[12:16].strip()
chain = line[21:22].strip()
x = float(line[30:38])
y = float(line[38:46])
z = float(line[46:54])

The supplied script turns those fields into an Atom object. It skips alternate location B by default and only reads ATOM records unless HETATM inclusion is requested. These are explicit teaching choices, not a universal cleaning rule.

2. Select and measure

from pathlib import Path
from pdb_practice import read_pdb, select_atoms, distance

pdb = Path("mini_structure.pdb")
atoms = read_pdb(pdb)
ca = select_atoms(atoms, chain="A", atom_name="CA")
print(len(ca), distance(ca[0], ca[1]))

The expected distance between the two selected chain-A C-alpha atoms is 5.000 Å. The coordinates are invented solely to make that result checkable.

3. Write a selected subset carefully

from pdb_practice import read_pdb, select_atoms, write_atom_subset

atoms = read_pdb("mini_structure.pdb")
chain_a = select_atoms(atoms, chain="A")
write_atom_subset(chain_a, "chain_A_atoms_only.pdb")

This writes selected coordinate lines and END. It does not preserve headers, secondary-structure annotations, CONECT/bond records, LINK/SSBOND, waters, other chains, or full model metadata. It is a programming exercise, not a research-grade preparation step. Never overwrite the source structure.

4. Exercises

  1. Print the atom names for chain A.
  2. Include HETATM records and compare the atom count. What did the extra record represent?
  3. Change the alternate-location policy to B and inspect which chain-A C-alpha coordinate is retained. Why must alternate conformations be handled consciously?
  4. Run the supplied tests and then add a test for selecting chain B.
Worked answers

Chain A protein atoms are N, CA, C, O, CA and CB when the default alternate policy keeps A and skips B. Enabling HETATM adds the synthetic NAG record. Chain A C-alpha under altloc B uses the alternate coordinate (4.500, 5.000, 0.000); the distance from residue 1 changes accordingly. Alternate locations represent alternative atom positions and should be resolved under an explicit occupancy/structural interpretation policy.

Production workflows

Real PDB files may contain several models, insertion codes, modified residues, anisotropic factors, occupancies, alternate locations, ligands, metals, waters, covalent links and records whose relationships must remain consistent. PDBx/mmCIF is now common for larger structures. Do not hand-edit fixed columns or discard records without checking downstream requirements. The test file checks this small parser’s stated scope only.

Reference: Biopython Bio.PDB tutorial and structural-file parsing.