bioregistry 0.13.8__py3-none-any.whl → 0.13.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,146 @@
1
+ """Add SWEET ontologies."""
2
+
3
+ from typing import cast
4
+
5
+ import click
6
+ import pystow
7
+
8
+ import bioregistry
9
+
10
+ MODULE = pystow.module("bioregistry", "sweet")
11
+
12
+ ALL_PREFIXES_URL = "https://github.com/ESIPFed/sweet/raw/refs/heads/master/sweetPrefixes.ttl"
13
+
14
+ MANUAL = {
15
+ "sosto": "Acute",
16
+ "sostri": "Catastrophic",
17
+ "sostsp": "Big",
18
+ "sorel": "hasPhenomena",
19
+ "sorelch": "atomicMass",
20
+ "sorelh": "hasAttribute",
21
+ "soreaer": "AbyssopelagicZone",
22
+ "sorelcl": "hasAverageAnnualPrecipitation",
23
+ "sorelm": "averageOver",
24
+ "sorelph": "colderThan",
25
+ "sorelsc": "causedBy",
26
+ "sorelt": "dayOfYear",
27
+ "sorelsp": "adjacentTo",
28
+ "sorepsd": "Counterclockwise",
29
+ "sorelpr": "fillValue",
30
+ "sostss": "Continental",
31
+ "sostrt": "Accurate",
32
+ "sostsl": "CaK",
33
+ "sosttf": "Annual",
34
+ "sosttg": "0MYA",
35
+ "sostv": "Clear",
36
+ }
37
+
38
+
39
+ @click.command()
40
+ def main() -> None:
41
+ """Add SWEET ontologies."""
42
+ graph = MODULE.ensure_rdf(url=ALL_PREFIXES_URL)
43
+ sparql = """
44
+ SELECT ?prefix ?namespace
45
+ WHERE {
46
+ ?x sh:prefix ?prefix;
47
+ sh:namespace ?namespace .
48
+ }
49
+ """
50
+ for sweet_internal_prefix, uri_prefix in graph.query(sparql): # type:ignore
51
+ sweet_internal_prefix = str(sweet_internal_prefix)
52
+ uri_prefix = str(uri_prefix)
53
+
54
+ if sweet_internal_prefix in {"soall", "sweet"}:
55
+ continue # this is the combine one, not its own prefix
56
+
57
+ sweet_internal_key = uri_prefix.removeprefix("http://sweetontology.net/").rstrip("/")
58
+ if not sweet_internal_key:
59
+ raise ValueError(f"no internal key found for {sweet_internal_prefix}")
60
+
61
+ download_rdf = (
62
+ f"https://github.com/ESIPFed/sweet/raw/refs/heads/master/src/{sweet_internal_key}.ttl"
63
+ )
64
+ inner_graph = MODULE.ensure_rdf(url=download_rdf)
65
+
66
+ ontology_name_query = """
67
+ SELECT ?name
68
+ WHERE { owl:Ontology ^rdf:type/rdfs:label ?name }
69
+ LIMIT 1
70
+ """
71
+ name = str(next(iter(inner_graph.query(ontology_name_query)))[0]) # type:ignore
72
+ name_short = name.removeprefix("SWEET Ontology ")
73
+
74
+ example_query = f"""
75
+ SELECT ?term
76
+ WHERE {{
77
+ ?term rdf:type owl:Class;
78
+ rdfs:label ?name ;
79
+ FILTER STRSTARTS(str(?term), "{uri_prefix}")
80
+ }}
81
+ LIMIT 1
82
+ """
83
+ example_records = list(inner_graph.query(example_query))
84
+ if example_records:
85
+ example_uri = cast(str, example_records[0][0]) # type:ignore[index]
86
+ example = example_uri.removeprefix(uri_prefix)
87
+ elif sweet_internal_prefix in MANUAL:
88
+ example = MANUAL[sweet_internal_prefix]
89
+ else:
90
+ raise ValueError(
91
+ f"[{sweet_internal_prefix}] missing example in {name_short} ({uri_prefix})"
92
+ )
93
+
94
+ if not sweet_internal_prefix.startswith("so"):
95
+ raise ValueError
96
+
97
+ nsl = name_short.lower()
98
+ if nsl.startswith("human "):
99
+ keywords = [nsl.removeprefix("human ")]
100
+ elif nsl.startswith("material "):
101
+ keywords = ["materials", nsl.removeprefix("material ")]
102
+ elif nsl.startswith("phenomena "):
103
+ keywords = ["phenomena", nsl.removeprefix("phenomena ")]
104
+ elif nsl.startswith("property relationships "):
105
+ keywords = [nsl.removeprefix("property relationships ")]
106
+ elif nsl.startswith("property "):
107
+ keywords = [nsl.removeprefix("property ")]
108
+ elif nsl.startswith("process "):
109
+ keywords = [nsl.removeprefix("process ")]
110
+ elif nsl.startswith("realm land "):
111
+ keywords = [nsl.removeprefix("realm land") + "land"]
112
+ elif nsl.startswith("realm "):
113
+ keywords = ["realm", nsl.removeprefix("realm ")]
114
+ elif nsl.startswith("representation "):
115
+ keywords = [nsl.removeprefix("realm ")]
116
+ elif nsl.startswith("state "):
117
+ keywords = [nsl.removeprefix("realm ")]
118
+ elif nsl.startswith("relationships "):
119
+ keywords = [nsl.removeprefix("relationships ")]
120
+ else:
121
+ keywords = [nsl.lower()]
122
+
123
+ prefix = f"sweet.{sweet_internal_prefix.removeprefix('so')}"
124
+ resource = bioregistry.Resource(
125
+ prefix=prefix,
126
+ synonyms=[sweet_internal_prefix],
127
+ name=name,
128
+ keywords=sorted(keywords),
129
+ homepage=str(uri_prefix),
130
+ uri_format=f"{uri_prefix}$1",
131
+ description=f"The Semantic Web for Earth and Environmental Terminology (SWEET) ontology for {name_short}",
132
+ example=example,
133
+ download_rdf=download_rdf,
134
+ part_of="sweet",
135
+ license="CC0-1.0",
136
+ repository="https://github.com/ESIPFed/sweet",
137
+ contributor=bioregistry.Author.get_charlie(),
138
+ github_request_issue=1772,
139
+ )
140
+ bioregistry.add_resource(resource)
141
+
142
+ bioregistry.manager.write_registry()
143
+
144
+
145
+ if __name__ == "__main__":
146
+ main()