forked from SainsburyWellcomeCentre/lasagna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ara_json.py
executable file
·73 lines (48 loc) · 1.61 KB
/
ara_json.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
import json
import os
import sys
"""
Dumps ARA JSON as a flattened file that we can feed into our tree reader
"""
def importData(fname,verbose=False):
"""
Import from ARA JSON
"""
#Error check
if os.path.exists(fname)==False:
print("Can not find file " + fname)
return
if fname.lower().endswith('json')==False:
print("Data should be a JSON file")
return
#Build tree
with open(fname) as f:
obj = json.load(f)
flattenedTree = tree_flatten(obj['msg'][0])
colNames = 'id|parent|atlas_id|acronym|name|color'
return (flattenedTree,colNames)
def tree_flatten(obj,flattened=''):
if obj['parent_structure_id'] is None:
obj['parent_structure_id']=0
flattened = flattened+"{id}|{parent_id}|{atlas_id}|{acronym}|{name}|{color}\n".format(
id=obj['id'],
parent_id=obj['parent_structure_id'],
atlas_id=obj['atlas_id'],
acronym=obj['acronym'],
name=obj['name'],
color=obj['color_hex_triplet'])
for child in obj.get('children',[]):
flattened = tree_flatten(child, flattened=flattened)
return flattened
#----------------------------------------------------------------------------
if __name__ == '__main__':
if len(sys.argv)>1:
fname = sys.argv[1]
(flattened,colNames) = importData(fname)
returnTree=True
#Optionally run flattened structure through tree
if returnTree:
import tree
tree.importData(flattened.split('\n'),colSep='|',displayTree=True,headerLine=colNames)
else:
print(flattened)