1
|
|
|
""" |
2
|
|
|
Run searches and write files. |
3
|
|
|
""" |
4
|
|
|
|
5
|
|
|
from __future__ import annotations |
6
|
|
|
|
7
|
|
|
from dataclasses import dataclass |
8
|
|
|
from pathlib import Path |
9
|
|
|
from typing import Callable, Optional, Sequence |
10
|
|
|
|
11
|
|
|
import pandas as pd |
|
|
|
|
12
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
13
|
|
|
from pocketutils.tools.common_tools import CommonTools |
|
|
|
|
14
|
|
|
from typeddfs import TypedDfs |
|
|
|
|
15
|
|
|
|
16
|
|
|
from mandos import logger |
17
|
|
|
from mandos.entries.api_singletons import Apis |
18
|
|
|
from mandos.entries.paths import EntryPaths |
19
|
|
|
from mandos.model import CompoundNotFoundError |
20
|
|
|
from mandos.model.apis.chembl_support.chembl_utils import ChemblUtils |
21
|
|
|
from mandos.model.searches import Search |
22
|
|
|
from mandos.search.chembl import ChemblSearch |
23
|
|
|
from mandos.search.pubchem import PubchemSearch |
24
|
|
|
|
25
|
|
|
InputFrame = (TypedDfs.typed("InputFrame").require("inchikey")).build() |
26
|
|
|
|
27
|
|
|
IdMatchFrame = ( |
28
|
|
|
TypedDfs.typed("IdMatchFrame") |
29
|
|
|
.require("inchikey", dtype=str) |
30
|
|
|
.reserve("chembl_id", "pubchem_id", "hmdb_id", dtype=str) |
31
|
|
|
.strict() |
32
|
|
|
).build() |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
@dataclass(frozen=True, repr=True) |
|
|
|
|
36
|
|
|
class ChemFinder: |
37
|
|
|
what: str |
38
|
|
|
how: Callable[[str], str] |
39
|
|
|
complain: bool = False |
40
|
|
|
|
41
|
|
|
@classmethod |
42
|
|
|
def chembl(cls, complain: bool = False) -> ChemFinder: |
|
|
|
|
43
|
|
|
def how(inchikey: str) -> str: |
44
|
|
|
return ChemblUtils(Apis.Chembl).get_compound(inchikey).chid |
45
|
|
|
|
46
|
|
|
return ChemFinder("ChEMBL", how, complain=complain) |
47
|
|
|
|
48
|
|
|
@classmethod |
49
|
|
|
def pubchem(cls, complain: bool = False) -> ChemFinder: |
|
|
|
|
50
|
|
|
def how(inchikey: str) -> str: |
51
|
|
|
return ChemblUtils(Apis.Chembl).get_compound(inchikey).chid |
52
|
|
|
|
53
|
|
|
return ChemFinder("PubChem", how, complain=complain) |
54
|
|
|
|
55
|
|
|
def find(self, inchikey: str) -> Optional[str]: |
|
|
|
|
56
|
|
|
try: |
57
|
|
|
return self.how(inchikey) |
58
|
|
|
except CompoundNotFoundError: |
59
|
|
|
if self.complain: |
60
|
|
|
logger.info(f"NOT FOUND: {self.what.rjust(8)} ] {inchikey}") |
61
|
|
|
logger.debug(f"Did not find {self.what} {inchikey}", exc_info=True) |
62
|
|
|
return None |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
class SearcherUtils: |
|
|
|
|
66
|
|
|
@classmethod |
67
|
|
|
def dl( |
|
|
|
|
68
|
|
|
cls, |
|
|
|
|
69
|
|
|
inchikeys: Sequence[str], |
|
|
|
|
70
|
|
|
pubchem: bool = True, |
|
|
|
|
71
|
|
|
chembl: bool = True, |
|
|
|
|
72
|
|
|
hmdb: bool = True, |
|
|
|
|
73
|
|
|
complain: bool = False, |
|
|
|
|
74
|
|
|
) -> IdMatchFrame: |
75
|
|
|
df = IdMatchFrame([pd.Series(dict(inchikey=c)) for c in inchikeys]) |
|
|
|
|
76
|
|
|
if chembl: |
77
|
|
|
df["chembl_id"] = df["inchikey"].map(ChemFinder.chembl(complain=complain).find) |
78
|
|
|
if pubchem: |
79
|
|
|
df["pubchem_id"] = df["inchikey"].map(ChemFinder.pubchem(complain=complain).find) |
80
|
|
|
return df |
81
|
|
|
|
82
|
|
|
@classmethod |
83
|
|
|
def read(cls, input_path: Path) -> InputFrame: |
|
|
|
|
84
|
|
|
df = InputFrame.read_file(input_path) |
|
|
|
|
85
|
|
|
logger.info(f"Read {len(df)} input compounds") |
86
|
|
|
return df |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
class Searcher: |
90
|
|
|
""" |
91
|
|
|
Executes one or more searches and saves the results to CSV files. |
92
|
|
|
Create and use once. |
93
|
|
|
""" |
94
|
|
|
|
95
|
|
|
def __init__(self, searches: Sequence[Search], to: Sequence[Path], input_path: Path): |
96
|
|
|
""" |
97
|
|
|
Constructor. |
98
|
|
|
|
99
|
|
|
Args: |
100
|
|
|
searches: |
101
|
|
|
input_path: Path to the input file of one of the formats: |
102
|
|
|
- .txt containing one InChI Key per line |
103
|
|
|
- .csv, .tsv, .tab, csv.gz, .tsv.gz, .tab.gz, or .feather containing a column called inchikey |
|
|
|
|
104
|
|
|
""" |
105
|
|
|
self.what = searches |
106
|
|
|
self.input_path: Optional[Path] = input_path |
107
|
|
|
self.input_df: InputFrame = None |
108
|
|
|
self.output_paths = { |
109
|
|
|
what.key: EntryPaths.output_path_of(what, input_path, path) |
110
|
|
|
for what, path in CommonTools.zip_list(searches, to) |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
def search(self) -> Searcher: |
114
|
|
|
""" |
115
|
|
|
Performs the search, and writes data. |
116
|
|
|
""" |
117
|
|
|
if self.input_df is not None: |
118
|
|
|
raise ValueError(f"Already ran a search") |
|
|
|
|
119
|
|
|
self.input_df = SearcherUtils.read(self.input_path) |
120
|
|
|
inchikeys = self.input_df["inchikey"].unique() |
121
|
|
|
has_pubchem = any((isinstance(what, PubchemSearch) for what in self.what)) |
|
|
|
|
122
|
|
|
has_chembl = any((isinstance(what, ChemblSearch) for what in self.what)) |
123
|
|
|
# find the compounds first so the user knows what's missing before proceeding |
124
|
|
|
SearcherUtils.dl(inchikeys, pubchem=has_pubchem, chembl=has_chembl) |
125
|
|
|
for what in self.what: |
126
|
|
|
output_path = self.output_paths[what.key] |
127
|
|
|
metadata_path = output_path.with_suffix(".metadata.json") |
128
|
|
|
df = what.find_to_df(inchikeys) |
|
|
|
|
129
|
|
|
# TODO keep any other columns in input_df |
|
|
|
|
130
|
|
|
df.to_csv(output_path) |
131
|
|
|
params = {k: str(v) for k, v in what.get_params().items() if k not in {"key", "api"}} |
132
|
|
|
metadata = NestedDotDict(dict(key=what.key, search=what.search_class, params=params)) |
133
|
|
|
metadata.write_json(metadata_path) |
134
|
|
|
logger.info(f"Wrote {what.key} to {output_path}") |
135
|
|
|
return self |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
__all__ = ["Searcher", "IdMatchFrame", "SearcherUtils"] |
139
|
|
|
|