jsonprofile 0.1.1__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,6 @@
1
+ from jsonprofile import profile, utils, validator
2
+
3
+ __version__ = "v0.1.1"
4
+
5
+
6
+ __all__ = ["profile", "validator", "utils", "cv_term_search"]
@@ -0,0 +1,159 @@
1
+ import logging
2
+
3
+ from ontology_lookup import OntologyLookupService
4
+ from ontology_lookup.models import (
5
+ CurieResolutionResponse,
6
+ IriResolutionResponse,
7
+ SearchTermSummary,
8
+ TermResponse,
9
+ )
10
+
11
+ from jsonprofile.profile.base import CvTerm as ProfileCvTerm
12
+ from jsonprofile.validator.base import (
13
+ CvTermSearch,
14
+ )
15
+
16
+ logger = logging.getLogger(__name__)
17
+
18
+
19
+ class OntologyLookupCvTermSearch(CvTermSearch):
20
+ def __init__(self, db_path: str, *args, **kwargs):
21
+ self.svc = OntologyLookupService(db_path=db_path)
22
+
23
+ def get_iri(self, cv_term: ProfileCvTerm) -> str | None:
24
+ if cv_term.cv_accession.startswith(("http://", "https://")):
25
+ return cv_term.cv_accession
26
+ result: None | IriResolutionResponse = self.svc.find_iri(
27
+ curie=cv_term.cv_accession, ontology=cv_term.cv_label
28
+ )
29
+
30
+ if not result:
31
+ return None
32
+ return result.iri or None
33
+
34
+ def get_curie(self, cv_term: ProfileCvTerm) -> str | None:
35
+ if not cv_term.cv_accession.startswith(("http://", "https://")):
36
+ return cv_term.cv_accession
37
+ result: CurieResolutionResponse = self.svc.find_curie(
38
+ iri=cv_term.cv_accession, ontology=cv_term.cv_label
39
+ )
40
+
41
+ if not result:
42
+ return None
43
+ return result.curie or None
44
+
45
+ def find_cv_term(
46
+ self,
47
+ source: str,
48
+ accession_or_label: str,
49
+ matched_accession: None | str = None,
50
+ allow_synonym_search: bool = False,
51
+ ) -> None | ProfileCvTerm:
52
+ if matched_accession:
53
+ result: TermResponse = self.svc.get_term_by_accession(
54
+ ontology=source, accession=matched_accession
55
+ )
56
+ if result:
57
+ return ProfileCvTerm(
58
+ cv_label=result.ontology, cv_accession=result.curie, name=result.label
59
+ )
60
+ result: TermResponse = self.svc.get_term_by_accession(
61
+ ontology=source, accession=accession_or_label
62
+ )
63
+ if result:
64
+ return ProfileCvTerm(
65
+ cv_label=result.ontology, cv_accession=result.curie, name=result.label
66
+ )
67
+ if allow_synonym_search:
68
+ result: list[SearchTermSummary] = self.svc.search_by_label(
69
+ ontology=source, label_or_synonym=accession_or_label
70
+ )
71
+ if result:
72
+ result = result[0]
73
+ else:
74
+ result: TermResponse = self.svc.get_term_by_exact_label(
75
+ ontology=source, label=accession_or_label
76
+ )
77
+ if result:
78
+ return ProfileCvTerm(
79
+ cv_label=result.ontology, cv_accession=result.curie, name=result.label
80
+ )
81
+ return None
82
+
83
+ def check_cv_term(
84
+ self,
85
+ cv_term: ProfileCvTerm,
86
+ parent_cv_term: None | ProfileCvTerm = None,
87
+ allow_synonym_search: bool = False,
88
+ ) -> tuple[bool, str]:
89
+ ontology = cv_term.cv_label
90
+ if not cv_term.cv_label:
91
+ if ":" in cv_term.cv_accession:
92
+ ontology = cv_term.cv_label.split(":", maxsplit=1)[0]
93
+ else:
94
+ return (
95
+ False,
96
+ f"Invalid ontology for {cv_term.name}",
97
+ )
98
+ if parent_cv_term:
99
+ result = self.svc.search_by_label(
100
+ ontology=ontology,
101
+ label_or_synonym=cv_term.name,
102
+ parent_curie=parent_cv_term.cv_accession,
103
+ search_in_synonyms=allow_synonym_search,
104
+ )
105
+ if result:
106
+ result_item = result[0]
107
+ if result_item.curie == cv_term.cv_accession:
108
+ return (
109
+ True,
110
+ f"{cv_term.cv_accession} is "
111
+ f"child of {parent_cv_term.cv_accession}",
112
+ )
113
+ else:
114
+ return (
115
+ False,
116
+ f"{cv_term.cv_accession} {cv_term.name} does not match.",
117
+ )
118
+ return (
119
+ False,
120
+ f"{cv_term.cv_accession} is not child of {parent_cv_term.cv_accession}",
121
+ )
122
+ result: list[SearchTermSummary] = self.svc.search_by_label(
123
+ ontology=ontology,
124
+ label_or_synonym=cv_term.name,
125
+ search_in_synonyms=allow_synonym_search,
126
+ limit=1,
127
+ )
128
+ if result:
129
+ return (
130
+ True,
131
+ f"{cv_term.cv_accession} is found result with label {result[0].label} ",
132
+ )
133
+ return (
134
+ False,
135
+ f"{cv_term.cv_accession} '{cv_term.name}' "
136
+ f"is not found in ontology {cv_term.cv_label}",
137
+ )
138
+
139
+ def find_cv_term_with_accession(
140
+ self, source: str, accession: str
141
+ ) -> tuple[ProfileCvTerm | None, list[str] | None]:
142
+ ontology = source
143
+ if not ontology:
144
+ if ":" in accession:
145
+ ontology = accession.split(":", maxsplit=1)[0]
146
+ else:
147
+ return None, []
148
+
149
+ result: None | TermResponse = self.svc.get_term_by_accession(
150
+ ontology=ontology, accession=accession
151
+ )
152
+ if result:
153
+ profile_cv_term = ProfileCvTerm(
154
+ cv_label=result.ontology,
155
+ cv_accession=result.curie,
156
+ name=result.label,
157
+ )
158
+ return profile_cv_term, result.synonyms or []
159
+ return None, None