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, Type, Union |
8
|
|
|
|
9
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
10
|
|
|
|
11
|
|
|
from mandos.model import AbstractHit, Search |
12
|
|
|
from mandos.search.chembl.protein_search import ProteinHit, ProteinSearch |
13
|
|
|
|
14
|
|
|
logger = logging.getLogger("mandos") |
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(AbstractHit, metaclass=abc.ABCMeta): |
31
|
|
|
""" |
32
|
|
|
A mechanism entry for a compound. |
33
|
|
|
""" |
34
|
|
|
|
35
|
|
|
go_type: str |
36
|
|
|
protein_hit: ProteinHit |
37
|
|
|
|
38
|
|
|
@property |
39
|
|
|
def predicate(self) -> str: |
40
|
|
|
return f"has GO {self.go_type} term" |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
class GoSearch(Search[GoHit], metaclass=abc.ABCMeta): |
44
|
|
|
""" |
45
|
|
|
Search for GO terms. |
46
|
|
|
""" |
47
|
|
|
|
48
|
|
|
@property |
49
|
|
|
def go_type(self) -> GoType: |
|
|
|
|
50
|
|
|
raise NotImplementedError() |
51
|
|
|
|
52
|
|
|
@property |
53
|
|
|
def protein_search(self) -> Type[ProteinSearch]: |
|
|
|
|
54
|
|
|
raise NotImplementedError() |
55
|
|
|
|
56
|
|
|
def find(self, compound: str) -> Sequence[GoHit]: |
|
|
|
|
57
|
|
|
matches = self.protein_search(self.api, self.config, self.tax).find(compound) |
58
|
|
|
terms = [] |
59
|
|
|
for match in matches: |
60
|
|
|
target = self.api.target.get(match.object_id) |
61
|
|
|
terms.extend(self._process(match, target)) |
62
|
|
|
return terms |
63
|
|
|
|
64
|
|
|
def _process(self, match: ProteinHit, target: NestedDotDict) -> Sequence[GoHit]: |
65
|
|
|
terms = set() |
66
|
|
|
if target.get("target_components") is not None: |
67
|
|
|
for comp in target["target_components"]: |
68
|
|
|
if comp.get("target_component_xrefs") is not None: |
69
|
|
|
for xref in comp["target_component_xrefs"]: |
70
|
|
|
if xref["xref_src_db"] == f"Go{self.go_type.name.capitalize()}": |
71
|
|
|
terms.add((xref["xref_id"], xref["xref_name"])) |
72
|
|
|
hits = [] |
73
|
|
|
for xref_id, xref_name in terms: |
74
|
|
|
hits.append( |
75
|
|
|
GoHit( |
76
|
|
|
None, |
77
|
|
|
compound_id=match.compound_id, |
78
|
|
|
inchikey=match.inchikey, |
79
|
|
|
compound_lookup=match.compound_lookup, |
80
|
|
|
compound_name=match.compound_name, |
81
|
|
|
object_id=xref_id, |
82
|
|
|
object_name=xref_name, |
83
|
|
|
go_type=self.go_type.name, |
84
|
|
|
protein_hit=match, |
85
|
|
|
) |
86
|
|
|
) |
87
|
|
|
return hits |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
class GoSearchFactory: |
|
|
|
|
91
|
|
|
@classmethod |
92
|
|
|
def create( |
|
|
|
|
93
|
|
|
cls, go_type: Union[str, GoType], protein_search: Type[ProteinSearch] |
|
|
|
|
94
|
|
|
) -> Type[GoSearch]: |
95
|
|
|
go_type = GoType.of(go_type) |
96
|
|
|
|
97
|
|
|
class X(GoSearch): |
|
|
|
|
98
|
|
|
@property |
99
|
|
|
def go_type(self) -> GoType: |
100
|
|
|
return go_type |
101
|
|
|
|
102
|
|
|
@property |
103
|
|
|
def protein_search(self) -> Type[ProteinSearch]: |
104
|
|
|
return protein_search |
105
|
|
|
|
106
|
|
|
X.__name__ = f"Go{go_type.name.capitalize()}From{protein_search.search_name}Search" |
107
|
|
|
return X |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
__all__ = ["GoHit", "GoSearch", "GoSearchFactory", "GoType"] |
111
|
|
|
|