1
|
|
|
import abc |
|
|
|
|
2
|
|
|
import logging |
3
|
|
|
from dataclasses import dataclass |
4
|
|
|
from typing import Sequence, TypeVar |
5
|
|
|
|
6
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
7
|
|
|
|
8
|
|
|
from mandos.model import AbstractHit, ChemblCompound, Search |
9
|
|
|
from mandos.model.targets import Target, TargetFactory |
10
|
|
|
from mandos.search.chembl.target_traversal_strategy import ( |
11
|
|
|
TargetTraversalStrategies, |
12
|
|
|
TargetTraversalStrategy, |
13
|
|
|
) |
14
|
|
|
|
15
|
|
|
logger = logging.getLogger("mandos") |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
@dataclass(frozen=True, order=True, repr=True) |
19
|
|
|
class ProteinHit(AbstractHit, metaclass=abc.ABCMeta): |
20
|
|
|
""" |
21
|
|
|
A protein target entry for a compound. |
22
|
|
|
""" |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
H = TypeVar("H", bound=ProteinHit, covariant=True) |
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class ProteinSearch(Search[H], metaclass=abc.ABCMeta): |
29
|
|
|
""" |
30
|
|
|
Abstract search. |
31
|
|
|
""" |
32
|
|
|
|
33
|
|
|
def find_all(self, compounds: Sequence[str]) -> Sequence[H]: |
|
|
|
|
34
|
|
|
logger.info( |
|
|
|
|
35
|
|
|
f"Using traversal strategy {self.traversal_strategy.__class__.__name__} for {self.search_name}" |
|
|
|
|
36
|
|
|
) |
37
|
|
|
return super().find_all(compounds) |
38
|
|
|
|
39
|
|
|
def query(self, parent_form: ChemblCompound) -> Sequence[NestedDotDict]: |
|
|
|
|
40
|
|
|
raise NotImplementedError() |
41
|
|
|
|
42
|
|
|
@property |
43
|
|
|
def traversal_strategy(self) -> TargetTraversalStrategy: |
|
|
|
|
44
|
|
|
if self.config.traversal_strategy is None: |
45
|
|
|
return self.default_traversal_strategy |
46
|
|
|
return TargetTraversalStrategies.by_name(self.config.traversal_strategy, self.api) |
47
|
|
|
|
48
|
|
|
@property |
49
|
|
|
def default_traversal_strategy(self) -> TargetTraversalStrategy: |
|
|
|
|
50
|
|
|
raise NotImplementedError() |
51
|
|
|
|
52
|
|
|
def should_include( |
53
|
|
|
self, lookup: str, compound: ChemblCompound, data: NestedDotDict, target: Target |
|
|
|
|
54
|
|
|
) -> bool: |
55
|
|
|
""" |
56
|
|
|
Filter based on the returned (activity/mechanism) data. |
57
|
|
|
IGNORE filters about the target itself, including whether it's a valid target. |
58
|
|
|
Return True in these cases (better yet, don't check). |
59
|
|
|
|
60
|
|
|
Args: |
61
|
|
|
lookup: |
62
|
|
|
compound: |
63
|
|
|
data: |
64
|
|
|
target: |
65
|
|
|
|
66
|
|
|
Returns: |
67
|
|
|
|
68
|
|
|
""" |
69
|
|
|
raise NotImplementedError() |
70
|
|
|
|
71
|
|
|
def to_hit( |
72
|
|
|
self, lookup: str, compound: ChemblCompound, data: NestedDotDict, best_target: Target |
|
|
|
|
73
|
|
|
) -> Sequence[H]: |
74
|
|
|
""" |
75
|
|
|
Gets the desired data as a NestedDotDict from the data from a single element |
76
|
|
|
returned by ``api_endpoint.filter``. |
77
|
|
|
This MUST MATCH the constructor, EXCEPT for object_id and object_name, |
78
|
|
|
which come from traversal and should be added by ``ProteinSearch.to_hit`` (parameter ``best_target``). |
|
|
|
|
79
|
|
|
|
80
|
|
|
Turns the final data into ``H``. |
81
|
|
|
Note that this has a default behavior but could be overridden to split into multiple hits |
82
|
|
|
and/or to add additional attributes that might come from ``best_target``. |
83
|
|
|
|
84
|
|
|
Args: |
85
|
|
|
lookup: |
86
|
|
|
compound: |
87
|
|
|
data: |
88
|
|
|
best_target: |
89
|
|
|
|
90
|
|
|
Returns: |
91
|
|
|
A sequence of hits. |
92
|
|
|
""" |
93
|
|
|
h = self.get_h() |
|
|
|
|
94
|
|
|
return [h(**data, object_id=best_target.chembl, object_name=best_target.name)] |
95
|
|
|
|
96
|
|
|
def find(self, lookup: str) -> Sequence[H]: |
97
|
|
|
""" |
98
|
|
|
|
99
|
|
|
Args: |
100
|
|
|
lookup: |
101
|
|
|
|
102
|
|
|
Returns:e |
103
|
|
|
|
104
|
|
|
""" |
105
|
|
|
form = self.get_compound(lookup) |
106
|
|
|
results = self.query(form) |
107
|
|
|
hits = [] |
108
|
|
|
for result in results: |
109
|
|
|
result = NestedDotDict(result) |
110
|
|
|
hits.extend(self.process(lookup, form, result)) |
111
|
|
|
return hits |
112
|
|
|
|
113
|
|
|
def process(self, lookup: str, compound: ChemblCompound, data: NestedDotDict) -> Sequence[H]: |
114
|
|
|
""" |
115
|
|
|
|
116
|
|
|
Args: |
117
|
|
|
lookup: |
118
|
|
|
compound: |
119
|
|
|
data: |
120
|
|
|
|
121
|
|
|
Returns: |
122
|
|
|
|
123
|
|
|
""" |
124
|
|
|
if data.get("target_chembl_id") is None: |
125
|
|
|
logger.debug(f"target_chembl_id missing from mechanism '{data}' for compound {lookup}") |
|
|
|
|
126
|
|
|
return [] |
127
|
|
|
chembl_id = data["target_chembl_id"] |
128
|
|
|
target_obj = TargetFactory.find(chembl_id, self.api) |
129
|
|
|
if not self.should_include(lookup, compound, data, target_obj): |
130
|
|
|
return [] |
131
|
|
|
# traverse() will return the source target if it's a non-traversable type (like DNA) |
132
|
|
|
# and the subclass decided whether to filter those |
133
|
|
|
# so don't worry about that here |
134
|
|
|
ancestors = self.traversal_strategy(target_obj) |
|
|
|
|
135
|
|
|
lst = [] |
136
|
|
|
for ancestor in ancestors: |
137
|
|
|
lst.extend(self.to_hit(lookup, compound, data, ancestor)) |
138
|
|
|
return lst |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
__all__ = ["ProteinHit", "ProteinSearch"] |
142
|
|
|
|