|
1 | 1 | """Serialization functions for SSSOM.""" |
2 | 2 |
|
| 3 | +from __future__ import annotations |
| 4 | + |
3 | 5 | import json |
4 | 6 | import logging as _logging |
5 | 7 | from contextlib import contextmanager |
6 | 8 | from pathlib import Path |
7 | 9 | from typing import ( |
| 10 | + TYPE_CHECKING, |
8 | 11 | Any, |
9 | 12 | Callable, |
10 | 13 | Collection, |
|
45 | 48 | sort_df_rows_columns, |
46 | 49 | ) |
47 | 50 |
|
| 51 | +if TYPE_CHECKING: |
| 52 | + import rdflib_endpoint |
| 53 | + |
48 | 54 | logging = _logging.getLogger(__name__) |
49 | 55 |
|
50 | 56 | # noinspection PyProtectedMember |
@@ -362,6 +368,45 @@ def to_rdf_graph(msdf: MappingSetDataFrame, *, hydrate: bool = False) -> Graph: |
362 | 368 | return cast(Graph, graph) |
363 | 369 |
|
364 | 370 |
|
| 371 | +EXAMPLE_SPARQL_QUERY = """\ |
| 372 | + PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
| 373 | + PREFIX owl: <http://www.w3.org/2002/07/owl#> |
| 374 | + PREFIX sssom: <https://w3id.org/sssom/> |
| 375 | + PREFIX obo: <http://purl.obolibrary.org/obo/> |
| 376 | + PREFIX semapv: <https://w3id.org/semapv/vocab/> |
| 377 | + PREFIX skos: <http://www.w3.org/2004/02/skos/core#> |
| 378 | + PREFIX pav: <http://purl.org/pav/> |
| 379 | + PREFIX orcid: <https://orcid.org/> |
| 380 | + PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 381 | +
|
| 382 | + SELECT ?s ?p ?o ?justification { |
| 383 | + [] a owl:Axiom ; |
| 384 | + owl:annotatedSource ?s ; |
| 385 | + owl:annotatedProperty ?p ; |
| 386 | + owl:annotatedTarget ?o ; |
| 387 | + sssom:mapping_justification ?justification ; |
| 388 | + } |
| 389 | + LIMIT 50 |
| 390 | +""" |
| 391 | + |
| 392 | + |
| 393 | +def get_rdflib_endpoint_app( |
| 394 | + msdf: MappingSetDataFrame, *, hydrate: bool = True |
| 395 | +) -> rdflib_endpoint.SparqlEndpoint: |
| 396 | + """Get a FastAPI app that serves the mappings from a SPARQL endpoint.""" |
| 397 | + from rdflib_endpoint import SparqlEndpoint |
| 398 | + |
| 399 | + graph = to_rdf_graph(msdf, hydrate=hydrate) |
| 400 | + app = SparqlEndpoint( |
| 401 | + graph=graph, |
| 402 | + cors_enabled=True, |
| 403 | + title=f"SSSOM SPARQL Endpoint for {msdf.metadata['mapping_set_id']}", |
| 404 | + description=msdf.metadata.get("mapping_set_description"), |
| 405 | + example_query=EXAMPLE_SPARQL_QUERY, |
| 406 | + ) |
| 407 | + return app |
| 408 | + |
| 409 | + |
365 | 410 | def to_fhir_json(msdf: MappingSetDataFrame) -> Dict[str, Any]: |
366 | 411 | """Convert a mapping set dataframe to a JSON object. |
367 | 412 |
|
|
0 commit comments