-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenerate_ontology.py
More file actions
108 lines (92 loc) · 3.15 KB
/
generate_ontology.py
File metadata and controls
108 lines (92 loc) · 3.15 KB
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
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "bioregistry",
# "click",
# "pyobo>=0.12.6",
# "pyyaml",
# ]
# ///
"""Generate ontology artifacts for GAIDeT."""
from pathlib import Path
import bioregistry
import click
import yaml
from pyobo import Annotation, Reference, Term, build_ontology
from pyobo.struct.typedef import (
exact_match,
has_contributor,
has_ontology_root_term,
label,
mapping_has_justification,
)
HERE = Path(__file__).parent.resolve()
TERMS_PATH = HERE.joinpath("_data", "terms.yml")
OWL_PATH = HERE.joinpath("gaidet.owl")
CURIE_PREFIX = "gaidet"
URI_PREFIX = "https://w3id.org/gaidet/" # TODO need PURL scheme
ONTOLOGY_IRI = "https://w3id.org/gaidet/gaidet.owl"
NAME = "Generative Artificial Intelligence Delegation Taxonomy"
DESCRIPTION = "An extension/complement to Contributor Roles Taxonomy and the Contributor Roles Ontology for generative artificial intelligence."
REPOSITORY = "https://github.com/panbibliotekar/gaidet-declaration"
ROOT = Reference(prefix="cro", identifier="0000000", name="contributor role")
def _annotate_mappings(term: Term, record) -> None:
for mapping in record.get("mappings", []):
predicate = Reference.from_curie(mapping["predicate"])
obj = Reference.from_curie(mapping["object"])
term.annotate_object(
predicate,
obj,
annotations=[
Annotation(
mapping_has_justification,
Reference.from_curie(mapping["mapping_justification"]),
),
Annotation(has_contributor, Reference.from_curie(mapping["contributor"])),
],
)
@click.command()
def main() -> None:
"""Generate ontology artifacts for GAIDeT."""
bioregistry.add_resource(
bioregistry.Resource(
prefix=CURIE_PREFIX,
uri_format=URI_PREFIX + "$1",
repository=REPOSITORY,
description=DESCRIPTION,
)
)
records = yaml.safe_load(TERMS_PATH.read_text())
terms = [Term(reference=ROOT)]
for record in records.values():
term = Term(
reference=Reference(prefix=CURIE_PREFIX, identifier=record["id"], name=record["label"])
)
term.annotate_string(label, record["label_uk"], language="uk")
term.append_parent(ROOT)
_annotate_mappings(term, record)
terms.append(term)
for child in record["terms"]:
child_term = Term.from_triple(
prefix=CURIE_PREFIX, identifier=child["id"], name=child["label"]
)
child_term.annotate_string(label, child["label_uk"], language="uk")
child_term.append_parent(term)
_annotate_mappings(child_term, child)
terms.append(child_term)
ontology = build_ontology(
prefix=CURIE_PREFIX,
terms=terms,
typedefs=[
exact_match,
has_ontology_root_term,
mapping_has_justification,
label,
has_contributor,
],
root_terms=[ROOT],
ontology_iri=ONTOLOGY_IRI,
)
ontology.write_owl(OWL_PATH)
if __name__ == "__main__":
main()