diff --git a/src/oaklib/converters/obo_graph_to_rdf_owl_converter.py b/src/oaklib/converters/obo_graph_to_rdf_owl_converter.py index 4abb75907..08e7a5888 100644 --- a/src/oaklib/converters/obo_graph_to_rdf_owl_converter.py +++ b/src/oaklib/converters/obo_graph_to_rdf_owl_converter.py @@ -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] @@ -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: diff --git a/src/oaklib/implementations/sqldb/sql_implementation.py b/src/oaklib/implementations/sqldb/sql_implementation.py index 377f505e0..ff90e2957 100644 --- a/src/oaklib/implementations/sqldb/sql_implementation.py +++ b/src/oaklib/implementations/sqldb/sql_implementation.py @@ -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, @@ -1181,10 +1185,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) diff --git a/src/oaklib/utilities/format_utilities.py b/src/oaklib/utilities/format_utilities.py new file mode 100644 index 000000000..9df7c3007 --- /dev/null +++ b/src/oaklib/utilities/format_utilities.py @@ -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", +} diff --git a/tests/test_cli.py b/tests/test_cli.py index 287d4e0fc..e9413b731 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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