1
|
|
|
""" |
2
|
|
|
PubChem querying API. |
3
|
|
|
""" |
4
|
|
|
from __future__ import annotations |
5
|
|
|
|
6
|
|
|
import gzip |
7
|
|
|
from pathlib import Path |
8
|
|
|
from typing import FrozenSet, Optional, Union, Set |
9
|
|
|
|
10
|
|
|
import orjson |
|
|
|
|
11
|
|
|
import pandas as pd |
|
|
|
|
12
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
13
|
|
|
|
14
|
|
|
from mandos import logger |
15
|
|
|
from mandos.model.settings import MANDOS_SETTINGS |
16
|
|
|
from mandos.model.apis.pubchem_api import PubchemApi, PubchemCompoundLookupError |
17
|
|
|
from mandos.model.apis.pubchem_support.pubchem_data import PubchemData |
18
|
|
|
from mandos.model.apis.querying_pubchem_api import QueryingPubchemApi |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
class CachingPubchemApi(PubchemApi): |
|
|
|
|
22
|
|
|
def __init__( |
23
|
|
|
self, |
|
|
|
|
24
|
|
|
query: Optional[QueryingPubchemApi], |
|
|
|
|
25
|
|
|
cache_dir: Path = MANDOS_SETTINGS.pubchem_cache_path, |
|
|
|
|
26
|
|
|
): |
27
|
|
|
self._cache_dir = cache_dir |
28
|
|
|
self._query = query |
29
|
|
|
|
30
|
|
|
def follow_link(self, inchikey_or_cid: Union[int, str]) -> Optional[Path]: |
|
|
|
|
31
|
|
|
link = self.link_path(inchikey_or_cid) |
32
|
|
|
cid = link.read_text(encoding="utf8").strip() |
33
|
|
|
if len(cid) == 0: |
34
|
|
|
return None |
35
|
|
|
return self.data_path(int(cid)) |
36
|
|
|
|
37
|
|
|
def get_links(self, cid: int) -> Set[Path]: |
|
|
|
|
38
|
|
|
data = self._read_json(self.data_path(cid)) |
39
|
|
|
siblings = set(data.siblings) |
40
|
|
|
for sibling in siblings: |
41
|
|
|
sibling_path = self.follow_link(sibling) |
42
|
|
|
if sibling_path is not None: |
43
|
|
|
inchikey_siblings = self._read_json(sibling_path).siblings |
44
|
|
|
siblings.update(inchikey_siblings) |
45
|
|
|
links = {data.inchikey, data.cid, *siblings} |
46
|
|
|
return {self.link_path(link) for link in links} |
47
|
|
|
|
48
|
|
|
def fetch_data(self, inchikey_or_cid: Union[str, int]) -> Optional[PubchemData]: |
|
|
|
|
49
|
|
|
followed = self.follow_link(inchikey_or_cid) |
50
|
|
|
if followed is not None: |
51
|
|
|
logger.debug(f"Found cached PubChem data") |
|
|
|
|
52
|
|
|
return self._read_json(followed) |
53
|
|
|
return self._download(inchikey_or_cid) |
54
|
|
|
|
55
|
|
|
def _download(self, inchikey_or_cid: Union[int, str]) -> PubchemData: |
56
|
|
|
if self._query is None: |
57
|
|
|
raise PubchemCompoundLookupError(f"{inchikey_or_cid} not cached") |
58
|
|
|
data: PubchemData = self._query.fetch_data(inchikey_or_cid) |
59
|
|
|
cid = data.parent_or_self |
60
|
|
|
path = self.data_path(cid) |
61
|
|
|
self._write_json(data.to_json(), path) |
62
|
|
|
links = {inchikey_or_cid, *self.get_links(cid)} |
63
|
|
|
for link in links: |
64
|
|
|
if not link.exists(): |
65
|
|
|
link.write_text(str(cid), encoding="utf8") |
66
|
|
|
logger.debug(f"Wrote PubChem data to {path.absolute()}") |
67
|
|
|
return data |
68
|
|
|
|
69
|
|
|
def link_path(self, inchikey_or_cid: Union[int, str]) -> Path: |
|
|
|
|
70
|
|
|
return self._cache_dir / "links" / f"{inchikey_or_cid}.txt" |
71
|
|
|
|
72
|
|
|
def data_path(self, cid: int) -> Path: |
|
|
|
|
73
|
|
|
return self._cache_dir / "data" / f"{cid}.json.gz" |
74
|
|
|
|
75
|
|
|
def similarity_path(self, inchi: str, min_tc: float) -> Path: |
|
|
|
|
76
|
|
|
if not (min_tc * 100).is_integer(): |
77
|
|
|
raise ValueError(f"min_tc {min_tc} is not an increment of 1%") |
78
|
|
|
percent = int(min_tc * 100) |
79
|
|
|
path = self._cache_dir / "similarity" / f"{inchi}_{percent}" |
80
|
|
|
return path.with_suffix(MANDOS_SETTINGS.archive_filename_suffix) |
81
|
|
|
|
82
|
|
|
def _write_json(self, encoded: str, path: Path) -> None: |
|
|
|
|
83
|
|
|
path.parent.mkdir(parents=True, exist_ok=True) |
84
|
|
|
path.write_bytes(gzip.compress(encoded.encode(encoding="utf8"))) |
85
|
|
|
|
86
|
|
|
def _read_json(self, path: Path) -> Optional[PubchemData]: |
|
|
|
|
87
|
|
|
deflated = gzip.decompress(path.read_bytes()) |
88
|
|
|
read = orjson.loads(deflated) |
89
|
|
|
return PubchemData(NestedDotDict(read)) if len(read) > 0 else None |
90
|
|
|
|
91
|
|
|
def find_similar_compounds(self, inchi: str, min_tc: float) -> FrozenSet[int]: |
92
|
|
|
path = self.similarity_path(inchi, min_tc) |
93
|
|
|
if path.exists(): |
94
|
|
|
df = pd.read_file(path) |
|
|
|
|
95
|
|
|
return frozenset(set(df["cid"].values)) |
96
|
|
|
found = self._query.find_similar_compounds(inchi, min_tc) |
97
|
|
|
df = pd.DataFrame([pd.Series(dict(cid=cid)) for cid in found]) |
|
|
|
|
98
|
|
|
path.parent.mkdir(parents=True, exist_ok=True) |
99
|
|
|
df.write_file(path) |
100
|
|
|
return frozenset(set(df["cid"].values)) |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
__all__ = ["CachingPubchemApi"] |
104
|
|
|
|