Skip to content

Commit

Permalink
Added format_utilities.py -- see #687
Browse files Browse the repository at this point in the history
  • Loading branch information
cmungall committed Dec 21, 2023
1 parent 420fa1a commit 0f3c1c0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/oaklib/converters/obo_graph_to_rdf_owl_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
HAS_RELATED_SYNONYM,
)
from oaklib.types import CURIE
from oaklib.utilities.format_utilities import RDFLIB_SYNTAX_ALIAS_MAP

TRIPLE = Tuple[rdflib.URIRef, rdflib.URIRef, Any]

Expand Down Expand Up @@ -54,8 +55,9 @@ def dump(self, source: GraphDocument, target: str = None, format="turtle", **kwa
:return:
"""
g = self.convert(source)
if format == "rdfxml":
format = "xml"
# TODO: simplify this, see https://github.com/INCATools/ontology-access-kit/issues/687
if format in RDFLIB_SYNTAX_ALIAS_MAP:
format = RDFLIB_SYNTAX_ALIAS_MAP[format]
if target is None:
print(g.serialize(format=format))
else:
Expand Down
9 changes: 8 additions & 1 deletion src/oaklib/implementations/sqldb/sql_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@
from oaklib.utilities.axioms.logical_definition_utilities import (
logical_definition_matches,
)
from oaklib.utilities.format_utilities import (
OBOGRAPHS_SYNTAX_ALIAS_MAP,
RDFLIB_SYNTAX_ALIAS_MAP,
)
from oaklib.utilities.graph.relationship_walker import walk_down, walk_up
from oaklib.utilities.identifier_utils import (
string_as_base64_curie,
Expand Down Expand Up @@ -1174,10 +1178,13 @@ def dump(self, path: str = None, syntax: str = None, **kwargs):
if syntax is None:
syntax = "ttl"
if syntax in ["ttl", "rdfxml", "owl"]:
if syntax in RDFLIB_SYNTAX_ALIAS_MAP:
syntax = RDFLIB_SYNTAX_ALIAS_MAP[syntax]
g = self.as_rdflib_graph()
logging.info(f"Dumping to {path}")
g.serialize(path, format=syntax)
elif syntax in ["json", "obojson"]:
elif syntax in OBOGRAPHS_SYNTAX_ALIAS_MAP.keys():
syntax = OBOGRAPHS_SYNTAX_ALIAS_MAP[syntax]
g = self.as_obograph(expand_curies=True)
gd = obograph.GraphDocument(graphs=[g])
json_dumper.dump(gd, path)
Expand Down
67 changes: 67 additions & 0 deletions src/oaklib/utilities/format_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
Utilities for working with formats
See https://github.com/INCATools/ontology-access-kit/issues/687
"""
from enum import Enum


class OntologyModel(str, Enum):
"""
Enumerated types for ontology models.
A model is an abstract representation of an ontology that is potentially
serializable in multiple syntaxes.
"""

OWL = "owl"
"""W3C OWL2 Ontology Model"""

SKOS = "skos"
"""W3C SKOS Ontology Model"""

SDO_RDFS = "sdo_rdfs"
"""Schema.org RDFS Ontology Model"""

OBOFORMAT = "oboformat"
"""OBO Format Ontology Model. Always serialized as obo format"""

OBOGRAPHS = "obographs"
"""OBO Graphs Ontology Model"""


MODEL_ALLOWED_SYNTAXES = {
OntologyModel.OWL: [
"ttl",
"rdfxml",
"n3",
"nt",
"pretty-xml",
"trix",
"trig",
"ofn",
"owx",
"omn",
"obo",
],
OntologyModel.SKOS: ["ttl", "rdfxml", "n3", "nt", "pretty-xml", "trix", "trig"],
OntologyModel.OBOFORMAT: ["obo"],
OntologyModel.OBOGRAPHS: ["json", "yaml"],
}


OBOGRAPHS_SYNTAX_ALIAS_MAP = {
"json": "json",
"obojson": "json",
"yaml": "yaml",
"obograph": "json",
"obographjson": "json",
"obographyaml": "yaml",
}


RDFLIB_SYNTAX_ALIAS_MAP = {
"owl": "turtle",
"ttl": "turtle",
"rdfxml": "xml",
}
3 changes: 3 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,9 @@ def test_dump(self):
if conf_path:
cmd.extend(["-c", conf_path])
result = self.runner.invoke(main, cmd)
if result.exit_code != 0:
print("STDOUT", result.stdout)
print("STDERR", result.stderr)
self.assertEqual(0, result.exit_code, f"input={input}, output_format={output_format}")
if output_format == "obojson":
obj: obograph.GraphDocument
Expand Down

0 comments on commit 0f3c1c0

Please sign in to comment.