Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/tablassert/coerce.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,18 @@ def coerce_pvalue_columns(lf: pl.LazyFrame) -> pl.LazyFrame:
""",
re.IGNORECASE | re.VERBOSE,
)
# The Biolink ``Association`` slot ``number_of_cases`` counts cases carrying the
# phenotype/disease, not the study population. It would otherwise match
# PREFIX ("number of cases") and be destroyed by the rename to ``study_size``,
# so the exact slot (separator-tolerant, like the patterns above) is exempt.
STUDY_SIZE_EXEMPT_PATTERN: re.Pattern[str] = re.compile(
rf"""
^
number {_SEP} of {_SEP} cases
$
""",
re.IGNORECASE | re.VERBOSE,
)


def study_size_target(name: str) -> str | None:
Expand All @@ -430,6 +442,8 @@ def study_size_target(name: str) -> str | None:
``"study_size"`` when the name matches any of the study-size patterns,
else ``None``.
"""
if STUDY_SIZE_EXEMPT_PATTERN.search(name):
return None
if STUDY_SIZE_EXACT_PATTERN.search(name):
return "study_size"
if STUDY_SIZE_COUNT_PATTERN.search(name):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2105,6 +2105,22 @@ def test_study_size_target_excludes_expanded_near_misses() -> None:
assert study_size_target(n) is None, n


def test_study_size_target_leaves_number_of_cases_alone() -> None:
"""study_size_target never touches the Biolink ``number_of_cases`` slot.

``number_of_cases`` counts cases carrying the phenotype/disease, not the
study population; coercing it to ``study_size`` destroys a legitimate edge
field.
"""
names: list[str] = ["number_of_cases", "Number of Cases", "number-of-cases", "numberofcases"]
for n in names:
assert study_size_target(n) is None, n

lf: pl.LazyFrame = pl.DataFrame({"number_of_cases": [42, 7]}).lazy()
result: pl.DataFrame = coerce_study_size_columns(lf).collect()
assert result.columns == ["number_of_cases"]


def test_coerce_study_size_columns_renames_n_column() -> None:
"""coerce_study_size_columns renames bare N to study_size."""
lf: pl.LazyFrame = pl.DataFrame({"n": [120, 450]}).lazy()
Expand Down
Loading