forked from ychen306/FHIR-Genomics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_loader.py
304 lines (269 loc) · 9.81 KB
/
example_loader.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
from models import db, Resource, User, Client
from indexer import index_search_elements
from fhir_parser import parse_resource
from fhir_spec import RESOURCES
import names
from vcf import VCFReader
import random
from functools import partial
from config import MAX_SEQ_PER_FILE, CONDITION_TO_SEQ_RATIO
import json
import os
BASEDIR = os.path.dirname(os.path.abspath(__file__))
RELIABILITIES = ['questionable', 'ongoing', 'ok', 'calibrating', 'early']
INTERPRETATIONS = [
{
'code': 'L',
'display': 'Below low normal',
'system': 'http://hl7.org/fhir/vs/observation-interpretation'
}, {
'code': 'IND',
'display': 'Intermediate',
'system': 'http://hl7.org/fhir/vs/observation-interpretation'
}, {
'code': 'H',
'display': 'Above high normal',
'system': 'http://hl7.org/fhir/vs/observation-interpretation'
}, {
'code': 'NEG',
'display': 'Negative',
'system': 'http://hl7.org/fhir/vs/observation-interpretation'
}, {
'code': 'POS',
'display': 'Positive',
'system': 'http://hl7.org/fhir/vs/observation-interpretation'
}
]
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_search_elements(resource, search_elements)
db.session.add(resource)
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': {
'text': gender,
'coding': [{
'code': 'F' if gender == 'female' else 'M',
'system': 'http://hl7.org/fhir/v3/AdministrativeGender'}]
}
}
print 'Created Patient called %s'% full_name
return save_resource('Patient', data)
def rand_lab(patient):
data = {
'resourceType': 'Procedure',
'text': {
'status': 'generated',
'div': '<div>DNA Sequencing lab</div>'
},
'subject': patient.get_reference(),
'type': {
'text': 'Sequencing lab',
'coding': [{
'code': 'SequencingLab',
'system': 'http://genomics.smartplatforms.org/dictionary#sequencinglab'
}]
}
}
print 'Created Procedure (Sequencing Lab)'
return save_resource('Procedure', data)
def rand_rx(patient):
data = {
"resourceType": "MedicationPrescription",
"text": {
"status": "generated",
"div": "<div>\n <p>Penicillin VK 5ml suspension to be administered by oral route</p>\n <p>ONE 5ml spoonful to be taken THREE times a day</p>\n <p>100ml bottle</p>\n <p>to patient ref: a23</p>\n <p>by doctor X</p>\n </div>"
},
"status": "active",
"patient": patient.get_reference(),
"medication": {
"reference": "Medication/example"
},
"dosageInstruction": [
{
"timingSchedule": {
"repeat": {
"frequency": 3,
"duration": 1,
"units": "d"
}
},
"route": {
"coding": [
{
"system": "http://snomed.info/sct",
"code": "394899003",
"display": "oral administration of treatment"
}
]
},
"doseQuantity": {
"value": 5,
"units": "ml",
"system": "http://unitsofmeasure.org",
"code": "ml"
}
}
],
"dispense": {
"quantity": {
"value": 100,
"units": "ml",
"system": "http://unitsofmeasure.org",
"code": "ml"
}
}
}
print 'Created Medication Prescription'
return save_resource('MedicationPrescription', data)
def load_patients_by_samples(samples):
return {sample: rand_patient() for sample in samples}
def load_labs_by_patients(patients):
# patients is a key-value pair of sample and patient
return {sample: rand_lab(patients[sample])
for sample in patients.keys()}
def load_meds_by_patients(patients):
return {sample: rand_rx(patients[sample])
for sample in patients.keys()}
def rand_conditions(patient):
'''
randomly assign a set of conditions to a poor patient
'''
conditions = random.sample(available_conditions,
random.randint(0, len(available_conditions)))
ret = []
for cond_tmpl in conditions:
print cond_tmpl['code']['text']
condition = dict(cond_tmpl)
condition['subject'] = patient.get_reference()
ret.append(save_resource('Condition', condition))
print 'Created condition %s'% condition['code'].get('text', '')
return ret
def make_observation(condition, sequence, patient, seq_id):
observation = {
'resourceType': 'Observation',
'extension': [
{
'url': 'http://genomics.smartplatforms.org/dictionary/GeneticObservation#assessedCondition',
'valueReference': condition.get_reference()
}, {
'url': 'http://genomics.smartplatforms.org/dictionary/GeneticObservation#variantGenotype',
'valueReference': sequence.get_reference()
}
],
'subject': patient.get_reference(),
'name': {
'coding': [{
'display': 'Genetic Observation',
'code': 'GeneticObservation',
'system': 'http://genomics.smartplatforms.org/dictionary'
}]
},
'interpretation': random.choice(INTERPRETATIONS),
'status': 'final',
'reliability': random.choice(RELIABILITIES)
}
if seq_id is not None:
observation['extension'].append({
'url': 'http://genomics.smartplatforms.org/dictionary/GeneticObservation#variantId',
'valueString': seq_id
})
print 'Created Observation (Genetic Observation)'
return save_resource('Observation', observation)
def load_conditions_by_patients(patients):
return {sample: rand_conditions(patients[sample])
for sample in patients.keys()}
def load_vcf_example(vcf_file):
reader = VCFReader(filename=vcf_file)
patients = load_patients_by_samples(reader.samples)
db.session.commit()
meds = load_meds_by_patients(patients)
labs = load_labs_by_patients(patients)
db.session.commit()
conditions = load_conditions_by_patients(patients)
db.session.commit()
count = 0
for record in reader:
sequence_tmpl = {
'text': {'status': 'generated'},
'resourceType': 'Sequence',
'type': 'dna',
'chromosome': record.CHROM,
'startPosition': record.POS,
'endPosition': record.end,
'assembly': 'GRCh37',
'source': {'sample': 'somatic'}
}
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['read'] = reads.split(delimiter)
# get genotype quality
if 'GQ' in dir(sample.data):
seq_data['quality'] = sample.data.GQ
# links sequence to patient and lab
referenced_patient = patients[sample_id]
referenced_lab = labs[sample_id]
seq_data['patient'] = referenced_patient.get_reference()
seq_data['source']['lab'] = referenced_lab.get_reference()
# get name of the variant
variant_id = record.ID
variant = variant_id if variant_id is not None else 'anonymous variant'
seq_data['text']['div'] = '<div>Genotype of %s is %s</div>'% (variant, reads)
sequence = save_resource('Sequence', seq_data)
print 'Created Sequence at %s:%s-%s'% (record.CHROM, record.POS, record.end)
# randomly link a DNA sequence to conditions that the user has
if (len(conditions[sample_id]) > 0 and
random.random() <= CONDITION_TO_SEQ_RATIO):
make_observation(random.choice(conditions[sample_id]),
sequence,
referenced_patient,
variant_id)
count += 1
if count >= MAX_SEQ_PER_FILE:
break
def load_condition_from_file(path):
print 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 init_superuser():
superuser = User(email='super')
db.session.add(superuser)
global test_resource
test_resource = partial(Resource, owner_id=superuser.email)
def load_examples():
init_superuser()
init_conditions()
for example_file in os.listdir(os.path.join(BASEDIR, 'examples/vcf')):
load_vcf_example(os.path.join(BASEDIR, 'examples/vcf', example_file))