Skip to content

Commit 8b86082

Browse files
authored
Enforce integer step counts for ancestor enrichment (#48)
1 parent a525ee5 commit 8b86082

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

pandasaurus/query.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,12 @@ def contextual_slim_enrichment(self, context: List[str]) -> pd.DataFrame:
192192

193193
return self.enriched_df
194194

195-
def ancestor_enrichment(self, step_count: str) -> pd.DataFrame:
195+
def ancestor_enrichment(self, step_count: int) -> pd.DataFrame:
196196
"""
197197
Perform ancestor enrichment analysis with a specified number of hops.
198198
199199
Args:
200-
step_count (str): The number of hops to consider when enriching terms.
200+
step_count (int): The number of hops to consider when enriching terms.
201201
202202
Returns:
203203
pd.DataFrame: A DataFrame containing enriched terms and associated information.
@@ -212,6 +212,10 @@ def ancestor_enrichment(self, step_count: str) -> pd.DataFrame:
212212
includes more distant ancestors.
213213
214214
"""
215+
if not isinstance(step_count, int):
216+
raise TypeError("step_count must be an integer")
217+
if step_count < 1:
218+
raise ValueError("step_count must be a positive integer")
215219
source_list = [term.get_iri() for term in self._term_list]
216220
query_string = get_ancestor_enrichment_query(source_list, step_count)
217221
object_list = list(set(uri for res in run_sparql_query(query_string) for uri in res.values()))

pandasaurus/utils/sparql_queries.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ def get_label_query(term_iri_list: List[str]) -> str:
4646
)
4747

4848

49-
def get_ancestor_enrichment_query(seed_list: List[str], step_count) -> str:
49+
def get_ancestor_enrichment_query(seed_list: List[str], step_count: int) -> str:
50+
"""Build a SPARQL query that walks up to `step_count` ancestors for the seeds."""
51+
if not isinstance(step_count, int):
52+
raise TypeError("step_count must be an integer")
53+
if step_count < 1:
54+
raise ValueError("step_count must be a positive integer")
5055
query = "SELECT * WHERE { GRAPH <http://reasoner.renci.org/nonredundant> "
5156
query += f"{{ VALUES ?s {{{' '.join(seed_list)} }} "
5257
query += "?s rdfs:subClassOf ?o0. "

test/test_query.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,32 @@ def test_ancestor_enrichment(mocker):
285285
assert expected_simple_df["o"].reset_index(drop=True).equals(df["o"].reset_index(drop=True))
286286

287287

288+
def test_ancestor_enrichment_requires_integer_step(mocker):
289+
mocker.patch(
290+
"pandasaurus.curie_validator.run_sparql_query",
291+
side_effect=[
292+
iter(get_enrichment_validate_curie_list_result()),
293+
iter(get_enrichment_find_obsolete_terms_data()),
294+
],
295+
)
296+
q = Query(blood_and_immune_test_data)
297+
with pytest.raises(TypeError):
298+
q.ancestor_enrichment("2")
299+
300+
301+
def test_ancestor_enrichment_requires_positive_step(mocker):
302+
mocker.patch(
303+
"pandasaurus.curie_validator.run_sparql_query",
304+
side_effect=[
305+
iter(get_enrichment_validate_curie_list_result()),
306+
iter(get_enrichment_find_obsolete_terms_data()),
307+
],
308+
)
309+
q = Query(blood_and_immune_test_data)
310+
with pytest.raises(ValueError):
311+
q.ancestor_enrichment(0)
312+
313+
288314
def test_synonym_lookup(mocker):
289315
q = Query(["CL:0000084", "CL:0000813", "CL:0000815", "CL:0000900"])
290316

test/utils/test_sparql_queries.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from test.data.query_data import get_blood_and_immune_test_data
22

3+
import pytest
4+
35
from pandasaurus.utils.sparql_queries import (
46
get_ancestor_enrichment_query,
57
get_contextual_enrichment_query,
@@ -96,6 +98,14 @@ def test_get_ancestor_enrichment_query():
9698
assert "o7" not in get_ancestor_enrichment_query(term_iri_list, 7)
9799

98100

101+
def test_get_ancestor_enrichment_query_requires_positive_int():
102+
term_iri_list = ["term1"]
103+
with pytest.raises(TypeError):
104+
get_ancestor_enrichment_query(term_iri_list, "3")
105+
with pytest.raises(ValueError):
106+
get_ancestor_enrichment_query(term_iri_list, 0)
107+
108+
99109
def test_get_synonym_query():
100110
term_iri_list = ["term1", "term2", "term3"]
101111
expected_query = (

0 commit comments

Comments
 (0)