1
|
|
|
from typing import Sequence |
|
|
|
|
2
|
|
|
|
3
|
|
|
import numpy as np |
|
|
|
|
4
|
|
|
|
5
|
|
|
from mandos.model.apis.pubchem_api import PubchemApi |
6
|
|
|
from mandos.model.concrete_hits import AcuteEffectHit, Ld50Hit |
7
|
|
|
from mandos.model.utils.setup import logger |
8
|
|
|
from mandos.search.pubchem import PubchemSearch |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class AcuteEffectSearch(PubchemSearch[AcuteEffectHit]): |
|
|
|
|
12
|
|
|
""" """ |
13
|
|
|
|
14
|
|
|
def __init__(self, key: str, api: PubchemApi, top_level: bool): |
15
|
|
|
super().__init__(key, api) |
16
|
|
|
self.top_level = top_level |
17
|
|
|
|
18
|
|
|
def find(self, inchikey: str) -> Sequence[AcuteEffectHit]: |
|
|
|
|
19
|
|
|
data = self.api.fetch_data(inchikey) |
20
|
|
|
results = [] |
21
|
|
|
for dd in data.toxicity.acute_effects: |
|
|
|
|
22
|
|
|
for effect in dd.effects: |
23
|
|
|
effect_name = effect.category.lower() if self.top_level else effect.lower() |
24
|
|
|
try: |
25
|
|
|
weight = -dd.mg_per_kg |
26
|
|
|
except ValueError: |
27
|
|
|
logger.error(f"Failed to parse {dd.dose} on {inchikey}") |
28
|
|
|
logger.opt(exception=True).debug( |
29
|
|
|
f"Failed to parse {dd.dose} on {inchikey}: {dd}" |
30
|
|
|
) |
31
|
|
|
continue |
32
|
|
|
source = self._format_source( |
33
|
|
|
organism=dd.organism, |
34
|
|
|
human=dd.organism.is_human, |
35
|
|
|
route=dd.route, |
36
|
|
|
test_type=dd.test_type, |
37
|
|
|
) |
38
|
|
|
predicate = self._format_predicate( |
39
|
|
|
organism=dd.organism, |
40
|
|
|
human=dd.organism.is_human, |
41
|
|
|
route=dd.route, |
42
|
|
|
test_type=dd.test_type, |
43
|
|
|
) |
44
|
|
|
results.append( |
45
|
|
|
self._create_hit( |
46
|
|
|
c_id=str(data.cid), |
47
|
|
|
c_origin=inchikey, |
48
|
|
|
c_matched=data.names_and_identifiers.inchikey, |
49
|
|
|
c_name=data.name, |
50
|
|
|
data_source=source, |
51
|
|
|
predicate=predicate, |
52
|
|
|
object_id=effect_name, |
53
|
|
|
object_name=effect_name, |
54
|
|
|
effect=effect.lower(), |
55
|
|
|
organism=dd.organism, |
56
|
|
|
human=dd.organism.is_human, |
57
|
|
|
route=dd.route, |
58
|
|
|
mg_per_kg=dd.mg_per_kg, |
59
|
|
|
test_type=dd.test_type, |
60
|
|
|
weight=weight, |
61
|
|
|
) |
62
|
|
|
) |
63
|
|
|
return results |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
class Ld50Search(PubchemSearch[Ld50Hit]): |
|
|
|
|
67
|
|
|
def find(self, inchikey: str) -> Sequence[Ld50Hit]: |
|
|
|
|
68
|
|
|
data = self.api.fetch_data(inchikey) |
69
|
|
|
results = [] |
70
|
|
|
for dd in data.toxicity.acute_effects: |
|
|
|
|
71
|
|
|
if dd.test_type != "LD50": |
72
|
|
|
continue |
73
|
|
|
try: |
74
|
|
|
weight = -dd.mg_per_kg |
75
|
|
|
except ValueError: |
76
|
|
|
logger.error(f"Failed to parse {dd.dose} on {inchikey}") |
77
|
|
|
logger.opt(exception=True).debug(f"Failed to parse {dd.dose} on {inchikey}: {dd}") |
78
|
|
|
continue |
79
|
|
|
logger.trace(f"NLP: Weight {weight} from {dd.mg_per_kg}") |
80
|
|
|
source = self._format_source( |
81
|
|
|
organism=dd.organism, |
82
|
|
|
human=dd.organism.is_human, |
83
|
|
|
route=dd.route, |
84
|
|
|
test_type=dd.test_type, |
85
|
|
|
) |
86
|
|
|
predicate = self._format_predicate( |
87
|
|
|
organism=dd.organism, |
88
|
|
|
human=dd.organism.is_human, |
89
|
|
|
route=dd.route, |
90
|
|
|
test_type=dd.test_type, |
91
|
|
|
) |
92
|
|
|
results.append( |
93
|
|
|
self._create_hit( |
94
|
|
|
inchikey=inchikey, |
95
|
|
|
c_id=str(data.cid), |
96
|
|
|
c_origin=inchikey, |
97
|
|
|
c_matched=data.names_and_identifiers.inchikey, |
98
|
|
|
c_name=data.name, |
99
|
|
|
data_source=source, |
100
|
|
|
predicate=predicate, |
101
|
|
|
object_id=str(dd.mg_per_kg), |
102
|
|
|
object_name=str(dd.mg_per_kg), |
103
|
|
|
weight=weight, |
104
|
|
|
organism=dd.organism, |
105
|
|
|
human=dd.organism.is_human, |
106
|
|
|
route=dd.route, |
107
|
|
|
cache_date=data.names_and_identifiers.modify_date, |
108
|
|
|
) |
109
|
|
|
) |
110
|
|
|
return results |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
__all__ = ["AcuteEffectSearch", "Ld50Search"] |
114
|
|
|
|