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