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
8 changes: 6 additions & 2 deletions pandasaurus/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ def contextual_slim_enrichment(self, context: List[str]) -> pd.DataFrame:

return self.enriched_df

def ancestor_enrichment(self, step_count: str) -> pd.DataFrame:
def ancestor_enrichment(self, step_count: int) -> pd.DataFrame:
"""
Perform ancestor enrichment analysis with a specified number of hops.

Args:
step_count (str): The number of hops to consider when enriching terms.
step_count (int): The number of hops to consider when enriching terms.

Returns:
pd.DataFrame: A DataFrame containing enriched terms and associated information.
Expand All @@ -212,6 +212,10 @@ def ancestor_enrichment(self, step_count: str) -> pd.DataFrame:
includes more distant ancestors.

"""
if not isinstance(step_count, int):
raise TypeError("step_count must be an integer")
if step_count < 1:
raise ValueError("step_count must be a positive integer")
source_list = [term.get_iri() for term in self._term_list]
query_string = get_ancestor_enrichment_query(source_list, step_count)
object_list = list(set(uri for res in run_sparql_query(query_string) for uri in res.values()))
Expand Down
7 changes: 6 additions & 1 deletion pandasaurus/utils/sparql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ def get_label_query(term_iri_list: List[str]) -> str:
)


def get_ancestor_enrichment_query(seed_list: List[str], step_count) -> str:
def get_ancestor_enrichment_query(seed_list: List[str], step_count: int) -> str:
"""Build a SPARQL query that walks up to `step_count` ancestors for the seeds."""
if not isinstance(step_count, int):
raise TypeError("step_count must be an integer")
if step_count < 1:
raise ValueError("step_count must be a positive integer")
query = "SELECT * WHERE { GRAPH <http://reasoner.renci.org/nonredundant> "
query += f"{{ VALUES ?s {{{' '.join(seed_list)} }} "
query += "?s rdfs:subClassOf ?o0. "
Expand Down
26 changes: 26 additions & 0 deletions test/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,32 @@ def test_ancestor_enrichment(mocker):
assert expected_simple_df["o"].reset_index(drop=True).equals(df["o"].reset_index(drop=True))


def test_ancestor_enrichment_requires_integer_step(mocker):
mocker.patch(
"pandasaurus.curie_validator.run_sparql_query",
side_effect=[
iter(get_enrichment_validate_curie_list_result()),
iter(get_enrichment_find_obsolete_terms_data()),
],
)
q = Query(blood_and_immune_test_data)
with pytest.raises(TypeError):
q.ancestor_enrichment("2")


def test_ancestor_enrichment_requires_positive_step(mocker):
mocker.patch(
"pandasaurus.curie_validator.run_sparql_query",
side_effect=[
iter(get_enrichment_validate_curie_list_result()),
iter(get_enrichment_find_obsolete_terms_data()),
],
)
q = Query(blood_and_immune_test_data)
with pytest.raises(ValueError):
q.ancestor_enrichment(0)


def test_synonym_lookup(mocker):
q = Query(["CL:0000084", "CL:0000813", "CL:0000815", "CL:0000900"])

Expand Down
10 changes: 10 additions & 0 deletions test/utils/test_sparql_queries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from test.data.query_data import get_blood_and_immune_test_data

import pytest

from pandasaurus.utils.sparql_queries import (
get_ancestor_enrichment_query,
get_contextual_enrichment_query,
Expand Down Expand Up @@ -96,6 +98,14 @@ def test_get_ancestor_enrichment_query():
assert "o7" not in get_ancestor_enrichment_query(term_iri_list, 7)


def test_get_ancestor_enrichment_query_requires_positive_int():
term_iri_list = ["term1"]
with pytest.raises(TypeError):
get_ancestor_enrichment_query(term_iri_list, "3")
with pytest.raises(ValueError):
get_ancestor_enrichment_query(term_iri_list, 0)


def test_get_synonym_query():
term_iri_list = ["term1", "term2", "term3"]
expected_query = (
Expand Down