Source code for boltz_data.ccd._from_path
from collections.abc import Mapping
from pathlib import Path
from boltz_data import fs
from boltz_data.cif import read_cif_from_file, read_single_cif_from_file
from ._compressed_dictionary import CompressedChemicalComponentDictionary
from ._from_mmcif import read_chemical_component_dictionary_from_mmcif
from ._models import ChemicalComponent
[docs]
def chemical_component_from_path(path: str | Path, /) -> ChemicalComponent:
"""
Read a single chemical component from a CIF file.
Args:
path: Path to the CIF file containing exactly one chemical component.
Returns:
The chemical component.
Raises:
ValueError: If the file does not contain exactly one chemical component.
"""
cif = read_single_cif_from_file(path)
ccd = read_chemical_component_dictionary_from_mmcif(cif)
if len(ccd) != 1:
msg = f"Expected exactly one chemical component in file {path}, found {len(ccd)}"
raise ValueError(msg)
return next(iter(ccd.values()))
[docs]
def chemical_component_dictionary_from_path(path: str | Path, /) -> Mapping[str, ChemicalComponent]:
"""
Read a chemical component dictionary from a file.
Supports both compressed pickle formats (.pkl.gz, .pkl) and CIF formats (.cif.gz, .cif).
Args:
path: Path to the file containing the chemical component dictionary.
Returns:
A mapping from component IDs to ChemicalComponent objects.
Raises:
ValueError: If the file format is not supported.
"""
match fs.get_extension(path, ignore_compression=True):
case ".pkl":
return CompressedChemicalComponentDictionary.from_file(path)
case ".cif":
return read_chemical_component_dictionary_from_mmcif(read_cif_from_file(path))
case _:
msg = f"Cannot open CCD from path {path}"
raise ValueError(msg)