|
1
|
|
|
from __future__ import annotations |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
import abc |
|
4
|
|
|
import typing |
|
5
|
|
|
from dataclasses import dataclass |
|
6
|
|
|
from pathlib import Path |
|
7
|
|
|
from typing import Optional, TypeVar, Union |
|
8
|
|
|
|
|
9
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
|
|
10
|
|
|
from pocketutils.core.hashers import Hasher |
|
|
|
|
|
|
11
|
|
|
from pocketutils.tools.common_tools import CommonTools |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
from mandos.model.settings import MANDOS_SETTINGS |
|
14
|
|
|
from mandos.model.utils import MiscUtils |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
class Api(metaclass=abc.ABCMeta): |
|
|
|
|
|
|
18
|
|
|
""" """ |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
class CompoundNotFoundError(LookupError): |
|
|
|
|
|
|
22
|
|
|
""" """ |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class MultipleMatchesError(ValueError): |
|
|
|
|
|
|
26
|
|
|
""" """ |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
T = TypeVar("T", covariant=True) |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
@dataclass(frozen=True, repr=True, order=True) |
|
33
|
|
|
class CompoundStruct: |
|
34
|
|
|
""" |
|
35
|
|
|
Uniform data view for ChEMBL, PubChem, etc. |
|
36
|
|
|
Contains the source db (e.g. "pubchem"), the ID as a str, the inchi, and the inchikey. |
|
37
|
|
|
""" |
|
38
|
|
|
|
|
39
|
|
|
db: str |
|
|
|
|
|
|
40
|
|
|
id: str |
|
|
|
|
|
|
41
|
|
|
inchi: str |
|
42
|
|
|
inchikey: str |
|
43
|
|
|
|
|
44
|
|
|
@property |
|
45
|
|
|
def simple_str(self) -> str: |
|
|
|
|
|
|
46
|
|
|
db = "" if self.db.lower() == "chembl" else self.db + " " |
|
|
|
|
|
|
47
|
|
|
return f"[{db}{self.id} : {self.inchikey}]" |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
class MandosResources: |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
start_time = MiscUtils.utc() |
|
53
|
|
|
start_time_local = start_time.astimezone() |
|
54
|
|
|
start_timestamp = start_time.isoformat(timespec="milliseconds") |
|
55
|
|
|
start_timestamp_filesys = start_time_local.strftime("%Y-%m-%d_%H-%M-%S") |
|
56
|
|
|
hasher: Hasher = Hasher("sha256", buffer_size=16 * 1024) |
|
57
|
|
|
|
|
58
|
|
|
@classmethod |
|
59
|
|
|
def contains(cls, *nodes: Union[Path, str], suffix: Optional[str] = None) -> bool: |
|
60
|
|
|
"""Returns whether a resource file (or dir) exists.""" |
|
61
|
|
|
return cls.path(*nodes, suffix=suffix).exists() |
|
62
|
|
|
|
|
63
|
|
|
@classmethod |
|
64
|
|
|
def path(cls, *nodes: Union[Path, str], suffix: Optional[str] = None) -> Path: |
|
65
|
|
|
"""Gets a path of a test resource file under ``resources/``.""" |
|
66
|
|
|
path = Path(Path(__file__).parent.parent, "resources", *nodes) |
|
67
|
|
|
return path.with_suffix(path.suffix if suffix is None else suffix) |
|
68
|
|
|
|
|
69
|
|
|
@classmethod |
|
70
|
|
|
def a_path(cls, *nodes: Union[Path, str], suffixes: Optional[typing.Set[str]] = None) -> Path: |
|
71
|
|
|
"""Gets a path of a test resource file under ``resources/``, ignoring suffix.""" |
|
72
|
|
|
path = Path(Path(__file__).parent.parent, "resources", *nodes) |
|
73
|
|
|
return CommonTools.only( |
|
74
|
|
|
[ |
|
75
|
|
|
p |
|
76
|
|
|
for p in path.parent.glob(path.stem + "*") |
|
77
|
|
|
if p.is_file() and (suffixes is None or p.suffix in suffixes) |
|
78
|
|
|
] |
|
79
|
|
|
) |
|
80
|
|
|
|
|
81
|
|
|
@classmethod |
|
82
|
|
|
def json(cls, *nodes: Union[Path, str], suffix: Optional[str] = None) -> NestedDotDict: |
|
83
|
|
|
"""Reads a JSON file under ``resources/``.""" |
|
84
|
|
|
return NestedDotDict.read_json(cls.path(*nodes, suffix=suffix)) |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
__all__ = [ |
|
88
|
|
|
"Api", |
|
89
|
|
|
"CompoundStruct", |
|
90
|
|
|
"CompoundNotFoundError", |
|
91
|
|
|
"MultipleMatchesError", |
|
92
|
|
|
"MandosResources", |
|
93
|
|
|
] |
|
94
|
|
|
|