This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScoreBuilder.py
313 lines (261 loc) · 10.7 KB
/
ScoreBuilder.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 12 11:35:22 2019
@author: danielbean
"""
# risk score builder
import pandas as pd
from py2neo import Graph
import urllib2, json
class MapperTemplate:
"""
Base class for mappers. Mappers return identifers lower than or equal to
the start id in whatever ontology they're using.
conf: dict containing connection information (host, port, user, pass)
"""
def __init__(self, conf):
self.conf = conf
def get_descendants(self, start, depth):
descendants = []
return descendants
class TestMapper(MapperTemplate):
"""
For testing of ScoreBuilder.
If depth == 0, the root is passed through as the child value - convenient
for manually-defined components.
"""
def get_descendants(self, start, depth):
if depth == 0:
return set([start])
return ["%s_to_depth_%s" % (start, depth)]
class UMLSMapper(MapperTemplate):
def __init__(self, conf):
MapperTemplate.__init__(self, conf)
# #connect to Neo4j server
print "connect to Neo4j @ %s" % conf['host']
self.g = Graph("bolt://" + conf['host'], password=conf['pass'])
def get_descendants(self, start, depth=1):
"""
Depth is implemented in the query for UMLS because an unrestricted depth has
an enormous overhead in the UMLS graph, so only query as deep as we need
"""
if depth == 0:
return set([start])
elif depth == -1:
depth_str = "*"
else:
depth_str = "*.." + str(depth)
query = """
match p = (c:Concept {CUI:{cui}})<-[r:isa%s]-(cc:Concept)
return c.CUI as root_cui, cc.CUI as child_cui, length(p) as distance
""" % depth_str
data_dict = self.g.run(query, cui = start).data()
df = pd.DataFrame(data_dict)
id_list = set(df['child_cui'].tolist())
id_list.add(start)
return id_list
## old code below
def recordsToList(self, cursor):
result = []
for record in cursor:
result.append(record.values()[0])
return result
def get_child_terms(self, umls):
q = "match (c:Concept {CUI:'%s'})<-[r:isa*]-(cc:Concept) return cc.CUI as isa_child_term" % umls
cursor = self.g.run(q)
children = self.recordsToList(cursor)
return children
class HPOMapper(MapperTemplate):
def __init__(self, conf):
MapperTemplate.__init__(self, conf)
# #connect to Neo4j server
print "connect to Neo4j @ %s" % conf['host']
self.g = Graph("bolt://" + conf['host'], password=conf['pass'])
def get_descendants(self, start, depth):
if depth == 0:
return set([start])
terms = self.get_all_child_terms(start)
if depth > 0: #-1 means unlimited and 0 means don't map
terms = terms[terms['distance'] <= depth]
id_list = set(terms['child_hpo'].tolist())
id_list.add(start)
return id_list
def get_all_child_terms(self, hpo_id):
query = """
match p = (t:Term {hpo:{hpo_id}})<-[r:IS_A*]-(a)
return t.hpo as root_hpo, t.name as root_name, a.hpo as child_hpo, a.name as child_name, length(p) as distance
"""
data_dict = self.g.run(query, hpo_id = hpo_id).data()
df = pd.DataFrame(data_dict)
return df
class HPOUMLSMapper(MapperTemplate):
"""
Like HPOMapper but only return child terms from HPO that have a UMLS equivalent
"""
def __init__(self, conf):
MapperTemplate.__init__(self, conf)
# #connect to Neo4j server
print "connect to Neo4j @ %s" % conf['host']
self.g = Graph("bolt://" + conf['host'], password=conf['pass'])
def get_descendants(self, start, depth):
if depth == 0:
return set([start])
terms = self.get_all_child_terms(start)
if depth > 0: #-1 means unlimited and 0 means don't map
terms = terms[terms['distance'] <= depth]
id_list = set(terms['umls'].tolist())
#note for this mapper we DO NOT add the start id to the output
#because it will be HPO and we want to return UMLS
return id_list
def get_all_child_terms(self, hpo_id):
#note this query gives distances always +1 from distance in HPO because
#it also includes the UMLS equivalent
query = """
match p = (t:Term {hpo:{hpo_id}})<-[r:IS_A*]-(a)-[rr:same]->(u:UMLS)
return t.hpo as root_hpo, t.name as root_name, a.hpo as child_hpo, a.name as child_name, u.umls as umls, length(p) as distance
"""
data_dict = self.g.run(query, hpo_id = hpo_id).data()
df = pd.DataFrame(data_dict)
has_child = df.shape[0] != 0
if has_child:
df['distance'] = df['distance'] - 1
#another query to map the root to UMLS
query = """
match p = (t:Term {hpo:{hpo_id}})<-[rr:same]->(u:UMLS)
return t.hpo as root_hpo, t.name as root_name, '' as child_hpo, '' as child_name, u.umls as umls, length(p) as distance
"""
data_dict = self.g.run(query, hpo_id = hpo_id).data()
df2 = pd.DataFrame(data_dict)
root_has_umls = df2.shape[0] != 0
if has_child:
out = pd.concat([df, df2], ignore_index=True)
elif root_has_umls:
df2['distance'] = df2['distance'] - 1
out = df2
else:
out = df2
return out
class ICD10_UMLSMapper(MapperTemplate):
"""
Use the bioontology API to map ICD10 oto UMLS CUI. Note does not do any
navigation of the ICD10 tree i.e. only supports depth==0
"""
def __init__(self, conf):
MapperTemplate.__init__(self, conf)
# #connect to Neo4j server
self.REST_URL = "http://data.bioontology.org/ontologies/ICD10/classes/http%3A%2F%2Fpurl.bioontology.org%2Fontology%2FICD10%2F"
self.API_KEY = conf['key']
#hacky fix for a missing term
self.manual_terms = {}
self.manual_terms['U80'] = {'cui':['C1269757'], 'prefLabel': 'Infection resistant to penicillin'}
def get_descendants(self, start, depth):
if depth != 0:
print "ICD10_UMLS mapper does not support depth limited search"
return
res = self.get_json(self.REST_URL, start)
cui = set(res['cui'])
return cui
def get_json(self, base_url, term):
url = base_url + term
opener = urllib2.build_opener()
opener.addheaders = [('Authorization', 'apikey token=' + self.API_KEY)]
try:
op = opener.open(url)
res = json.loads(op.read())
res['mapping'] = 'bioportal'
except:
print "not found"
if term in self.manual_terms:
res = self.manual_terms[term]
res['mapping'] = 'manual'
else:
res = {'cui': "", "prefLabel": "NOT FOUND", 'mapping': 'NONE'}
return res
class ScoreBuilder:
def __init__(self, name = ""):
self.name = name
self.mappers = {}
def add_mapper(self, name, mapper):
"""
name: value in the ontology column of the score configuration file that
should trigger this mapper
mapper: instance of a class inherited from MapperTemplate
"""
self.mappers[name] = mapper
def build(self, score_config_fname, exclusion_fname=None):
"""
score_config_fname: definition of the component parts of this score as csv
exclusion_fname: (optional) csv of concepts to exclude in the chosen ontology
"""
df = pd.read_csv(score_config_fname)
exclude = set()
if exclusion_fname != None:
exclude_def = pd.read_csv(exclusion_fname)
exclude = set(exclude_def['id'].tolist())
score_concepts = self.do_mapping(df, exclude)
score_def = self.define_score(df, score_concepts)
return score_def
def do_mapping(self, df, exclude):
by_source = df.groupby('ontology')
rows = []
for name, group in by_source:
mapper = self.mappers[name]
for index, row in group.iterrows():
#get descendants
r = mapper.get_descendants(row['root'], row['depth'])
for ch in r:
if ch not in exclude:
rows.append({'child': ch, 'root': row['root'], 'component': row['component']})
score_concepts = pd.DataFrame(rows)
return score_concepts
def define_score(self, score_config, score_concepts):
definition = {}
for name, group in score_config.groupby('component'):
#print name
definition[name] = {}
this_concept = score_concepts[score_concepts['component'] == name]
this_concept_umls = this_concept['child'].unique()
this_concept_umls = list(this_concept_umls)
this_concept_points = group['points'].unique()
definition[name]['concepts'] = this_concept_umls
if len(this_concept_points) > 1:
print "different points values provided for group %s" % group
print this_concept_points
break
definition[name]['points'] = this_concept_points[0]
return definition
if __name__ == "__main__":
import yaml
with open('config.yml') as f:
conf = yaml.load(f)
sb = ScoreBuilder('test')
tm = TestMapper({}) #empty configuration for test mapper
sb.add_mapper('test', tm)
definition = sb.build('input_files/test_definition.csv')
print definition
#HPO
hpom = HPOMapper(conf['HPO'])
sb.add_mapper('HPO', hpom)
hpo_umls = HPOUMLSMapper(conf['HPO'])
sb.add_mapper('HPO_UMLS', hpo_umls)
#terms = hpom.get_descendants("HP:0001892", 1)
definition = sb.build('input_files/hpo_test_definition.csv')
print definition
for comp in definition:
print "%s: %s concepts" % (comp, len(definition[comp]['concepts']))
#UMLS
umls = UMLSMapper(conf['UMLS'])
sb.add_mapper('UMLS', umls)
definition = sb.build('input_files/umls_test_definition.csv')
print definition
for comp in definition:
print "%s: %s concepts" % (comp, len(definition[comp]['concepts']))
#ICD10
icd = ICD10_UMLSMapper(conf['ICD10'])
sb.add_mapper('ICD10_UMLS', icd)
#icd.get_descendants('W19', 0)
definition = sb.build('input_files/icd10_test_definition.csv')
print definition
for comp in definition:
print "%s: %s concepts" % (comp, len(definition[comp]['concepts']))