-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathesIndex.py
executable file
·211 lines (186 loc) · 5.5 KB
/
esIndex.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
BIOCADDIE Terminology server utilities
Index terms to elasticsearch
Created on : 2015-05-25 ( [email protected] )
Last modified: Aug 05, 2015, Wed 12:03:37 -0500
"""
import argparse
# from utils.snomedct import SNOMEDCT
# from utils.umls import UMLS
from sqlalchemy import create_engine
from elasticsearch import Elasticsearch
from elasticsearch.exceptions import TransportError
from utils.semTypes import INV_SEM_TYPES
# Global vars
es = None
conn = None
#
# LNC, RXNORM
# NCI: is removed b/o false entries like: TP53, prostate carcinoma
#
# CREATE TABLE indexdata AS
# SELECT LUI , STR, GROUP_CONCAT(DISTINCT CUI) AS CUIS,
# GROUP_CONCAT(DISTINCT CONCAT( SAB, ':', CODE )) AS CODES
# FROM MRCONSO
# WHERE LAT = 'ENG'
# AND SAB IN ('MSH', 'SNOMEDCT_US', 'NCBI', 'GO', 'HGNC', 'FMA')
# AND SUPPRESS IN ('N')
# GROUP BY LUI
# ORDER BY LUI;
#
# ALTER TABLE indexdata ADD PRIMARY KEY(LUI);
#
# CREATE TABLE temp_strs AS SELECT str FROM indexdata;
#
SEM_TYPE_NAMEs = [
'(foundation metadata concept)',
'(context-dependent category)',
'(morphologic abnormality)',
'(administrative concept)',
'(navigational concept)',
'(contextual qualifier)',
'(geographic location)',
'(religion/philosophy)',
'(biological function)',
'(separate procedure)',
'(namespace concept)',
'(observable entity)',
'(assessment scale)',
'(record artifact)',
'(allelic variant)',
'(qualifier value)',
'(living organism)',
'(physical object)',
'(cell structure)',
'(surface region)',
'(physical force)',
'(regime/therapy)',
'(body structure)',
'(tumor staging)',
'(clinical exam)',
'(staging scale)',
'(combined site)',
'(manifestation)',
'(invertebrate)',
'(ethnic group)',
'(environment)',
'(Drosophila)', #
'(medication)',
'(occupation)',
'(attribute)',
'(procedure)',
'(substance)',
'(diagnosis)',
'(treatment)',
'(situation)',
'(eukaryote)',
'(diagnosis)',
'(superior)',
'(inferior)',
'(obsolete)',
'(bacteria)',
'(lab test)',
'(disorder)',
'(specimen)',
'(organism)',
'(Medicine)',
'(etiology)',
'(function)',
'(property)',
'(disease)',
'(finding)',
'(product)',
'(symptom)',
'(degrees)',
'(history)',
'(lateral)',
'(person)',
'(fungus)',
'(medial)',
'(device)',
'(action)',
'(event)',
'(yeast)', #
'(human)',
'(PLANT)', #
'(cell)',
]
SQL = "SELECT LUI , STR, CUIS, CODES, TUIS FROM indexdata LIMIT %d, %d"
# NCBI Taxonomy, 2014_04_01
# NCI Thesaurus, 2014_03E
def query(offset, count=100):
"""Run the same sql from a given offset for a given count"""
sql = SQL % (offset, count)
result = conn.execute(sql)
res = []
for row in result:
res.append(row)
return res
def addIndex(concept, index, doctype):
"""Indexes a given umls concept to index with doc_type"""
tuis = concept[4].split(',')
sset = [INV_SEM_TYPES[tui] for tui in tuis]
body = {
'lui': concept[0],
'term': concept[1],
'cui': concept[2].split(','),
'sab': concept[3].split(','),
'tui': tuis,
'sset': sset, # subset
}
es.index(index=index, doc_type=doctype, body=body)
def processAll(index, doctype):
"""process all concepts in smaller chunks"""
offset = 0
count = 100
hasMore = True
while hasMore:
concepts = query(offset, count)
for concept in concepts:
addIndex(concept, index, doctype)
# check the number of results
numResults = len(concepts)
offset += numResults
# is the result count equals to expected number of items?
hasMore = numResults == count
def parseArgs():
parser = argparse.ArgumentParser(description='Creates ElasticSearch index '
'using UMLS concept descriptions',
fromfile_prefix_chars='@')
parser.add_argument('-d', '--delete', action='store_true', required=False,
help='Delete the previous index and restart')
parser.add_argument('-H', '--host', default='localhost', required=False,
help='Host for ElasticSearch server')
parser.add_argument('-p', '--port', type=int, default=9200, required=False,
help='Port for ElasticSearch server')
parser.add_argument('-i', '--index', required=True,
help='Name of the ElasticSearch index to create')
parser.add_argument('-t', '--doctype', required=True,
help='Document type of the ElasticSearch index '
'to create')
parser.add_argument('-s', '--constr', required=True,
help='Connection string for sqlalchemy')
return parser.parse_args()
def main(args):
global conn
global es
try:
es = Elasticsearch([
{u'host': args.host, u'port': args.port},
], sniff_on_start=True)
except TransportError:
print 'Unable to sniff host %s:%s' % (args.host, args.port)
exit(1)
if args.delete:
print 'deleting index'
es.indices.delete(index=args.index, ignore=[400, 404])
engine = create_engine(args.constr)
conn = engine.connect()
try:
processAll(args.index, args.doctype)
finally:
conn.close()
if __name__ == '__main__':
main(parseArgs())