Gemmi

0.7.5 · active · verified Sun Apr 12

Gemmi is a C++ library with comprehensive Python bindings designed for structural biology, particularly macromolecular crystallography. It provides tools for working with various file formats like mmCIF, PDB, MTZ, MRC/CCP4, and general CIF/STAR files, handling macromolecular models, refinement restraints, reflection data, and crystallographic symmetry. Currently at version 0.7.5, it is an actively developed open-source project maintained by CCP4 and Global Phasing Ltd.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load a PDB file using `gemmi.read_file()` and access basic structural elements like models, chains, residues, and atoms. It creates a dummy PDB file for a self-contained, runnable example.

import gemmi
import os

# Create a dummy PDB file for demonstration
pdb_content = """
ATOM      1  N   ALA A   1      29.186  15.021  19.530  1.00 19.34           N
ATOM      2  CA  ALA A   1      28.710  16.299  19.988  1.00 18.23           C
ATOM      3  C   ALA A   1      27.241  16.353  20.306  1.00 17.51           C
ATOM      4  O   ALA A   1      26.702  17.433  20.370  1.00 18.00           O
ATOM      5  CB  ALA A   1      29.417  17.309  19.066  1.00 20.00           C
TER
"""
with open("test.pdb", "w") as f:
    f.write(pdb_content)

# Load the PDB file
doc = gemmi.read_file("test.pdb")

# Access components of the structure
model = doc.models[0]
chain = model.chains[0]
residue = chain.residues[0]

print(f"File contains {len(doc.models)} model(s).")
print(f"First model has {len(model.chains)} chain(s).")
print(f"First chain has {len(chain.residues)} residue(s).")
print(f"First residue is {residue.name} {residue.seqid.num} and has {len(residue.atoms)} atoms.")

# Clean up the dummy file
os.remove("test.pdb")

view raw JSON →