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 20
/
gel_util.py
260 lines (227 loc) · 9.41 KB
/
gel_util.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
import utils
import re
import json
import joblib as jl
from study_analyzer import StudyAnalyzer, StudyConcept, load_ruler, load_study_settings
from os.path import isfile, join
import logging
from concept_mapping import get_concepts_names, get_umls
from concept_mapping import icd10_wildcard_queries, get_umls_client_inst
"""
a script for supporting GeL phenome model,
e.g., human phenotype ontology mapping generation
"""
def parse_disease_phenotypes(disease_phenotype_csv, disease_model_json):
lines = utils.read_text_file(disease_phenotype_csv)
dis_to_data = {}
for l in lines[1:]:
arr = l.split(',')
lv4_id = arr[4]
lv4_disease = arr[5]
hpo_label = arr[7]
hpo_id = arr[8]
test = arr[9]
test_id = arr[10]
dis_data = []
if lv4_disease in dis_to_data:
dis_data = dis_to_data[lv4_disease]
else:
dis_to_data[lv4_disease] = dis_data
if len(test.strip()) > 0:
dis_data.append({'test': test,
'test_id': test_id})
else:
dis_data.append({'hpo_label': hpo_label,
'hpo_id': hpo_id})
utils.save_json_array(dis_to_data, disease_model_json)
def convert_100k_hpos_to_json():
csv = '/Users/honghan.wu/git/autoimmune-kconnect/resources/100k/' \
'Rare Disease Conditions Phenotypes and Clinical Tests - v1.8.1.csv'
model_file = '/Users/honghan.wu/git/autoimmune-kconnect/resources/100k/diseae_model.js'
parse_disease_phenotypes(csv, model_file)
def generate_hpo_umls_mapping(hpo_dump):
lines = utils.read_text_file(hpo_dump)
# lines = [u'id: HP:3000076', u'def: "An abnormality', u'xref: UMLS:C4073283']
maps = []
cur_map = None
for l in lines:
m = re.match(r'^id\: (HP\:\d+)', l)
if m is not None:
print 'start with %s' % m.group(1)
cur_map = {'hp': m.group(1), 'cuis': []}
maps.append(cur_map)
m = re.match(r'^xref: (UMLS:C\d+)', l)
if m is not None:
cur_map['cuis'].append(m.group(1))
if l == 'is_obsolete: true':
cur_map['is_obsolete'] = True
m = re.match(r'^replaced_by: (HP:\d+)', l)
if m is not None:
cur_map['replaced_by'] = m.group(1)
hpo2umls = {}
obsolete2replace = {}
for cur_map in maps:
hpo2umls[cur_map['hp']] = cur_map['cuis'] if cur_map['hp'] not in hpo2umls \
else cur_map['cuis'] + hpo2umls[cur_map['hp']]
if 'is_obsolete' in cur_map and 'replaced_by' in cur_map:
obsolete2replace[cur_map['hp']] = cur_map['replaced_by']
for obs in obsolete2replace:
if obsolete2replace[obs] in hpo2umls:
hpo2umls[obs] = hpo2umls[obsolete2replace[obs]]
print json.dumps(hpo2umls)
def export_pickled_study_concept_2_flat_json(pickle_file, output_file):
if isfile(pickle_file):
obj = {}
sa = StudyAnalyzer.deserialise(pickle_file)
for sc in sa.study_concepts:
for t in sc.term_to_concepts:
for c in sc.term_to_concepts[t]['closure']:
obj[c] = {"tc": {"closure": 1, "mapped": c}, "concepts": [c]}
utils.save_json_array(obj, output_file)
print 'flat json saved to %s' % output_file
def load_study_ruler(study_folder, rule_config_file, study_config='study.json'):
sa = None
if study_folder is not None and study_folder != '':
r = utils.load_json_data(join(study_folder, study_config))
ret = load_study_settings(study_folder,
umls_instance=None,
rule_setting_file=r['rule_setting_file'],
concept_filter_file=None if 'concept_filter_file' not in r else r['concept_filter_file'],
do_disjoint_computing=True if 'do_disjoint' not in r else r['do_disjoint'],
export_study_concept_only=False if 'export_study_concept' not in r else r['export_study_concept']
)
sa = ret['study_analyzer']
ruler = ret['ruler']
else:
logging.info('no study configuration provided, applying rules to all annotations...')
ruler = load_ruler(rule_config_file)
return {'sa': sa, 'ruler': ruler}
def break_down_study_concepts(scs, umls, new_mapping_file):
mmc = {}
for sc in scs:
cui = sc.term_to_concept[sc.terms[0]]['mapped']
m = {
"tc": {
"closure": 1,
"mapped": cui
},
"concepts": [cui]
}
mmc[sc.name] = m
c2n = get_concepts_names(umls, list(sc.concept_closure))
for c in sc.concept_closure:
if c != cui:
# for each single concept create a studyconcept
mc = {
"tc": {
"closure": 1,
"mapped": c
},
"concepts": [c]
}
mmc[c2n[c]] = mc
utils.save_json_array(mmc, new_mapping_file)
def regenerate_manual_mapped_concepts(tsv, closure_file):
selected_concepts = set()
c2l = {}
for l in utils.read_text_file(tsv):
arr = l.split('\t')
selected_concepts.add(arr[1])
c2l[arr[1]] = arr[0]
t2closure = utils.load_json_data(closure_file)
mapped_concepts = []
map = {}
v_map = {}
for t in t2closure:
disjoint_list = list(set(t2closure[t]) & selected_concepts)
if len(disjoint_list) > 0:
mapped_concepts += disjoint_list
map[t] = {
"tc":
{
"closure": len(disjoint_list), "mapped": disjoint_list[0]
},
"concepts": disjoint_list
}
v_map[t] = [('%s [%s]' % (c2l[c], c)) for c in disjoint_list]
print json.dumps(map)
print selected_concepts - set(mapped_concepts)
print json.dumps(v_map)
def icd10_mapping_convert(json_file, output_json):
c2concepts = utils.load_json_data(json_file)
result = {}
for c in c2concepts:
r = {
"tc": {
"closure": len(c2concepts[c]),
"mapped": c2concepts[c][0]
},
"concepts": c2concepts[c]
}
result[c] = r
utils.save_json_array(result, output_json)
logging.info('all done')
def process_icd_to_umls(icds, icd2umls=None):
lines = []
concepts = []
for icd in icds:
if icd2umls is not None and icd in icd2umls:
concepts.append(icd2umls[icd])
else:
lines.append('%s\ticd' % icd)
icd2umls = icd10_wildcard_queries(lines)
for icd in icd2umls:
concepts += icd2umls[icd]
return concepts
def parsing_tsv_to_manual_mapped(tsv_file, icd2umls_file):
icd2umls = {}
for l in utils.read_text_file(icd2umls_file):
cols = l.split('\t')
icd2umls[cols[0]] = cols[1]
lines = utils.read_text_file(tsv_file)
condition2code = {}
for l in lines:
cols = l.split('\t')
c = cols[0]
icds = cols[len(cols) - 1].split(',')
concepts = []
condition2code[c] = concepts
for icd in icds:
icd = icd.strip().upper()
icd_codes = []
m = re.match(r'([A-Z])(\d+)\-[A-Z]{0,1}(\d+)', icd)
if m is not None:
logging.info('range mappings: %s' % m.group(0))
for num in range(int(m.group(2)), int(m.group(3)), 1):
icd = '%s%02d' % (m.group(1), num)
icd_codes.append(icd)
else:
icd_codes.append(icd)
concepts += process_icd_to_umls(icd_codes, icd2umls=icd2umls)
logging.info(json.dumps(condition2code))
def extend_manual_mappings(mapping_file, new_mapping_file):
umls = get_umls_client_inst('./resources/HW_UMLS_KEY.txt')
m = utils.load_json_data(mapping_file)
for k in m:
logging.info('working on %s' % k)
new_concepts = [] + m[k]['concepts']
for c in m[k]['concepts']:
new_concepts += umls.transitive_narrower(c)
m[k]['concepts'] = list(set(new_concepts))
logging.info('saving new results to %s' % new_mapping_file)
utils.save_json_array(m, new_mapping_file)
if __name__ == "__main__":
logging.basicConfig(level='INFO')
# generate_hpo_umls_mapping('./resources/hp.obo')
# export_pickled_study_concept_2_flat_json('', '')
# s = load_study_ruler('./studies/IMPARTS/', rule_config_file=None)
# break_down_study_concepts(s['sa'].study_concepts, get_umls(), './studies/IMPARTS/broken_down_mappings.json')
# regenerate_manual_mapped_concepts('./studies/autoimmune.v3/label2concept.tsv',
# './studies/autoimmune.v3/sc2closure.json')
# icd10_mapping_convert('./studies/sickle/icd10_mapping.json', './studies/sickle/manual_mapped_concepts.json')
# parsing_tsv_to_manual_mapped('./studies/ktr_charlson/charlson_icd10_top_priority.tsv',
# './onto_res/icd10_umls.tsv')
# icd10_mapping_convert('./studies/ktr_charlson/charlson_icd10_top_priority.json',
# './studies/ktr_charlson/manual_mapped_concepts.json')
extend_manual_mappings('./studies/ktr_charlson/manual_mapped_concepts.json',
'./studies/ktr_charlson/manual_mapped_concepts_extended.json')