|
1
|
|
|
""" |
|
|
|
|
|
|
2
|
|
|
PubChem data views and processors. |
|
3
|
|
|
""" |
|
4
|
|
|
from __future__ import annotations |
|
5
|
|
|
|
|
6
|
|
|
import abc |
|
7
|
|
|
import re |
|
8
|
|
|
from datetime import date, datetime |
|
9
|
|
|
from typing import Mapping, Optional, Sequence, Union, FrozenSet, Any, Dict |
|
10
|
|
|
from typing import Tuple as Tup |
|
11
|
|
|
from urllib.parse import unquote as url_unescape |
|
12
|
|
|
|
|
13
|
|
|
import orjson |
|
|
|
|
|
|
14
|
|
|
from pocketutils.core.exceptions import MultipleMatchesError |
|
|
|
|
|
|
15
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
|
|
16
|
|
|
from pocketutils.tools.common_tools import CommonTools |
|
|
|
|
|
|
17
|
|
|
from pocketutils.tools.string_tools import StringTools |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
from mandos import logger |
|
20
|
|
|
|
|
21
|
|
|
# noinspection PyProtectedMember |
|
22
|
|
|
from mandos.model.pubchem_support._nav_fns import Filter, Mapx, Flatmap |
|
23
|
|
|
|
|
24
|
|
|
# noinspection PyProtectedMember |
|
25
|
|
|
from mandos.model.pubchem_support._nav_model import FilterFn |
|
26
|
|
|
|
|
27
|
|
|
# noinspection PyProtectedMember |
|
28
|
|
|
from mandos.model.pubchem_support._nav import JsonNavigator |
|
29
|
|
|
from mandos.model.pubchem_support.pubchem_models import ( |
|
30
|
|
|
ComputedProperty, |
|
31
|
|
|
Codes, |
|
32
|
|
|
CoOccurrenceType, |
|
33
|
|
|
ClinicalTrial, |
|
34
|
|
|
GhsCode, |
|
35
|
|
|
AssociatedDisorder, |
|
36
|
|
|
AtcCode, |
|
37
|
|
|
DrugbankInteraction, |
|
38
|
|
|
DrugbankDdi, |
|
39
|
|
|
PubmedEntry, |
|
40
|
|
|
Publication, |
|
41
|
|
|
AssayType, |
|
42
|
|
|
Activity, |
|
43
|
|
|
CoOccurrence, |
|
44
|
|
|
DrugGeneInteraction, |
|
45
|
|
|
ChemicalGeneInteraction, |
|
46
|
|
|
Bioactivity, |
|
47
|
|
|
AcuteEffectEntry, |
|
48
|
|
|
DrugbankTargetType, |
|
49
|
|
|
) |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
class Misc: |
|
|
|
|
|
|
53
|
|
|
empty_frozenset = frozenset([]) |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
class Patterns: |
|
|
|
|
|
|
57
|
|
|
ghs_code = re.compile(r"((?:H\d+)(?:\+H\d+)*)") |
|
58
|
|
|
ghs_code_singles = re.compile(r"(H\d+)") |
|
59
|
|
|
pubchem_compound_url = re.compile(r"^https:\/\/pubchem\.ncbi\.nlm\.nih\.gov\/compound\/(.+)$") |
|
60
|
|
|
atc_codes = re.compile(r"([A-Z])([0-9]{2})?([A-Z])?([A-Z])?([A-Z])?") |
|
61
|
|
|
mesh_codes = re.compile(r"[A-Z]") |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
class PubchemDataView(metaclass=abc.ABCMeta): |
|
|
|
|
|
|
65
|
|
|
"""""" |
|
66
|
|
|
|
|
67
|
|
|
def __init__(self, data: NestedDotDict): |
|
68
|
|
|
self._data = data |
|
69
|
|
|
|
|
70
|
|
|
def to_json(self) -> str: |
|
|
|
|
|
|
71
|
|
|
def default(obj: Any) -> Any: |
|
|
|
|
|
|
72
|
|
|
if isinstance(obj, NestedDotDict): |
|
73
|
|
|
# noinspection PyProtectedMember |
|
74
|
|
|
return dict(obj._x) |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
# noinspection PyProtectedMember |
|
77
|
|
|
data = dict(self._data._x) |
|
|
|
|
|
|
78
|
|
|
encoded = orjson.dumps(data, default=default, option=orjson.OPT_INDENT_2) |
|
79
|
|
|
encoded = encoded.decode(encoding="utf8") |
|
80
|
|
|
encoded = StringTools.retab(encoded, 2) |
|
81
|
|
|
return encoded |
|
82
|
|
|
|
|
83
|
|
|
@property |
|
84
|
|
|
def cid(self) -> int: |
|
|
|
|
|
|
85
|
|
|
if self._data["record.RecordType"] != "CID": |
|
86
|
|
|
raise ValueError( |
|
87
|
|
|
"RecordType for {} is {}".format( |
|
88
|
|
|
self._data["record.RecordNumber"], self._data["record.RecordType"] |
|
89
|
|
|
) |
|
90
|
|
|
) |
|
91
|
|
|
return self._data["record.RecordNumber"] |
|
92
|
|
|
|
|
93
|
|
|
@property |
|
94
|
|
|
def _toc(self) -> JsonNavigator: |
|
95
|
|
|
return self._nav / "Section" % "TOCHeading" |
|
96
|
|
|
|
|
97
|
|
|
@property |
|
98
|
|
|
def _tables(self) -> JsonNavigator: |
|
99
|
|
|
return JsonNavigator.create(self._data) / "external_tables" |
|
100
|
|
|
|
|
101
|
|
|
@property |
|
102
|
|
|
def _links(self) -> JsonNavigator: |
|
103
|
|
|
return JsonNavigator.create(self._data) / "link_sets" |
|
104
|
|
|
|
|
105
|
|
|
@property |
|
106
|
|
|
def _classifications(self) -> JsonNavigator: |
|
107
|
|
|
return self._nav / "classifications" |
|
108
|
|
|
|
|
109
|
|
|
@property |
|
110
|
|
|
def _nav(self) -> JsonNavigator: |
|
111
|
|
|
return JsonNavigator.create(self._data) / "record" |
|
112
|
|
|
|
|
113
|
|
|
@property |
|
114
|
|
|
def _refs(self) -> Mapping[int, str]: |
|
115
|
|
|
return {z["ReferenceNumber"]: z["SourceName"] for z in (self._nav / "Reference").contents} |
|
116
|
|
|
|
|
117
|
|
|
def _has_ref(self, name: str) -> FilterFn: |
|
118
|
|
|
return FilterFn(lambda dot: self._refs.get(dot.get_as("ReferenceNumber", int)) == name) |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
class PubchemMiniDataView(PubchemDataView, metaclass=abc.ABCMeta): |
|
|
|
|
|
|
122
|
|
|
"""""" |
|
123
|
|
|
|
|
124
|
|
|
@property |
|
125
|
|
|
def _whoami(self) -> str: |
|
126
|
|
|
raise NotImplementedError() |
|
127
|
|
|
|
|
128
|
|
|
@property |
|
129
|
|
|
def _mini(self) -> JsonNavigator: |
|
130
|
|
|
return self._toc / self._whoami / "Section" % "TOCHeading" |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
class TitleAndSummary(PubchemDataView): |
|
|
|
|
|
|
134
|
|
|
"""""" |
|
135
|
|
|
|
|
136
|
|
|
@property |
|
137
|
|
|
def safety(self) -> FrozenSet[str]: |
|
|
|
|
|
|
138
|
|
|
return ( |
|
139
|
|
|
self._toc |
|
140
|
|
|
/ "Chemical Safety" |
|
141
|
|
|
/ "Information" |
|
142
|
|
|
/ self._has_ref("PubChem") |
|
143
|
|
|
/ "Value" |
|
144
|
|
|
/ "StringWithMarkup" |
|
145
|
|
|
/ "Markup" |
|
146
|
|
|
>> "Extra" |
|
147
|
|
|
).to_set |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
class RelatedRecords(PubchemMiniDataView): |
|
|
|
|
|
|
151
|
|
|
"""""" |
|
152
|
|
|
|
|
153
|
|
|
@property |
|
154
|
|
|
def _whoami(self) -> str: |
|
155
|
|
|
return "Related Records" |
|
156
|
|
|
|
|
157
|
|
|
@property |
|
158
|
|
|
def parent(self) -> Optional[int]: |
|
|
|
|
|
|
159
|
|
|
parent = ( |
|
160
|
|
|
self._mini |
|
161
|
|
|
/ "Parent Compound" |
|
162
|
|
|
/ "Information" |
|
163
|
|
|
/ "Value" |
|
164
|
|
|
/ "StringWithMarkup" |
|
165
|
|
|
// ["String"] |
|
166
|
|
|
// Flatmap.require_only() |
|
167
|
|
|
) |
|
168
|
|
|
parent = parent / Mapx.extract_group_1(r"CID (\d+) +.*") // Flatmap.request_only() |
|
169
|
|
|
return self.cid if parent.get is None else int(parent.get) |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
class NamesAndIdentifiers(PubchemMiniDataView): |
|
|
|
|
|
|
173
|
|
|
"""""" |
|
174
|
|
|
|
|
175
|
|
|
@property |
|
176
|
|
|
def _whoami(self) -> str: |
|
177
|
|
|
return "Names and Identifiers" |
|
178
|
|
|
|
|
179
|
|
|
@property |
|
180
|
|
|
def inchikey(self) -> str: |
|
|
|
|
|
|
181
|
|
|
return self.descriptor("InChI Key") |
|
182
|
|
|
|
|
183
|
|
|
@property |
|
184
|
|
|
def inchi(self) -> str: |
|
|
|
|
|
|
185
|
|
|
return self.descriptor("InChI") |
|
186
|
|
|
|
|
187
|
|
|
@property |
|
188
|
|
|
def molecular_formula(self) -> str: |
|
|
|
|
|
|
189
|
|
|
return ( |
|
190
|
|
|
self._mini |
|
191
|
|
|
/ "Molecular Formula" |
|
192
|
|
|
/ "Information" |
|
193
|
|
|
/ self._has_ref("PubChem") |
|
194
|
|
|
/ "Value" |
|
195
|
|
|
/ "StringWithMarkup" |
|
196
|
|
|
// ["String"] |
|
197
|
|
|
// Flatmap.require_only() |
|
198
|
|
|
// Flatmap.require_only() |
|
199
|
|
|
).get |
|
200
|
|
|
|
|
201
|
|
|
def descriptor(self, key: str) -> str: |
|
|
|
|
|
|
202
|
|
|
return ( |
|
203
|
|
|
self._mini |
|
204
|
|
|
/ "Computed Descriptors" |
|
205
|
|
|
/ "Section" |
|
206
|
|
|
% "TOCHeading" |
|
207
|
|
|
/ key |
|
208
|
|
|
/ "Information" |
|
209
|
|
|
/ self._has_ref("PubChem") |
|
210
|
|
|
/ "Value" |
|
211
|
|
|
/ "StringWithMarkup" |
|
212
|
|
|
// ["String"] |
|
213
|
|
|
// Flatmap.require_only() |
|
214
|
|
|
// Flatmap.require_only() |
|
215
|
|
|
).get |
|
216
|
|
|
|
|
217
|
|
|
@property |
|
218
|
|
|
def create_date(self) -> date: |
|
|
|
|
|
|
219
|
|
|
return ( |
|
220
|
|
|
self._toc |
|
221
|
|
|
/ "Create Date" |
|
222
|
|
|
/ "Information" |
|
223
|
|
|
/ self._has_ref("PubChem") |
|
224
|
|
|
/ "Value" |
|
225
|
|
|
// ["DateISO8601"] |
|
226
|
|
|
// Flatmap.require_only() |
|
227
|
|
|
/ date.fromisoformat |
|
228
|
|
|
// Flatmap.require_only() |
|
229
|
|
|
).get |
|
230
|
|
|
|
|
231
|
|
|
@property |
|
232
|
|
|
def modify_date(self) -> date: |
|
|
|
|
|
|
233
|
|
|
return ( |
|
234
|
|
|
self._toc |
|
235
|
|
|
/ "Modify Date" |
|
236
|
|
|
/ "Information" |
|
237
|
|
|
/ self._has_ref("PubChem") |
|
238
|
|
|
/ "Value" |
|
239
|
|
|
// ["DateISO8601"] |
|
240
|
|
|
// Flatmap.require_only() |
|
241
|
|
|
/ date.fromisoformat |
|
242
|
|
|
// Flatmap.require_only() |
|
243
|
|
|
).get |
|
244
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
class ChemicalAndPhysicalProperties(PubchemMiniDataView): |
|
|
|
|
|
|
247
|
|
|
"""""" |
|
248
|
|
|
|
|
249
|
|
|
@property |
|
250
|
|
|
def _whoami(self) -> str: |
|
251
|
|
|
return "Chemical and Physical Properties" |
|
252
|
|
|
|
|
253
|
|
|
@property |
|
254
|
|
|
def xlogp3(self) -> str: |
|
|
|
|
|
|
255
|
|
|
return self.single_property("XLogP3").req_is(float) |
|
256
|
|
|
|
|
257
|
|
|
@property |
|
258
|
|
|
def mol_weight(self) -> str: |
|
|
|
|
|
|
259
|
|
|
weight = self.single_property("Molecular Weight") |
|
260
|
|
|
if weight.unit != "g/mol": |
|
261
|
|
|
raise ValueError(f"Expected g/mol for weight; got {weight.unit}") |
|
262
|
|
|
return weight.req_is(float) |
|
263
|
|
|
|
|
264
|
|
|
@property |
|
265
|
|
|
def tpsa(self) -> str: |
|
|
|
|
|
|
266
|
|
|
weight = self.single_property("Topological Polar Surface Area") |
|
267
|
|
|
if weight.unit != "Ų": |
|
268
|
|
|
raise ValueError(f"Expected Ų for weight; got {weight.unit}") |
|
269
|
|
|
return weight.req_is(float) |
|
270
|
|
|
|
|
271
|
|
|
@property |
|
272
|
|
|
def charge(self) -> int: |
|
|
|
|
|
|
273
|
|
|
return self.single_property("Formal Charge", "PubChem").value |
|
274
|
|
|
|
|
275
|
|
|
@property |
|
276
|
|
|
def complexity_rating(self) -> int: |
|
|
|
|
|
|
277
|
|
|
return self.single_property("Complexity", "PubChem").value |
|
278
|
|
|
|
|
279
|
|
|
def single_property(self, key: str, ref: Optional[str] = "PubChem") -> ComputedProperty: |
|
|
|
|
|
|
280
|
|
|
return CommonTools.only( |
|
281
|
|
|
[kvr for kvr in self.computed if kvr.key == key and (ref is None or kvr.ref == ref)] |
|
282
|
|
|
) |
|
283
|
|
|
|
|
284
|
|
|
@property |
|
285
|
|
|
def computed(self) -> FrozenSet[ComputedProperty]: |
|
|
|
|
|
|
286
|
|
|
cid = self.cid |
|
287
|
|
|
props = { |
|
288
|
|
|
dot["TOCHeading"]: dot["Information"] |
|
289
|
|
|
for dot in (self._mini / "Computed Properties" / "Section").get |
|
290
|
|
|
} |
|
291
|
|
|
results: Dict[Tup[str, str], ComputedProperty] = {} |
|
292
|
|
|
for heading, info in props.items(): |
|
293
|
|
|
for dot in info: |
|
294
|
|
|
try: |
|
295
|
|
|
dot = NestedDotDict(dot) |
|
296
|
|
|
kvr = self._extract_kvr(heading, dot) |
|
297
|
|
|
if kvr is not None: |
|
298
|
|
|
if (kvr.key, kvr.ref) in results: |
|
299
|
|
|
raise MultipleMatchesError(f"Multiple matches for {kvr} on {cid}") |
|
300
|
|
|
results[(kvr.key, kvr.ref)] = kvr |
|
301
|
|
|
except (KeyError, ValueError): |
|
302
|
|
|
logger.debug(f"Failed on {dot} for cid {cid}") |
|
303
|
|
|
raise |
|
304
|
|
|
return frozenset(results.values()) |
|
305
|
|
|
|
|
306
|
|
|
def _extract_kvr(self, heading: str, dot: NestedDotDict) -> Optional[ComputedProperty]: |
|
307
|
|
|
if "Value" not in dot or "Reference" not in dot: |
|
308
|
|
|
return None |
|
309
|
|
|
ref = ", ".join(dot["Reference"]) |
|
310
|
|
|
value, unit = self._extract_value_and_unit(dot["Value"]) |
|
311
|
|
|
return ComputedProperty(heading, value, unit, ref) |
|
312
|
|
|
|
|
313
|
|
|
def _extract_value_and_unit( |
|
|
|
|
|
|
314
|
|
|
self, dot: NestedDotDict |
|
|
|
|
|
|
315
|
|
|
) -> Tup[Union[None, int, str, float, bool], str]: |
|
316
|
|
|
value, unit = None, None |
|
317
|
|
|
if "Number" in dot and len(["Number"]) == 1: |
|
318
|
|
|
value = dot["Number"][0] |
|
319
|
|
|
elif "Number" in dot and len(["Number"]) > 1: |
|
320
|
|
|
value = ", ".join([str(s) for s in dot["Number"]]) |
|
321
|
|
|
elif ( |
|
322
|
|
|
"StringWithMarkup" in dot |
|
|
|
|
|
|
323
|
|
|
and len(dot["StringWithMarkup"]) == 1 |
|
|
|
|
|
|
324
|
|
|
and "String" in dot["StringWithMarkup"][0] |
|
|
|
|
|
|
325
|
|
|
): |
|
326
|
|
|
value = dot["StringWithMarkup"][0]["String"] |
|
327
|
|
|
elif ( |
|
328
|
|
|
"StringWithMarkup" in dot |
|
|
|
|
|
|
329
|
|
|
and len(dot["StringWithMarkup"]) > 1 |
|
|
|
|
|
|
330
|
|
|
and all(["String" in swump for swump in dot["StringWithMarkup"]]) |
|
|
|
|
|
|
331
|
|
|
): |
|
332
|
|
|
value = ", ".join([str(s) for s in dot["StringWithMarkup"]]) |
|
333
|
|
|
else: |
|
334
|
|
|
value = None |
|
335
|
|
|
if "Unit" in dot and value is not None: |
|
336
|
|
|
unit = dot["Unit"] |
|
337
|
|
|
if isinstance(value, str): |
|
338
|
|
|
value = value.strip().replace("\n", "").replace("\r", "").strip() |
|
339
|
|
|
return value, unit |
|
340
|
|
|
|
|
341
|
|
|
|
|
342
|
|
|
class DrugAndMedicationInformation(PubchemDataView): |
|
|
|
|
|
|
343
|
|
|
"""""" |
|
344
|
|
|
|
|
345
|
|
|
@property |
|
346
|
|
|
def mini(self) -> JsonNavigator: |
|
|
|
|
|
|
347
|
|
|
return self._toc / "Drug and Medication Information" / "Section" % "TOCHeading" |
|
348
|
|
|
|
|
349
|
|
|
@property |
|
350
|
|
|
def indication_summary_drugbank(self) -> Optional[str]: |
|
|
|
|
|
|
351
|
|
|
return ( |
|
352
|
|
|
self.mini |
|
353
|
|
|
/ "Drug Indication" |
|
354
|
|
|
/ "Information" |
|
355
|
|
|
/ self._has_ref("DrugBank") |
|
356
|
|
|
/ "Value" |
|
357
|
|
|
/ "StringWithMarkup" |
|
358
|
|
|
>> "String" |
|
359
|
|
|
>> Flatmap.join_nonnulls() |
|
360
|
|
|
).get |
|
361
|
|
|
|
|
362
|
|
|
@property |
|
363
|
|
|
def indication_summary_livertox(self) -> Optional[str]: |
|
|
|
|
|
|
364
|
|
|
return ( |
|
365
|
|
|
self.mini |
|
366
|
|
|
/ "LiverTox Summary" |
|
367
|
|
|
/ "Information" |
|
368
|
|
|
/ self._has_ref("LiverTox") |
|
369
|
|
|
/ "Value" |
|
370
|
|
|
/ "StringWithMarkup" |
|
371
|
|
|
>> "String" |
|
372
|
|
|
>> Flatmap.join_nonnulls() |
|
373
|
|
|
).get |
|
374
|
|
|
|
|
375
|
|
|
@property |
|
376
|
|
|
def livertox_classes(self) -> FrozenSet[str]: |
|
|
|
|
|
|
377
|
|
|
return ( |
|
378
|
|
|
self.mini |
|
379
|
|
|
/ "Drug Classes" |
|
380
|
|
|
/ "Information" |
|
381
|
|
|
/ self._has_ref("LiverTox") |
|
382
|
|
|
/ "Value" |
|
383
|
|
|
/ "StringWithMarkup" |
|
384
|
|
|
>> "String" |
|
385
|
|
|
).to_set |
|
386
|
|
|
|
|
387
|
|
|
@property |
|
388
|
|
|
def dea_class(self) -> FrozenSet[str]: |
|
|
|
|
|
|
389
|
|
|
return ( |
|
390
|
|
|
self.mini |
|
391
|
|
|
/ "DEA Drug Facts" |
|
392
|
|
|
/ "Information" |
|
393
|
|
|
/ self._has_ref("Drug Enforcement Administration (DEA)") |
|
394
|
|
|
/ "Value" |
|
395
|
|
|
/ "StringWithMarkup" |
|
396
|
|
|
>> "String" |
|
397
|
|
|
).to_set |
|
398
|
|
|
|
|
399
|
|
|
@property |
|
400
|
|
|
def dea_schedule(self) -> Optional[Codes.DeaSchedule]: |
|
|
|
|
|
|
401
|
|
|
return ( |
|
402
|
|
|
self.mini |
|
403
|
|
|
/ "DEA Controlled Substances" |
|
404
|
|
|
/ "Information" |
|
405
|
|
|
/ self._has_ref("Drug Enforcement Administration (DEA)") |
|
406
|
|
|
/ "Value" |
|
407
|
|
|
/ "StringWithMarkup" |
|
408
|
|
|
// ["String"] |
|
409
|
|
|
// Flatmap.require_only() |
|
410
|
|
|
/ Mapx.extract_group_1(r" *Schedule ([IV]+).*") |
|
411
|
|
|
/ Codes.DeaSchedule |
|
412
|
|
|
// Flatmap.request_only() |
|
413
|
|
|
).get |
|
414
|
|
|
|
|
415
|
|
|
@property |
|
416
|
|
|
def hsdb_uses(self) -> FrozenSet[str]: |
|
|
|
|
|
|
417
|
|
|
mesh = "National Library of Medicine's Medical Subject Headings" |
|
418
|
|
|
return ( |
|
419
|
|
|
self.mini |
|
420
|
|
|
/ "Therapeutic Uses" |
|
421
|
|
|
/ "Information" |
|
422
|
|
|
/ self._has_ref("Hazardous Substances Data Bank (HSDB)") |
|
423
|
|
|
/ FilterFn(lambda dot: dot.req_as("Reference", str).startswith(mesh)) |
|
424
|
|
|
/ "Value" |
|
425
|
|
|
/ "StringWithMarkup" |
|
426
|
|
|
>> "String" |
|
427
|
|
|
).to_set |
|
428
|
|
|
|
|
429
|
|
|
@property |
|
430
|
|
|
def clinical_trials(self) -> FrozenSet[ClinicalTrial]: |
|
|
|
|
|
|
431
|
|
|
trials = (self._tables / "clinicaltrials").get |
|
432
|
|
|
objs = [] |
|
433
|
|
|
for trial in trials: |
|
434
|
|
|
source = self._refs[int(trial["srcid"])] |
|
435
|
|
|
obj = ClinicalTrial( |
|
436
|
|
|
Codes.ClinicaltrialId.of(trial["ctid"]), |
|
437
|
|
|
trial["title"], |
|
438
|
|
|
frozenset([Codes.GenericDiseaseCode.of(z) for z in trial["diseaseids"].split("|")]), |
|
439
|
|
|
frozenset(trial["conditions"].split("|")), |
|
440
|
|
|
trial["phase"], |
|
441
|
|
|
trial["status"], |
|
442
|
|
|
frozenset(trial["interventions"].split("|")), |
|
443
|
|
|
frozenset([Codes.PubchemCompoundId.of(z) for z in trial["cids"].split("|")]), |
|
444
|
|
|
source, |
|
445
|
|
|
) |
|
446
|
|
|
objs.append(obj) |
|
447
|
|
|
return frozenset(objs) |
|
448
|
|
|
|
|
449
|
|
|
|
|
450
|
|
|
class PharmacologyAndBiochemistry(PubchemMiniDataView): |
|
|
|
|
|
|
451
|
|
|
"""""" |
|
452
|
|
|
|
|
453
|
|
|
@property |
|
454
|
|
|
def _whoami(self) -> str: |
|
455
|
|
|
return "Pharmacology and Biochemistry" |
|
456
|
|
|
|
|
457
|
|
|
@property |
|
458
|
|
|
def summary_drugbank_text(self) -> Optional[str]: |
|
|
|
|
|
|
459
|
|
|
return ( |
|
460
|
|
|
self._mini |
|
461
|
|
|
/ "Pharmacology" |
|
462
|
|
|
/ "Information" |
|
463
|
|
|
/ self._has_ref("DrugBank") |
|
464
|
|
|
/ "Value" |
|
465
|
|
|
/ "StringWithMarkup" |
|
466
|
|
|
>> "String" |
|
467
|
|
|
>> Flatmap.join_nonnulls() |
|
468
|
|
|
).get |
|
469
|
|
|
|
|
470
|
|
|
@property |
|
471
|
|
|
def summary_ncit_text(self) -> Optional[str]: |
|
|
|
|
|
|
472
|
|
|
return ( |
|
473
|
|
|
self._mini |
|
474
|
|
|
/ "Pharmacology" |
|
475
|
|
|
/ "Information" |
|
476
|
|
|
/ self._has_ref("NCI Thesaurus (NCIt)") |
|
477
|
|
|
/ Filter.key_equals("Name", "Pharmacology") |
|
478
|
|
|
/ "Value" |
|
479
|
|
|
/ "StringWithMarkup" |
|
480
|
|
|
>> "String" |
|
481
|
|
|
>> Flatmap.join_nonnulls() |
|
482
|
|
|
).get |
|
483
|
|
|
|
|
484
|
|
|
@property |
|
485
|
|
|
def summary_ncit_links(self) -> FrozenSet[str]: |
|
|
|
|
|
|
486
|
|
|
return ( |
|
487
|
|
|
self._mini |
|
488
|
|
|
/ "Pharmacology" |
|
489
|
|
|
/ "Information" |
|
490
|
|
|
/ self._has_ref("NCI Thesaurus (NCIt)") |
|
491
|
|
|
/ Filter.key_equals("Name", "Pharmacology") |
|
492
|
|
|
/ "Value" |
|
493
|
|
|
/ "StringWithMarkup" |
|
494
|
|
|
/ "Markup" |
|
495
|
|
|
/ Filter.key_equals("Type", "PubChem Internal Link") |
|
496
|
|
|
// ["URL"] |
|
497
|
|
|
// Flatmap.require_only() |
|
498
|
|
|
/ Mapx.extract_group_1(Patterns.pubchem_compound_url) |
|
499
|
|
|
/ url_unescape |
|
500
|
|
|
/ Mapx.lowercase_unless_acronym() # TODO necessary but unfortunate -- cocaine and Cocaine |
|
|
|
|
|
|
501
|
|
|
).to_set |
|
502
|
|
|
|
|
503
|
|
|
@property |
|
504
|
|
|
def mesh(self) -> FrozenSet[str]: |
|
|
|
|
|
|
505
|
|
|
return ( |
|
506
|
|
|
self._mini |
|
507
|
|
|
/ "MeSH Pharmacological Classification" |
|
508
|
|
|
/ "Information" |
|
509
|
|
|
/ self._has_ref("MeSH") |
|
510
|
|
|
>> "Name" |
|
511
|
|
|
).to_set |
|
512
|
|
|
|
|
513
|
|
|
@property |
|
514
|
|
|
def atc(self) -> FrozenSet[AtcCode]: |
|
|
|
|
|
|
515
|
|
|
strs = ( |
|
516
|
|
|
self._mini |
|
517
|
|
|
/ "ATC Code" |
|
518
|
|
|
/ "Information" |
|
519
|
|
|
/ self._has_ref("WHO Anatomical Therapeutic Chemical (ATC) Classification") |
|
520
|
|
|
/ Filter.key_equals("Name", "ATC Code") |
|
521
|
|
|
/ "Value" |
|
522
|
|
|
/ "StringWithMarkup" |
|
523
|
|
|
>> "String" |
|
524
|
|
|
).to_set |
|
525
|
|
|
return frozenset( |
|
526
|
|
|
[AtcCode(s.split(" - ")[0].strip(), s.split(" - ")[1].strip()) for s in strs] |
|
527
|
|
|
) |
|
528
|
|
|
|
|
529
|
|
|
@property |
|
530
|
|
|
def moa_summary_drugbank_links(self) -> FrozenSet[str]: |
|
|
|
|
|
|
531
|
|
|
return self._get_moa_links("DrugBank") |
|
532
|
|
|
|
|
533
|
|
|
@property |
|
534
|
|
|
def moa_summary_drugbank_text(self) -> Optional[str]: |
|
|
|
|
|
|
535
|
|
|
return self._get_moa_text("DrugBank") |
|
536
|
|
|
|
|
537
|
|
|
@property |
|
538
|
|
|
def moa_summary_hsdb_links(self) -> FrozenSet[str]: |
|
|
|
|
|
|
539
|
|
|
return self._get_moa_links("Hazardous Substances Data Bank (HSDB)") |
|
540
|
|
|
|
|
541
|
|
|
@property |
|
542
|
|
|
def moa_summary_hsdb_text(self) -> Optional[str]: |
|
|
|
|
|
|
543
|
|
|
return self._get_moa_text("Hazardous Substances Data Bank (HSDB)") |
|
544
|
|
|
|
|
545
|
|
|
def _get_moa_text(self, ref: str) -> Optional[str]: |
|
546
|
|
|
return ( |
|
547
|
|
|
self._mini |
|
548
|
|
|
/ "Mechanism of Action" |
|
549
|
|
|
/ "Information" |
|
550
|
|
|
/ self._has_ref(ref) |
|
551
|
|
|
/ "Value" |
|
552
|
|
|
/ "StringWithMarkup" |
|
553
|
|
|
>> "String" |
|
554
|
|
|
>> Flatmap.join_nonnulls(sep=" /// ") |
|
555
|
|
|
).get |
|
556
|
|
|
|
|
557
|
|
|
def _get_moa_links(self, ref: str) -> FrozenSet[str]: |
|
558
|
|
|
return ( |
|
559
|
|
|
self._mini |
|
560
|
|
|
/ "Mechanism of Action" |
|
561
|
|
|
/ "Information" |
|
562
|
|
|
/ self._has_ref(ref) |
|
563
|
|
|
/ "Value" |
|
564
|
|
|
/ "StringWithMarkup" |
|
565
|
|
|
/ "Markup" |
|
566
|
|
|
/ Filter.key_equals("Type", "PubChem Internal Link") |
|
567
|
|
|
// ["URL"] |
|
568
|
|
|
// Flatmap.require_only() |
|
569
|
|
|
/ Mapx.extract_group_1(Patterns.pubchem_compound_url) |
|
570
|
|
|
/ url_unescape |
|
571
|
|
|
/ Mapx.lowercase_unless_acronym() # TODO necessary but unfortunate -- cocaine and Cocaine |
|
|
|
|
|
|
572
|
|
|
).to_set |
|
573
|
|
|
|
|
574
|
|
|
@property |
|
575
|
|
|
def biochem_reactions(self) -> FrozenSet[str]: |
|
|
|
|
|
|
576
|
|
|
# TODO from multiple sources |
|
|
|
|
|
|
577
|
|
|
return frozenset({s.strip() for s in (self._tables / "pathwayreaction" >> "name").to_set}) |
|
578
|
|
|
|
|
579
|
|
|
|
|
580
|
|
|
class SafetyAndHazards(PubchemMiniDataView): |
|
|
|
|
|
|
581
|
|
|
"""""" |
|
582
|
|
|
|
|
583
|
|
|
@property |
|
584
|
|
|
def _whoami(self) -> str: |
|
585
|
|
|
return "Safety and Hazards" |
|
586
|
|
|
|
|
587
|
|
|
@property |
|
588
|
|
|
def ghs_codes(self) -> FrozenSet[GhsCode]: |
|
|
|
|
|
|
589
|
|
|
codes = ( |
|
590
|
|
|
self._mini |
|
591
|
|
|
/ "Hazards Identification" |
|
592
|
|
|
/ "Section" |
|
593
|
|
|
% "TOCHeading" |
|
594
|
|
|
/ "GHS Classification" |
|
595
|
|
|
/ "Information" |
|
596
|
|
|
/ self._has_ref("European Chemicals Agency (ECHA)") |
|
597
|
|
|
/ Filter.key_equals("Name", "GHS Hazard Statements") |
|
598
|
|
|
/ "Value" |
|
599
|
|
|
/ "StringWithMarkup" |
|
600
|
|
|
// ["String"] |
|
601
|
|
|
// Flatmap.require_only() |
|
602
|
|
|
/ Mapx.extract_group_1(r"^(H\d+)[ :].*$") |
|
603
|
|
|
// Mapx.split_and_flatten_nonnulls("+") # TODO: how is this being used to flatten? |
|
|
|
|
|
|
604
|
|
|
).get |
|
605
|
|
|
return frozenset([GhsCode.find(code) for code in codes]) |
|
606
|
|
|
|
|
607
|
|
|
|
|
608
|
|
|
class Toxicity(PubchemMiniDataView): |
|
|
|
|
|
|
609
|
|
|
"""""" |
|
610
|
|
|
|
|
611
|
|
|
@property |
|
612
|
|
|
def _whoami(self) -> str: |
|
613
|
|
|
return "Toxicity" |
|
614
|
|
|
|
|
615
|
|
|
@property |
|
616
|
|
|
def acute_effects(self) -> FrozenSet[AcuteEffectEntry]: |
|
|
|
|
|
|
617
|
|
|
return ( |
|
618
|
|
|
self._tables |
|
619
|
|
|
/ "chemidplus" |
|
620
|
|
|
// ["gid", "effect", "organism", "testtype", "route", "dose"] |
|
621
|
|
|
/ FilterFn(lambda dot: dot.get_as("effect", str) is not None) |
|
622
|
|
|
/ [ |
|
623
|
|
|
int, |
|
624
|
|
|
Mapx.split_to(Codes.ChemIdPlusEffect.of, ";"), |
|
625
|
|
|
Codes.ChemIdPlusOrganism.of, |
|
626
|
|
|
str, |
|
627
|
|
|
str, |
|
628
|
|
|
str, |
|
629
|
|
|
] |
|
630
|
|
|
// Flatmap.construct(AcuteEffectEntry) |
|
631
|
|
|
).to_set |
|
632
|
|
|
|
|
633
|
|
|
|
|
634
|
|
|
class AssociatedDisordersAndDiseases(PubchemMiniDataView): |
|
|
|
|
|
|
635
|
|
|
"""""" |
|
636
|
|
|
|
|
637
|
|
|
@property |
|
638
|
|
|
def _whoami(self) -> str: |
|
639
|
|
|
return "Associated Disorders and Diseases" |
|
640
|
|
|
|
|
641
|
|
|
@property |
|
642
|
|
|
def associated_disorders_and_diseases(self) -> FrozenSet[AssociatedDisorder]: |
|
|
|
|
|
|
643
|
|
|
return ( |
|
644
|
|
|
self._tables |
|
645
|
|
|
/ "ctd_chemical_disease" |
|
646
|
|
|
// ["gid", "diseaseextid", "diseasename", "directevidence", "dois"] |
|
647
|
|
|
/ [str, Codes.MeshCode.of, Mapx.req_is(str), Mapx.req_is(str), Mapx.n_bar_items()] |
|
648
|
|
|
// Flatmap.construct(AssociatedDisorder) |
|
649
|
|
|
).to_set |
|
650
|
|
|
|
|
651
|
|
|
|
|
652
|
|
|
class Literature(PubchemMiniDataView): |
|
|
|
|
|
|
653
|
|
|
"""""" |
|
654
|
|
|
|
|
655
|
|
|
@property |
|
656
|
|
|
def _whoami(self) -> str: |
|
657
|
|
|
return "Literature" |
|
658
|
|
|
|
|
659
|
|
|
@property |
|
660
|
|
|
def depositor_pubmed_articles(self) -> FrozenSet[PubmedEntry]: |
|
|
|
|
|
|
661
|
|
|
def split_mesh_headings(s: str) -> FrozenSet[Codes.MeshHeading]: |
|
|
|
|
|
|
662
|
|
|
# this is a nightmare |
|
663
|
|
|
# these fields are comma-delimited strings, but there are commas within each |
|
664
|
|
|
# all of the examples I've seen with this are for chem name cis/trans |
|
665
|
|
|
# we can fix those |
|
666
|
|
|
# however, there may be some left incorrectly split |
|
667
|
|
|
# and it's possible that we join some when we shouldn't |
|
668
|
|
|
# ex: 6-Cyano-7-nitroquinoxaline-2,3-dione,Animals,Anticonvulsants,Cocaine,Death, [...] |
|
669
|
|
|
# ex: 2,3,4,5-Tetrahydro-7,8-dihydroxy-1-phenyl-1H-3-benzazepine,Animals,Benzazepines, [...] |
|
|
|
|
|
|
670
|
|
|
if s is None: |
|
671
|
|
|
return Misc.empty_frozenset |
|
672
|
|
|
bits = [] |
|
673
|
|
|
current_bit = " " |
|
674
|
|
|
for bit in s.split(","): |
|
675
|
|
|
if current_bit[-1].isdigit() and bit[0].isdigit(): |
|
676
|
|
|
current_bit += bit |
|
677
|
|
|
else: |
|
678
|
|
|
bits.append(current_bit.strip()) |
|
679
|
|
|
current_bit = bit |
|
680
|
|
|
# get the one at the end |
|
681
|
|
|
bits.append(current_bit) |
|
682
|
|
|
return frozenset({b.strip() for b in bits if b.strip() != ""}) |
|
683
|
|
|
|
|
684
|
|
|
def split_mesh_subheadings(s: Optional[str]) -> FrozenSet[Codes.MeshSubheading]: |
|
|
|
|
|
|
685
|
|
|
if s is None: |
|
686
|
|
|
return Misc.empty_frozenset |
|
687
|
|
|
return frozenset({k.strip() for k in s.split(",") if k.strip() != ""}) |
|
688
|
|
|
|
|
689
|
|
|
def split_mesh_codes(s: Optional[str]) -> FrozenSet[Codes.MeshCode]: |
|
|
|
|
|
|
690
|
|
|
if s is None: |
|
691
|
|
|
return Misc.empty_frozenset |
|
692
|
|
|
z = [bit.split(" ")[0] for bit in s.split(",")] |
|
|
|
|
|
|
693
|
|
|
return frozenset({b.strip() for b in z if b.strip() != ""}) |
|
694
|
|
|
|
|
695
|
|
|
def split_sources(s: Optional[str]) -> FrozenSet[str]: |
|
|
|
|
|
|
696
|
|
|
return frozenset(s.split(",")) |
|
697
|
|
|
|
|
698
|
|
|
def split_cids(s: Optional[str]) -> FrozenSet[int]: |
|
|
|
|
|
|
699
|
|
|
if s is None: |
|
700
|
|
|
return Misc.empty_frozenset |
|
701
|
|
|
return frozenset([int(q) for q in s.split(",")]) |
|
702
|
|
|
|
|
703
|
|
|
def get_text(s: Optional[str]) -> Optional[str]: |
|
|
|
|
|
|
704
|
|
|
if s is None: |
|
705
|
|
|
return None |
|
706
|
|
|
return StringTools.strip_brackets_and_quotes(s.strip()).strip() |
|
707
|
|
|
|
|
708
|
|
|
keys = { |
|
709
|
|
|
"pmid": Codes.PubmedId.of, |
|
710
|
|
|
"articletype": Mapx.req_is(str), |
|
711
|
|
|
"pmidsrcs": split_sources, |
|
712
|
|
|
"meshheadings": split_mesh_headings, |
|
713
|
|
|
"meshsubheadings": split_mesh_subheadings, |
|
714
|
|
|
"meshcodes": split_mesh_codes, |
|
715
|
|
|
"cids": split_cids, |
|
716
|
|
|
"articletitle": get_text, |
|
717
|
|
|
"articleabstract": get_text, |
|
718
|
|
|
"articlejourname": get_text, |
|
719
|
|
|
"articlepubdate": Mapx.int_date(), |
|
720
|
|
|
} |
|
721
|
|
|
entries = ( |
|
722
|
|
|
self._tables |
|
723
|
|
|
/ "pubmed" |
|
724
|
|
|
// list(keys.keys()) |
|
725
|
|
|
/ list(keys.values()) |
|
726
|
|
|
// Flatmap.construct(PubmedEntry) |
|
727
|
|
|
).to_set |
|
728
|
|
|
return entries |
|
729
|
|
|
|
|
730
|
|
|
@property |
|
731
|
|
|
def chemical_cooccurrences(self) -> FrozenSet[CoOccurrence]: |
|
|
|
|
|
|
732
|
|
|
return self._get_cooccurrences(CoOccurrenceType.chemical) |
|
733
|
|
|
|
|
734
|
|
|
@property |
|
735
|
|
|
def gene_cooccurrences(self) -> FrozenSet[CoOccurrence]: |
|
|
|
|
|
|
736
|
|
|
return self._get_cooccurrences(CoOccurrenceType.gene) |
|
737
|
|
|
|
|
738
|
|
|
@property |
|
739
|
|
|
def disease_cooccurrences(self) -> FrozenSet[CoOccurrence]: |
|
|
|
|
|
|
740
|
|
|
return self._get_cooccurrences(CoOccurrenceType.disease) |
|
741
|
|
|
|
|
742
|
|
|
def _get_cooccurrences(self, kind: CoOccurrenceType) -> FrozenSet[CoOccurrence]: |
|
743
|
|
|
links = (self._links / kind.x_name / "LinkDataSet" / "LinkData").get |
|
744
|
|
|
results = set() |
|
745
|
|
|
for link in links: |
|
746
|
|
|
link = NestedDotDict(link) |
|
747
|
|
|
try: |
|
748
|
|
|
neighbor_id = str(link["ID_2"][kind.id_name]) |
|
749
|
|
|
except KeyError: |
|
750
|
|
|
raise KeyError(f"Could not find ${kind.id_name} in ${link['ID_2']}") |
|
751
|
|
|
neighbor_id = self._guess_neighbor(kind, neighbor_id) |
|
752
|
|
|
evidence = link["Evidence"][kind.x_name] |
|
753
|
|
|
neighbor_name = evidence["NeighborName"] |
|
754
|
|
|
ac = evidence["ArticleCount"] |
|
|
|
|
|
|
755
|
|
|
nac = evidence["NeighborArticleCount"] |
|
756
|
|
|
qac = evidence["QueryArticleCount"] |
|
757
|
|
|
score = evidence["CooccurrenceScore"] |
|
758
|
|
|
articles = [NestedDotDict(k) for k in evidence["Article"]] |
|
759
|
|
|
pubs = { |
|
760
|
|
|
Publication( |
|
761
|
|
|
pmid=Codes.PubmedId.of(pub["PMID"]), |
|
762
|
|
|
pub_date=datetime.strptime(pub["PublicationDate"].strip(), "%Y-%m-%d").date(), |
|
763
|
|
|
is_review=bool(pub["IsReview"]), |
|
764
|
|
|
title=pub["Title"].strip(), |
|
765
|
|
|
journal=pub["Journal"].strip(), |
|
766
|
|
|
relevance_score=pub.req_as("RelevanceScore", int), |
|
767
|
|
|
) |
|
768
|
|
|
for pub in articles |
|
769
|
|
|
} |
|
770
|
|
|
results.add( |
|
771
|
|
|
CoOccurrence( |
|
772
|
|
|
neighbor_id=neighbor_id, |
|
773
|
|
|
neighbor_name=neighbor_name, |
|
774
|
|
|
kind=kind, |
|
775
|
|
|
article_count=ac, |
|
776
|
|
|
query_article_count=qac, |
|
777
|
|
|
neighbor_article_count=nac, |
|
778
|
|
|
score=score, |
|
779
|
|
|
publications=frozenset(pubs), |
|
780
|
|
|
) |
|
781
|
|
|
) |
|
782
|
|
|
return frozenset(results) |
|
783
|
|
|
|
|
784
|
|
|
def _guess_neighbor(self, kind: CoOccurrenceType, neighbor_id: str) -> str: |
|
|
|
|
|
|
785
|
|
|
if kind is CoOccurrenceType.chemical: |
|
|
|
|
|
|
786
|
|
|
return Codes.PubchemCompoundId(neighbor_id) |
|
787
|
|
|
elif kind is CoOccurrenceType.gene and neighbor_id.startswith("EC:"): |
|
788
|
|
|
return Codes.EcNumber(neighbor_id) |
|
789
|
|
|
elif kind is CoOccurrenceType.gene: |
|
790
|
|
|
return Codes.GeneId(neighbor_id) |
|
791
|
|
|
elif kind is CoOccurrenceType.disease: |
|
792
|
|
|
return Codes.MeshCode(neighbor_id) |
|
793
|
|
|
else: |
|
794
|
|
|
raise ValueError(f"Could not find ID type for {kind} ID {neighbor_id}") |
|
795
|
|
|
|
|
796
|
|
|
|
|
797
|
|
|
class Patents(PubchemMiniDataView): |
|
|
|
|
|
|
798
|
|
|
"""""" |
|
799
|
|
|
|
|
800
|
|
|
@property |
|
801
|
|
|
def _whoami(self) -> str: |
|
802
|
|
|
return "Patents" |
|
803
|
|
|
|
|
804
|
|
|
@property |
|
805
|
|
|
def associated_disorders_and_diseases(self) -> FrozenSet[AssociatedDisorder]: |
|
|
|
|
|
|
806
|
|
|
return ( |
|
807
|
|
|
self._tables |
|
808
|
|
|
/ "patent" |
|
809
|
|
|
// ["diseasename", "directevidence", "dois"] |
|
810
|
|
|
/ [Mapx.req_is(str), Mapx.req_is(str), Mapx.n_bar_items()] |
|
811
|
|
|
// Flatmap.construct(AssociatedDisorder) |
|
812
|
|
|
).to_set |
|
813
|
|
|
|
|
814
|
|
|
|
|
815
|
|
|
class BiomolecularInteractionsAndPathways(PubchemMiniDataView): |
|
|
|
|
|
|
816
|
|
|
"""""" |
|
817
|
|
|
|
|
818
|
|
|
@property |
|
819
|
|
|
def _whoami(self) -> str: |
|
820
|
|
|
return "Biomolecular Interactions and Pathways" |
|
821
|
|
|
|
|
822
|
|
|
@property |
|
823
|
|
|
def drug_gene_interactions(self) -> FrozenSet[DrugGeneInteraction]: |
|
|
|
|
|
|
824
|
|
|
# the order of this dict is crucial |
|
825
|
|
|
keys = { |
|
826
|
|
|
"genename": Mapx.req_is(str, nullable=True), |
|
827
|
|
|
"geneclaimname": Mapx.req_is(str, nullable=True), |
|
828
|
|
|
"interactionclaimsource": Mapx.req_is(str, nullable=True), |
|
829
|
|
|
"interactiontypes": Mapx.split("|", nullable=True), |
|
830
|
|
|
"pmids": Mapx.split(",", nullable=True), |
|
831
|
|
|
"dois": Mapx.split("|", nullable=True), |
|
832
|
|
|
} |
|
833
|
|
|
return ( |
|
834
|
|
|
self._tables |
|
835
|
|
|
/ "dgidb" |
|
836
|
|
|
// list(keys.keys()) |
|
837
|
|
|
/ list(keys.values()) |
|
838
|
|
|
// Flatmap.construct(DrugGeneInteraction) |
|
839
|
|
|
).to_set |
|
840
|
|
|
|
|
841
|
|
|
@property |
|
842
|
|
|
def chemical_gene_interactions(self) -> FrozenSet[ChemicalGeneInteraction]: |
|
|
|
|
|
|
843
|
|
|
# the order of this dict is crucial |
|
844
|
|
|
# YES, the | used in pmids really is different from the , used in DrugGeneInteraction |
|
845
|
|
|
keys = { |
|
846
|
|
|
"genesymbol": Codes.GenecardSymbol.of_nullable, |
|
847
|
|
|
"interaction": Mapx.split("|", nullable=True), |
|
848
|
|
|
"taxid": Mapx.get_int(nullable=True), |
|
849
|
|
|
"taxname": Mapx.req_is(str, True), |
|
850
|
|
|
"pmids": Mapx.split("|", nullable=True), |
|
851
|
|
|
} |
|
852
|
|
|
return ( |
|
853
|
|
|
self._tables |
|
854
|
|
|
/ "ctdchemicalgene" |
|
855
|
|
|
// list(keys.keys()) |
|
856
|
|
|
/ list(keys.values()) |
|
857
|
|
|
// Flatmap.construct(ChemicalGeneInteraction) |
|
858
|
|
|
).to_set |
|
859
|
|
|
|
|
860
|
|
|
@property |
|
861
|
|
|
def drugbank_interactions(self) -> FrozenSet[DrugbankInteraction]: |
|
|
|
|
|
|
862
|
|
|
keys = { |
|
863
|
|
|
"gid": int, |
|
864
|
|
|
"genesymbol": Codes.GenecardSymbol, |
|
865
|
|
|
"drugaction": Mapx.req_is(str), |
|
866
|
|
|
"targetcomponentname": Mapx.req_is(str), |
|
867
|
|
|
"targettype": Mapx.req_is(str, then_convert=DrugbankTargetType), |
|
868
|
|
|
"targetname": Mapx.req_is(str), |
|
869
|
|
|
"generalfunc": Mapx.req_is(str), |
|
870
|
|
|
"specificfunc": Mapx.req_is(str), |
|
871
|
|
|
"pmids": Mapx.split(","), |
|
872
|
|
|
"dois": Mapx.split("|"), |
|
873
|
|
|
} |
|
874
|
|
|
return ( |
|
875
|
|
|
self._tables |
|
876
|
|
|
/ "drugbank" |
|
877
|
|
|
// list(keys.keys()) |
|
878
|
|
|
/ list(keys.values()) |
|
879
|
|
|
// Flatmap.construct(DrugbankInteraction) |
|
880
|
|
|
).to_set |
|
881
|
|
|
|
|
882
|
|
|
@property |
|
883
|
|
|
def drugbank_legal_groups(self) -> FrozenSet[str]: |
|
|
|
|
|
|
884
|
|
|
q = set() |
|
|
|
|
|
|
885
|
|
|
for x in (self._tables / "drugbank" // ["druggroup"] // Flatmap.require_only()).to_set: |
|
|
|
|
|
|
886
|
|
|
for y in x.split(";"): |
|
|
|
|
|
|
887
|
|
|
q.add(y.strip()) |
|
888
|
|
|
return frozenset(q) |
|
889
|
|
|
|
|
890
|
|
|
@property |
|
891
|
|
|
def drugbank_ddis(self) -> FrozenSet[DrugbankDdi]: |
|
|
|
|
|
|
892
|
|
|
keys = { |
|
893
|
|
|
"dbid2": Codes.DrugbankCompoundId, |
|
894
|
|
|
"cid2": Codes.PubchemCompoundId, |
|
895
|
|
|
"name": Mapx.req_is(str), |
|
896
|
|
|
"descr": Mapx.req_is(str), |
|
897
|
|
|
} |
|
898
|
|
|
return ( |
|
899
|
|
|
self._tables |
|
900
|
|
|
/ "drugbankddi" |
|
901
|
|
|
// list(keys.keys()) |
|
902
|
|
|
/ list(keys.values()) |
|
903
|
|
|
// Flatmap.construct(DrugbankDdi) |
|
904
|
|
|
).to_set |
|
905
|
|
|
|
|
906
|
|
|
|
|
907
|
|
|
class BiologicalTestResults(PubchemMiniDataView): |
|
|
|
|
|
|
908
|
|
|
"""""" |
|
909
|
|
|
|
|
910
|
|
|
@property |
|
911
|
|
|
def _whoami(self) -> str: |
|
912
|
|
|
return "Biological Test Results" |
|
913
|
|
|
|
|
914
|
|
|
@property |
|
915
|
|
|
def bioactivity(self) -> FrozenSet[Bioactivity]: |
|
|
|
|
|
|
916
|
|
|
keys = { |
|
917
|
|
|
"aid": Mapx.get_int(), |
|
918
|
|
|
"aidtype": (lambda s: AssayType[s.lower().strip()]), |
|
919
|
|
|
"aidsrcname": Mapx.req_is(str), |
|
920
|
|
|
"aidname": Mapx.req_is(str), |
|
921
|
|
|
"aidmdate": Mapx.int_date(), |
|
922
|
|
|
"geneid": Mapx.str_to(Codes.GeneId, nullable=True, flex_type=True), |
|
923
|
|
|
"taxid": Mapx.str_to(str, flex_type=True, nullable=True), |
|
924
|
|
|
"pmid": lambda s: None |
|
925
|
|
|
if s is None |
|
|
|
|
|
|
926
|
|
|
else Codes.PubmedId(StringTools.strip_off_end(str(s), ".0")), |
|
|
|
|
|
|
927
|
|
|
"activity": (lambda s: None if s is None else Activity[s.lower()]), |
|
928
|
|
|
"acname": Mapx.str_to(str, nullable=True), |
|
929
|
|
|
"acvalue": (lambda x: None if x is None else float(x)), |
|
930
|
|
|
"targetname": Mapx.req_is(str, nullable=True), |
|
931
|
|
|
"cmpdname": Mapx.req_is(str, nullable=False), |
|
932
|
|
|
} |
|
933
|
|
|
return ( |
|
934
|
|
|
self._tables |
|
935
|
|
|
/ "bioactivity" |
|
936
|
|
|
// list(keys.keys()) |
|
937
|
|
|
/ list(keys.values()) |
|
938
|
|
|
// Flatmap.construct(Bioactivity) |
|
939
|
|
|
).to_set |
|
940
|
|
|
|
|
941
|
|
|
|
|
942
|
|
|
class Classification(PubchemMiniDataView): |
|
|
|
|
|
|
943
|
|
|
"""""" |
|
944
|
|
|
|
|
945
|
|
|
@property |
|
946
|
|
|
def _whoami(self) -> str: |
|
947
|
|
|
return "Classification" |
|
948
|
|
|
|
|
949
|
|
|
@property |
|
950
|
|
|
def mesh_tree(self) -> Sequence[str]: |
|
|
|
|
|
|
951
|
|
|
raise NotImplementedError() |
|
952
|
|
|
|
|
953
|
|
|
@property |
|
954
|
|
|
def chebi_tree(self) -> Sequence[str]: |
|
|
|
|
|
|
955
|
|
|
raise NotImplementedError() |
|
956
|
|
|
|
|
957
|
|
|
@property |
|
958
|
|
|
def atc_tree(self) -> FrozenSet[Sequence[str]]: |
|
|
|
|
|
|
959
|
|
|
raise NotImplementedError() |
|
960
|
|
|
|
|
961
|
|
|
@property |
|
962
|
|
|
def chemid(self) -> FrozenSet[Sequence[str]]: |
|
|
|
|
|
|
963
|
|
|
raise NotImplementedError() |
|
964
|
|
|
|
|
965
|
|
|
@property |
|
966
|
|
|
def g2p_tree(self) -> FrozenSet[Sequence[str]]: |
|
|
|
|
|
|
967
|
|
|
raise NotImplementedError() |
|
968
|
|
|
|
|
969
|
|
|
@property |
|
970
|
|
|
def chembl_tree(self) -> FrozenSet[Sequence[str]]: |
|
|
|
|
|
|
971
|
|
|
raise NotImplementedError() |
|
972
|
|
|
|
|
973
|
|
|
@property |
|
974
|
|
|
def cpdat_tree(self) -> FrozenSet[Sequence[str]]: |
|
|
|
|
|
|
975
|
|
|
raise NotImplementedError() |
|
976
|
|
|
|
|
977
|
|
|
@property |
|
978
|
|
|
def dea(self) -> FrozenSet[str]: |
|
|
|
|
|
|
979
|
|
|
raise NotImplementedError() |
|
980
|
|
|
|
|
981
|
|
|
|
|
982
|
|
|
class PubchemData(PubchemDataView): |
|
|
|
|
|
|
983
|
|
|
@property |
|
984
|
|
|
def name(self) -> Optional[str]: |
|
|
|
|
|
|
985
|
|
|
return self._data.get("record.RecordTitle") |
|
986
|
|
|
|
|
987
|
|
|
@property |
|
988
|
|
|
def title_and_summary(self) -> TitleAndSummary: |
|
|
|
|
|
|
989
|
|
|
return TitleAndSummary(self._data) |
|
990
|
|
|
|
|
991
|
|
|
@property |
|
992
|
|
|
def names_and_identifiers(self) -> NamesAndIdentifiers: |
|
|
|
|
|
|
993
|
|
|
return NamesAndIdentifiers(self._data) |
|
994
|
|
|
|
|
995
|
|
|
@property |
|
996
|
|
|
def chemical_and_physical_properties(self) -> ChemicalAndPhysicalProperties: |
|
|
|
|
|
|
997
|
|
|
return ChemicalAndPhysicalProperties(self._data) |
|
998
|
|
|
|
|
999
|
|
|
@property |
|
1000
|
|
|
def related_records(self) -> RelatedRecords: |
|
|
|
|
|
|
1001
|
|
|
return RelatedRecords(self._data) |
|
1002
|
|
|
|
|
1003
|
|
|
@property |
|
1004
|
|
|
def drug_and_medication_information(self) -> DrugAndMedicationInformation: |
|
|
|
|
|
|
1005
|
|
|
return DrugAndMedicationInformation(self._data) |
|
1006
|
|
|
|
|
1007
|
|
|
@property |
|
1008
|
|
|
def pharmacology_and_biochemistry(self) -> PharmacologyAndBiochemistry: |
|
|
|
|
|
|
1009
|
|
|
return PharmacologyAndBiochemistry(self._data) |
|
1010
|
|
|
|
|
1011
|
|
|
@property |
|
1012
|
|
|
def safety_and_hazards(self) -> SafetyAndHazards: |
|
|
|
|
|
|
1013
|
|
|
return SafetyAndHazards(self._data) |
|
1014
|
|
|
|
|
1015
|
|
|
@property |
|
1016
|
|
|
def toxicity(self) -> Toxicity: |
|
|
|
|
|
|
1017
|
|
|
return Toxicity(self._data) |
|
1018
|
|
|
|
|
1019
|
|
|
@property |
|
1020
|
|
|
def literature(self) -> Literature: |
|
|
|
|
|
|
1021
|
|
|
return Literature(self._data) |
|
1022
|
|
|
|
|
1023
|
|
|
@property |
|
1024
|
|
|
def associated_disorders_and_diseases(self) -> AssociatedDisordersAndDiseases: |
|
|
|
|
|
|
1025
|
|
|
return AssociatedDisordersAndDiseases(self._data) |
|
1026
|
|
|
|
|
1027
|
|
|
@property |
|
1028
|
|
|
def biomolecular_interactions_and_pathways(self) -> BiomolecularInteractionsAndPathways: |
|
|
|
|
|
|
1029
|
|
|
return BiomolecularInteractionsAndPathways(self._data) |
|
1030
|
|
|
|
|
1031
|
|
|
@property |
|
1032
|
|
|
def biological_test_results(self) -> BiologicalTestResults: |
|
|
|
|
|
|
1033
|
|
|
return BiologicalTestResults(self._data) |
|
1034
|
|
|
|
|
1035
|
|
|
@property |
|
1036
|
|
|
def classification(self) -> Classification: |
|
|
|
|
|
|
1037
|
|
|
return Classification(self._data) |
|
1038
|
|
|
|
|
1039
|
|
|
@property |
|
1040
|
|
|
def parent_or_none(self) -> Optional[int]: |
|
|
|
|
|
|
1041
|
|
|
return self.related_records.parent |
|
1042
|
|
|
|
|
1043
|
|
|
@property |
|
1044
|
|
|
def parent_or_self(self) -> int: |
|
|
|
|
|
|
1045
|
|
|
parent = self.related_records.parent |
|
1046
|
|
|
return self.cid if parent is None else parent |
|
1047
|
|
|
|
|
1048
|
|
|
|
|
1049
|
|
|
__all__ = [ |
|
1050
|
|
|
"PubchemData", |
|
1051
|
|
|
"TitleAndSummary", |
|
1052
|
|
|
"RelatedRecords", |
|
1053
|
|
|
"ChemicalAndPhysicalProperties", |
|
1054
|
|
|
"DrugAndMedicationInformation", |
|
1055
|
|
|
"PharmacologyAndBiochemistry", |
|
1056
|
|
|
"SafetyAndHazards", |
|
1057
|
|
|
"Toxicity", |
|
1058
|
|
|
"AssociatedDisordersAndDiseases", |
|
1059
|
|
|
"Literature", |
|
1060
|
|
|
"NamesAndIdentifiers", |
|
1061
|
|
|
"BiomolecularInteractionsAndPathways", |
|
1062
|
|
|
"Classification", |
|
1063
|
|
|
] |
|
1064
|
|
|
|