|
1
|
|
|
from __future__ import annotations |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
import logging |
|
4
|
|
|
from dataclasses import dataclass |
|
5
|
|
|
from typing import Sequence |
|
6
|
|
|
|
|
7
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
from mandos.model import AbstractHit, ChemblCompound, Search |
|
10
|
|
|
|
|
11
|
|
|
logger = logging.getLogger("mandos") |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
@dataclass(frozen=True, order=True, repr=True) |
|
15
|
|
|
class AtcHit(AbstractHit): |
|
16
|
|
|
""" |
|
17
|
|
|
An ATC code found for a compound. |
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
|
|
level: int |
|
21
|
|
|
|
|
22
|
|
|
@property |
|
23
|
|
|
def predicate(self) -> str: |
|
24
|
|
|
return f"has ATC L-{self.level} code" |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
class AtcSearch(Search[AtcHit]): |
|
|
|
|
|
|
28
|
|
|
"""""" |
|
29
|
|
|
|
|
30
|
|
|
def find(self, lookup: str) -> Sequence[AtcHit]: |
|
31
|
|
|
""" |
|
32
|
|
|
|
|
33
|
|
|
Args: |
|
34
|
|
|
lookup: |
|
35
|
|
|
|
|
36
|
|
|
Returns: |
|
37
|
|
|
|
|
38
|
|
|
""" |
|
39
|
|
|
# 'atc_classifications': ['S01HA01', 'N01BC01', 'R02AD03', 'S02DA02'] |
|
40
|
|
|
# 'indication_class': 'Anesthetic (topical)' |
|
41
|
|
|
ch = self.get_compound_dot_dict(lookup) |
|
|
|
|
|
|
42
|
|
|
compound = self.compound_dot_dict_to_obj(ch) |
|
43
|
|
|
hits = [] |
|
44
|
|
|
if "atc_classifications" in ch: |
|
45
|
|
|
for atc in ch["atc_classifications"]: |
|
46
|
|
|
hits.extend(self.process(lookup, compound, atc)) |
|
47
|
|
|
return hits |
|
48
|
|
|
|
|
49
|
|
|
def process(self, lookup: str, compound: ChemblCompound, atc: str) -> Sequence[AtcHit]: |
|
50
|
|
|
""" |
|
51
|
|
|
|
|
52
|
|
|
Args: |
|
53
|
|
|
lookup: |
|
54
|
|
|
compound: |
|
55
|
|
|
atc: |
|
56
|
|
|
|
|
57
|
|
|
Returns: |
|
58
|
|
|
|
|
59
|
|
|
""" |
|
60
|
|
|
dots = NestedDotDict(self.api.atc_class.get(atc)) |
|
61
|
|
|
return [self._code(lookup, compound, dots, 3), self._code(lookup, compound, dots, 4)] |
|
62
|
|
|
|
|
63
|
|
|
def _code(self, lookup: str, compound: ChemblCompound, dots: NestedDotDict, level: int): |
|
|
|
|
|
|
64
|
|
|
# 'level1': 'N', 'level1_description': 'NERVOUS SYSTEM', 'level2': 'N05', ... |
|
65
|
|
|
return AtcHit( |
|
66
|
|
|
None, |
|
67
|
|
|
compound.chid, |
|
68
|
|
|
compound.inchikey, |
|
69
|
|
|
lookup, |
|
70
|
|
|
compound.name, |
|
71
|
|
|
object_id=dots.get(f"level{level}"), |
|
72
|
|
|
object_name=dots.get(f"level{level}_description"), |
|
73
|
|
|
level=level, |
|
74
|
|
|
) |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
__all__ = ["AtcHit", "AtcSearch"] |
|
78
|
|
|
|