-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscooner_elastic_parser_indexer.py
174 lines (124 loc) · 6.37 KB
/
scooner_elastic_parser_indexer.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
#!/usr/bin/python
import json
import gzip
from Bio import Entrez
from pprint import pprint
from Bio import SeqIO
from datetime import datetime
from elasticsearch import Elasticsearch
from elasticsearch import helpers
import glob #for iterating through files in a directory
def to_if(name): #Function to accept name of a month and return corresponding integer representation
if name == "Jan": return '01'
elif name == "Feb": return '02'
elif name == "Mar": return '03'
elif name == "Apr": return '04'
elif name == "May": return '05'
elif name == "Jun": return '06'
elif name == "Jul": return '07'
elif name == "Aug": return '08'
elif name == "Sep": return '09'
elif name == "Oct": return '10'
elif name == "Nov": return '11'
elif name == "Dec": return '12'
def get_actions():
for filename in glob.glob('pubmed18n06**.xml.gz'): #Eg. for iterating through dumps with the name format specified with wildcards
try:
handle = gzip.open(filename)
record = Entrez.read(handle)
except EOFError: #skip End of File Errors while reading from a file
pass
docID = 0 #variable to store ID of an article which equals PMID
for article in record['PubmedArticle']:
abstractString = ''
abstracttext = ''
try:
for abstract1 in article['MedlineCitation']['Article']['Abstract']['AbstractText']:
try:
abstracttext1 = abstract1.attributes['Label'] + ': ' + abstract1 + ' '
except:
abstracttext1 = abstract1
abstracttext = abstracttext + abstracttext1
abstractString = abstracttext
except:
continue
globalAbstract = abstractString
keyString = ''
keyList = []
try:
for keylist in article['MedlineCitation']['KeywordList']:
for key in keylist:
keyList.append(str(key))
except:
keyList = []
keyString = str(keyList)
#these lines deal with the extraction of mesh_minor and mesh_major fields
Major_List = []
Minor_List = []
try:
for MeSH in article['MedlineCitation']['MeshHeadingList']:
if MeSH['DescriptorName'].attributes['MajorTopicYN'] == 'N':
Minor_List.append(MeSH['DescriptorName'])
elif MeSH['DescriptorName'].attributes['MajorTopicYN'] == 'Y':
Major_List.append(MeSH['DescriptorName'])
except:
Major_List = []
Minor_List = []
##########################################################################################
#extraction of article title from the dump
articleList = ('title:' + article['MedlineCitation']['Article']['ArticleTitle']).split(':')
try:
#lines to deal with part of 'PubDate' tag where 'MedlineDate' appears
dateString = article['MedlineCitation']['Article']['Journal']['JournalIssue']['PubDate']['MedlineDate'].split('-')[0].split(' ')
year = dateString[0]
month = dateString[1]
monthNumber = str(to_if(month))
if(monthNumber == 'None'):
monthNumber = '01'
day = '01'
dateString = dateString = year + '' + monthNumber + '' + day
dateList = datetime.strptime(dateString, '%Y%m%d') #conversion into a datetime object
date = str(dateList.date()) #conversion into a string representation of the date
except:
try:
date = article['MedlineCitation']['Article']['Journal']['JournalIssue']['PubDate']['Day'] #extract day
except:
date = '01'
try:
month = article['MedlineCitation']['Article']['Journal']['JournalIssue']['PubDate']['Month'] #extract month
monthNumber = str(to_if(month))
if(monthNumber == 'None'): #if monthNumber is not specified in the 'Month' field, a monthNumber of '01' is assumed
monthNumber = '01'
except:
month = "Dec"
monthNumber = str(to_if(month)) #conversion into a number representation of a month
try:
year = article['MedlineCitation']['Article']['Journal']['JournalIssue']['PubDate']['Year'] #to extract the year
dateString = year + '' + monthNumber + '' + date
dateList = datetime.strptime(dateString, '%Y%m%d') #conversion into a datetime object
date = str(dateList.date())
except KeyError: #continue if key is not available
continue
docID = article['MedlineCitation']['PMID']
titleString = article['MedlineCitation']['Article']['ArticleTitle']
abstractString = str(globalAbstract)
################################################################
#code to generate document for each corresponding pubmed article
action = {
'_op_type': 'index',
'_index': 'scooner-pubmed',
'_type': '_doc',
'_id': docID,
'_source': {
"title": titleString,
"abstract": abstractString,
"date": date,
"datasource": "pubmed",
"keywords": keyList,
"mesh_major": Major_List,
"mesh_minor": Minor_List
}
}
yield action #yield an iterable
es = Elasticsearch() #define an Elasticsearch object
helpers.bulk(es, get_actions()) #bulk index into Elasticsearch