-
Notifications
You must be signed in to change notification settings - Fork 8
feat: support extended (12-char) PDB IDs in output CIF patching #292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
smallfishabc
wants to merge
7
commits into
main
Choose a base branch
from
feat/extended-pdb-ids
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+229
−12
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9cd7006
feat: support extended (12-char) PDB IDs in output CIF patching
699941c
Added features according to the reviews
smallfishabc 0b70ab4
Potential fix for pull request finding
smallfishabc 2c44eb9
Potential fix for pull request finding
smallfishabc 4af5477
Potential fix for pull request finding
smallfishabc 6d1ed55
Fix a wrong suggestion from copilot
smallfishabc 99e6f9e
Merge branch 'main' into feat/extended-pdb-ids
smallfishabc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| """Helper for loading standalone scripts as importable modules in tests. | ||
| Scripts under ``scripts/`` live outside the installed ``sampleworks`` package, so they | ||
| can't be imported normally. ``load_script`` imports one by filesystem path instead. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import importlib.util | ||
| from pathlib import Path | ||
| from types import ModuleType | ||
|
|
||
|
|
||
| def load_script(script_path: Path) -> ModuleType: | ||
| """Import a standalone ``.py`` script by path so tests don't require it on ``sys.path``. | ||
| Parameters | ||
| ---------- | ||
| script_path : Path | ||
| Filesystem path to the script to import. | ||
| Returns | ||
| ------- | ||
| ModuleType | ||
| The imported module, with the script's top-level names available as attributes. | ||
| """ | ||
| spec = importlib.util.spec_from_file_location(script_path.stem, script_path) | ||
| assert spec is not None and spec.loader is not None | ||
| mod = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(mod) | ||
| return mod |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| """Unit tests for the RCSB id extraction in ``scripts/patch_output_cif_files.py``. | ||
|
|
||
| This is the "main checkpoint" of the patching pipeline: the id is read from the folder | ||
| segment of a path and drives both the RCSB ``fetch`` and the reference-input lookup, so a | ||
| wrong id here would silently patch coordinates into the wrong template. These tests pin the | ||
| exact capture for legacy and extended ids and assert that malformed folders are rejected. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from .script_loader import load_script | ||
|
|
||
|
|
||
| _SCRIPT_PATH = Path(__file__).resolve().parents[2] / "scripts" / "patch_output_cif_files.py" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def script(): | ||
| return load_script(_SCRIPT_PATH) | ||
|
|
||
|
|
||
| # An independent copy of the script's DEFAULT_RCSB_PATTERN, kept separate on purpose: the | ||
| # test is a spec, not a mirror of the implementation. If the script's default ever changes, | ||
| # this literal does not move with it -- the resulting behavior difference should surface as | ||
| # a failure here for a human to review, rather than being silently tracked. | ||
| _DEFAULT_REGEX = r"grid_search_results/(pdb_[A-Za-z0-9]{8}|[0-9][A-Za-z0-9]{3})" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("folder", "expected"), | ||
| [ | ||
| # --- WORKS: the folder is exactly a PDB id -> extracted verbatim --- | ||
| ("4hhb", "4hhb"), # legacy, lowercase | ||
|
smallfishabc marked this conversation as resolved.
|
||
| ("1VME", "1VME"), # legacy, uppercase | ||
| ("9BN8", "9BN8"), # legacy, mixed case + digits | ||
| ("pdb_00004hhb", "pdb_00004hhb"), # extended transitional -- full id, NOT 0000 / 4hhb | ||
| ("pdb_1000axyz", "pdb_1000axyz"), # extended, genuinely-new id with no legacy form | ||
| # --- REJECTED (-> None, skipped): not a bare PDB id, so we never mis-pick an entry --- | ||
| ("TEST", None), # not a PDB id at all (letter-led) | ||
| ("logs", None), # scratch folder | ||
| ("data", None), # scratch folder | ||
| ("4hhb_final", None), # id is only a PREFIX of the folder (would else capture "4hhb") | ||
| ("4hhbEXTRA", None), # id prefix, no separator | ||
| ("pdb_1000abcd_final", None), # extended-id prefix + suffix | ||
| ("protease_pdb_1000abcd", None), # id embedded after a prefix (reviewer's example) | ||
| ("1abc_pdb_1000abcd", None), # two id-like substrings -> ambiguous (would else pick "1abc") | ||
| ], | ||
| ) | ||
| def test_extract_rcsb_id_from_folder(script, folder: str, expected: str | None) -> None: | ||
| """Only a folder that is *exactly* a PDB id is extracted; everything else returns None. | ||
|
|
||
| The rejected rows are the important part: a folder like ``4hhb_final`` or | ||
| ``1abc_pdb_1000abcd`` embeds an id-shaped substring, and without the whole-component rule | ||
| the extractor would silently capture ``4hhb`` / ``1abc`` and patch the wrong entry. These | ||
| are skipped instead. Extracting an id embedded inside a larger folder name is deliberately | ||
| NOT supported by the default pattern (it is ambiguous when several id-like parts appear). | ||
| """ | ||
| path = Path(f"/data/results/grid_search_results/{folder}/trial_1/refined.cif") | ||
| assert script.extract_rcsb_id(path, _DEFAULT_REGEX) == expected | ||
|
|
||
|
|
||
| def test_extract_rcsb_id_no_match_returns_none(script) -> None: | ||
| """A path that doesn't contain the anchor at all yields None (not an exception).""" | ||
| path = Path("/data/results/some_other_dir/4hhb/refined.cif") | ||
| assert script.extract_rcsb_id(path, _DEFAULT_REGEX) is None | ||
|
|
||
|
|
||
| def test_loose_pattern_captures_non_id_raises(script) -> None: | ||
| """A permissive custom --rcsb-pattern that captures a non-id raises InvalidRcsbIdError. | ||
|
|
||
| This is distinct from a plain no-match (which returns None): the captured token matched | ||
| the pattern but isn't a real PDB id, so the user is told their pattern is the culprit. | ||
| A real id captured by the same loose pattern is still accepted. | ||
| """ | ||
| loose = r"grid_search_results/(.{4})" | ||
| base = Path("/data/results/grid_search_results") | ||
| with pytest.raises(script.InvalidRcsbIdError, match="not a valid PDB id"): | ||
| script.extract_rcsb_id(base / "TEST" / "refined.cif", loose) | ||
| assert script.extract_rcsb_id(base / "4hhb" / "refined.cif", loose) == "4hhb" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "bad_regex", | ||
| [ | ||
| r"grid_search_results/pdb_[A-Za-z0-9]{8}", # zero capturing groups | ||
| r"grid_search_results/(pdb_)([A-Za-z0-9]{8})", # two capturing groups | ||
| ], | ||
| ) | ||
| def test_extract_rcsb_id_requires_single_group(script, bad_regex: str) -> None: | ||
| """A misconfigured pattern (not exactly one group) fails loudly, not silently.""" | ||
| path = Path("/data/results/grid_search_results/pdb_00004hhb/refined.cif") | ||
| with pytest.raises(ValueError, match="exactly one capturing group"): | ||
| script.extract_rcsb_id(path, bad_regex) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.