Skip to content

Commit

Permalink
#313: improve get_head_join_node transformation to BC format
Browse files Browse the repository at this point in the history
  • Loading branch information
nilskre committed Apr 22, 2024
1 parent ae7ceb5 commit 42d5f4b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
16 changes: 10 additions & 6 deletions biocypher/_ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,18 +569,22 @@ def _get_head_join_node(self, adapter: OntologyAdapter) -> str:
"""

head_join_node = None
head_join_node_label = adapter.get_head_join_node().replace("_", " ")
user_defined_head_join_node_label = adapter.get_head_join_node()
head_join_node_label_in_bc_format = to_lower_sentence_case(
user_defined_head_join_node_label.replace("_", " ")
)

if self._head_ontology._switch_label_and_id:
head_join_node = head_join_node_label
head_join_node = head_join_node_label_in_bc_format
elif not self._head_ontology._switch_label_and_id:
for node, data in self._head_ontology.get_nx_graph().nodes(
for node_id, data in self._head_ontology.get_nx_graph().nodes(
data=True
):
if "label" in data and data["label"] == to_lower_sentence_case(
head_join_node_label
if (
"label" in data
and data["label"] == head_join_node_label_in_bc_format
):
head_join_node = node
head_join_node = node_id
break

if head_join_node not in self._head_ontology.get_nx_graph().nodes:
Expand Down
4 changes: 2 additions & 2 deletions test/ontologies/missing_label.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

owl:ID_0 a owl:Class ;
rdfs:label "Label_Root" ;
rdfs:label "Test_Missing_Label_Root" ;
rdfs:comment "The class of OWL individuals." .

owl:ID_1A a owl:Class ;
rdfs:label "Label_Level1A" ;
rdfs:label "Test_Missing_Label_Level1A" ;
rdfs:subClassOf owl:ID_0 ;
rdfs:comment "Level1A." .

Expand Down
66 changes: 59 additions & 7 deletions test/test_ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ def test_multiple_parents(ontology_file):
def test_missing_label_on_node():
ontology_adapter = OntologyAdapter(
ontology_file="test/ontologies/missing_label.ttl",
root_label="Label_Root",
root_label="Test_Missing_Label_Root",
)
result = ontology_adapter.get_nx_graph()
# Expected hierarchy:
# label root
# ├── label level1a
# test missing label root
# ├── test missing label level1a
# (└── level1B) <- missing label on this node (should not be part of the graph)
expected_edges = [("label level1a", "label root")]
expected_edges = [("test missing label level1a", "test missing label root")]
for edge in expected_edges:
assert edge in result.edges
assert len(result.edges) == len(expected_edges)
Expand Down Expand Up @@ -229,7 +229,59 @@ def test_root_node_not_found():
)


def test_reverse_labels_from_yaml_config():
def test_switch_id_and_label_from_yaml_config():
bc = BioCypher(
head_ontology={
"url": "test/ontologies/reverse_labels.ttl",
"root_node": "Label_Root",
"switch_label_and_id": True,
},
tail_ontologies={
"tail": {
"url": "test/ontologies/missing_label.ttl",
"head_join_node": "Label_Level1A",
"tail_join_node": "Test_Missing_Label_Root",
"switch_label_and_id": True,
}
},
)
expected_not_switched = [
"label level1b",
"label root",
"label level1a",
"test missing label level1a",
]
for node in bc._get_ontology()._nx_graph.nodes:
assert node in expected_not_switched


def test_mixed_switch_id_and_label_from_yaml_config():
bc = BioCypher(
head_ontology={
"url": "test/ontologies/reverse_labels.ttl",
"root_node": "Label_Root",
"switch_label_and_id": True,
},
tail_ontologies={
"tail": {
"url": "test/ontologies/missing_label.ttl",
"head_join_node": "Label_Level1A",
"tail_join_node": "Test_Missing_Label_Root",
"switch_label_and_id": False,
}
},
)
expected_not_switched = [
"label level1b",
"label root",
"label level1a",
"ID_1A",
]
for node in bc._get_ontology()._nx_graph.nodes:
assert node in expected_not_switched


def test_do_not_switch_id_and_label_from_yaml_config():
bc = BioCypher(
head_ontology={
"url": "test/ontologies/reverse_labels.ttl",
Expand All @@ -240,7 +292,7 @@ def test_reverse_labels_from_yaml_config():
"tail": {
"url": "test/ontologies/missing_label.ttl",
"head_join_node": "Label_Level1A",
"tail_join_node": "Label_Root",
"tail_join_node": "Test_Missing_Label_Root",
"switch_label_and_id": False,
}
},
Expand All @@ -260,7 +312,7 @@ def test_head_join_node_not_found():
"tail": {
"url": "test/ontologies/missing_label.ttl",
"head_join_node": "not present",
"tail_join_node": "Label_Root",
"tail_join_node": "Test_Missing_Label_Root",
}
},
)
Expand Down

0 comments on commit 42d5f4b

Please sign in to comment.