From 418f5d27e1764f9472a7455195170b92b5af1dc5 Mon Sep 17 00:00:00 2001 From: Patrick Kunzmann Date: Mon, 28 Oct 2024 10:10:45 +0100 Subject: [PATCH] Allow setting the internal CCD --- doc/apidoc.json | 3 ++- src/biotite/structure/info/ccd.py | 8 ++++++-- tests/structure/test_info.py | 33 +++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/doc/apidoc.json b/doc/apidoc.json index 1106aabbc..cce4cb16a 100644 --- a/doc/apidoc.json +++ b/doc/apidoc.json @@ -362,7 +362,8 @@ ], "Low-level CCD access": [ "get_ccd", - "get_from_ccd" + "get_from_ccd", + "set_ccd_path" ] }, "biotite.structure.io.pdbx" : { diff --git a/src/biotite/structure/info/ccd.py b/src/biotite/structure/info/ccd.py index eb62e6508..adcbc92f0 100644 --- a/src/biotite/structure/info/ccd.py +++ b/src/biotite/structure/info/ccd.py @@ -4,9 +4,12 @@ __name__ = "biotite.structure.info" __author__ = "Patrick Kunzmann" -__all__ = ["get_ccd", "get_from_ccd"] +__all__ = ["get_ccd", "set_ccd_path", "get_from_ccd"] import functools +import importlib +import inspect +import pkgutil from pathlib import Path import numpy as np @@ -28,6 +31,7 @@ def get_ccd(): ------- ccd : BinaryCIFBlock The CCD. + It contains the categories `chem_comp`, `chem_comp_atom` and `chem_comp_bond`. Warnings -------- @@ -79,7 +83,7 @@ def set_ccd_path(ccd_path): # Clear caches in all functions in biotite.structure.info info_modules = [ importlib.import_module(f"biotite.structure.info.{mod_name}") - for _, mod_name, _ in pkgutil.iter_modules([Path(__file__).parent]) + for _, mod_name, _ in pkgutil.iter_modules([str(Path(__file__).parent)]) ] for module in info_modules: for _, function in inspect.getmembers(module, callable): diff --git a/tests/structure/test_info.py b/tests/structure/test_info.py index 2d823aaf1..90b9cbc90 100644 --- a/tests/structure/test_info.py +++ b/tests/structure/test_info.py @@ -8,10 +8,30 @@ import pytest import biotite.structure as struc import biotite.structure.info as strucinfo +import biotite.structure.io.pdbx as pdbx +from biotite.structure.info.ccd import _CCD_FILE as INTERNAL_CCD_FILE from biotite.structure.io import load_structure from tests.util import data_dir +@pytest.fixture +def fake_ccd_path(tmp_path): + block = pdbx.BinaryCIFBlock() + block["chem_comp"] = pdbx.BinaryCIFCategory({"id": "FOO", "name": "Foo"}) + block["chem_comp_atom"] = pdbx.BinaryCIFCategory({"comp_id": "FOO"}) + block["chem_comp_bond"] = pdbx.BinaryCIFCategory({"comp_id": "FOO"}) + file = pdbx.BinaryCIFFile() + file["components"] = block + original_path = INTERNAL_CCD_FILE + path = tmp_path / "components.bcif" + file.write(path) + + yield path + + # Restore the original internal CCD path + strucinfo.set_ccd_path(original_path) + + @pytest.mark.parametrize( "function, included, excluded", [ @@ -159,3 +179,16 @@ def test_standardize_order(multi_model, seed): assert ( restored[..., restored.element != "H"] == original[..., original.element != "H"] ) + + +def test_set_ccd_path(fake_ccd_path): + """ + Test if the CCD path can be set and the CCD is loaded correctly from it. + """ + # Access CCD before setting it to a new path to check if the cache is cleared + strucinfo.all_residues() + + strucinfo.set_ccd_path(fake_ccd_path) + + # The new fake CCD has only a single compound + assert strucinfo.all_residues() == ["FOO"]