-
Notifications
You must be signed in to change notification settings - Fork 9
/
ontoconvert
executable file
·308 lines (284 loc) · 9.59 KB
/
ontoconvert
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python
"""Converts file format of input ontology and write it to output file(s).
"""
import argparse
import warnings
from rdflib.util import guess_format
from ontopy import get_ontology
from ontopy.utils import annotate_source, rename_iris, copy_annotation
import owlready2 # pylint: disable=wrong-import-order
import owlready2.reasoning # pylint: disable=wrong-import-order
def main(argv: list = None):
"""Main run function.
Parameters:
argv: List of arguments, similar to `sys.argv[1:]`.
Mainly for testing purposes, since it allows one to invoke the tool
manually / through Python.
"""
# pylint: disable=too-many-branches,too-many-statements,invalid-name
# pylint: disable=too-many-locals
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("input", help="IRI/file to OWL source.")
parser.add_argument("output", help="Output file name.")
parser.add_argument(
"--input-format",
"-f",
help=(
"Input format (default is to infer from input). Available "
'formats: "xml" (rdf/xml), "n3", "nt", "trix", "rdfa"'
),
)
parser.add_argument(
"--output-format",
"-F",
help=(
"Output format (default is to infer from output. Available "
'formats: "xml" (rdf/xml), "n3", "turtle", "nt", "pretty-xml", '
'"trix"'
),
)
parser.add_argument(
"--output-dir",
"-d",
default=".",
help=(
"Output directory. If `output` is a relative path, it will be "
"relative to this directory."
),
)
parser.add_argument(
"--overwrite",
"-w",
action="store_true",
help=(
"Whether to remove `output` if it already exists. "
"The default is to append to it."
),
)
parser.add_argument(
"--copy-annotation",
"-c",
action="append",
default=[],
metavar="FROM-->TO",
help=(
"Copy annotation FROM to annotation TO in each class and "
"property in the ontology. FROM and TO may be given as "
"full IRIs or (if they already exists as annotations in the "
"ontology) as entity names. "
"This option be given multiple times."
),
)
parser.add_argument(
"--copy-emmo-annotations",
"-e",
action="store_true",
help=(
"Make a copy of EMMO annotations to plain RDFS for increased "
"interoperability. "
"Alias for: `--copy-annotation="
"http://www.w3.org/2004/02/skos/core#prefLabel"
"-->http://www.w3.org/2000/01/rdf-schema#label "
"--copy-annotation=elucidation"
"-->http://www.w3.org/2000/01/rdf-schema#comment` "
"--copy-annotation=definition"
"-->http://www.w3.org/2000/01/rdf-schema#comment` "
"--copy-annotation=comment"
"-->http://www.w3.org/2000/01/rdf-schema#comment`"
),
)
parser.add_argument(
"--no-catalog",
"-n",
action="store_false",
dest="url_from_catalog",
default=None,
help="Whether to not read catalog file even if it exists.",
)
parser.add_argument(
"--reasoner",
"--infer",
"-i",
nargs="?",
const="HermiT",
choices=["HermiT", "Pellet", "FaCT++"],
metavar="NAME",
help=(
"Add additional relations inferred by the reasoner. Supported "
'reasoners are "HermiT" (default), "Pellet" and "FaCT++".'
),
)
parser.add_argument(
"--no-infer-imported",
"--no-reason-imported",
action="store_true",
help="Do not infer imported ontologies.",
)
parser.add_argument(
"--java-executable",
help="Path to Java executable to use. Default is `java`.",
)
parser.add_argument(
"--java-memory",
help="Maximum memory allocated to Java in MB. Default is 2000.",
)
parser.add_argument(
"--iri",
"-I",
help="IRI of converted ontology.",
)
parser.add_argument(
"--base-iri",
"-b",
help=(
"Base IRI of converted ontology. The default is the base iri of "
"the input ontology."
"\n\nThis argument can be used to workaround the bug in Owlready2 "
"that changes the base IRI of the ontology to always end with a "
"slash."
),
)
parser.add_argument(
"--quiet",
"-q",
action="store_true",
help="Don't print a lot of stuff to stdout during reasoning.",
)
parser.add_argument(
"--recursive",
"-r",
action="store_true",
help=(
"Whether to also convert imported ontologies recursively using "
"rdflib. The output is written to a directory structure matching "
"the input. "
"This option requires Protege catalog files to be present. "
"It cannot be combined with other options like --squash, "
"--inferred, --annotate-source, and --rename-iris."
),
)
parser.add_argument(
"--squash",
"-s",
action="store_true",
help=(
"Whether to also squash imported ontologies into a single output "
"file. Cannot be combined with --recursive."
),
)
parser.add_argument(
"--annotate-source",
"-a",
action="store_true",
help=(
"Whether to annotate all entities with the base IRI of the source "
"ontology using `dcterms:source` relations. This is contextual "
"information that is otherwise lost when ontologies are inferred "
"and/or squashed."
),
)
parser.add_argument(
"--rename-iris",
"-R",
nargs="?",
const="prefLabel",
metavar="ANNOTATION",
help=(
"For all entities that have the given annotation ('prefLabel' "
"by default), change the name of the entity to the value of the "
"annotation.\n"
"For all changed entities, an `equivalentTo` annotation is "
"added, referring to the old name.\n"
"This option is useful to create a copy of an ontology with "
"more human readable IRIs."
),
)
parser.add_argument(
"--catalog-file",
"-C",
nargs="?",
const="catalog-v001.xml",
metavar="FILENAME",
help='Whether to write catalog file. Defaults to "catalog-v001.xml".',
)
parser.add_argument(
"--append-catalog",
"-A",
action="store_true",
help="Whether to append to (possible) existing catalog file.",
)
args = parser.parse_args(args=argv)
# Inferred default input and output file formats
if args.input_format:
input_format = args.input_format
else:
input_format = guess_format(args.input)
if args.output_format:
output_format = args.output_format
else:
output_format = guess_format(args.output)
if not output_format:
output_format = "xml"
# Settings for running Java
if args.java_executable:
owlready2.JAVA_EXE = args.java_executable
if args.java_memory:
owlready2.reasoning.JAVA_MEMORY = int(args.java_memory)
# Annotations to copy with --copy-emmo-annotations
if args.copy_emmo_annotations:
args.copy_annotation.extend(
[
"http://www.w3.org/2004/02/skos/core#prefLabel"
"-->http://www.w3.org/2000/01/rdf-schema#label",
"elucidation-->http://www.w3.org/2000/01/rdf-schema#comment",
"definition-->http://www.w3.org/2000/01/rdf-schema#comment",
"conceptualisation"
"-->http://www.w3.org/2000/01/rdf-schema#comment",
"comment-->http://www.w3.org/2000/01/rdf-schema#comment",
]
)
# Perform conversion
with warnings.catch_warnings(record=True) as warnings_handle:
warnings.simplefilter("always")
onto = get_ontology(args.input).load(
format=input_format,
url_from_catalog=args.url_from_catalog,
)
if args.iri:
onto.iri = args.iri
if args.base_iri:
onto.base_iri = args.base_iri
if args.annotate_source:
annotate_source(onto)
if args.rename_iris:
rename_iris(onto, args.rename_iris)
if args.reasoner:
include_imported = not args.no_infer_imported
verbose = not args.quiet
onto.sync_reasoner(
reasoner=args.reasoner,
include_imported=include_imported,
debug=verbose,
)
for cpy in args.copy_annotation:
src, dst = cpy.split("-->", 1)
copy_annotation(onto, src.strip(), dst.strip())
onto.save(
args.output,
format=output_format,
dir=args.output_dir,
mkdir=True,
overwrite=args.overwrite,
recursive=args.recursive,
squash=args.squash,
write_catalog_file=bool(args.catalog_file),
append_catalog=args.append_catalog,
catalog_file=args.catalog_file,
)
for warning in warnings_handle:
print(
f"\033[93mWARNING\033[0m: [{warning.category.__name__}] "
f"{warning.message}"
)
if __name__ == "__main__":
main()