Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented entities_metadata_statements() for SQLImplementation() #679

Merged
merged 13 commits into from
Nov 16, 2023
28 changes: 28 additions & 0 deletions src/oaklib/implementations/sqldb/sql_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
DEFINITION,
LANGUAGE_TAG,
METADATA_MAP,
METADATA_STATEMENT,
PRED_CURIE,
PREFIX_MAP,
RELATIONSHIP,
Expand Down Expand Up @@ -671,6 +672,33 @@ def entity_metadata_map(self, curie: CURIE, include_all_triples=False) -> METADA
self.add_missing_property_values(curie, m)
return dict(m)

def entities_metadata_statements(
self,
curies: Iterable[CURIE],
predicates: Optional[List[PRED_CURIE]] = None,
include_nested_metadata=False,
**kwargs,
) -> Iterator[METADATA_STATEMENT]:
q = self.session.query(Statements)
if not include_nested_metadata:
subquery = self.session.query(RdfTypeStatement.subject).filter(
RdfTypeStatement.object == "owl:AnnotationProperty"
)
annotation_properties = {row.subject for row in subquery}
annotation_properties = annotation_properties.union(STANDARD_ANNOTATION_PROPERTIES)
q = q.filter(Statements.predicate.in_(tuple(annotation_properties)))
q = q.filter(Statements.subject.in_(curies))
if predicates is not None:
q = q.filter(Statements.predicate.in_(predicates))
for row in q:
if row.value is not None:
v = _python_value(row.value, row.datatype)
elif row.object is not None:
v = row.object
else:
v = None
yield row.subject, row.predicate, v, row.datatype, {}
hrshdhgd marked this conversation as resolved.
Show resolved Hide resolved

def ontologies(self) -> Iterable[CURIE]:
for row in self.session.query(OntologyNode):
yield row.id
Expand Down
6 changes: 5 additions & 1 deletion src/oaklib/interfaces/basic_ontology_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,11 @@ def entity_metadata_map(self, curie: CURIE) -> METADATA_MAP:
raise NotImplementedError

def entities_metadata_statements(
self, curies: Iterable[CURIE], predicates: Optional[List[PRED_CURIE]] = None
self,
curies: Iterable[CURIE],
predicates: Optional[List[PRED_CURIE]] = None,
include_nested_metadata=False,
hrshdhgd marked this conversation as resolved.
Show resolved Hide resolved
**kwargs,
) -> Iterator[METADATA_STATEMENT]:
"""
Retrieve metadata statements (entity annotations) for a collection of entities.
Expand Down
126 changes: 126 additions & 0 deletions tests/test_implementations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
INTRACELLULAR,
INTRACELLULAR_ORGANELLE,
MAMMALIA,
MEMBRANE,
NUCLEAR_ENVELOPE,
NUCLEAR_MEMBRANE,
NUCLEUS,
Expand Down Expand Up @@ -1880,3 +1881,128 @@ def test_annotate_text(self, oi: TextAnnotatorInterface):
test.assertEqual(object_label, ann.object_label)
test.assertEqual(subject_start, ann.subject_start)
test.assertEqual(subject_end, ann.subject_end)

def test_entities_metadata_statements(self, oi: BasicOntologyInterface):
test = self.test

cases = [
(
[MEMBRANE],
[OIO_CREATION_DATE],
[("GO:0016020", "oio:creation_date", "2014-03-06T11:37:54Z", "xsd:string", {})],
),
(
[MEMBRANE],
None,
[
(
"GO:0016020",
"IAO:0000115",
"A lipid bilayer along with all the proteins and protein complexes embedded in it an attached to it.", # noqa:E501
"xsd:string",
{},
),
("GO:0016020", "oio:creation_date", "2014-03-06T11:37:54Z", "xsd:string", {}),
("GO:0016020", "oio:hasAlternativeId", "GO:0098589", "xsd:string", {}),
("GO:0016020", "oio:hasAlternativeId", "GO:0098805", "xsd:string", {}),
(
"GO:0016020",
"oio:hasDbXref",
"Wikipedia:Biological_membrane",
"xsd:string",
{},
),
("GO:0016020", "oio:hasNarrowSynonym", "membrane region", "xsd:string", {}),
("GO:0016020", "oio:hasNarrowSynonym", "region of membrane", "xsd:string", {}),
("GO:0016020", "oio:hasNarrowSynonym", "whole membrane", "xsd:string", {}),
("GO:0016020", "oio:hasOBONamespace", "cellular_component", "xsd:string", {}),
("GO:0016020", "oio:id", "GO:0016020", "xsd:string", {}),
("GO:0016020", "oio:inSubset", "obo:go#goslim_yeast", None, {}),
("GO:0016020", "oio:inSubset", "obo:go#goslim_plant", None, {}),
("GO:0016020", "oio:inSubset", "obo:go#goslim_pir", None, {}),
("GO:0016020", "oio:inSubset", "obo:go#goslim_metagenomics", None, {}),
("GO:0016020", "oio:inSubset", "obo:go#goslim_flybase_ribbon", None, {}),
("GO:0016020", "oio:inSubset", "obo:go#goslim_chembl", None, {}),
("GO:0016020", "oio:inSubset", "obo:go#goslim_candida", None, {}),
("GO:0016020", "oio:inSubset", "obo:go#goslim_aspergillus", None, {}),
("GO:0016020", "rdfs:label", "membrane", "xsd:string", {}),
],
),
(
[MEMBRANE],
["oio:inSubset"],
[
("GO:0016020", "oio:inSubset", "obo:go#goslim_yeast", None, {}),
("GO:0016020", "oio:inSubset", "obo:go#goslim_plant", None, {}),
("GO:0016020", "oio:inSubset", "obo:go#goslim_pir", None, {}),
("GO:0016020", "oio:inSubset", "obo:go#goslim_metagenomics", None, {}),
("GO:0016020", "oio:inSubset", "obo:go#goslim_flybase_ribbon", None, {}),
("GO:0016020", "oio:inSubset", "obo:go#goslim_chembl", None, {}),
("GO:0016020", "oio:inSubset", "obo:go#goslim_candida", None, {}),
("GO:0016020", "oio:inSubset", "obo:go#goslim_aspergillus", None, {}),
],
),
(
[NUCLEUS],
None,
[
(
"GO:0005634",
"IAO:0000115",
"A membrane-bounded organelle of eukaryotic cells in which chromosomes are housed and replicated. In most cells, the nucleus contains all of the cell's chromosomes except the organellar chromosomes, and is the site of RNA synthesis and processing. In some species, or in specialized cell types, RNA metabolism or DNA replication may be absent.", # noqa:E501
"xsd:string",
{},
),
(
"GO:0005634",
"oio:hasDbXref",
"NIF_Subcellular:sao1702920020",
"xsd:string",
{},
),
("GO:0005634", "oio:hasDbXref", "Wikipedia:Cell_nucleus", "xsd:string", {}),
("GO:0005634", "oio:hasExactSynonym", "cell nucleus", "xsd:string", {}),
("GO:0005634", "oio:hasNarrowSynonym", "horsetail nucleus", "xsd:string", {}),
("GO:0005634", "oio:hasOBONamespace", "cellular_component", "xsd:string", {}),
("GO:0005634", "oio:id", "GO:0005634", "xsd:string", {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_yeast", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_plant", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_pir", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_mouse", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_metagenomics", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_generic", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_flybase_ribbon", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_drosophila", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_chembl", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_candida", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_aspergillus", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_agr", None, {}),
("GO:0005634", "rdfs:label", "nucleus", "xsd:string", {}),
],
),
(
[NUCLEUS],
["oio:inSubset"],
[
("GO:0005634", "oio:inSubset", "obo:go#goslim_yeast", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_plant", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_pir", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_mouse", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_metagenomics", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_generic", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_flybase_ribbon", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_drosophila", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_chembl", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_candida", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_aspergillus", None, {}),
("GO:0005634", "oio:inSubset", "obo:go#goslim_agr", None, {}),
],
),
]
for case in cases:
curies, predicates, expected_result = case
results = list(oi.entities_metadata_statements(curies=curies, predicates=predicates))
test.assertCountEqual(results, expected_result)
test.assertCountEqual(results[0], expected_result[0])
for idx, result in enumerate(results):
test.assertEqual(result, expected_result[idx])
3 changes: 3 additions & 0 deletions tests/test_implementations/test_sqldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,3 +899,6 @@ def test_transitive_object_properties(self):

def test_simple_subproperty_of_chains(self):
self.compliance_tester.test_simple_subproperty_of_chains(self.oi)

def test_entities_metadata_statements(self):
hrshdhgd marked this conversation as resolved.
Show resolved Hide resolved
self.compliance_tester.test_entities_metadata_statements(self.oi)