-
Notifications
You must be signed in to change notification settings - Fork 0
Standardize SSSOM object_id normalization and curie_map-based parsing #352
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1fdf407
Standardize SSSOM object_id normalization and curie_map-based prefix …
turbomam 285c0bd
Format metpo utils package init for ruff format check
turbomam c4ae38a
Address Copilot review: centralize SSSOM parsing fallbacks and tighte…
turbomam ce1ac84
Format sssom_utils for CI ruff formatter check
turbomam 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Shared utilities for METPO scripts.""" |
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,77 @@ | ||
| """Helpers for parsing SSSOM metadata and identifiers.""" | ||
|
|
||
| import re | ||
turbomam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from pathlib import Path | ||
|
|
||
| CURIE_MAP_LINE = re.compile(r"^#\s{0,4}([A-Za-z][\w.-]*):\s*(\S+)\s*$") | ||
turbomam marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def strip_angle_brackets(identifier: str) -> str: | ||
| """Return identifier without surrounding angle brackets.""" | ||
| text = str(identifier).strip() | ||
| if text.startswith("<") and text.endswith(">"): | ||
| return text[1:-1].strip() | ||
| return text | ||
|
|
||
|
|
||
| def parse_sssom_curie_map(sssom_path: str | Path) -> dict[str, str]: | ||
| """Parse ``# curie_map`` prefixes from an SSSOM TSV header.""" | ||
| curie_map: dict[str, str] = {} | ||
| in_curie_map = False | ||
|
|
||
| with Path(sssom_path).open(encoding="utf-8") as handle: | ||
| for line in handle: | ||
| if not line.startswith("#"): | ||
| break | ||
|
|
||
| text = line.rstrip("\n") | ||
| if text.strip() == "# curie_map:": | ||
| in_curie_map = True | ||
| continue | ||
|
|
||
| if in_curie_map: | ||
| match = CURIE_MAP_LINE.match(text) | ||
| if match: | ||
| prefix, expansion = match.groups() | ||
| curie_map[prefix] = expansion | ||
| continue | ||
|
|
||
| # End curie_map block at the first non-entry comment line. | ||
| if text.startswith("# ") and ":" in text: | ||
| in_curie_map = False | ||
turbomam marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return curie_map | ||
|
|
||
|
|
||
| def extract_prefix(identifier: str, curie_map: dict[str, str] | None = None) -> str | None: | ||
| """ | ||
| Extract prefix from CURIE/IRI identifier. | ||
|
|
||
| Preference order: | ||
| 1. CURIE prefix when object is already CURIE-like. | ||
| 2. curie_map expansion match when object is an IRI. | ||
| 3. conservative structural fallbacks (OBO IRIs). | ||
| """ | ||
| text = strip_angle_brackets(identifier) | ||
| if not text: | ||
| return None | ||
|
|
||
| lowered = text.lower() | ||
| is_iri = lowered.startswith(("http://", "https://")) | ||
| if ":" in text and not is_iri: | ||
| return text.split(":", 1)[0] | ||
|
|
||
| if curie_map: | ||
| best_prefix: str | None = None | ||
| best_len = -1 | ||
| for prefix, expansion in curie_map.items(): | ||
| if text.startswith(expansion) and len(expansion) > best_len: | ||
| best_prefix = prefix | ||
| best_len = len(expansion) | ||
| if best_prefix is not None: | ||
| return best_prefix | ||
|
|
||
| if "/obo/" in text and "_" in text: | ||
| return text.split("/obo/")[1].split("_")[0] | ||
|
|
||
| return None | ||
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.