-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare_wikidata_lotus_data_prefect.py
288 lines (235 loc) · 9.3 KB
/
prepare_wikidata_lotus_data_prefect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import lotus_client
import pandas_utils
import rdkit_mol_identifiers
from meta_constants import MetaColumns
from metadata_cleanup_prefect import clean_structure_add_mol_id_columns_prefect
from pandas_utils import (
notnull,
isnull,
divide_chunks,
groupby_join_unique_values,
isnull_or_empty,
)
import pandas as pd
import numpy as np
from SPARQLWrapper import SPARQLWrapper, JSON
import sys
from rdkit_mol_identifiers import split_inchikey, clean_structure_add_mol_id_columns
wikidata_sparql_url = "https://query.wikidata.org/sparql"
from prefect import task, flow
def ensure_identifier(item, identifier: str = "wd"):
"""
:param item: may be url or item or already identified: http://www.wikidata.org/entity/Q193572 or Q193572
:param identifier:
:return: identifier:item e.g. wd:Q193572
"""
if isnull_or_empty(item):
return None
item = str(item)
prefix = identifier + ":"
if item.startswith("http"):
o = item.split("/")
if len(o) > 1:
return prefix + o[-1]
if item.startswith(prefix):
return item
return prefix + item
def _create_input_list(taxon_list, identifier: str = "wd") -> str:
items = [
ensure_identifier(item, identifier)
for item in taxon_list
if notnull(item) and len(str(item)) > 0
]
return " ".join(items)
def _get_lotus_compound_taxon_relations_sparql():
return """
#title: Which are the available referenced structure-organism pairs on Wikidata?
SELECT DISTINCT ?structure ?inchikey ?taxon ?reference WHERE {
?structure wdt:P235 ?inchikey; # get the inchikey
p:P703[ # statement found in taxon
ps:P703 ?taxon; # get the taxon
(prov:wasDerivedFrom/pr:P248) ?reference ]. # get the reference
}
"""
def _get_reference_info_sparql(references):
input_list = _create_input_list(references, "wd")
return """
#title: get NCBI ids from input taxon and parent
SELECT DISTINCT ?reference ?doi
WHERE {
VALUES ?reference {
# wd:Q30046 # example
PLACEHOLDER
}
OPTIONAL { ?reference wdt:P356 ?doi. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
""".replace(
"PLACEHOLDER", input_list
)
def _get_structure_info_sparql(structures):
input_list = _create_input_list(structures, "wd")
return """
#title: get NCBI ids from input taxon and parent
SELECT DISTINCT ?structure ?structureLabel ?inchikey ?inchi ?isomeric_smiles ?canonical_smiles ?formula
WHERE {
VALUES ?structure {
# wd:Q30046 # example
PLACEHOLDER
}
OPTIONAL { ?structure wdt:P235 ?inchikey. }
OPTIONAL { ?structure wdt:P2017 ?isomeric_smiles .}
OPTIONAL { ?structure wdt:P233 ?canonical_smiles .}
OPTIONAL { ?structure wdt:P274 ?formula .}
OPTIONAL { ?structure wdt:P234 ?inchi .}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
""".replace(
"PLACEHOLDER", input_list
)
def _get_parent_taxon_ncbi_sparql(taxon_list):
input_list = _create_input_list(taxon_list, "wd")
return """
#title: get NCBI ids from input taxon and parent
SELECT DISTINCT ?taxon ?taxon_name ?taxon_rankLabel ?ncbi_id ?parent_taxon ?parent_taxon_name ?parent_taxon_rankLabel ?parent_ncbi_id
WHERE {
VALUES ?taxon {
# wd:Q30046 # example
PLACEHOLDER
}
OPTIONAL { ?taxon wdt:P225 ?taxon_name. }
OPTIONAL { ?taxon wdt:P685 ?ncbi_id. }
OPTIONAL { ?taxon wdt:P685 ?taxon_rank. }
?taxon wdt:P171 ?parent_taxon.
?parent_taxon wdt:P225 ?parent_taxon_name.
OPTIONAL { ?parent_taxon wdt:P105 ?parent_taxon_rank. }
OPTIONAL { ?parent_taxon wdt:P685 ?parent_ncbi_id. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
""".replace(
"PLACEHOLDER", input_list
)
def get_sparql_json_results(
sparql_query: str, endpoint_url: str = "https://query.wikidata.org/sparql"
):
user_agent = "WDQS-example Python/%s.%s" % (
sys.version_info[0],
sys.version_info[1],
)
# TODO adjust user agent; see https://w.wiki/CX6
sparql = SPARQLWrapper(endpoint_url, agent=user_agent)
sparql.setQuery(sparql_query)
sparql.setReturnFormat(JSON)
return sparql.queryAndConvert()
def extract_values(result):
return {key: result[key]["value"] for key in result}
def load_as_dataframe(
sparql_query: str, endpoint_url: str = "https://query.wikidata.org/sparql"
) -> pd.DataFrame:
results = get_sparql_json_results(sparql_query, endpoint_url)["results"]["bindings"]
new_results = [extract_values(result) for result in results]
df = pd.DataFrame(new_results)
label_cols = [col for col in df.columns if col.endswith("Label")]
df = df.rename(columns={old: old[:-5] + "_label" for old in label_cols})
return df
@task(name="Extract lotus from wikidata")
def extract_lotus_compound_taxon_relations():
query = _get_lotus_compound_taxon_relations_sparql()
df = load_as_dataframe(query, wikidata_sparql_url)
df["split_inchikey"] = [split_inchikey(inchikey) for inchikey in df["inchikey"]]
df = df.sort_values(["inchikey"])
return df
@task(name="parent taxon")
def query_dataframe_parent_taxon_ncbi(taxon_list):
chunks = divide_chunks(drop_duplicates(taxon_list), 250)
queries = [_get_parent_taxon_ncbi_sparql(chunk) for chunk in chunks]
dfs = [load_as_dataframe(query) for query in queries]
return pd.concat(dfs, sort=False)
@task(name="structure info")
def query_dataframe_structure_info(structures):
chunks = divide_chunks(drop_duplicates(structures), 250)
queries = [_get_structure_info_sparql(chunk) for chunk in chunks]
dfs = [load_as_dataframe(query) for query in queries]
df = pd.concat(dfs, sort=False).sort_values(["inchikey"])
df["split_inchikey"] = [split_inchikey(inchikey) for inchikey in df["inchikey"]]
return df
@task(name="reference info")
def query_dataframe_reference_info(references):
chunks = divide_chunks(drop_duplicates(references), 250)
queries = [_get_reference_info_sparql(chunk) for chunk in chunks]
dfs = [load_as_dataframe(query) for query in queries]
return pd.concat(dfs, sort=False)
def drop_duplicates(input_list):
return list(set(input_list))
def query_dataframe_lotus_compound_taxon_relations():
query = _get_lotus_compound_taxon_relations_sparql()
df = load_as_dataframe(query, wikidata_sparql_url)
# add more information to the taxon, structure, and reference
df = (
df.merge(query_dataframe_parent_taxon_ncbi(df["taxon"]), on="taxon", how="left")
.sort_values(["parent_ncbi_id"])
.drop_duplicates(["taxon", "structure", "reference"])
)
df = (
df.merge(
query_dataframe_reference_info(df["reference"]), on="reference", how="left"
)
.sort_values(["doi"])
.drop_duplicates(["taxon", "structure", "reference"])
)
structures_df = query_dataframe_structure_info(df["structure"]).drop(
columns=["inchikey"]
)
df = (
df.merge(structures_df, on="structure", how="left")
.sort_values(["inchikey"])
.drop_duplicates(["taxon", "structure", "reference"])
)
return df
@task(name="save lotus dump")
def save_lotus_dump(df):
pandas_utils.save_dataframe(df, "data/lotus_download.parquet")
pandas_utils.save_dataframe(df, "data/lotus_download.csv")
@task(name="Create unique inchikey entries")
def save_unique_inchikey_dump(df):
df = groupby_join_unique_values(df, columns=["split_inchikey"], as_lists=False)
pandas_utils.save_dataframe(df, "data/lotus_unique_split_inchikey_download.parquet")
pandas_utils.save_dataframe(df, "data/lotus_unique_split_inchikey_download.csv")
@flow(name="download lotus")
def download_lotus_prefect():
df = extract_lotus_compound_taxon_relations()
# add more information to the taxon, structure, and reference
parents = query_dataframe_parent_taxon_ncbi.submit(df["taxon"])
references = query_dataframe_reference_info.submit(df["reference"])
structures_df = query_dataframe_structure_info.submit(df["structure"])
df = (
df.merge(parents.result(), on="taxon", how="left")
.sort_values(["parent_ncbi_id"])
.drop_duplicates(["taxon", "structure", "reference"])
)
df = (
df.merge(references.result(), on="reference", how="left")
.sort_values(["doi"])
.drop_duplicates(["taxon", "structure", "reference"])
)
# inchikey is already present from df
structures_df = structures_df.result().drop(columns=["inchikey", "split_inchikey"])
df = (
df.merge(structures_df, on="structure", how="left")
.sort_values(["inchikey"])
.drop_duplicates(["taxon", "structure", "reference"])
)
# clean strucutres and drop not needed columns
excluded_cols = [
MetaColumns.smiles,
MetaColumns.canonical_smiles,
MetaColumns.isomeric_smiles,
MetaColumns.inchi,
]
columns = [col for col in df.columns if col not in excluded_cols]
df = clean_structure_add_mol_id_columns_prefect(structures_df)[columns]
save_lotus_dump(df)
save_unique_inchikey_dump(df)
return df
if __name__ == "__main__":
download_lotus_prefect()