Skip to content

Commit

Permalink
feat: add is_valid_term_id method to OntologyParser (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayib-jose-gloria authored Mar 15, 2024
1 parent 0465ee7 commit 72c2073
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
16 changes: 16 additions & 0 deletions api/python/src/cellxgene_ontology_guide/ontology_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ def _parse_ontology_name(self, term_id: str) -> str:

return ontology_name

def is_valid_term_id(self, term_id: str) -> bool:
"""
Check if an ontology term ID is valid and defined in a supported ontology. If deprecated but defined
in the ontology, it is considered valid.
:param term_id: str ontology term to check
:return: boolean flag indicating whether the term is supported
"""
try:
ontology_name = self._parse_ontology_name(term_id)
if term_id in self.cxg_schema.ontology(ontology_name):
return True
except ValueError:
return False
return False

def get_term_ancestors(self, term_id: str, include_self: bool = False) -> List[str]:
"""
Get the ancestor ontology terms for a given term. If include_self is True, the term itself will be included as
Expand Down
7 changes: 7 additions & 0 deletions api/python/tests/test_ontology_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ def test_parse_ontology_name__not_supported(ontology_parser):
ontology_parser._parse_ontology_name("GO:0000001")


@pytest.mark.parametrize(
"term_id,expected", [("CL:0000001", True), ("CL:0000003", True), ("CL:0000009", False), ("GO:0000001", False)]
)
def test_is_valid_term_id(ontology_parser, term_id, expected):
assert ontology_parser.is_valid_term_id(term_id) == expected


def test_get_term_ancestors(ontology_parser):
assert ontology_parser.get_term_ancestors("CL:0000004") == ["CL:0000000", "CL:0000001", "CL:0000002"]
assert ontology_parser.get_term_ancestors("CL:0000004", include_self=True) == [
Expand Down

0 comments on commit 72c2073

Please sign in to comment.