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