forked from beasleyjonm/AOP-COP-Path-Extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPubMedSearch.py
199 lines (184 loc) · 11.8 KB
/
PubMedSearch.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
import pandas as pd
import requests as rq
import xml.etree.cElementTree as ElementTree
import time
def PubMedCoMentions(dff,selected_columns,expand=True):
expand = True
number = len(selected_columns)
two_term_dict = dict()
three_term_dict = dict()
comention_counts_1_2 = list()
comention_counts_1_2_link = list()
comention_counts_1_3 = list()
comention_counts_1_3_link = list()
comention_counts_2_3 = list()
comention_counts_2_3_link = list()
comention_counts_1_2_3 = list()
comention_counts_1_2_3_link = list()
URL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
if number not in [2,3]:
ammended_answers = dff.to_dict('records')
ammended_columns = [{"name": i.replace("`","").replace("biolink:",""), "id": i, "hideable":True, "selectable": False, "presentation":"markdown"} if " link" in i else {"name": i.replace("`","").replace("biolink:",""), "id": i, "hideable": True, "selectable": [True if "node" in i and " counts" not in i else False]} for i in dff.columns]
hidden_columns=[i for i in dff.columns if " link" in i]+[i for i in dff.columns if "esnd" in i]+[i for i in dff.columns if "MetaData" in i]
message = "Please select 2 or 3 node columns for PubMed search."
return (ammended_answers, ammended_columns, hidden_columns, message)
print("Running PubMed Check")
if number == 2:
print('number=2')
Term1=selected_columns[0].replace('`','').replace('biolink:','')
Term2=selected_columns[1].replace('`','').replace('biolink:','')
if f"{Term1}-{Term2} counts" or f"{Term1}-{Term2} counts" not in dff.columns:
term1_list=dff[selected_columns[0]].tolist()
term2_list=dff[selected_columns[1]].tolist()
for (term1, term2) in zip(term1_list, term2_list):
key=f"{term1}:{term2}"
if key not in two_term_dict.keys():
if(expand):
two_term = f'{term1} AND {term2}'
else:
two_term = f'"{term1}"[All Fields] AND "{term2}"[All Fields]'
PARAMS = {'db':'pubmed','term':two_term,'retmax':'0','api_key':'0595c1cc493e78f5a76d62b9f0cdc845e309'}
time.sleep(0.1)
r = rq.get(url = URL, params = PARAMS)
if(r.status_code != rq.codes.ok):
time.sleep(1.0)
r = rq.get(url = URL, params = PARAMS)
tree = ElementTree.fromstring(r.text)
cnt = int(tree.find("Count").text)
#print(f"{term1}-{term2}:{cnt}")
two_term_dict[key] = cnt
else:
cnt = two_term_dict[key]
comention_counts_1_2.append(cnt)
comention_counts_1_2_link.append(f"{str(cnt)} <a href='https://pubmed.ncbi.nlm.nih.gov/?term={term1} AND {term2}' target='_blank' rel='noopener noreferrer'>[Link]</a>")
dff.insert(0, f"{Term1}-{Term2} counts", comention_counts_1_2)
dff.insert(0, f"{Term1}-{Term2} link", comention_counts_1_2_link)
elif number == 3:
#print('number=3')
Term1=selected_columns[0].replace('`','').replace('biolink:','')
Term2=selected_columns[1].replace('`','').replace('biolink:','')
Term3=selected_columns[2].replace('`','').replace('biolink:','')
term1_list=dff[selected_columns[0]].tolist()
term2_list=dff[selected_columns[1]].tolist()
term3_list=dff[selected_columns[2]].tolist()
for (term1, term2, term3) in zip(term1_list, term2_list, term3_list):
onetwokey=f"{term1}_{term2}"
onethreekey=f"{term1}_{term3}"
twothreekey=f"{term2}_{term3}"
onetwothreekey=f"{term1}_{term2}_{term3}"
if(expand):
term_1_2 = f'{term1} AND {term2}'
term_1_3 = f'{term1} AND {term3}'
term_2_3 = f'{term2} AND {term3}'
term_1_2_3 = f'{term1} AND {term2} AND {term3}'
else:
term_1_2 = f'"{term1}"[All Fields] AND "{term2}"[All Fields]'
term_1_3 = f'"{term1}"[All Fields] AND "{term3}"[All Fields]'
term_2_3 = f'"{term2}"[All Fields] AND "{term3}"[All Fields]'
term_1_2_3 = f'"{term1}"[All Fields] AND "{term2}"[All Fields] AND "{term3}"[All Fields]'
if f"{Term1}-{Term2} counts" or f"{Term2}-{Term1} counts" not in dff.columns:
if onetwokey not in two_term_dict.keys():
PARAMS = {'db':'pubmed','term':term_1_2,'retmax':'0','api_key':'0595c1cc493e78f5a76d62b9f0cdc845e309'}
time.sleep(0.1)
r = rq.get(url = URL, params = PARAMS)
if(r.status_code != rq.codes.ok):
time.sleep(1.0)
r = rq.get(url = URL, params = PARAMS)
tree = ElementTree.fromstring(r.text)
cnt = int(tree.find("Count").text)
two_term_dict[onetwokey] = cnt
else:
cnt = two_term_dict[onetwokey]
comention_counts_1_2.append(cnt)
comention_counts_1_2_link.append(f"{str(cnt)} <a href='https://pubmed.ncbi.nlm.nih.gov/?term={term1} AND {term2}' target='_blank' rel='noopener noreferrer'>[Link]</a>")
if f"{Term1}-{Term3} counts" or f"{Term3}-{Term1} counts" not in dff.columns:
if onethreekey not in two_term_dict.keys():
PARAMS = {'db':'pubmed','term':term_1_3,'retmax':'0','api_key':'0595c1cc493e78f5a76d62b9f0cdc845e309'}
time.sleep(0.1)
r = rq.get(url = URL, params = PARAMS)
if(r.status_code != rq.codes.ok):
time.sleep(1.0)
r = rq.get(url = URL, params = PARAMS)
tree = ElementTree.fromstring(r.text)
cnt = int(tree.find("Count").text)
two_term_dict[onethreekey] = cnt
else:
cnt = two_term_dict[onethreekey]
comention_counts_1_3.append(cnt)
comention_counts_1_3_link.append(f"{str(cnt)} <a href='https://pubmed.ncbi.nlm.nih.gov/?term={term1} AND {term3}' target='_blank' rel='noopener noreferrer'>[Link]</a>")
if f"{Term2}-{Term3} counts" or f"{Term3}-{Term2} counts" not in dff.columns:
if twothreekey not in two_term_dict.keys():
PARAMS = {'db':'pubmed','term':term_2_3,'retmax':'0','api_key':'0595c1cc493e78f5a76d62b9f0cdc845e309'}
time.sleep(0.1)
r = rq.get(url = URL, params = PARAMS)
if(r.status_code != rq.codes.ok):
time.sleep(1.0)
r = rq.get(url = URL, params = PARAMS)
tree = ElementTree.fromstring(r.text)
cnt = int(tree.find("Count").text)
two_term_dict[twothreekey] = cnt
else:
cnt = two_term_dict[twothreekey]
comention_counts_2_3.append(cnt)
comention_counts_2_3_link.append(f"{str(cnt)} <a href='https://pubmed.ncbi.nlm.nih.gov/?term={term2} AND {term3}' target='_blank' rel='noopener noreferrer'>[Link]</a>")
if f"{Term1}-{Term2}-{Term3} counts" or f"{Term1}-{Term3}-{Term2} counts" not in dff.columns:
if f"{Term2}-{Term1}-{Term3} counts" or f"{Term2}-{Term3}-{Term1} counts" not in dff.columns:
if f"{Term3}-{Term1}-{Term2} counts" or f"{Term3}-{Term2}-{Term1} counts" not in dff.columns:
if onetwothreekey not in three_term_dict.keys():
PARAMS = {'db':'pubmed','term':term_1_2_3,'retmax':'0','api_key':'0595c1cc493e78f5a76d62b9f0cdc845e309'}
time.sleep(0.1)
r = rq.get(url = URL, params = PARAMS)
if(r.status_code != rq.codes.ok):
time.sleep(1.0)
r = rq.get(url = URL, params = PARAMS)
tree = ElementTree.fromstring(r.text)
cnt = int(tree.find("Count").text)
three_term_dict[onetwothreekey] = cnt
#print(f"{term1}-{term2}-{term3}")
else:
cnt = three_term_dict[onetwothreekey]
comention_counts_1_2_3.append(cnt)
comention_counts_1_2_3_link.append(f"{str(cnt)} <a href=\"https://pubmed.ncbi.nlm.nih.gov/?term={term1} AND {term2} AND {term3}\" target=\"_blank\" rel=\"noopener noreferrer\">[Link]</a>")
print(dff.columns)
if f"{Term1}-{Term2} counts" or f"{Term2}-{Term1} counts"not in dff.columns:
dff.insert(0, f"{Term1}-{Term2} counts", comention_counts_1_2)
if f"{Term1}-{Term2} link" or f"{Term2}-{Term1} link"not in dff.columns:
dff.insert(0, f"{Term1}-{Term2} link", comention_counts_1_2_link)
if f"{Term1}-{Term3} counts" or f"{Term3}-{Term1} counts" not in dff.columns:
dff.insert(0, f"{Term1}-{Term3} counts", comention_counts_1_3)
if f"{Term1}-{Term3} link" or f"{Term3}-{Term1} link" not in dff.columns:
dff.insert(0, f"{Term1}-{Term3} link", comention_counts_1_3_link)
if f"{Term2}-{Term3} counts" or f"{Term3}-{Term2} counts" not in dff.columns:
dff.insert(0, f"{Term2}-{Term3} counts", comention_counts_2_3)
if f"{Term2}-{Term3} link" or f"{Term3}-{Term2} link"not in dff.columns:
dff.insert(0, f"{Term2}-{Term3} link", comention_counts_2_3_link)
if f"{Term1}-{Term2}-{Term3} counts" or f"{Term1}-{Term3}-{Term2} counts" not in dff.columns:
if f"{Term2}-{Term1}-{Term3} counts" or f"{Term2}-{Term3}-{Term1} counts" not in dff.columns:
if f"{Term3}-{Term1}-{Term2} counts" or f"{Term3}-{Term2}-{Term1} counts" not in dff.columns:
dff.insert(0, f"{Term1}-{Term2}-{Term3} counts", comention_counts_1_2_3)
if f"{Term1}-{Term2}-{Term3} link" or f"{Term1}-{Term3}-{Term2} link" not in dff.columns:
if f"{Term2}-{Term1}-{Term3} link" or f"{Term2}-{Term3}-{Term1} link" not in dff.columns:
if f"{Term3}-{Term1}-{Term2} link" or f"{Term3}-{Term2}-{Term1} link" not in dff.columns:
dff.insert(0, f"{Term1}-{Term2}-{Term3} link", comention_counts_1_2_3_link)
ammended_answers = dff.to_dict('records')
ammended_columns = [{"name": i.replace("`","").replace("biolink:",""), "id": i, "hideable":True, "selectable": False, "presentation":"markdown"} if " link" in i else {"name": i.replace("`","").replace("biolink:",""), "id": i, "hideable": True, "selectable": [True if "node" in i and " counts" not in i else False]} for i in dff.columns]
hidden_columns=[i for i in dff.columns if " link" in i]+[i for i in dff.columns if "esnd" in i]+[i for i in dff.columns if "MetaData" in i]
message = "Finished retrieving PubMed Abstract Co-Mentions!"
return (ammended_answers, ammended_columns, hidden_columns, message)
def PubMedCoMentionsSimple(term1,term2,expand=True):
URL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
if(expand):
two_term = f'{term1} AND {term2}'
else:
two_term = f'"{term1}"[All Fields] AND "{term2}"[All Fields]'
print(two_term)
PARAMS = {'db':'pubmed','term':two_term,'retmax':'0','api_key':'0595c1cc493e78f5a76d62b9f0cdc845e309'}
time.sleep(0.1)
r = rq.get(url = URL, params = PARAMS)
if(r.status_code != rq.codes.ok):
time.sleep(1.0)
r = rq.get(url = URL, params = PARAMS)
tree = ElementTree.fromstring(r.text)
cnt = int(tree.find("Count").text)
print(cnt)
return cnt