|
1
|
|
|
import abc |
|
|
|
|
|
|
2
|
|
|
import dataclasses |
|
3
|
|
|
import logging |
|
4
|
|
|
import typing |
|
5
|
|
|
from typing import Generic, Sequence, TypeVar |
|
6
|
|
|
|
|
7
|
|
|
from mandos.model.hits import AbstractHit |
|
8
|
|
|
from mandos.model import CompoundNotFoundError |
|
9
|
|
|
|
|
10
|
|
|
logger = logging.getLogger("mandos") |
|
11
|
|
|
|
|
12
|
|
|
H = TypeVar("H", bound=AbstractHit, covariant=True) |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class Search(Generic[H], metaclass=abc.ABCMeta): |
|
16
|
|
|
""" |
|
17
|
|
|
Something to search and how to do it. |
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
|
|
@property |
|
21
|
|
|
def search_name(self) -> str: |
|
|
|
|
|
|
22
|
|
|
return self.__class__.__name__.lower().replace("search", "") |
|
23
|
|
|
|
|
24
|
|
|
def find_all(self, inchikeys: Sequence[str]) -> Sequence[H]: |
|
25
|
|
|
""" |
|
26
|
|
|
Loops over every compound and calls ``find``. |
|
27
|
|
|
Just comes with better logging. |
|
28
|
|
|
|
|
29
|
|
|
Args: |
|
30
|
|
|
inchikeys: |
|
31
|
|
|
|
|
32
|
|
|
Returns: |
|
33
|
|
|
|
|
34
|
|
|
""" |
|
35
|
|
|
lst = [] |
|
36
|
|
|
for i, compound in enumerate(inchikeys): |
|
37
|
|
|
try: |
|
38
|
|
|
x = self.find(compound) |
|
|
|
|
|
|
39
|
|
|
except CompoundNotFoundError: |
|
40
|
|
|
logger.error(f"Failed to find compound {compound}. Skipping.") |
|
|
|
|
|
|
41
|
|
|
continue |
|
42
|
|
|
lst.extend(x) |
|
43
|
|
|
logger.debug(f"Found {len(x)} {self.search_name} annotations for {compound}") |
|
|
|
|
|
|
44
|
|
|
if i > 0 and i % 20 == 0 or i == len(inchikeys) - 1: |
|
45
|
|
|
logger.info( |
|
|
|
|
|
|
46
|
|
|
f"Found {len(lst)} {self.search_name} annotations for {i} of {len(inchikeys)} compounds" |
|
|
|
|
|
|
47
|
|
|
) |
|
48
|
|
|
return lst |
|
49
|
|
|
|
|
50
|
|
|
def find(self, inchikey: str) -> Sequence[H]: |
|
51
|
|
|
""" |
|
52
|
|
|
To override. |
|
53
|
|
|
|
|
54
|
|
|
Args: |
|
55
|
|
|
inchikey: |
|
56
|
|
|
|
|
57
|
|
|
Returns: |
|
58
|
|
|
Something |
|
59
|
|
|
|
|
60
|
|
|
Raises: |
|
61
|
|
|
CompoundNotFoundError |
|
62
|
|
|
""" |
|
63
|
|
|
raise NotImplementedError() |
|
64
|
|
|
|
|
65
|
|
|
@classmethod |
|
66
|
|
|
def hit_fields(cls) -> Sequence[str]: |
|
67
|
|
|
""" |
|
68
|
|
|
Gets the fields in the Hit type parameter. |
|
69
|
|
|
|
|
70
|
|
|
Returns: |
|
71
|
|
|
|
|
72
|
|
|
""" |
|
73
|
|
|
# Okay, there's a lot of magic going on here |
|
74
|
|
|
# We need to access the _parameter_ H on cls -- raw `H` doesn't work |
|
75
|
|
|
# get_args and __orig_bases__ do this for us |
|
76
|
|
|
# then dataclasses.fields gives us the dataclass fields |
|
77
|
|
|
# there's also actual_h.__annotations__, but that doesn't include ClassVar and InitVar |
|
78
|
|
|
# (not that we're using those) |
|
79
|
|
|
# If this magic is too magical, we can make this an abstract method |
|
80
|
|
|
# But that would be a lot of excess code and it might be less modular |
|
81
|
|
|
actual_h = typing.get_args(cls.get_h())[0] |
|
|
|
|
|
|
82
|
|
|
return [f.name for f in dataclasses.fields(actual_h)] |
|
83
|
|
|
|
|
84
|
|
|
@classmethod |
|
85
|
|
|
def get_h(cls): |
|
86
|
|
|
""" |
|
87
|
|
|
What is my hit type? |
|
88
|
|
|
|
|
89
|
|
|
Returns: |
|
90
|
|
|
|
|
91
|
|
|
""" |
|
92
|
|
|
# noinspection PyUnresolvedReferences |
|
93
|
|
|
return cls.__orig_bases__[0] |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
__all__ = ["Search"] |
|
97
|
|
|
|