Files
leonarski_fandClaude Opus 5.5 f861a3575d Monomer library subset: state that it is modified, with the LGPL notices
The embedded CCP4 Monomer Library subset is not an unmodified copy:
links_and_mods.cif and ener_lib.cif are cut down and metals.json becomes
metals.cif. The notices entry now says so and states how the LGPL-3.0
obligations are met (modified files stay LGPL, preferred form and the
generating script in the tree, the user can replace the embedded copy).

Each of the three derived files carries a CIF comment at its top naming its
origin, the modification and the date, and update_monomers.py writes that
comment whenever it regenerates them (the files still read with gemmi).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK
2026-11-15 16:15:13 +01:00

167 lines
8.3 KiB
Python

#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
# SPDX-License-Identifier: GPL-3.0-only
"""Copy the subset of the CCP4 Monomer Library (https://github.com/MonomerLibrary/monomers, LGPL-3.0)
that rugnux's restrained refinement carries in its binary into this directory.
update_monomers.py /path/to/monomers
COMPOUNDS below is THE list: a compound named there is restrained by the refinement, everything else
is held fixed. To add one, add its code and run this again; CMake embeds every .cif of this directory
(MonomerLibrary.cpp), so nothing else needs to change.
The compound files are copied verbatim. links_and_mods.cif is cut down to the links and modifications
that can apply to these compounds (the polymer links, the termini, the disulfide, ...), and ener_lib.cif
to its atom-type table (_lib_atom: the van der Waals and ion radii and the hydrogen-bond types).
metals.json, the library's statistics of metal-ligand distances, becomes metals.cif: for each metal ion of
COMPOUNDS and each donor element of METAL_LIGANDS, the distance per coordination number (the restraints of
metal sites the links of links_and_mods.cif do not describe).
These three are modified versions of library files, so each is written with a comment at its top that says
so, from what and when (the notice the library's LGPL-3.0 asks of a modified copy).
Requires the gemmi Python module."""
import datetime
import json
import os
import shutil
import sys
import gemmi
AMINO_ACIDS = ["ALA", "ARG", "ASN", "ASP", "CYS", "GLN", "GLU", "GLY", "HIS", "ILE",
"LEU", "LYS", "MET", "PHE", "PRO", "SER", "THR", "TRP", "TYR", "VAL", "MSE"]
NUCLEOTIDES = ["DA", "DC", "DG", "DT", "A", "C", "G", "U"]
# The modified nucleotides of tRNA and rRNA most often met, polymer residues joined by the same backbone link.
MODIFIED_NUCLEOTIDES = ["PSU", "5MC", "1MA", "OMG", "OMC", "OMU", "H2U", "M2G", "7MG", "2MG", "5MU"]
NUCLEOTIDE_LIGANDS = ["ATP", "ADP", "AMP", "GTP", "GDP", "GMP"]
# The cofactors most often met, and the oxygen a heme iron binds. (Not carbon monoxide: CMO.cif has C-O at 1.25 A,
# where bound CO is near 1.15 A.)
COFACTORS = ["NAD", "NAI", "NAP", "NDP", "FAD", "FMN", "HEM", "HEC", "SAM", "SAH", "OXY"]
# The chromophores of the photoreceptors time-resolved crystallography studies, with the library's links to the
# protein where it has them: retinal (its Schiff base to a lysine, LYS-RET), the p-coumaric acid of the
# photoactive yellow protein (its thioester to a cysteine is not in the library), the GFP-type chromophores
# (CRO-pept), and the bilins of the phytochromes (phycocyanobilin's thioether to a cysteine, CYS-CYC). The flavins of
# the LOV domains and photolyases are among the COFACTORS.
CHROMOPHORES = ["RET", "HC4", "CRO", "GYS", "CR2", "NRQ", "BLA", "LBV", "CYC"]
# N-acetyl-D-glucosamine, beta (NAG) and alpha (NDG): the lysozyme ligand, alone or as chitobiose/chitotriose.
SUGARS = ["NAG", "NDG"]
ADDITIVES = ["DMS", "PEG", "PGE", "PG4", "1PE", "GOL", "EDO", "SO4", "PO4", "ACT", "MPD"]
WATER_AND_IONS = ["HOH", "NA", "K", "MG", "CA", "ZN", "MN", "FE", "FE2", "NI", "CU", "CO", "CD",
"CL", "BR", "IOD"]
COMPOUNDS = (AMINO_ACIDS + NUCLEOTIDES + MODIFIED_NUCLEOTIDES + NUCLEOTIDE_LIGANDS + COFACTORS + CHROMOPHORES +
SUGARS + ADDITIVES + WATER_AND_IONS)
# The donor elements of metal sites whose distances metals.cif carries.
METAL_LIGANDS = ["O", "N", "S"]
# Groups of the compounds above: the polymers, and the pyranoses for the glycosidic links (and that of a sugar to
# Asn, Ser or Thr); links and modifications of other groups are left out.
GROUPS = {".", "peptide", "P-peptide", "M-peptide", "DNA/RNA", "NON-POLYMER", "pyranose"}
MODIFIED = {
"links_and_mods.cif": "links_and_mods.cif, cut down to the links and modifications that can apply to the "
"compounds of update_monomers.py",
"ener_lib.cif": "ener_lib.cif, cut down to its atom-type table (_lib_atom)",
"metals.cif": "metals.json, converted to CIF and cut down to the metal ions of update_monomers.py and their "
"donor elements",
}
def write_modified(doc, out):
"""Write a cut-down library file with the notice of the modification at its top."""
doc.write_file(out)
with open(out) as f:
text = f.read()
notice = ("# Modified from the CCP4 Monomer Library (https://github.com/MonomerLibrary/monomers),\n"
"# LGPL-3.0-only, see COPYING: " + MODIFIED[os.path.basename(out)] + ".\n"
"# Generated by update_monomers.py on " + datetime.date.today().isoformat() + ".\n")
with open(out, "w") as f:
f.write(notice + text)
def compound_path(lib, code):
return os.path.join(lib, code[0].lower(), code + ".cif")
def applies(comp, group):
return (comp == "." or comp in COMPOUNDS) and group in GROUPS
def trim_links_and_mods(lib, out):
doc = gemmi.cif.read(os.path.join(lib, "links_and_mods.cif"))
links = doc.find_block("link_list").find("_chem_link.", ["id", "comp_id_1", "mod_id_1", "group_comp_1",
"comp_id_2", "mod_id_2", "group_comp_2", "name"])
mods = doc.find_block("mod_list").find("_chem_mod.", ["id", "name", "comp_id", "group_id"])
keep_links = [list(r) for r in links if applies(r[1], r[3]) and applies(r[4], r[6])]
keep_mods = [list(r) for r in mods if applies(r[2], r[3])]
keep = {"link_" + r[0] for r in keep_links} | {"mod_" + r[0] for r in keep_mods}
new = gemmi.cif.Document()
for name, tag, rows in (("link_list", "_chem_link.", keep_links), ("mod_list", "_chem_mod.", keep_mods)):
block = new.add_new_block(name)
old = doc.find_block(name).find_loop_item(tag + "id").loop
loop = block.init_loop(tag, [t[len(tag):] for t in old.tags])
for r in rows:
loop.add_row(r)
for block in doc:
if block.name in keep:
new.add_copied_block(block)
write_modified(new, out)
return len(keep_links), len(keep_mods)
def trim_ener_lib(lib, out):
doc = gemmi.cif.read(os.path.join(lib, "ener_lib.cif"))
atoms = doc[0].find_loop_item("_lib_atom.type").loop
new = gemmi.cif.Document()
block = new.add_new_block(doc[0].name)
loop = block.init_loop("_lib_atom.", [t[len("_lib_atom."):] for t in atoms.tags])
for i in range(atoms.length()):
loop.add_row([atoms[i, j] for j in range(atoms.width())])
write_modified(new, out)
def trim_metals(lib, out):
"""metals.json -> metals.cif: one row per metal ion of COMPOUNDS, ligand element and coordination number,
with the mean distance, its standard deviation and the number of sites behind it."""
with open(os.path.join(lib, "metals.json")) as f:
table = json.load(f)["metal_coordination"]
metals = set()
for code in WATER_AND_IONS:
for atom in gemmi.make_chemcomp_from_block(gemmi.cif.read(compound_path(lib, code))[-1]).atoms:
if atom.el.is_metal:
metals.add(atom.el.name)
new = gemmi.cif.Document()
block = new.add_new_block("metal_coordination")
loop = block.init_loop("_metal_coordination.", ["metal", "ligand", "coordination", "distance", "std", "count"])
n = 0
for metal in sorted(metals):
for ligand in METAL_LIGANDS:
for row in table.get(metal, {}).get(ligand, []):
loop.add_row([metal, ligand, str(row["coord"]), "%.4f" % row["mean"], "%.4f" % row["std"],
str(row["count"])])
n += 1
write_modified(new, out)
return n
def main():
if len(sys.argv) != 2:
sys.exit(__doc__)
lib = sys.argv[1]
here = os.path.dirname(os.path.abspath(__file__))
for f in os.listdir(here):
if f.endswith(".cif"):
os.remove(os.path.join(here, f))
for code in COMPOUNDS:
shutil.copyfile(compound_path(lib, code), os.path.join(here, code + ".cif"))
n_links, n_mods = trim_links_and_mods(lib, os.path.join(here, "links_and_mods.cif"))
trim_ener_lib(lib, os.path.join(here, "ener_lib.cif"))
n_metal_rows = trim_metals(lib, os.path.join(here, "metals.cif"))
shutil.copyfile(os.path.join(lib, "COPYING"), os.path.join(here, "COPYING"))
print(f"{len(COMPOUNDS)} compounds, {n_links} links, {n_mods} modifications, {n_metal_rows} metal distances")
if __name__ == "__main__":
main()