Skip to content

Commit

Permalink
test: get_ancestors
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Hegeman committed Nov 19, 2023
1 parent 45c7f34 commit b9d6d5b
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 122 deletions.
237 changes: 115 additions & 122 deletions scripts/compute_mappings/compute_ancestor_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,39 @@
# In[ ]:
import json
from collections import defaultdict
from typing import Dict, List
from typing import Dict, Iterable, Iterator, List

import yaml
from owlready2 import get_ontology # noqa
from owlready2.entity import ThingClass
from owlready2.namespace import Ontology

# In[ ]:

RO__PART_OF = "BFO_0000050" # RO ontology property "part of"


def get_ancestors(onto, class_name): # type: ignore
z = onto.search_one(iri=f"http://purl.obolibrary.org/obo/{class_name}")
def get_ancestors(onto: Ontology, class_name: str) -> Iterator[str]:
print("djh call")
entity: ThingClass = onto.search_one(iri=f"http://purl.obolibrary.org/obo/{class_name}")

def recurse(x): # type: ignore
# print(f"djh: {entity} {type(entity)}")
# return entity
def recurse(x: ThingClass) -> Iterator[str]:
for e in x.is_a:
if hasattr(e, "value") and e.property.name == RO__PART_OF:
val = e.value.name.replace("obo.", "")
z = onto.search_one(iri=f"http://purl.obolibrary.org/obo/{val}")
z = onto.search_one(iri=f"http://purl.obolibrary.org/obo/{e.value.name.replace('obo.', '')}")
yield z.name
yield from recurse(z) # type: ignore
yield from recurse(z)

yield z.name
yield from recurse(z) # type: ignore
yield entity.name
yield from recurse(entity)


# In[ ]:


def create_ancestors_mapping(onto, classes, prefix=None) -> Dict[str, List[str]]:
def create_ancestors_mapping(onto: Ontology, classes: Iterable[ThingClass], prefix: str = None) -> Dict[str, List[str]]:
ancestors: Dict[str, List[str]] = defaultdict(list)
for cls in classes:
class_key = cls.name.replace("_", ":")
Expand All @@ -56,115 +60,104 @@ def create_ancestors_mapping(onto, classes, prefix=None) -> Dict[str, List[str]]
# In[ ]:


# Load owl.info to grab latest ontology sources
owl_info_yml = "cellxgene_schema_cli/cellxgene_schema/ontology_files/owl_info.yml"
with open(owl_info_yml, "r") as owl_info_handle:
owl_info = yaml.safe_load(owl_info_handle)


# In[ ]:


human_latest_key = owl_info["HsapDv"]["latest"]
human_ontology = owl_info["HsapDv"]["urls"][human_latest_key]

human_onto = get_ontology(human_ontology)
human_onto.load()

m_human = create_ancestors_mapping(human_onto, human_onto.classes(), "HsapDv") # type: ignore


# In[ ]:


mouse_latest_key = owl_info["MmusDv"]["latest"]
mouse_ontology = owl_info["MmusDv"]["urls"][mouse_latest_key]

mouse_onto = get_ontology(mouse_ontology)
mouse_onto.load()

m_mouse = create_ancestors_mapping(mouse_onto, mouse_onto.classes(), "MmusDv") # type: ignore


# In[ ]:


uberon_latest_key = owl_info["UBERON"]["latest"]
uberon_ontology = owl_info["UBERON"]["urls"][uberon_latest_key]

tissue_onto = get_ontology(uberon_ontology)
tissue_onto.load()


# In[ ]:


uberon_classes = [
"UBERON_0007236",
"UBERON_0000106",
"UBERON_0014859",
"UBERON_0008264",
"UBERON_0007233",
"UBERON_0000112",
"UBERON_8000003",
"UBERON_0014857",
"UBERON_0009849",
"UBERON_0034920",
"UBERON_0000069",
"UBERON_0000109",
"UBERON_8000001",
"UBERON_0000068",
"UBERON_0018685",
"UBERON_0000107",
"UBERON_0007222",
"UBERON_0000092",
"UBERON_0018378",
"UBERON_0014864",
"UBERON_0004730",
"UBERON_0000111",
"UBERON_0007220",
"UBERON_0014405",
"UBERON_0014862",
"UBERON_8000000",
"UBERON_0000071",
"UBERON_0014860",
"UBERON_0012101",
"UBERON_0000113",
"UBERON_0014858",
"UBERON_0007232",
"UBERON_0000070",
"UBERON_0000110",
"UBERON_8000002",
"UBERON_0014856",
"UBERON_0004728",
"UBERON_0034919",
"UBERON_0000108",
"UBERON_0000066",
"UBERON_0004707",
"UBERON_0000105",
"UBERON_0018241",
"UBERON_0007221",
"UBERON_0014406",
"UBERON_0014863",
"UBERON_0004729",
"UBERON_0014861",
]


# In[ ]:


uc = [tissue_onto.search_one(iri=f"http://purl.obolibrary.org/obo/{c}") for c in uberon_classes]

m_uberon = create_ancestors_mapping(tissue_onto, uc, "UBERON") # type: ignore


# In[ ]:


with open("development_stage_ontology_mapping.json", "w") as f:
d = {}
d.update(m_human)
d.update(m_mouse)
d.update(m_uberon)
json.dump(d, f)
if __name__ == "__main__":
# Load owl.info to grab latest ontology sources
owl_info_yml = "cellxgene_schema_cli/cellxgene_schema/ontology_files/owl_info.yml"
with open(owl_info_yml, "r") as owl_info_handle:
owl_info = yaml.safe_load(owl_info_handle)

# In[ ]:

human_latest_key = owl_info["HsapDv"]["latest"]
human_ontology = owl_info["HsapDv"]["urls"][human_latest_key]

human_onto = get_ontology(human_ontology)
human_onto.load()

m_human = create_ancestors_mapping(human_onto, human_onto.classes(), "HsapDv") # type: ignore

# In[ ]:

mouse_latest_key = owl_info["MmusDv"]["latest"]
mouse_ontology = owl_info["MmusDv"]["urls"][mouse_latest_key]

mouse_onto = get_ontology(mouse_ontology)
mouse_onto.load()

m_mouse = create_ancestors_mapping(mouse_onto, mouse_onto.classes(), "MmusDv") # type: ignore

# In[ ]:

uberon_latest_key = owl_info["UBERON"]["latest"]
uberon_ontology = owl_info["UBERON"]["urls"][uberon_latest_key]

tissue_onto = get_ontology(uberon_ontology)
tissue_onto.load()

# In[ ]:

uberon_classes = [
"UBERON_0007236",
"UBERON_0000106",
"UBERON_0014859",
"UBERON_0008264",
"UBERON_0007233",
"UBERON_0000112",
"UBERON_8000003",
"UBERON_0014857",
"UBERON_0009849",
"UBERON_0034920",
"UBERON_0000069",
"UBERON_0000109",
"UBERON_8000001",
"UBERON_0000068",
"UBERON_0018685",
"UBERON_0000107",
"UBERON_0007222",
"UBERON_0000092",
"UBERON_0018378",
"UBERON_0014864",
"UBERON_0004730",
"UBERON_0000111",
"UBERON_0007220",
"UBERON_0014405",
"UBERON_0014862",
"UBERON_8000000",
"UBERON_0000071",
"UBERON_0014860",
"UBERON_0012101",
"UBERON_0000113",
"UBERON_0014858",
"UBERON_0007232",
"UBERON_0000070",
"UBERON_0000110",
"UBERON_8000002",
"UBERON_0014856",
"UBERON_0004728",
"UBERON_0034919",
"UBERON_0000108",
"UBERON_0000066",
"UBERON_0004707",
"UBERON_0000105",
"UBERON_0018241",
"UBERON_0007221",
"UBERON_0014406",
"UBERON_0014863",
"UBERON_0004729",
"UBERON_0014861",
]

# In[ ]:

uc = [tissue_onto.search_one(iri=f"http://purl.obolibrary.org/obo/{c}") for c in uberon_classes]

m_uberon = create_ancestors_mapping(tissue_onto, uc, "UBERON") # type: ignore

# In[ ]:

with open("development_stage_ontology_mapping.json", "w") as f:
d = {}
d.update(m_human)
d.update(m_mouse)
d.update(m_uberon)
json.dump(d, f)
63 changes: 63 additions & 0 deletions scripts/compute_mappings/tests/test_compute_ancestor_mapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from unittest.mock import Mock

from owlready2 import Ontology

from scripts.compute_mappings.compute_ancestor_mapping import RO__PART_OF, get_ancestors


class TestAncestorMapping:
class FakeThingClass:
def __init__(self, name, is_a=None, value=None, property=None):
self.name = name
self.is_a = is_a or []
self.value = value
self.property = property

#
# graph:
#
# A B
# \ /
# C D
# \ /
# E
#

# @patch("scripts.compute_mappings.compute_ancestor_mapping.Ontology.search_one")
def test_get_ancestors(self):
# Arrange
A = self.FakeThingClass("obo.A_")
B = self.FakeThingClass("obo.B_")
C_A = self.FakeThingClass("C_to_A", value=A, property=self.FakeThingClass(RO__PART_OF))
C_B = self.FakeThingClass("C_to_A", value=B, property=self.FakeThingClass(RO__PART_OF))

# Should be skipped if missing 'value' attr
C_NA = self.FakeThingClass(
"should be skipped", value=self.FakeThingClass("NA"), property=self.FakeThingClass(RO__PART_OF)
)
delattr(C_NA, "value")

# Should be skipped if property.name is not RO__PART_OF
C_NA_2 = self.FakeThingClass(
"should be skipped", value=self.FakeThingClass("NA_2"), property=self.FakeThingClass("not_ro_part_of")
)

C = self.FakeThingClass("C_", is_a=[C_NA, C_NA_2, C_A, C_B])
D = self.FakeThingClass("D_")
E_C = self.FakeThingClass("C_to_A", value=C, property=self.FakeThingClass(RO__PART_OF))
E_D = self.FakeThingClass("C_to_A", value=D, property=self.FakeThingClass(RO__PART_OF))
E = self.FakeThingClass("E_", is_a=[E_C, E_D])

return_values = [D, B, A, C, E]
attrs = {"search_one.side_effect": lambda **x: return_values.pop()}
ontology = Mock(spec=Ontology, **attrs)

# Act
ancestors = get_ancestors(ontology, "E:")

# Assert
assert list(ancestors) == ["E_", "C_", "obo.A_", "obo.B_", "D_"]
print(ontology.call_args_list)

# def test_create_ancestors_mapping(self):
# create_ancestors_mapping()

0 comments on commit b9d6d5b

Please sign in to comment.