|
1
|
|
|
from __future__ import annotations |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
import abc |
|
4
|
|
|
import enum |
|
5
|
|
|
import logging |
|
|
|
|
|
|
6
|
|
|
from dataclasses import dataclass |
|
7
|
|
|
from typing import Sequence, Union |
|
8
|
|
|
|
|
9
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
from mandos import logger |
|
|
|
|
|
|
12
|
|
|
from mandos.model.chembl_api import ChemblApi |
|
13
|
|
|
from mandos.search.chembl import ChemblHit, ChemblSearch |
|
14
|
|
|
from mandos.search.chembl.binding_search import BindingSearch, BindingHit |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
class GoType(enum.Enum): |
|
|
|
|
|
|
18
|
|
|
component = enum.auto() |
|
19
|
|
|
function = enum.auto() |
|
20
|
|
|
process = enum.auto() |
|
21
|
|
|
|
|
22
|
|
|
@classmethod |
|
23
|
|
|
def of(cls, s: Union[str, GoType]) -> GoType: |
|
|
|
|
|
|
24
|
|
|
if isinstance(s, GoType): |
|
25
|
|
|
return s |
|
26
|
|
|
return GoType[s.lower()] |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
@dataclass(frozen=True, order=True, repr=True) |
|
30
|
|
|
class GoHit(ChemblHit, metaclass=abc.ABCMeta): |
|
31
|
|
|
""" |
|
32
|
|
|
A mechanism entry for a compound. |
|
33
|
|
|
""" |
|
34
|
|
|
|
|
35
|
|
|
go_type: str |
|
36
|
|
|
binding: BindingHit |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
class GoSearch(ChemblSearch[GoHit]): |
|
40
|
|
|
""" |
|
41
|
|
|
Search for GO terms. |
|
42
|
|
|
""" |
|
43
|
|
|
|
|
44
|
|
|
def __init__(self, key: str, api: ChemblApi, go_type: GoType, binding_search: BindingSearch): |
|
45
|
|
|
super().__init__(key, api) |
|
46
|
|
|
self.go_type = go_type |
|
47
|
|
|
self.binding_search = binding_search |
|
48
|
|
|
|
|
49
|
|
|
@property |
|
50
|
|
|
def data_source(self) -> str: |
|
|
|
|
|
|
51
|
|
|
return "ChEMBL :: GO terms" |
|
52
|
|
|
|
|
53
|
|
|
def find(self, compound: str) -> Sequence[GoHit]: |
|
|
|
|
|
|
54
|
|
|
matches = self.binding_search.find(compound) |
|
55
|
|
|
terms = [] |
|
56
|
|
|
for match in matches: |
|
57
|
|
|
target = self.api.target.get(match.object_id) |
|
58
|
|
|
terms.extend(self._process(compound, match, target)) |
|
59
|
|
|
return terms |
|
60
|
|
|
|
|
61
|
|
|
def _process(self, lookup: str, match: BindingHit, target: NestedDotDict) -> Sequence[GoHit]: |
|
62
|
|
|
terms = set() |
|
63
|
|
|
if target.get("target_components") is not None: |
|
64
|
|
|
for comp in target["target_components"]: |
|
65
|
|
|
if comp.get("target_component_xrefs") is not None: |
|
66
|
|
|
for xref in comp["target_component_xrefs"]: |
|
67
|
|
|
if xref["xref_src_db"] == f"Go{self.go_type.name.capitalize()}": |
|
68
|
|
|
terms.add((xref["xref_id"], xref["xref_name"])) |
|
69
|
|
|
hits = [] |
|
70
|
|
|
for xref_id, xref_name in terms: |
|
71
|
|
|
hits.append( |
|
72
|
|
|
GoHit( |
|
73
|
|
|
None, |
|
74
|
|
|
origin_inchikey=lookup, |
|
75
|
|
|
matched_inchikey=match.matched_inchikey, |
|
76
|
|
|
compound_id=match.compound_id, |
|
77
|
|
|
compound_name=match.compound_name, |
|
78
|
|
|
predicate=f"is associated with {self.go_type.name} term", |
|
79
|
|
|
object_id=xref_id, |
|
80
|
|
|
object_name=xref_name, |
|
81
|
|
|
search_key=self.key, |
|
82
|
|
|
search_class=self.search_class, |
|
83
|
|
|
data_source=self.data_source, |
|
84
|
|
|
go_type=self.go_type.name, |
|
85
|
|
|
binding=match, |
|
86
|
|
|
) |
|
87
|
|
|
) |
|
88
|
|
|
return hits |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
__all__ = ["GoHit", "GoSearch", "GoType"] |
|
92
|
|
|
|