Skip to content

Commit

Permalink
fix docstring language + enforce unique ontology list in ontology_info
Browse files Browse the repository at this point in the history
  • Loading branch information
nayib-jose-gloria committed Oct 23, 2024
1 parent 323a906 commit 5a2525c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 29 deletions.
5 changes: 3 additions & 2 deletions api/python/src/cellxgene_ontology_guide/supported_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ class CXGSchema:
supported_ontologies: Dict[str, Any]
"""A dictionary of supported ontologies for the schema version."""
imported_ontologies: Dict[str, str]
"""A dictionary of ontologies with terms imported into supported ontologies, mapped to the supported ontology
importing them."""
"""In our supported ontologies, the CxG schema can support terms imported from different ontologies.
This dictionary maps these 'additional ontologies' to their supported ontology name. For example,
for ZFS ontology terms imported into the ZFA ontology, imported_ontologies would be {"ZFS":"ZFA", ...}"""
ontology_file_names: Dict[str, str]
"""A dictionary of ontology names and their corresponding file names."""

Expand Down
31 changes: 4 additions & 27 deletions asset-schemas/ontology_info_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,12 @@
},
"ontologies": {
"type": "object",
"properties": {
"CL": {
"$ref": "#/definitions/ontologyEntry"
},
"EFO": {
"$ref": "#/definitions/ontologyEntry"
},
"HANCESTRO": {
"$ref": "#/definitions/ontologyEntry"
},
"HsapDv": {
"$ref": "#/definitions/ontologyEntry"
},
"MONDO": {
"$ref": "#/definitions/ontologyEntry"
},
"MmusDv": {
"$ref": "#/definitions/ontologyEntry"
},
"NCBITaxon": {
"$ref": "#/definitions/ontologyEntry"
},
"UBERON": {
"$ref": "#/definitions/ontologyEntry"
},
"PATO": {
"patternProperties": {
"^[A-Za-z0-9]+$": {
"$ref": "#/definitions/ontologyEntry"
}
}
},
"additionalProperties": false
}
},
"required": [
Expand Down
19 changes: 19 additions & 0 deletions tools/ontology-builder/src/validate_json_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,31 @@ def verify_json(schema_file_name: str, json_file_name: str, registry: Registry)

try:
validate(instance=data, schema=schema, registry=registry)
# custom logic for ontology_info definition
if "ontology_info" in schema_file_name:
validate_unique_ontologies(data)
except Exception:
logger.exception(f"Error validating {json_file_name} against {schema_file_name}")
return False
return True


def validate_unique_ontologies(data):
""""
Custom validation logic to check that all ontologies (including additional_ontologies) defined in ontology_info
are unique across entries
"""
for schema_version, version_info in data.items():
all_ontologies = []
for ontology, ontology_info in version_info["ontologies"].items():
all_ontologies.append(ontology)
all_ontologies.extend(ontology_info.get("additional_ontologies", []))
if len(all_ontologies) != len(set(all_ontologies)):
logger.error("Ontology entries must be unique across all ontology entries, including "
f"additional_ontologies. Duplicates found in definition for {schema_version}")
raise ValueError


def main(path: str = env.ONTOLOGY_ASSETS_DIR) -> None:
"""
Verify the curated JSON lists match their respective JSON schema in asset-schemas
Expand Down
82 changes: 82 additions & 0 deletions tools/ontology-builder/tests/test_validate_json_schemas.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import gzip
import json
import os

import pytest
from referencing import Resource
Expand Down Expand Up @@ -108,3 +109,84 @@ def test_invalid_schema(self, schema_file_fixture, tmpdir, registry_fixture):

# Assert validation fails due to invalid schema
assert verify_json(schema_file_fixture, str(json_file), registry_fixture) is False


class TestVerifyJsonCustomLogic:
@pytest.fixture
def ontology_info_schema_file_fixture(self, tmpdir):
ontology_info_schema = os.path.join(
os.path.realpath(__file__).rsplit("/", maxsplit=4)[0], "asset-schemas", "ontology_info_schema.json"
)
with open(ontology_info_schema, "r") as f:
schema_data = json.load(f)
schema_file = tmpdir.join("ontology_info_schema.json")
with open(str(schema_file), "w") as f:
json.dump(schema_data, f)
return str(schema_file)

@pytest.fixture
def ontology_info_registry_fixture(self, ontology_info_schema_file_fixture, tmpdir):
return register_schemas(tmpdir)

@pytest.fixture
def ontology_info_json_data(self):
return {
"2.0.0": {
"ontologies": {
"A": {
"version": "v1",
"source": "https://example.org/ontology/download",
"filename": "a.owl",
"additional_ontologies": ["C"],
},
"B": {
"version": "v2",
"source": "https://example.org/ontology/download",
"filename": "b.owl",
"additional_ontologies": ["D"],
},
}
},
"1.0.0": {
"ontologies": {
"A": {
"version": "v1",
"source": "https://example.org/ontology/download",
"filename": "a.owl",
},
"B": {
"version": "v1",
"source": "https://example.org/ontology/download",
"filename": "b.owl",
},
}
},
}

def test_validate_unique_ontologies(
self, ontology_info_json_data, ontology_info_schema_file_fixture, tmpdir, ontology_info_registry_fixture
):
json_file = tmpdir.join("ontology_info.json")
with open(str(json_file), "w") as f:
json.dump(ontology_info_json_data, f)

# Assert validation passes
assert verify_json(ontology_info_schema_file_fixture, str(json_file), ontology_info_registry_fixture) is True

@pytest.mark.parametrize("additional_ontologies", [["C"], ["A"]])
def test_validate_unique_ontologies__invalid(
self,
additional_ontologies,
ontology_info_json_data,
ontology_info_schema_file_fixture,
tmpdir,
ontology_info_registry_fixture,
):
# Create invalid JSON data
ontology_info_json_data["2.0.0"]["ontologies"]["B"]["additional_ontologies"] = additional_ontologies
json_file = tmpdir.join("ontology_info.json")
with open(str(json_file), "w") as f:
json.dump(ontology_info_json_data, f)

# Assert validation fails
assert verify_json(ontology_info_schema_file_fixture, str(json_file), ontology_info_registry_fixture) is False

0 comments on commit 5a2525c

Please sign in to comment.