1
|
|
|
from __future__ import annotations |
|
|
|
|
2
|
|
|
import enum |
3
|
|
|
import re |
4
|
|
|
from dataclasses import dataclass |
5
|
|
|
from datetime import date |
6
|
|
|
from typing import Union, Optional, FrozenSet, Sequence |
7
|
|
|
|
8
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
9
|
|
|
|
10
|
|
|
from mandos import MandosResources |
11
|
|
|
from mandos.model.pubchem_support._nav_fns import Mapx |
12
|
|
|
|
13
|
|
|
hazards = { |
14
|
|
|
d["code"]: d for d in NestedDotDict.read_toml(MandosResources.path("hazards.toml"))["signals"] |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
@dataclass(frozen=True, repr=True, eq=True, order=True) |
|
|
|
|
19
|
|
|
class ComputedProperty: |
20
|
|
|
key: str |
21
|
|
|
value: Union[int, str, float, bool] |
22
|
|
|
unit: Optional[str] |
23
|
|
|
ref: str |
24
|
|
|
|
25
|
|
|
def req_is(self, type_) -> Union[int, str, float, bool]: |
|
|
|
|
26
|
|
|
if not isinstance(self.value, type_): |
27
|
|
|
raise TypeError(f"{self.key}->{self.value} has {type(self.value)}, not {type_}") |
28
|
|
|
return self.value |
29
|
|
|
|
30
|
|
|
@property |
31
|
|
|
def as_str(self) -> str: |
|
|
|
|
32
|
|
|
return f"{self.value} {self.unit}" |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
class Code(str): |
|
|
|
|
36
|
|
|
@property |
37
|
|
|
def type_name(self) -> str: |
|
|
|
|
38
|
|
|
return self.__class__.__name__.lower() |
39
|
|
|
|
40
|
|
|
@classmethod |
41
|
|
|
def of(cls, value: Union[str, int, float]): |
|
|
|
|
42
|
|
|
if isinstance(value, float): |
43
|
|
|
try: |
44
|
|
|
value = int(value) |
45
|
|
|
except ArithmeticError: |
46
|
|
|
value = str(value) |
47
|
|
|
value = str(value).strip() |
48
|
|
|
return cls(value) |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
class Codes: |
52
|
|
|
""" |
53
|
|
|
These turn out to be extremely useful for documenting return types. |
54
|
|
|
For example, ``DrugbankInteraction`` might have a ``gene`` field, |
55
|
|
|
which can be described as a ``GenecardSymbol`` if known. |
56
|
|
|
""" |
57
|
|
|
|
58
|
|
|
class EcNumber(Code): |
59
|
|
|
""" |
60
|
|
|
e.g. 'EC:4.6.1.1' |
61
|
|
|
""" |
62
|
|
|
|
63
|
|
|
class GeneId(Code): |
64
|
|
|
""" |
65
|
|
|
GeneCard, UniProt gene name, etc. |
66
|
|
|
e.g. 'slc1a2' |
67
|
|
|
""" |
68
|
|
|
|
69
|
|
|
class GenecardSymbol(GeneId): |
|
|
|
|
70
|
|
|
"""""" |
71
|
|
|
|
72
|
|
|
class UniprotId(GeneId): |
|
|
|
|
73
|
|
|
"""""" |
74
|
|
|
|
75
|
|
|
class PubchemCompoundId(Code): |
76
|
|
|
""" |
77
|
|
|
e.g. 2352 |
78
|
|
|
""" |
79
|
|
|
|
80
|
|
|
@property |
81
|
|
|
def value(self) -> int: |
|
|
|
|
82
|
|
|
return int(self) |
83
|
|
|
|
84
|
|
|
class AtcCode(Code): |
|
|
|
|
85
|
|
|
"""""" |
86
|
|
|
|
87
|
|
|
class PubmedId(Code): |
|
|
|
|
88
|
|
|
"""""" |
89
|
|
|
|
90
|
|
|
class Doi(Code): |
|
|
|
|
91
|
|
|
"""""" |
92
|
|
|
|
93
|
|
|
class MeshCode(Code): |
|
|
|
|
94
|
|
|
"""""" |
95
|
|
|
|
96
|
|
|
class PdbId(Code): |
|
|
|
|
97
|
|
|
"""""" |
98
|
|
|
|
99
|
|
|
class MeshHeading(Code): |
|
|
|
|
100
|
|
|
"""""" |
101
|
|
|
|
102
|
|
|
class MeshSubheading(Code): |
|
|
|
|
103
|
|
|
"""""" |
104
|
|
|
|
105
|
|
|
class DrugbankCompoundId(Code): |
|
|
|
|
106
|
|
|
"""""" |
107
|
|
|
|
108
|
|
|
class DeaSchedule(Code): |
|
|
|
|
109
|
|
|
"""""" |
110
|
|
|
|
111
|
|
|
@property |
112
|
|
|
def value(self) -> int: |
|
|
|
|
113
|
|
|
return Mapx.roman_to_arabic(1, 5)(self) |
114
|
|
|
|
115
|
|
|
class GhsCode(Code): |
|
|
|
|
116
|
|
|
"""""" |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
class CoOccurrenceType(enum.Enum): |
|
|
|
|
120
|
|
|
chemical = enum.auto() |
121
|
|
|
gene = enum.auto() |
122
|
|
|
disease = enum.auto() |
123
|
|
|
|
124
|
|
|
@property |
125
|
|
|
def x_name(self) -> str: |
|
|
|
|
126
|
|
|
if self is CoOccurrenceType.chemical: |
|
|
|
|
127
|
|
|
return "ChemicalNeighbor" |
128
|
|
|
elif self is CoOccurrenceType.gene: |
129
|
|
|
return "ChemicalGeneSymbolNeighbor" |
130
|
|
|
elif self is CoOccurrenceType.disease: |
131
|
|
|
return "ChemicalDiseaseNeighbor" |
132
|
|
|
raise AssertionError(f"{self} not found!!") |
133
|
|
|
|
134
|
|
|
@property |
135
|
|
|
def id_name(self) -> str: |
|
|
|
|
136
|
|
|
if self is CoOccurrenceType.chemical: |
|
|
|
|
137
|
|
|
return "CID" |
138
|
|
|
elif self is CoOccurrenceType.gene: |
139
|
|
|
return "GeneSymbol" |
140
|
|
|
elif self is CoOccurrenceType.disease: |
141
|
|
|
return "MeSH" |
142
|
|
|
raise AssertionError(f"{self} not found!!") |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
@dataclass(frozen=True, repr=True, eq=True) |
|
|
|
|
146
|
|
|
class ClinicalTrial: |
147
|
|
|
title: str |
148
|
|
|
conditions: FrozenSet[str] |
149
|
|
|
phase: str |
150
|
|
|
status: str |
151
|
|
|
interventions: FrozenSet[str] |
152
|
|
|
cids: FrozenSet[Codes.PubchemCompoundId] |
153
|
|
|
source: str |
154
|
|
|
|
155
|
|
|
@property |
156
|
|
|
def known_phase(self) -> int: |
|
|
|
|
157
|
|
|
return { |
158
|
|
|
"Phase 4": 4, |
159
|
|
|
"Phase 3": 3, |
160
|
|
|
"Phase 2": 2, |
161
|
|
|
"Phase 1": 1, |
162
|
|
|
"Early Phase 1": 1, |
163
|
|
|
"N/A": 0, |
164
|
|
|
}.get(self.status, 0) |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
@dataclass(frozen=True, repr=True, eq=True) |
|
|
|
|
168
|
|
|
class GhsCode: |
169
|
|
|
code: Codes.GhsCode |
170
|
|
|
statement: str |
171
|
|
|
clazz: str |
172
|
|
|
categories: FrozenSet[str] |
173
|
|
|
signal_word: str |
174
|
|
|
type: str |
175
|
|
|
|
176
|
|
|
@classmethod |
177
|
|
|
def find(cls, code: str) -> GhsCode: |
|
|
|
|
178
|
|
|
h = hazards[code] |
|
|
|
|
179
|
|
|
cats = h["category"] # TODO |
|
|
|
|
180
|
|
|
return GhsCode( |
181
|
|
|
code=Codes.GhsCode(code), |
182
|
|
|
statement=h["statement"], |
183
|
|
|
clazz=h["class"], |
184
|
|
|
categories=cats, |
185
|
|
|
signal_word=h["signal_word"], |
186
|
|
|
type=h["type"], |
187
|
|
|
) |
188
|
|
|
|
189
|
|
|
@property |
190
|
|
|
def level(self) -> int: |
|
|
|
|
191
|
|
|
return int(self.code[1]) |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
@dataclass(frozen=True, repr=True, eq=True) |
|
|
|
|
195
|
|
|
class AssociatedDisorder: |
196
|
|
|
disease: str |
197
|
|
|
evidence_type: str |
198
|
|
|
n_refs: int |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
@dataclass(frozen=True, repr=True, eq=True) |
|
|
|
|
202
|
|
|
class AtcCode: |
203
|
|
|
code: str |
204
|
|
|
name: str |
205
|
|
|
|
206
|
|
|
@property |
207
|
|
|
def level(self) -> int: |
|
|
|
|
208
|
|
|
return len(self.parts) |
209
|
|
|
|
210
|
|
|
@property |
211
|
|
|
def parts(self) -> Sequence[str]: |
|
|
|
|
212
|
|
|
pat = re.compile(r"([A-Z])([0-9]{2})?([A-Z])?([A-Z])?([A-Z])?") |
213
|
|
|
match = pat.fullmatch(self.code) |
214
|
|
|
return [g for g in match.groups() if g is not None] |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
@dataclass(frozen=True, repr=True, eq=True) |
|
|
|
|
218
|
|
|
class DrugbankInteraction: |
219
|
|
|
gene_symbol: Codes.GeneId |
220
|
|
|
action: str |
221
|
|
|
target_name: str |
222
|
|
|
general_function: Sequence[str] |
223
|
|
|
specific_function: str |
224
|
|
|
pmids: FrozenSet[Codes.PubmedId] |
225
|
|
|
dois: FrozenSet[Codes.Doi] |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
@dataclass(frozen=True, repr=True, eq=True) |
|
|
|
|
229
|
|
|
class DrugbankDdi: |
230
|
|
|
drug_drugbank_id: Codes.DrugbankCompoundId |
231
|
|
|
drug_pubchem_id: Codes.PubchemCompoundId |
232
|
|
|
drug_drugbank_name: str |
233
|
|
|
description: str |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
class AssayType(enum.Enum): |
|
|
|
|
237
|
|
|
confirmatory = enum.auto() |
238
|
|
|
literature = enum.auto() |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
class Activity(enum.Enum): |
|
|
|
|
242
|
|
|
active = enum.auto() |
243
|
|
|
inactive = enum.auto() |
244
|
|
|
unspecified = enum.auto() |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
@dataclass(frozen=True, repr=True, eq=True) |
|
|
|
|
248
|
|
|
class Bioactivity: |
249
|
|
|
assay_id: int |
250
|
|
|
assay_type: AssayType |
251
|
|
|
assay_ref: str |
252
|
|
|
assay_name: str |
253
|
|
|
assay_made_date: date |
254
|
|
|
gene_id: Optional[Codes.GeneId] |
255
|
|
|
tax_id: Optional[int] |
256
|
|
|
pmid: Optional[Codes.PubmedId] |
257
|
|
|
activity: Optional[Activity] |
258
|
|
|
activity_name: Optional[str] |
259
|
|
|
activity_value: float |
260
|
|
|
target_name: Optional[str] |
261
|
|
|
|
262
|
|
|
|
263
|
|
|
@dataclass(frozen=True, repr=True, eq=True) |
|
|
|
|
264
|
|
|
class PdbEntry: |
265
|
|
|
pdbid: Codes.PdbId |
266
|
|
|
title: str |
267
|
|
|
exp_method: str |
268
|
|
|
resolution: float |
269
|
|
|
lig_names: FrozenSet[str] |
270
|
|
|
cids: FrozenSet[Codes.PubchemCompoundId] |
271
|
|
|
uniprot_ids: FrozenSet[Codes.UniprotId] |
272
|
|
|
pmids: FrozenSet[Codes.PubmedId] |
273
|
|
|
dois: FrozenSet[Codes.Doi] |
274
|
|
|
|
275
|
|
|
|
276
|
|
|
@dataclass(frozen=True, repr=True, eq=True) |
|
|
|
|
277
|
|
|
class PubmedEntry: |
278
|
|
|
pmid: Codes.PubmedId |
279
|
|
|
article_type: str |
280
|
|
|
pmidsrcs: FrozenSet[str] |
281
|
|
|
mesh_headings: FrozenSet[Codes.MeshHeading] |
282
|
|
|
mesh_subheadings: FrozenSet[Codes.MeshSubheading] |
283
|
|
|
mesh_codes: FrozenSet[Codes.MeshCode] |
284
|
|
|
cids: FrozenSet[Codes.PubchemCompoundId] |
285
|
|
|
article_title: str |
286
|
|
|
article_abstract: str |
287
|
|
|
journal_name: str |
288
|
|
|
pub_date: date |
289
|
|
|
|
290
|
|
|
|
291
|
|
|
@dataclass(frozen=True, repr=True, eq=True) |
|
|
|
|
292
|
|
|
class Publication: |
293
|
|
|
pmid: Codes.PubmedId |
294
|
|
|
pub_date: date |
295
|
|
|
is_review: bool |
296
|
|
|
title: str |
297
|
|
|
journal: str |
298
|
|
|
relevance_score: int |
299
|
|
|
|
300
|
|
|
|
301
|
|
|
@dataclass(frozen=True, repr=True, eq=True) |
|
|
|
|
302
|
|
|
class CoOccurrence: |
303
|
|
|
neighbor_id: str |
304
|
|
|
neighbor_name: str |
305
|
|
|
kind: CoOccurrenceType |
306
|
|
|
# https://pubchemdocs.ncbi.nlm.nih.gov/knowledge-panels |
307
|
|
|
article_count: int |
308
|
|
|
query_article_count: int |
309
|
|
|
neighbor_article_count: int |
310
|
|
|
score: int |
311
|
|
|
publications: FrozenSet[Publication] |
312
|
|
|
|
313
|
|
|
def strip_pubs(self) -> CoOccurrence: |
|
|
|
|
314
|
|
|
return CoOccurrence( |
315
|
|
|
self.neighbor_id, |
316
|
|
|
self.neighbor_name, |
317
|
|
|
self.kind, |
318
|
|
|
self.article_count, |
319
|
|
|
self.query_article_count, |
320
|
|
|
self.neighbor_article_count, |
321
|
|
|
self.score, |
322
|
|
|
frozenset({}), |
323
|
|
|
) |
324
|
|
|
|
325
|
|
|
|
326
|
|
|
@dataclass(frozen=True, repr=True, eq=True) |
|
|
|
|
327
|
|
|
class DrugGeneInteraction: |
328
|
|
|
"""""" |
329
|
|
|
|
330
|
|
|
gene_name: Optional[str] |
331
|
|
|
gene_claim_id: Optional[str] |
332
|
|
|
source: str |
333
|
|
|
interactions: FrozenSet[str] |
334
|
|
|
pmids: FrozenSet[Codes.PubmedId] |
335
|
|
|
dois: FrozenSet[Codes.Doi] |
336
|
|
|
|
337
|
|
|
|
338
|
|
|
@dataclass(frozen=True, repr=True, eq=True) |
|
|
|
|
339
|
|
|
class CompoundGeneInteraction: |
340
|
|
|
gene_name: Optional[Codes.GeneId] |
341
|
|
|
interactions: FrozenSet[str] |
342
|
|
|
tax_name: Optional[str] |
343
|
|
|
pmids: FrozenSet[Codes.PubmedId] |
344
|
|
|
|
345
|
|
|
|
346
|
|
|
__all__ = [ |
347
|
|
|
"ClinicalTrial", |
348
|
|
|
"AssociatedDisorder", |
349
|
|
|
"AtcCode", |
350
|
|
|
"AssayType", |
351
|
|
|
"DrugbankInteraction", |
352
|
|
|
"DrugbankDdi", |
353
|
|
|
"Bioactivity", |
354
|
|
|
"Activity", |
355
|
|
|
"DrugGeneInteraction", |
356
|
|
|
"CompoundGeneInteraction", |
357
|
|
|
"GhsCode", |
358
|
|
|
"PubmedEntry", |
359
|
|
|
"Code", |
360
|
|
|
"Codes", |
361
|
|
|
"CoOccurrenceType", |
362
|
|
|
"CoOccurrence", |
363
|
|
|
"Publication", |
364
|
|
|
"ComputedProperty", |
365
|
|
|
] |
366
|
|
|
|