Download construction/tests/test_structure_analysis.py from HFMI-PROFOUND/BILBY-PRO: direct link, hf CLI and curl.
- Browser
- Download file 4 kB
-
https://huggingface.co/datasets/HFMI-PROFOUND/BILBY-PRO/resolve/main/construction/tests/test_structure_analysis.py
- Command line
-
hf download hf://datasets/HFMI-PROFOUND/BILBY-PRO/construction/tests/test_structure_analysis.py
-
curl -L -o test_structure_analysis.py https://huggingface.co/datasets/HFMI-PROFOUND/BILBY-PRO/resolve/main/construction/tests/test_structure_analysis.py
4 kB
| # Copyright 2026 Emile de Bruyn | |
| # SPDX-License-Identifier: Apache-2.0 | |
| import numpy as np | |
| import pandas as pd | |
| import xarray as xr | |
| from motifbench_prosite.structure_analysis import RMSDAnalysisConfig, compute_motif_stats | |
| def _make_dummy_dataset(): | |
| coords = np.array( | |
| [ | |
| [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [2.0, 0.0, 0.0]], | |
| [[0.0, 0.0, 0.0], [1.1, 0.0, 0.0], [2.1, 0.0, 0.0]], | |
| ], | |
| dtype=np.float32, | |
| ) | |
| ds = xr.Dataset( | |
| { | |
| "motif_entry": ("instance", ["E1", "E1"]), | |
| "pattern": ("instance", ["G", "G"]), | |
| "pdb": ("instance", ["TEST", "TEST"]), | |
| "chain": ("instance", ["A", "A"]), | |
| "start": ("instance", [1, 1]), | |
| "end": ("instance", [3, 3]), | |
| "length": ("instance", [3, 3]), | |
| "motif_len": ("instance", [3, 3]), | |
| "coords": ( | |
| ("instance", "res_idx", "xyz"), | |
| coords, | |
| ), | |
| }, | |
| coords={ | |
| "instance": [0, 1], | |
| "res_idx": [0, 1, 2], | |
| "xyz": ["x", "y", "z"], | |
| }, | |
| ) | |
| return ds | |
| def test_compute_motif_stats_basic(): | |
| ds = _make_dummy_dataset() | |
| cfg = RMSDAnalysisConfig(min_instances=1, rmsd_threshold=5.0, cluster_threshold=5.0) | |
| stats = compute_motif_stats(ds, cfg, max_workers=1) | |
| assert not stats.empty | |
| assert set(["motif_entry", "n_instances", "common_len", "median_pairwise_rmsd"]).issubset(stats.columns) | |
| assert stats.loc[0, "motif_entry"] == "E1" | |
| assert stats.loc[0, "n_instances"] == 2 | |
| assert stats.loc[0, "common_len"] == 3 | |
| assert stats.loc[0, "median_pairwise_rmsd"] >= 0.0 | |
| class _FakeFsCfg: | |
| def __init__(self, conservation_ratio=0.8): | |
| self.foldseek_conservation = True | |
| self.foldseek_conservation_ratio = conservation_ratio | |
| self.foldseek_conservation_workers = 1 | |
| class _FakeFsRunner: | |
| def __init__(self, clusters): | |
| # clusters: List[List[str]] of member basenames | |
| self._clusters = clusters | |
| self.cfg = type("Cfg", (), {"threads": 1}) | |
| def enabled(self): | |
| return True | |
| def easy_cluster(self, frag_pdb_dir, out_dir, threads_override=None): | |
| out_dir.mkdir(parents=True, exist_ok=True) | |
| tsv = out_dir / "db_cluster.tsv" | |
| lines = [] | |
| for idx, members in enumerate(self._clusters): | |
| for m in members: | |
| lines.append(f"{idx}\t{m}\n") | |
| tsv.write_text("".join(lines)) | |
| return tsv | |
| def test_compute_motif_stats_foldseek_positive(tmp_path): | |
| ds = _make_dummy_dataset() | |
| cfg = RMSDAnalysisConfig(min_instances=1, rmsd_threshold=5.0, cluster_threshold=5.0) | |
| # all instances in one cluster -> ratio 1.0 >= 0.8 | |
| runner = _FakeFsRunner(clusters=[["E1_0.pdb", "E1_1.pdb"]]) | |
| fs_cfg = _FakeFsCfg(conservation_ratio=0.8) | |
| stats = compute_motif_stats( | |
| ds, | |
| cfg, | |
| max_workers=1, | |
| foldseek_runner=runner, | |
| foldseek_cfg=fs_cfg, | |
| tmpdir=tmp_path, | |
| ) | |
| assert "foldseek_max_cluster_ratio" in stats.columns | |
| assert stats.loc[0, "foldseek_max_cluster_ratio"] == 1.0 | |
| # conserved by Foldseek | |
| assert stats.loc[0, "motif_entry"] == "E1" | |
| def test_compute_motif_stats_foldseek_negative(tmp_path): | |
| ds = _make_dummy_dataset() | |
| cfg = RMSDAnalysisConfig(min_instances=1, rmsd_threshold=0.1, cluster_threshold=5.0) | |
| # split into two clusters -> ratio 0.5 < 0.8 | |
| runner = _FakeFsRunner(clusters=[["E1_0.pdb"], ["E1_1.pdb"]]) | |
| fs_cfg = _FakeFsCfg(conservation_ratio=0.8) | |
| stats = compute_motif_stats( | |
| ds, | |
| cfg, | |
| max_workers=1, | |
| foldseek_runner=runner, | |
| foldseek_cfg=fs_cfg, | |
| tmpdir=tmp_path, | |
| ) | |
| assert "foldseek_max_cluster_ratio" in stats.columns | |
| assert stats.loc[0, "foldseek_max_cluster_ratio"] == 0.5 | |
| # Should not be marked conserved under Foldseek ratio threshold | |
| assert stats.loc[0, "motif_entry"] == "E1" | |