Skip to content

Commit

Permalink
Adding utilities to score mondo grounding predictions
Browse files Browse the repository at this point in the history
Fixes #14
  • Loading branch information
cmungall committed May 10, 2024
1 parent 97ac8c6 commit 3ad1e06
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/malco/post_process/mondo_score_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from functools import lru_cache
from typing import List

from oaklib import get_adapter
from oaklib.datamodels.vocabulary import IS_A
from oaklib.interfaces import OboGraphInterface, MappingProviderInterface

FULL_SCORE = 1.0
PARTIAL_SCORE = 0.5


@lru_cache
def mondo_adapter() -> OboGraphInterface:
"""
Get the adapter for the MONDO ontology.
Returns:
Adapter: The adapter.
"""
return get_adapter("sqlite:obo:mondo")


@lru_cache()
def omim_mappings(term: str) -> List[str]:
"""
Get the OMIM mappings for a term.
Example:
>>> omim_mappings("MONDO:0007566")
['OMIM:132800']
Args:
term (str): The term.
Returns:
str: The OMIM mappings.
"""
adapter = mondo_adapter()
if not isinstance(adapter, MappingProviderInterface):
raise ValueError("Adapter is not an MappingProviderInterface")
omims = []
for m in adapter.sssom_mappings([term], "OMIM"):
if m.predicate_id == "skos:exactMatch":
omims.append(m.object_id)
return omims


def score_grounded_result(prediction: str, ground_truth: str) -> float:
"""
Score the grounded result.
Exact match:
>>> score_grounded_result("OMIM:132800", "OMIM:132800")
1.0
The predicted Mondo is equivalent to the ground truth OMIM
(via skos:exactMatches in Mondo):
>>> score_grounded_result("MONDO:0007566", "OMIM:132800")
1.0
The predicted Mondo is a disease entity that groups multiple
OMIMs, one of which is the ground truth:
>>> score_grounded_result("MONDO:0008029", "OMIM:158810")
0.5
Args:
prediction (str): The prediction.
ground_truth (str): The ground truth.
Returns:
float: The score.
"""
if prediction == ground_truth:
# predication is the correct OMIM
return FULL_SCORE
if ground_truth in omim_mappings(prediction):
# prediction is a MONDO that directly maps to a correct OMIM
return FULL_SCORE
mondo = mondo_adapter()
for mondo_descendant in mondo.descendants([prediction], predicates=[IS_A], reflexive=True):
if ground_truth in omim_mappings(mondo_descendant):
# prediction is a MONDO that maps to a correct OMIM via a descendant
return PARTIAL_SCORE
return 0.0


0 comments on commit 3ad1e06

Please sign in to comment.