-
Notifications
You must be signed in to change notification settings - Fork 8
/
load_example.py
384 lines (322 loc) · 12.7 KB
/
load_example.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
__author__ = 'HemingY'
'''
Load randomly generated example data into the database
'''
from flask import g
from fhir.models import db, Resource, User, Client, commit_buffers
from fhir.indexer import index_resource
from fhir.fhir_parser import parse_resource
from fhir.fhir_spec import RESOURCES
import names
from argparse import ArgumentParser
import random
from functools import partial
import json
import os
import fhir.ga4gh
from vcf import VCFReader
BASEDIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'fhir')
MAX_SEQ_PER_FILE = 10
PRE_EXTENSION_OBS_URL = 'http://hl7.org/fhir/StructureDefinition/observation-genetics'
PRE_EXTENSION_REPORT_URL = 'http://hl7.org/fhir/StructureDefinition/diagnosticreport-genetics'
class MockG(object):
def __init__(self):
self._nodep_buffers = {}
BUF = MockG()
def save_resource(resource_type, resource_data):
'''
save a resource to database and index its elements by search params
'''
valid, search_elements = parse_resource(resource_type, resource_data)
assert valid
resource = test_resource(resource_type, resource_data)
index_resource(resource, search_elements, g=BUF)
return resource
def rand_patient():
'''
generate random resource and index its elements by search params
'''
gender = 'female' if random.random() < 0.5 else 'male'
first_name = names.get_first_name(gender=gender)
last_name = names.get_last_name()
full_name = '%s %s'% (first_name, last_name)
data = {
'resourceType': 'Patient',
'text': {
'status': 'generated',
'div': "<div><p>%s</p></div>"% full_name
},
'name': [{'text': full_name}],
'gender': gender
}
print 'Created Patient called %s'% full_name
return save_resource('Patient', data)
def rand_observations(patientId, index):
gene_name = gene_names[index]
f = file (BASEDIR + '/examples/loinc-code.txt')
code, text = None, None
for line in f.readlines():
line = line.split('\t')
if gene_name and gene_name + ' gene mutation analysis' in line[1]:
code = line[0]
text = line[1]
break
if not code:
code = '55233-1'
text = 'Genetic analysis master panel'
observation = {
'resourceType': 'observationforgenetics',
'category': {'text': 'Laboratory',
'coding': [{
'system': "http://hl7.org/fhir/ValueSet/observation-category",
'code': "laboratory"
}]
},
'code': {'text': text,
'coding': [{
'system': "http://loinc.org",
'code': code
}]
},
'subject': patientId,
'status': 'final'
}
if random.random() < 0.2:
valueCodeableConcept = {'text': 'Negative',
'coding': [{
'system': "http://snomed.info/sct",
'code': "260385009"
}]
}
else:
valueCodeableConcept = {'text': 'Positive',
'coding': [{
'system': "http://snomed.info/sct",
'code': "10828004"
}]
}
observation['valueCodeableConcept'] = valueCodeableConcept
extension = []
# get source randomly
if random.random() < 0.5:
source = {'url': PRE_EXTENSION_OBS_URL+'Source',
'valueCodeableConcept': {'text': 'Somatic',
'coding': [{
'system': "http://hl7.org/fhir/LOINC-48002-0-answerlist",
'code': "LA6684-0"
}]}}
else:
source = {'url': PRE_EXTENSION_OBS_URL+'Source',
'valueCodeableConcept': {'text': 'Germline',
'coding': [{
'system': "http://hl7.org/fhir/LOINC-48002-0-answerlist",
'code': "LA6683-2"
}]}}
extension.append(source)
# get gene name
if gene_name:
gene_id = None
f = file(os.path.join(BASEDIR, 'examples/genename/genenames-HGNC.txt'))
for line in f.readlines():
lis_line = line[0:-1].split('\t')
if lis_line[1] == gene_name:
gene_id = lis_line[0].split(':')[1]
break
if gene_id:
gene = {'url': PRE_EXTENSION_OBS_URL+'Gene',
'valueCodeableConcept': {'text': gene_name,
'coding':[{
'system': 'http://www.genenames.org/',
'code': gene_id
}]}}
else:
gene = {'url': PRE_EXTENSION_OBS_URL+'Gene',
'valueCodeableConcept': {'text': gene_name}}
extension.append(gene)
# get sequence reference
sequence = {'url': PRE_EXTENSION_OBS_URL + 'Sequence',
'valueReference': sequence_ids[index]}
extension.append(sequence)
observation['extension'] = extension
print 'Created Observation-genetics instance'
return save_resource('observationforgenetics', observation)
def load_vcf_example(vcf_file):
reader = VCFReader(filename=vcf_file)
count = 0
serial = 0
for record in reader:
serial += 1
if serial % 1000 != 0:
continue
sequence_tmpl = {
'text': {'status': 'generated'},
'resourceType': 'Sequence',
'type': 'DNA',
'coordinate': [
{
'chromosome': {'text': record.CHROM},
'start': record.POS,
'end': record.end,
'genomeBuild': {'text': 'GRCh37'}
}],
'species': {'text': 'Homo sapiens',
'coding': [{
'system': 'http://snomed.info/sct',
'code': '337915000'}]},
'referenceAllele': record.REF
}
seq_data = dict(sequence_tmpl)
for sample in record.samples:
sample_id = sample.sample
reads = sample.gt_bases
if '/' in reads:
delimiter = '/'
elif '|' in reads:
delimiter = '|'
else:
delimiter = '.'
seq_data = dict(sequence_tmpl)
seq_data['observedAllele'] = reads.split(delimiter)[1]
# get name of the variant
variant_id = record.ID
variant = variant_id if variant_id is not None else 'anonymous variant'
seq_data['variationID'] = [{
'coding': [{
'system': 'http://www.ncbi.nlm.nih.gov/projects/SNP/snp_ref.cgi',
'code': variant_id
}]}]
seq_data['text']['div'] = '<div>Genotype of %s is %s</div>'% (variant, reads)
gene_name = record.INFO.get('SNPEFF_GENE_NAME')
sequence = save_resource('Sequence', seq_data)
print 'Created Sequence at %s:%s-%s'% (record.CHROM, record.POS, record.end)
count += 1
sequence_ids.append(sequence.get_reference())
gene_names.append(gene_name)
if MAX_SEQ_PER_FILE is not None and count >= MAX_SEQ_PER_FILE:
break
def create_diagnosticreport(patientId):
conditionId = rand_conditions(patientId)
print conditionId
performerId = rand_practitioner(patientId)
extension = []
assessed_condition = {'url': PRE_EXTENSION_REPORT_URL + 'AssessedCondition',
'valueReference': conditionId}
extension.append(assessed_condition)
results = []
sequence_index = []
for _ in xrange(random.randint(0,10)):
index = random.randint(0,len(sequence_ids)-1)
if random.randint(0,len(sequence_ids)-1) not in sequence_index:
sequence_index.append(index)
results.append(rand_observations(patientId, index).get_reference())
data = {
'resourceType': 'reportforgenetics',
'extension': extension,
'status': 'final',
'code': {'text': 'Gene mutation analysis'},
'subject': patientId,
'effectiveDateTime': rand_date(),
'issued': rand_date(),
'performer': performerId,
'result': results
}
return save_resource('reportforgenetics', data)
def rand_practitioner(patientId):
'''
randomly assign a set of conditions to a poor patient
'''
practitioner = random.sample(available_practitioner, 1)
practitionerId = 0
for cond_tmpl in practitioner:
practitioner = dict(cond_tmpl)
practitioner['subject'] = patientId
practitioner = save_resource('Practitioner', practitioner)
practitionerId = practitioner.get_reference()
print 'Created practitioner'
break
return practitionerId
def init_practitioner():
practitioner_dir = os.path.join(BASEDIR, 'examples/Practitioner')
global available_practitioner
load_instance = partial(load_practitioner_from_file, relevant_dir=practitioner_dir)
list_of_file = os.listdir(practitioner_dir)
list_of_instance = []
for i in list_of_file:
if '.json' in i:
list_of_instance.append(i)
available_practitioner = map(load_instance, list_of_instance)
def load_practitioner_from_file(path, relevant_dir):
abspath = os.path.join(relevant_dir, path)
with open(abspath) as f:
return json.loads(f.read())
def rand_conditions(patientid):
'''
randomly assign a set of conditions to a poor patient
'''
conditions = random.sample(available_conditions,
random.randint(1, len(available_conditions)))
conditionid = 0
for cond_tmpl in conditions:
condition = dict(cond_tmpl)
condition['patient'] = patientid
conditionid = save_resource('Condition', condition).get_reference()
print 'Created condition %s'% condition['code'].get('text', '')
break
return conditionid
def load_condition_from_file(path):
abspath = os.path.join(BASEDIR, 'examples/conditions', path)
with open(abspath) as condition_f:
return json.loads(condition_f.read())
def init_conditions():
condition_dir = os.path.join(BASEDIR, 'examples/conditions')
global available_conditions
available_conditions = map(load_condition_from_file, os.listdir(condition_dir))
def rand_date():
date = "%d-%d-%dT%d:%d:00+01:00" % (random.randint(2010, 2015),
random.randint(1, 11),
random.randint(1, 27),
random.randint(0, 23),
random.randint(0, 59))
return date
def load_from_file(path, relevant_dir):
abspath = os.path.join(relevant_dir, path)
with open(abspath) as f:
return json.loads(f.read())
def init(resource):
dir = os.path.join(BASEDIR, 'examples/' + resource)
ids = []
load_instance = partial(load_from_file, relevant_dir=dir)
list_of_file = os.listdir(dir)
list_of_instance = []
for i in list_of_file:
if '.json' in i:
list_of_instance.append(i)
availables = map(load_instance, list_of_instance)
for i in availables:
instance = dict(i)
resource_instance = save_resource(resource, instance)
print 'Created %s' % resource
ids.append(resource_instance.get_reference())
return ids
def init_superuser():
superuser = User(email='super')
db.session.add(superuser)
global test_resource
test_resource = partial(Resource, owner_id=superuser.email)
if __name__ == '__main__':
from server import app
with app.app_context():
init_superuser()
init_practitioner()
init('Organization')
init_conditions()
patient_ids = []
sequence_ids = []
gene_names = []
for example_file in os.listdir(os.path.join(BASEDIR, 'examples/vcf')):
load_vcf_example(os.path.join(BASEDIR, 'examples/vcf', example_file))
sequence_amount = len(sequence_ids)
for _ in xrange(10):
patient = rand_patient()
create_diagnosticreport(patient.get_reference())
commit_buffers(BUF)