|
1
|
|
|
""" |
|
2
|
|
|
API that web-scrapes ChEMBL. |
|
3
|
|
|
""" |
|
4
|
|
|
from __future__ import annotations |
|
5
|
|
|
|
|
6
|
|
|
import abc |
|
7
|
|
|
import enum |
|
8
|
|
|
from functools import cached_property |
|
|
|
|
|
|
9
|
|
|
from pathlib import Path |
|
10
|
|
|
from typing import Optional, Type |
|
11
|
|
|
|
|
12
|
|
|
import pandas as pd |
|
|
|
|
|
|
13
|
|
|
from pocketutils.core.enums import CleverEnum |
|
|
|
|
|
|
14
|
|
|
from pocketutils.core.query_utils import QueryExecutor |
|
|
|
|
|
|
15
|
|
|
from typeddfs import TypedDf, TypedDfs |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
from mandos.model import Api |
|
18
|
|
|
from mandos.model.settings import QUERY_EXECUTORS, SETTINGS |
|
19
|
|
|
from mandos.model.utils.setup import logger |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
class SarPredictionResult(CleverEnum): |
|
|
|
|
|
|
23
|
|
|
active = enum.auto() |
|
24
|
|
|
inactive = enum.auto() |
|
25
|
|
|
empty = enum.auto() |
|
26
|
|
|
both = enum.auto() |
|
27
|
|
|
|
|
28
|
|
|
@property |
|
29
|
|
|
def yes_no_mixed(self) -> str: |
|
|
|
|
|
|
30
|
|
|
return { |
|
31
|
|
|
SarPredictionResult.active: "yes", |
|
32
|
|
|
SarPredictionResult.inactive: "no", |
|
33
|
|
|
SarPredictionResult.empty: "mixed", |
|
34
|
|
|
SarPredictionResult.both: "mixed", |
|
35
|
|
|
}[self] |
|
36
|
|
|
|
|
37
|
|
|
@property |
|
38
|
|
|
def score(self) -> int: |
|
|
|
|
|
|
39
|
|
|
return { |
|
40
|
|
|
SarPredictionResult.active: 1, |
|
41
|
|
|
SarPredictionResult.inactive: -1, |
|
42
|
|
|
SarPredictionResult.empty: 0, |
|
43
|
|
|
SarPredictionResult.both: 0, |
|
44
|
|
|
}[self] |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
class ChemblScrapeTable(TypedDf, metaclass=abc.ABCMeta): |
|
|
|
|
|
|
48
|
|
|
"""""" |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
def _parse_conf(df: pd.DataFrame): |
|
|
|
|
|
|
52
|
|
|
df = df.copy() |
|
53
|
|
|
for t in [70, 80, 90]: |
|
|
|
|
|
|
54
|
|
|
df[f"confidence_{t}"] = df[f"confidence_{t}"].map(SarPredictionResult.of) |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
ChemblTargetPredictionTable = ( |
|
58
|
|
|
TypedDfs.typed("ChemblTargetPredictionTable") |
|
59
|
|
|
.subclass(ChemblScrapeTable) |
|
60
|
|
|
.require("target_chembl_id", "target_pref_name", "target_organism", dtype=str) |
|
61
|
|
|
.require("confidence_70", "confidence_80", "confidence_90", dtype=SarPredictionResult) |
|
62
|
|
|
.require("activity_threshold", dtype=float) |
|
63
|
|
|
.post(_parse_conf) |
|
64
|
|
|
.strict() |
|
65
|
|
|
.secure() |
|
66
|
|
|
.hash(directory=True) |
|
67
|
|
|
).build() |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
class ChemblScrapePage(CleverEnum): |
|
|
|
|
|
|
71
|
|
|
target_predictions = enum.auto() |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
class ChemblScrapeApi(Api, metaclass=abc.ABCMeta): |
|
|
|
|
|
|
75
|
|
|
def fetch_predictions(self, cid: str) -> ChemblTargetPredictionTable: |
|
|
|
|
|
|
76
|
|
|
return self._fetch_page( |
|
77
|
|
|
cid, ChemblScrapePage.target_predictions, ChemblTargetPredictionTable |
|
78
|
|
|
) |
|
79
|
|
|
|
|
80
|
|
|
def _fetch_page(self, cid: str, page: ChemblScrapePage, table_type: Type[ChemblScrapeTable]): |
|
81
|
|
|
raise NotImplementedError() |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
class QueryingChemblScrapeApi(ChemblScrapeApi): |
|
|
|
|
|
|
85
|
|
|
def __init__(self, executor: QueryExecutor = QUERY_EXECUTORS.chembl): |
|
86
|
|
|
self._executor = executor |
|
87
|
|
|
|
|
88
|
|
|
@property |
|
89
|
|
|
def scraper(self): |
|
|
|
|
|
|
90
|
|
|
return self.Scraper.create(self._executor) |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
@cached_property |
|
93
|
|
|
def By(self): |
|
|
|
|
|
|
94
|
|
|
from mandos.model.utils.scrape import By |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
return By |
|
97
|
|
|
|
|
98
|
|
|
@cached_property |
|
99
|
|
|
def Scraper(self): |
|
|
|
|
|
|
100
|
|
|
from mandos.model.utils.scrape import Scraper |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
return Scraper |
|
103
|
|
|
|
|
104
|
|
|
def _fetch_page( |
|
|
|
|
|
|
105
|
|
|
self, chembl_id: str, page: ChemblScrapePage, table_type: Type[ChemblScrapeTable] |
|
|
|
|
|
|
106
|
|
|
): |
|
107
|
|
|
url = f"https://www.ebi.ac.uk/chembl/embed/#compound_report_card/{chembl_id}/{page}" |
|
108
|
|
|
scraper = self.scraper |
|
109
|
|
|
scraper.go(url) |
|
110
|
|
|
rows = [] |
|
111
|
|
|
i = 2 |
|
112
|
|
|
while True: |
|
113
|
|
|
table = scraper.find_element("table", self.By.TAG_NAME) |
|
|
|
|
|
|
114
|
|
|
for tr in table.find_elements("tr"): |
|
|
|
|
|
|
115
|
|
|
rows += [td.text.strip() for td in tr.find_elements("td")] |
|
116
|
|
|
# noinspection PyBroadException |
|
117
|
|
|
try: |
|
118
|
|
|
scraper.find_elements(str(i), self.By.LINK_TEXT) |
|
|
|
|
|
|
119
|
|
|
except Exception: |
|
|
|
|
|
|
120
|
|
|
break |
|
121
|
|
|
i += 1 |
|
122
|
|
|
header = rows[0] |
|
123
|
|
|
rows = rows[1:] |
|
124
|
|
|
return table_type.of(pd.DataFrame(rows, columns=header)) |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
class CachingChemblScrapeApi(ChemblScrapeApi): |
|
|
|
|
|
|
128
|
|
|
def __init__( |
|
129
|
|
|
self, |
|
|
|
|
|
|
130
|
|
|
query: Optional[QueryingChemblScrapeApi], |
|
|
|
|
|
|
131
|
|
|
cache_dir: Path = SETTINGS.chembl_cache_path, |
|
|
|
|
|
|
132
|
|
|
): |
|
133
|
|
|
self._cache_dir = cache_dir |
|
134
|
|
|
self._query = query |
|
135
|
|
|
|
|
136
|
|
|
def _fetch_page(self, cid: str, page: ChemblScrapePage, table_type: Type[ChemblScrapeTable]): |
|
137
|
|
|
path = self.path(cid, page) |
|
138
|
|
|
if path.exists(): |
|
|
|
|
|
|
139
|
|
|
return ChemblScrapeTable.read_file(path) |
|
140
|
|
|
elif self._query is None: |
|
141
|
|
|
return ChemblScrapeTable.new_empty() |
|
142
|
|
|
data: TypedDf = self._query._fetch_page(cid, page, table_type) |
|
|
|
|
|
|
143
|
|
|
data.write_file(path.resolve(), mkdirs=True) |
|
144
|
|
|
logger.debug(f"Scraped page {page} for {cid} with {len(data):,} rows") |
|
145
|
|
|
return data |
|
146
|
|
|
|
|
147
|
|
|
def path(self, cid: str, page: ChemblScrapePage): |
|
|
|
|
|
|
148
|
|
|
return (self._cache_dir / page.name / cid).with_suffix(SETTINGS.archive_filename_suffix) |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
__all__ = [ |
|
152
|
|
|
"ChemblScrapeApi", |
|
153
|
|
|
"ChemblScrapePage", |
|
154
|
|
|
"ChemblScrapePage", |
|
155
|
|
|
"ChemblTargetPredictionTable", |
|
156
|
|
|
"QueryingChemblScrapeApi", |
|
157
|
|
|
"CachingChemblScrapeApi", |
|
158
|
|
|
] |
|
159
|
|
|
|