1
|
|
|
""" |
2
|
|
|
An abstraction for the ChEMBL REST API. |
3
|
|
|
Designed to facilitate testing, but also improves static type checking. |
4
|
|
|
""" |
5
|
|
|
from __future__ import annotations |
6
|
|
|
|
7
|
|
|
import abc |
8
|
|
|
from typing import Any, Callable, Iterator, Mapping, Optional, Sequence |
9
|
|
|
|
10
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
11
|
|
|
|
12
|
|
|
from mandos import logger |
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class ChemblFilterQuery(metaclass=abc.ABCMeta): |
16
|
|
|
""" |
17
|
|
|
Wraps the result of calling ``filter`` on a ChEMBL query. |
18
|
|
|
Supports iterating over results (``__iter__`), getting a single item (``__getitem__`), and calling ``only(lst)``. |
|
|
|
|
19
|
|
|
""" |
20
|
|
|
|
21
|
|
|
def only(self, items: Sequence[str]) -> ChemblFilterQuery: |
22
|
|
|
""" |
23
|
|
|
Turns this into a query for a single record. |
24
|
|
|
|
25
|
|
|
Args: |
26
|
|
|
items: |
27
|
|
|
|
28
|
|
|
Returns: |
29
|
|
|
|
30
|
|
|
""" |
31
|
|
|
raise NotImplementedError() |
32
|
|
|
|
33
|
|
|
def __getitem__(self, item: int) -> NestedDotDict: |
34
|
|
|
raise NotImplementedError() |
35
|
|
|
|
36
|
|
|
def __len__(self) -> int: |
37
|
|
|
raise NotImplementedError() |
38
|
|
|
|
39
|
|
|
def __iter__(self) -> Iterator[NestedDotDict]: |
40
|
|
|
raise NotImplementedError() |
41
|
|
|
|
42
|
|
|
@classmethod |
43
|
|
|
def mock(cls, items: Sequence[dict]): |
44
|
|
|
""" |
45
|
|
|
Mocks. |
46
|
|
|
|
47
|
|
|
Args: |
48
|
|
|
items: |
49
|
|
|
|
50
|
|
|
Returns: |
51
|
|
|
|
52
|
|
|
""" |
53
|
|
|
|
54
|
|
|
class F(ChemblFilterQuery): |
|
|
|
|
55
|
|
|
def only(self, _: Sequence[str]) -> ChemblFilterQuery: |
56
|
|
|
return self |
57
|
|
|
|
58
|
|
|
def __getitem__(self, item: int) -> NestedDotDict: |
59
|
|
|
return NestedDotDict(items[item]) |
60
|
|
|
|
61
|
|
|
def __len__(self) -> int: |
62
|
|
|
return len(items) |
63
|
|
|
|
64
|
|
|
def __iter__(self) -> Iterator[NestedDotDict]: |
65
|
|
|
return iter([NestedDotDict(x) for x in items]) |
66
|
|
|
|
67
|
|
|
return F() |
68
|
|
|
|
69
|
|
|
@classmethod |
70
|
|
|
def wrap(cls, query): |
71
|
|
|
""" |
72
|
|
|
Wraps. |
73
|
|
|
|
74
|
|
|
Args: |
75
|
|
|
query: |
76
|
|
|
|
77
|
|
|
Returns: |
78
|
|
|
|
79
|
|
|
""" |
80
|
|
|
|
81
|
|
|
class F(ChemblFilterQuery): |
|
|
|
|
82
|
|
|
def only(self, items: Sequence[str]) -> ChemblFilterQuery: |
83
|
|
|
# TODO technically not returning this |
|
|
|
|
84
|
|
|
return getattr(query, "only")(items) |
85
|
|
|
|
86
|
|
|
def __getitem__(self, item: int) -> NestedDotDict: |
87
|
|
|
return NestedDotDict(query[item]) |
88
|
|
|
|
89
|
|
|
def __len__(self) -> int: |
90
|
|
|
return len(query) |
91
|
|
|
|
92
|
|
|
def __iter__(self) -> Iterator[NestedDotDict]: |
93
|
|
|
return iter([NestedDotDict(x) for x in query]) |
94
|
|
|
|
95
|
|
|
return F() |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
class ChemblEntrypoint: |
99
|
|
|
""" |
100
|
|
|
Wraps just part of a node in the ChEMBL REST API. |
101
|
|
|
Ex ``Chembl.target``. |
102
|
|
|
""" |
103
|
|
|
|
104
|
|
|
def filter(self, **kwargs) -> ChemblFilterQuery: |
|
|
|
|
105
|
|
|
raise NotImplementedError() |
106
|
|
|
|
107
|
|
|
def get(self, arg: str) -> Optional[NestedDotDict]: |
|
|
|
|
108
|
|
|
raise NotImplementedError() |
109
|
|
|
|
110
|
|
|
@classmethod |
111
|
|
|
def mock( |
112
|
|
|
cls, |
|
|
|
|
113
|
|
|
get_items: Mapping[str, dict], |
|
|
|
|
114
|
|
|
filter_items: Optional[Callable[[Mapping[str, Any]], Sequence[dict]]] = None, |
|
|
|
|
115
|
|
|
) -> ChemblEntrypoint: |
116
|
|
|
""" |
117
|
|
|
|
118
|
|
|
Args: |
119
|
|
|
get_items: Map from single arg for calling ``get`` to the item to return |
120
|
|
|
filter_items: Map from kwarg-set for calling ``filter`` to the list of items to return; |
121
|
|
|
If None, returns ``items`` in all cases |
122
|
|
|
|
123
|
|
|
Returns: |
124
|
|
|
|
125
|
|
|
""" |
126
|
|
|
|
127
|
|
|
class X(ChemblEntrypoint): |
|
|
|
|
128
|
|
|
def filter(self, **kwargs) -> ChemblFilterQuery: |
129
|
|
|
if filter_items is None: |
130
|
|
|
return ChemblFilterQuery.mock(list(get_items.values())) |
131
|
|
|
items = filter_items(kwargs) |
132
|
|
|
return ChemblFilterQuery.mock(items) |
133
|
|
|
|
134
|
|
|
def get(self, arg: str) -> Optional[NestedDotDict]: |
135
|
|
|
return NestedDotDict(get_items[arg]) |
136
|
|
|
|
137
|
|
|
return X() |
138
|
|
|
|
139
|
|
|
@classmethod |
140
|
|
|
def wrap(cls, obj) -> ChemblEntrypoint: |
141
|
|
|
""" |
142
|
|
|
|
143
|
|
|
Args: |
144
|
|
|
obj: |
145
|
|
|
|
146
|
|
|
Returns: |
147
|
|
|
|
148
|
|
|
""" |
149
|
|
|
|
150
|
|
|
class X(ChemblEntrypoint): |
|
|
|
|
151
|
|
|
def filter(self, **kwargs) -> ChemblFilterQuery: |
152
|
|
|
query = getattr(obj, "filter")(**kwargs) |
153
|
|
|
return ChemblFilterQuery.wrap(query) |
154
|
|
|
|
155
|
|
|
def get(self, arg: str) -> Optional[NestedDotDict]: |
156
|
|
|
return NestedDotDict(getattr(obj, "get")(arg)) |
157
|
|
|
|
158
|
|
|
return X() |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
class ChemblApi(metaclass=abc.ABCMeta): |
162
|
|
|
""" |
163
|
|
|
Wraps the whole ChEMBL API. |
164
|
|
|
""" |
165
|
|
|
|
166
|
|
|
@property |
167
|
|
|
def activity(self) -> ChemblEntrypoint: |
|
|
|
|
168
|
|
|
return self.__dict__["activity"] |
169
|
|
|
|
170
|
|
|
@property |
171
|
|
|
def assay(self) -> ChemblEntrypoint: |
|
|
|
|
172
|
|
|
return self.__dict__["assay"] |
173
|
|
|
|
174
|
|
|
@property |
175
|
|
|
def atc_class(self) -> ChemblEntrypoint: |
|
|
|
|
176
|
|
|
return self.__dict__["atc_class"] |
177
|
|
|
|
178
|
|
|
@property |
179
|
|
|
def drug(self) -> ChemblEntrypoint: |
|
|
|
|
180
|
|
|
return self.__dict__["drug"] |
181
|
|
|
|
182
|
|
|
@property |
183
|
|
|
def drug_indication(self) -> ChemblEntrypoint: |
|
|
|
|
184
|
|
|
return self.__dict__["drug_indication"] |
185
|
|
|
|
186
|
|
|
@property |
187
|
|
|
def mechanism(self) -> ChemblEntrypoint: |
|
|
|
|
188
|
|
|
return self.__dict__["mechanism"] |
189
|
|
|
|
190
|
|
|
@property |
191
|
|
|
def molecule(self) -> ChemblEntrypoint: |
|
|
|
|
192
|
|
|
return self.__dict__["molecule"] |
193
|
|
|
|
194
|
|
|
@property |
195
|
|
|
def molecule_form(self) -> ChemblEntrypoint: |
|
|
|
|
196
|
|
|
return self.__dict__["molecule_form"] |
197
|
|
|
|
198
|
|
|
@property |
199
|
|
|
def organism(self) -> ChemblEntrypoint: |
|
|
|
|
200
|
|
|
return self.__dict__["mechanism"] |
201
|
|
|
|
202
|
|
|
@property |
203
|
|
|
def go_slim(self) -> ChemblEntrypoint: |
|
|
|
|
204
|
|
|
return self.__dict__["go_slim"] |
205
|
|
|
|
206
|
|
|
@property |
207
|
|
|
def target(self) -> ChemblEntrypoint: |
|
|
|
|
208
|
|
|
return self.__dict__["target"] |
209
|
|
|
|
210
|
|
|
@property |
211
|
|
|
def target_relation(self) -> ChemblEntrypoint: |
|
|
|
|
212
|
|
|
return self.__dict__["target_relation"] |
213
|
|
|
|
214
|
|
|
@property |
215
|
|
|
def protein_class(self) -> ChemblEntrypoint: |
|
|
|
|
216
|
|
|
return self.__dict__["protein_class"] |
217
|
|
|
|
218
|
|
|
@property |
219
|
|
|
def target_component(self) -> ChemblEntrypoint: |
|
|
|
|
220
|
|
|
return self.__dict__["target_component"] |
221
|
|
|
|
222
|
|
|
@property |
223
|
|
|
def target_prediction(self) -> ChemblEntrypoint: |
|
|
|
|
224
|
|
|
return self.__dict__["target_prediction"] |
225
|
|
|
|
226
|
|
|
def __getattribute__(self, item: str) -> ChemblEntrypoint: |
227
|
|
|
raise NotImplementedError() |
228
|
|
|
|
229
|
|
|
@classmethod |
230
|
|
|
def mock(cls, entrypoints: Mapping[str, ChemblEntrypoint]) -> ChemblApi: |
231
|
|
|
""" |
232
|
|
|
Mocks. |
233
|
|
|
|
234
|
|
|
Args: |
235
|
|
|
entrypoints: |
236
|
|
|
|
237
|
|
|
Returns: |
238
|
|
|
|
239
|
|
|
""" |
240
|
|
|
|
241
|
|
|
class X(ChemblApi): |
|
|
|
|
242
|
|
|
def __getattribute__(self, item: str) -> ChemblEntrypoint: |
243
|
|
|
return entrypoints[item] |
244
|
|
|
|
245
|
|
|
return X() |
246
|
|
|
|
247
|
|
|
@classmethod |
248
|
|
|
def wrap(cls, obj) -> ChemblApi: |
249
|
|
|
""" |
250
|
|
|
Wraps. |
251
|
|
|
|
252
|
|
|
Args: |
253
|
|
|
obj: |
254
|
|
|
|
255
|
|
|
Returns: |
256
|
|
|
|
257
|
|
|
""" |
258
|
|
|
|
259
|
|
|
class X(ChemblApi): |
|
|
|
|
260
|
|
|
def __getattribute__(self, item: str) -> ChemblEntrypoint: |
261
|
|
|
return ChemblEntrypoint.wrap(getattr(obj, item)) |
262
|
|
|
|
263
|
|
|
return X() |
264
|
|
|
|
265
|
|
|
|
266
|
|
|
__all__ = [ |
267
|
|
|
"ChemblApi", |
268
|
|
|
"ChemblEntrypoint", |
269
|
|
|
"ChemblFilterQuery", |
270
|
|
|
] |
271
|
|
|
|