-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenomicVariations_csv.py
543 lines (458 loc) · 35.3 KB
/
genomicVariations_csv.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import json
from tqdm import tqdm
import re
import conf.conf as conf
import csv
import sys
from validators.genomicVariations import GenomicVariations
with open("files/headers/genomicVariations.txt", "r") as txt_file:
list_of_headers=txt_file.read().splitlines()
with open('files/deref_schemas/genomicVariations.json') as json_file:
dict_properties = json.load(json_file)
csv_filename = sys.argv[1]
output_path = sys.argv[2]
def generate(dict_properties,list_of_headers):
#csv_filename = conf.csv_filename
with open(csv_filename, 'r' ) as theFile:
reader = csv.DictReader(theFile)
num_rows = sum(1 for row in reader)
total_dict =[]
k=0
pbar = tqdm(total = num_rows)
with open(csv_filename, 'r' ) as theFile:
reader = csv.DictReader(theFile)
i=1
for line in reader:
dict_of_properties={}
list_of_filled_items=[]
for kline, vline in line.items():
property_value = kline
property_value=property_value.replace('\ufeff', '')
valor = vline
if property_value not in list_of_headers:
raise Exception(('the header {} is not allowed. Please, take a look at files/headers/genomicVariations.txt to check the headers allowed.').format(property_value))
if i > 0:
if valor != '':
list_of_filled_items.append(property_value)
if valor:
dict_of_properties[property_value]=valor
elif valor == 0:
dict_of_properties[property_value]=valor
#print(dict_properties)
#print(dict_of_properties)
#print(dict_properties)
#print(dict_of_properties)
definitivedict={}
for key, value in dict_properties.items():
if isinstance(value, list):
value_list=[]
item_dict={}
for item in value:
outcome = 0
if isinstance(item, dict):
for ki, vi in item.items():
if isinstance(vi, list):
vi_list=[]
subitem_dict={}
for subitem in vi:
if isinstance(subitem, dict):
for k, v in subitem.items():
if isinstance(v, dict):
subitem_dict[k]={}
for k1, v1 in v.items():
if isinstance(v1, dict):
subitem_dict[k][k1]={}
for k2, v2 in v1.items():
if isinstance(v2, dict):
subitem_dict[k][k1][k2]={}
for k3, v3 in v2.items():
new_item = ""
new_item = key + "|" + ki + "|" + k + "|" + k1 + "|" + k2 + "|" + k3
for propk, propv in dict_of_properties.items():
if propk == new_item:
subitem_dict[k][k1][k2][k3]=propv
if subitem_dict[k][k1][k2]=={}:
del subitem_dict[k][k1][k2]
else:
new_item = ""
new_item = key + "|" + ki + "|" + k + "|" + k1 + "|" + k2
for propk, propv in dict_of_properties.items():
if propk == new_item:
subitem_dict[k][k1][k2]=propv
if subitem_dict[k][k1]=={}:
del subitem_dict[k][k1]
else:
new_item = ""
new_item = key + "|" + ki + "|" + k + "|" + k1
for propk, propv in dict_of_properties.items():
if propk == new_item:
subitem_dict[k][k1]=propv
if subitem_dict[k]=={}:
del subitem_dict[k]
elif isinstance(v, list):
listitemv=[]
vivdict={}
for itemv in v:
if isinstance(itemv, dict):
#print('ki is {}'.format(ki))
#print('k is {}'.format(k))
#print(itemv)
for kiv, viv in itemv.items():
if isinstance(viv, list):
for itemviv in viv:
if isinstance(itemviv, dict):
for kivi, vivi in itemviv.items():
if isinstance(vivi, list):
for vivitem1 in vivi:
for kivivi, vivivi in vivitem1.items():
if isinstance(vivivi, dict):
for kivivi1, vivivi1 in vivivi.items():
new_item = ""
new_item = key + "|" + ki + "|" + k + "|" + kiv + "|" + kivi + "|" + kivivi + "|" + kivivi1
for propk, propv in dict_of_properties.items():
if propk == new_item:
#print(propk)
try:
vivdict[kiv][kivi][kivivi][kivivi1]=propv
except Exception:
vivdict[kiv]={}
vivdict[kiv][kivi]={}
vivdict[kiv][kivi][kivivi]={}
vivdict[kiv][kivi][kivivi][kivivi1]=propv
elif isinstance(vivivi, list):
for vivivitem in vivivi:
for kivitem, vivitem in vivivitem.items():
new_item = ""
new_item = key + "|" + ki + "|" + k + "|" + kiv + "|" + kivi + "|" + kivivi + "|" + kivitem
for propk, propv in dict_of_properties.items():
if propk == new_item:
try:
vivdict[kiv][kivi][kivivi][kivitem]=propv
except Exception:
vivdict[kiv][kivi]={}
vivdict[kiv][kivi][kivivi]={}
vivdict[kiv][kivi][kivivi][kivitem]=propv
else:
new_item = ""
new_item = key + "|" + ki + "|" + k + "|" + kiv + "|" + kivi + "|" + kivivi
#print(new_item)
for propk, propv in dict_of_properties.items():
if propk == new_item:
try:
vivdict[kiv][kivi][kivivi]=propv
except Exception:
vivdict[kiv]={}
vivdict[kiv][kivi]={}
vivdict[kiv][kivi][kivivi]=propv
else:
new_item = ""
new_item = key + "|" + ki + "|" + k + "|" + kiv + "|" + kivi
for propk, propv in dict_of_properties.items():
if propk == new_item:
try:
if 'value' in propk:
vivdict[kiv][kivi]=int(propv)
else:
vivdict[kiv][kivi]=propv
except Exception:
vivdict[kiv]={}
vivdict[kiv][kivi]=propv
elif propk == key + "|" + ki + "|" + k + "|" + kiv:
vivdict[kiv]=propv
else:
new_item = ""
new_item = key + "|" + ki + "|" + k + "|" + kiv
for propk, propv in dict_of_properties.items():
if propk == new_item:
#print(propk)
vivdict[kiv]=propv
if vivdict != {}:
#print(vivdict)
subitem_dict[k]=vivdict
elif ki == 'copies':
new_item = ""
new_item = key + "|" + ki
for propk, propv in dict_of_properties.items():
if propk == new_item:
propv = re.sub(r'\s', '', propv)
respropv = json.loads(propv)
subitem_dict=respropv
else:
new_item = ""
new_item = key + "|" + ki + "|" + k
for propk, propv in dict_of_properties.items():
if propk == new_item:
try:
propv = re.sub(r'\s', '', propv)
respropv = json.loads(propv)
subitem_dict[k]=respropv
except Exception:
subitem_dict[k]=propv
if subitem_dict != {}:
if subitem_dict not in vi_list and subitem_dict != {}:
vi_list.append(subitem_dict)
item_dict[ki]=vi_list[0]
elif isinstance(vi, dict):
vi_dict={}
for ki1, vi1 in vi.items():
if isinstance(vi1, dict):
vi_dict[ki1]={}
for ki2, vi2 in vi1.items():
new_item = ""
new_item = key + "|" + ki + "|" + ki1 + "|" + ki2
for propk, propv in dict_of_properties.items():
if propk == new_item:
vi_dict[ki1][ki2]=propv
if vi_dict != {}:
item_dict[ki]=vi_dict
if vi_dict[ki1]=={}:
del vi_dict[ki1]
elif ki1 == 'variation':
vi_dict[ki1]={}
new_item = ""
new_item = key + "|" + ki + "|" + ki1
for propk, propv in dict_of_properties.items():
if propk == new_item:
vi_dict[ki1]=propv
elif isinstance(vi1, list):
vi_dict[ki1]=[]
dictkvi={}
for itemvi1 in vi1:
for kvi, vvi in itemvi1.items():
new_item = ""
new_item = key + "|" + ki + "|" + ki1 + "|" + kvi
for propk, propv in dict_of_properties.items():
if propk == new_item:
dictkvi[kvi]=vvi
vi_dict[ki1]=dictkvi
else:
new_item = ""
new_item = key + "|" + ki + "|" + ki1
for propk, propv in dict_of_properties.items():
if propk == new_item:
vi_dict[ki1]=propv
item_dict[ki]=vi_dict
if vi_dict=={}:
del vi_dict
else:
new_item = ""
new_item = key + "|" + ki
for propk, propv in dict_of_properties.items():
if propk == new_item:
if '|' in propv:
outcome +=1
v1_keys=[]
item_dict[ki]=propv
if ki == 'members':
new_item = ""
new_item = key + "|" + ki
for propk, propv in dict_of_properties.items():
if propk == new_item:
#print(propk)
propv = re.sub(r'\s', '', propv)
respropv = json.loads(propv)
item_dict[ki]=respropv
if item_dict != {} and item_dict != [{}]:
if outcome > 0:
if item_dict not in value_list:
value_list.append(item_dict)
#print(value_list)
if value_list != []:
itemdict={}
definitivedict[key]=[]
v_array=[]
for itemvl in value_list:
for kvl, vvl in itemvl.items():
if isinstance(vvl, str):
if '|' in vvl:
itemv={}
v_array = vvl.split('|')
itemv[kvl]=v_array
v_key = kvl
elif isinstance(vvl, dict):
v1_array=[]
itemdict[kvl]={}
v1_keys = []
for kvl1, vvl1 in vvl.items():
itemdict[kvl][kvl1]={}
if isinstance(vvl1, str) and '|' in vvl1:
vvl1_array = vvl1.split('|')
for vvlitem in vvl1_array:
v1_array.append(vvlitem)
v1_bigkeys = kvl
if kvl1 not in v1_keys:
v1_keys.append(kvl1)
if v1_keys != []:
n=0
list_to_def=[]
half_array_number = len(v1_array)/2
itemdict[v1_bigkeys]={}
while n < int(half_array_number):
newdict={}
newdict[v1_bigkeys]={}
num=int(half_array_number+n)
newdict[v_key]=v_array[n]
newdict[v1_bigkeys][v1_keys[0]]=v1_array[n]
newdict[v1_bigkeys][v1_keys[1]]=v1_array[num]
list_to_def.append(newdict)
n +=1
for itemldf in list_to_def:
definitivedict[key].append(itemldf)
elif len(v_array) > 1:
list_to_def=[]
for itva in v_array:
newdict={}
newdict[v_key]=itva
list_to_def.append(newdict)
for itemldf in list_to_def:
definitivedict[key].append(itemldf)
else:
for itemvl in value_list:
definitivedict[key].append(itemvl)
else:
if key == 'caseLevelData':
definitivedict[key]=[]
definitivedict[key].append(item_dict)
else:
definitivedict[key]=item_dict
elif isinstance(value, dict):
value_dict={}
for kd, vd in value.items():
if isinstance(vd, list):
vd_list=[]
value_dict[kd]={}
for itemvd in vd:
if isinstance(itemvd, dict):
dict_mol={}
for kd1, vd1 in itemvd.items():
if isinstance(vd1, dict):
value_dict[kd][kd1]={}
for kd2, vd2 in vd1.items():
new_item = ""
new_item = key + "|" + kd + "|" + kd1 + "|" + kd2
for propk, propv in dict_of_properties.items():
if propk == new_item:
value_dict[kd][kd1][kd2]=propv
if value_dict[kd][kd1]=={}:
del value_dict[kd][kd1]
elif isinstance(vd1, list):
value_dict[kd][kd1]={}
for item in vd1:
for kd2, vd2 in item.items():
if isinstance(vd2, list):
value_dict[kd][kd1][kd2]={}
for itemvd2 in vd2:
for kd3, vd3 in itemvd2.items():
new_item = ""
new_item = key + "|" + kd + "|" + kd1 + "|" + kd2 + "|" + kd3
for propk, propv in dict_of_properties.items():
if propk == new_item:
value_dict[kd][kd1][kd2][kd3]=propv
else:
new_item = ""
new_item = key + "|" + kd + "|" + kd1 + "|" + kd2
for propk, propv in dict_of_properties.items():
if propk == new_item:
value_dict[kd][kd1][kd2]=propv
if value_dict[kd][kd1]=={}:
del value_dict[kd][kd1]
else:
new_item = ""
new_item = key + "|" + kd + "|" + kd1
for propk, propv in dict_of_properties.items():
if propk == new_item:
if '|' in propv:
propv_splitted = propv.split('|')
for itemsplitted in propv:
value_dict[kd][kd1]=propv_splitted
if value_dict not in vd_list:
vd_list.append(value_dict)
else:
if value_dict == {}:
value_dict[kd]={}
if 'molecularEffects' in kd:
try:
dict_mol[kd1]=propv
value_dict[kd].append(dict_mol)
except Exception:
dict_mol[kd1]=propv
value_dict[kd]=[]
else:
value_dict[kd][kd1]=propv
else:
new_item = ""
new_item = key + "|" + kd
for propk, propv in dict_of_properties.items():
if propk == new_item:
value_dict[kd]=[]
value_dict[kd].append(propv)
value_dict = {ka:va for ka,va in value_dict.items() if va != {}}
if value_dict != {}:
definitivedict[key]=value_dict
elif isinstance(vd, dict):
value_dict[kd]={}
for kd1, vd1 in vd.items():
if isinstance(vd1, dict):
value_dict[kd][kd1]={}
for kd2, vd2 in vd1.items():
if isinstance(vd2, dict):
value_dict[kd][kd1][kd2]={}
for kd3, vd3 in vd2.items():
new_item = ""
new_item = key + "|" + kd + "|" + kd1 + "|" + kd2 + "|" + kd3
for propk, propv in dict_of_properties.items():
if propk == new_item:
value_dict[kd][kd1][kd2][kd3]=propv
definitivedict[key]=value_dict
else:
new_item = ""
new_item = key + "|" + kd + "|" + kd1 + "|" + kd2
for propk, propv in dict_of_properties.items():
if propk == new_item:
value_dict[kd][kd1][kd2]=propv
definitivedict[key]=value_dict
else:
new_item = ""
new_item = key + "|" + kd + "|" + kd1
for propk, propv in dict_of_properties.items():
if propk == new_item:
value_dict[kd][kd1]=propv
definitivedict[key]=value_dict
else:
new_item = ""
new_item = key + "|" + kd
for propk, propv in dict_of_properties.items():
if propk == new_item:
value_dict[kd]=propv
definitivedict[key]=value_dict
else:
new_item = ""
new_item = key + "|" + kd
for propk, propv in dict_of_properties.items():
if propk == new_item:
value_dict[kd]=propv
definitivedict[key]=value_dict
else:
new_item = ""
new_item = key
for propk, propv in dict_of_properties.items():
if propk == new_item:
definitivedict[key]=propv
#print(definitivedict)
GenomicVariations(**definitivedict)
definitivedict["datasetId"]=conf.datasetId
total_dict.append(definitivedict)
pbar.update(1)
if i > num_rows:
break
i+=1
pbar.close()
return total_dict, i
dict_generado, total_i=generate(dict_properties,list_of_headers)
output = conf.output_docs_folder + 'genomicVariations.json'
if total_i-1 > 0:
print('Successfully converted {} registries into {}'.format(total_i-1, output))
else:
print('No registries found.')
with open(output, 'w') as f:
json.dump(dict_generado, f)