-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractionCode.py
More file actions
214 lines (188 loc) · 9.3 KB
/
ExtractionCode.py
File metadata and controls
214 lines (188 loc) · 9.3 KB
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
from Bio import Entrez
import pandas as pd
import time
def search(query):
Entrez.email = "cxr244@case.edu"
handle = Entrez.esearch(db='pubmed',
sort='relevance',
retmax='20',
retmode='xml',
term=query)
results = Entrez.read(handle)
return results
def fetch_details(id_list):
ids = ','.join(id_list)
Entrez.email = 'cxr244@case.edu'
handle = Entrez.efetch(db='pubmed',
retmode='xml',
id=ids)
results = Entrez.read(handle)
return results
if __name__ == '__main__':
# initialData = {"Skip": ["N","N"], "Name": ["Chandruganesh Rasendran", "Alexander Haueisen"]}
df = pd.read_excel("C:/Chandru/CWRU/Research/CCF/OphthoAvgPubsPaper/SusAuthorsDataAnalysis.xlsx", sheet_name="InputForCode")
df = pd.DataFrame(df, columns = ["Skip", 'Name','Affliation','LastName'])
# results=search("Chandruganesh Rasendran")
# df = pd.DataFrame(initialData, columns = ["Skip", "Name"])
FinalList = pd.DataFrame()
for index, row in df.iterrows():
if(df.at[index,'Skip'] == "N"):
# Do stuff
AuthorList = pd.DataFrame()
print(row['Affliation'])
if(row['Affliation'] == "Ignore"):
print("Searching just name")
results = search(row['Name'] + " [AU]")
else:
results = search(row['Name'] + " [AU] AND " + row['Affliation'] + " [Affiliation]")
# results = search(row['Name'] + " [AU]")
id_list = results['IdList']
title = []
date = []
year = []
month = []
day = []
authors = []
journal = []
firstauthorlastname = []
firstauthorfirstname = []
lastauthorlastname = []
lastauthorfirstname = []
names = []
if(len(id_list) > 0):
papers = fetch_details(id_list)
# TODO for below, if there is an error, append nothing and move on
# Adding new rows if columns are too high (likely use length of columns and then add same number of rows )
# Change how dates are recorded to be more user friendly if possible
for i, paper in enumerate(papers['PubmedArticle']):
size = len(authors)
add = "TRUE"
finishLoop = "FALSE"
for i in range(len(paper['MedlineCitation']['Article']['AuthorList'])):
try:
if add == "TRUE" and paper['MedlineCitation']['Article']['AuthorList'][i].get('LastName',"None") != "None" and paper['MedlineCitation']['Article']['AuthorList'][i]['LastName'] == row['LastName']:
if(paper['MedlineCitation']['Article']['AuthorList'][i]['ForeName'][0] == row['Name'][0]) and size == len(authors):
if(len(paper['MedlineCitation']['Article']['AuthorList'][i]['AffiliationInfo']) == 0):
authors.append("Error")
else:
authors.append(paper['MedlineCitation']['Article']['AuthorList'][i]['AffiliationInfo'][0]['Affiliation'])
else:
add = "FALSE"
except (KeyError,IndexError,ValueError):
# Enters here if author of interest has no affliation OR there is an author without last name
nearestAffliation = "FALSE"
for i in range(len(paper['MedlineCitation']['Article']['AuthorList'])):
if nearestAffliation == "FALSE" and paper['MedlineCitation']['Article']['AuthorList'][i]['AffiliationInfo'] and add == "TRUE":
authors.append(paper['MedlineCitation']['Article']['AuthorList'][i]['AffiliationInfo'][0]['Affiliation'])
nearestAffliation = "TRUE"
if (size + 1 == len(authors)):
title.append("{}) {}".format(i+1, paper['MedlineCitation']['Article']['ArticleTitle']))
try:
# date.append(paper['MedlineCitation']['Article']['ArticleDate']['Year'])
if(len(paper['MedlineCitation']['Article']['ArticleDate']) == 0):
date.append("Error")
else:
year.append(paper['MedlineCitation']['Article']['ArticleDate'][0]['Year'])
month.append(paper['MedlineCitation']['Article']['ArticleDate'][0]['Month'])
day.append(paper['MedlineCitation']['Article']['ArticleDate'][0]['Day'])
date.append(month[-1] + "/"+ day[-1]+ "/" + year[-1])
except (KeyError,IndexError,ValueError):
date.append("Error")
try:
journal.append(paper['MedlineCitation']['Article']['Journal']['Title'])
except (KeyError,IndexError,ValueError):
journal.append("Error")
# authors.append(paper['MedlineCitation']['Article']['AuthorList'])
try:
firstauthorlastname.append(paper['MedlineCitation']['Article']['AuthorList'][0].get('LastName',"None"))
except (KeyError,IndexError,ValueError):
try:
firstauthorlastname.append(paper['MedlineCitation']['Article']['AuthorList'][1].get('LastName',"None"))
except:
firstauthorlastname.append("Error")
try:
firstauthorfirstname.append(paper['MedlineCitation']['Article']['AuthorList'][0].get('ForeName',"None"))
except (KeyError,IndexError,ValueError):
try:
firstauthorfirstname.append(paper['MedlineCitation']['Article']['AuthorList'][1].get('ForeName',"None"))
except:
firstauthorfirstname.append("Error")
try:
lastauthorlastname.append(paper['MedlineCitation']['Article']['AuthorList'][-1].get('LastName',"None"))
except (KeyError,IndexError,ValueError):
try:
lastauthorlastname.append(paper['MedlineCitation']['Article']['AuthorList'][-2].get('LastName',"None"))
except:
lastauthorlastname.append("Error")
try:
lastauthorfirstname.append(paper['MedlineCitation']['Article']['AuthorList'][-1].get('ForeName',"None"))
except (KeyError,IndexError,ValueError):
try:
lastauthorfirstname.append(paper['MedlineCitation']['Article']['AuthorList'][-2].get('ForeName',"None"))
except:
lastauthorfirstname.append("Error")
titleLength = len(title)
for i in range(len(title)):
names.append(row['Name'])
# df = pd.concat([df,df.tail(1)])
# print(df)
if(len(title) == 0):
names.append(row['Name'])
AuthorList["Title"] = title
AuthorList["Date"] = date
AuthorList["Journal"] = journal
AuthorList["First Last Name"] = firstauthorlastname
AuthorList["First First Name"] = firstauthorfirstname
AuthorList["Last Last Name"] = lastauthorlastname
AuthorList["Last First Name"] = lastauthorfirstname
AuthorList["Authors"] = authors
print(AuthorList)
print(names)
AuthorList.insert(0,"Name", names)
FinalList = pd.concat([FinalList,AuthorList])
writer = pd.ExcelWriter("C:/Chandru/CWRU/Research/CCF/OphthoAvgPubsPaper/DataAnalysis2.xlsx", engine = "xlsxwriter")
FinalList.to_excel(writer, 'Sheet1')
writer.save()
# print("{}) {}".format(i+1, paper['MedlineCitation']['Article']['ArticleTitle']))
# print(paper['MedlineCitation']['Article']['ArticleDate'])
# print(paper['MedlineCitation']['Article']['Journal']['Title'])
# print(paper['MedlineCitation']['Article']['AuthorList'][0]['LastName'])
# print(paper['MedlineCitation']['Article']['AuthorList'][0]['ForeName'])
# print(paper['MedlineCitation']['Article']['AuthorList'][-1]['LastName'])
# print(paper['MedlineCitation']['Article']['AuthorList'][-1]['ForeName'])
# if __name__ == '__main__':
# df = pd.read_excel("C:/Chandru/CWRU/Research/CCF/OphthoAvgPubsPaper/DataAnalysis.xlsx", sheet_name="Sheet3")
# df = pd.DataFrame(df, columns = ["Skip", 'Resident'])
# for index, row in df.iterrows():
# if df.at[index,'Skip'] == "N":
# results=search(df.at(index,'Resident'))
# id_list = results['IdList']
# papers = fetch_details(id_list)
# title = []
# date = []
# firstauthorlastname = []
# firstauthorfirstname = []
# lastauthorlastname = []
# lastauthorfirstname = []
# for i, paper in enumerate(papers['PubmedArticle']):
# title.append("{}) {}".format(i+1, paper['MedlineCitation']['Article']['ArticleTitle']))
# date.append(paper['MedlineCitation']['Article']['ArticleDate'])
# firstauthorlastname.append(paper['MedlineCitation']['Article']['ArticleDate'])
# firstauthorfirstname.append(paper['MedlineCitation']['Article']['ArticleDate'])
# lastauthorlastname.append(paper['MedlineCitation']['Article']['ArticleDate'])
# lastauthorfirstname.append(paper['MedlineCitation']['Article']['ArticleDate'])
# writer = pd.ExcelWriter("C:/Chandru/CWRU/Research/CCF/OphthoAvgPubsPaper/DataAnalysis2.xlsx", engine = "xlsxwriter")
# df.to_excel(writer, 'Sheet1')
# writer.save()
# pubmed search the name
# results = search('Alexander Haueisen')
# id_list = results['IdList']
# papers = fetch_details(id_list)
# for i, paper in enumerate(papers['PubmedArticle']):
# print("{}) {}".format(i+1, paper['MedlineCitation']['Article']['ArticleTitle']))
# print(paper['MedlineCitation']['Article']['ArticleDate'])
# print(paper['MedlineCitation']['Article']['Journal']['Title'])
# print(paper['MedlineCitation']['Article']['AuthorList'][0]['LastName'])
# print(paper['MedlineCitation']['Article']['AuthorList'][0]['ForeName'])
# print(paper['MedlineCitation']['Article']['AuthorList'][-1]['LastName'])
# print(paper['MedlineCitation']['Article']['AuthorList'][-1]['ForeName'])