1
|
|
|
import logging |
|
|
|
|
2
|
|
|
from dataclasses import dataclass |
3
|
|
|
from typing import Sequence, Set, Optional |
4
|
|
|
|
5
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
6
|
|
|
|
7
|
|
|
from mandos.model.chembl_api import ChemblApi |
8
|
|
|
from mandos.model.chembl_support import ChemblCompound |
9
|
|
|
from mandos.model.chembl_support.chembl_target_graphs import ChemblTargetGraph |
10
|
|
|
from mandos.model.taxonomy import Taxonomy |
11
|
|
|
from mandos.search.chembl._protein_search import ProteinHit, ProteinSearch |
12
|
|
|
from mandos.search.chembl.target_traversal import TargetTraversalStrategy, TargetTraversalStrategies |
|
|
|
|
13
|
|
|
|
14
|
|
|
logger = logging.getLogger("mandos") |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
@dataclass(frozen=True, order=True, repr=True) |
18
|
|
|
class MechanismHit(ProteinHit): |
19
|
|
|
""" |
20
|
|
|
A mechanism entry for a compound. |
21
|
|
|
""" |
22
|
|
|
|
23
|
|
|
action_type: str |
24
|
|
|
direct_interaction: bool |
25
|
|
|
description: str |
26
|
|
|
exact_target_id: str |
27
|
|
|
|
28
|
|
|
@property |
29
|
|
|
def predicate(self) -> str: |
30
|
|
|
return self.action_type.lower() |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
class MechanismSearch(ProteinSearch[MechanismHit]): |
34
|
|
|
""" |
35
|
|
|
Search for ``mechanisms``. |
36
|
|
|
""" |
37
|
|
|
|
38
|
|
|
def __init__( |
|
|
|
|
39
|
|
|
self, |
|
|
|
|
40
|
|
|
chembl_api: ChemblApi, |
|
|
|
|
41
|
|
|
tax: Taxonomy, |
|
|
|
|
42
|
|
|
traversal_strategy: str, |
|
|
|
|
43
|
|
|
allowed_target_types: Set[str], |
|
|
|
|
44
|
|
|
min_confidence_score: Optional[int], |
|
|
|
|
45
|
|
|
): |
46
|
|
|
super().__init__(chembl_api, tax, traversal_strategy) |
47
|
|
|
self.allowed_target_types = allowed_target_types |
48
|
|
|
self.min_confidence_score = min_confidence_score |
49
|
|
|
|
50
|
|
|
def query(self, parent_form: ChemblCompound) -> Sequence[NestedDotDict]: |
|
|
|
|
51
|
|
|
return list(self.api.mechanism.filter(parent_molecule_chembl_id=parent_form.chid)) |
52
|
|
|
|
53
|
|
|
def should_include( |
|
|
|
|
54
|
|
|
self, lookup: str, compound: ChemblCompound, data: NestedDotDict, target: ChemblTargetGraph |
|
|
|
|
55
|
|
|
) -> bool: |
56
|
|
|
if target.type.name.lower() not in {s.lower() for s in self.allowed_target_types}: |
57
|
|
|
logger.warning(f"Excluding {target} with type {target.type}") |
|
|
|
|
58
|
|
|
return False |
59
|
|
|
return True |
60
|
|
|
|
61
|
|
|
def to_hit( |
|
|
|
|
62
|
|
|
self, lookup: str, compound: ChemblCompound, data: NestedDotDict, target: ChemblTargetGraph |
|
|
|
|
63
|
|
|
) -> Sequence[MechanismHit]: |
64
|
|
|
# these must match the constructor of the Hit, |
65
|
|
|
# EXCEPT for object_id and object_name, which come from traversal |
66
|
|
|
x = NestedDotDict( |
|
|
|
|
67
|
|
|
dict( |
68
|
|
|
record_id=data["mec_id"], |
69
|
|
|
compound_id=compound.chid, |
70
|
|
|
inchikey=compound.inchikey, |
71
|
|
|
compound_name=compound.name, |
72
|
|
|
compound_lookup=lookup, |
73
|
|
|
action_type=data["action_type"], |
74
|
|
|
direct_interaction=data["direct_interaction"], |
75
|
|
|
description=data["mechanism_of_action"], |
76
|
|
|
exact_target_id=data["target_chembl_id"], |
77
|
|
|
) |
78
|
|
|
) |
79
|
|
|
return [MechanismHit(**x, object_id=target.chembl, object_name=target.name)] |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
__all__ = ["MechanismHit", "MechanismSearch"] |
83
|
|
|
|