forked from nconrad/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaps-to-json.py
More file actions
executable file
·457 lines (329 loc) · 14 KB
/
maps-to-json.py
File metadata and controls
executable file
·457 lines (329 loc) · 14 KB
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
#!/usr/bin/env python
#
# Usage:
# ./maps-to-json.py [save|save-all|convert] <file_name|directory> <workspace_path>
#
# Params:
#
import io
import json
import sys
import os
import argparse
from xml.dom import minidom
from api.patric import workspace as wsclient
from api.kbase import workspace as kbwsclient
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file', help="file to upload")
parser.add_argument('-d', '--dir', help="directory to upload")
parser.add_argument('-w', '--ws', help="workspace path where files will be saved", required=True)
parser.add_argument('-k', '--kbase', help="save to kbase workspace", action='store_true')
args = parser.parse_args()
with open(os.environ['HOME'] + '/.rastauth') as file:
token = file.read()
print '\n Token: ', token
#fba = fbaclient.fbaModelServices('http://kbase.us/services/KBaseFBAModeling'); #kbase
ws = wsclient.Workspace(token=token); #patric
kbws = kbwsclient.Workspace(url="https://ci.kbase.us/services/ws"); #kbase
# this is the kegg rxn/cpds to seed mapping json files.
with open('mappings/rxn_mapping.json') as file:
json_str = file.read()
rxn_mapping = json.loads(json_str)
with open('mappings/cpd_mapping.json') as file:
json_str = file.read()
cpd_mapping = json.loads(json_str)
MAPSJSON = []
def graph_to_json(file_name):
print 'Converting graph data from file:', file_name
try:
xmldoc = minidom.parse(file_name)
except:
sys.stderr.write("could not parse xml file: "+file_name+'\n')
return
itemlist = xmldoc.getElementsByTagName('pathway')
try:
name = itemlist[0].attributes['title'].value
except:
name = "None"
map_number = itemlist[0].attributes['number'].value
link = itemlist[0].attributes['link'].value
json_obj = {'id': 'map'+map_number,
'name': name,
'link': link,
'source_id': 'map'+map_number,
'source': 'KEGG',
'reactions':[],
'compounds':[],
'reaction_ids': [],
'compound_ids': [],
'linkedmaps': []
}
entries = xmldoc.getElementsByTagName('entry')
reaction_ids = []
compound_ids = []
for obj in entries:
if (obj.attributes['type'].value == 'group'):
continue
if (obj.attributes['type'].value == 'map'):
e_obj = obj.attributes
g_obj = obj.getElementsByTagName('graphics')[0].attributes
obj = {"id": e_obj['id'].value,
"shape": g_obj['type'].value,
"name": g_obj['name'].value,
"h": int(g_obj['height'].value),
"w": int(g_obj['width'].value),
"x": int(g_obj['x'].value),
"y": int(g_obj['y'].value),
}
name = e_obj['name'].value;
if 'ec' in name:
name = name.split('ec')[1]
elif 'map' in name:
name = name.split('map')[1]
obj['map_id'] = 'map'+name
obj['map_ref'] = 'map'+name
json_obj['linkedmaps'].append(obj)
continue
if (obj.attributes['type'].value == 'enzyme'
and obj.getElementsByTagName('graphics')):
try:
obj.attributes['reaction'].value
except:
continue
e_obj = obj.attributes
# get list of kegg rxn ids from the entry
kegg_rxns = e_obj['reaction'].value \
.replace('rn:','').split(' ')
reactions = xmldoc.getElementsByTagName('reaction') # substrate and product data from kgml
# for each kegg_rxn, get the seed rxn id, add to list
rxns = []
for kegg_rxn in kegg_rxns:
try:
rxn_id = rxn_mapping[kegg_rxn]
except:
#sys.stderr.write("Warning: [Converting "+file_name+
# "] Could not find seed rxn id for KEGG id: "+kegg_rxn+'\n')
rxn_id = kegg_rxn
rxns.append(rxn_id)
reaction_ids.append(rxn_id)
for reaction in reactions:
r_obj = reaction.attributes
#rn_ids = r_obj['name'].value.split(' ')
#rn_ids = [id.split(':')[1] for id in rn_ids]
#print rn_ids
if r_obj['id'].value == e_obj['id'].value:
if r_obj['type'].value == 'reversible':
reversible = 1
else:
reversible = 0
substrates = []
for substrate in reaction.getElementsByTagName('substrate'):
s_obj = {'compound_ref': substrate.attributes['name'].value.split(':')[1],
'id': int(substrate.attributes['id'].value)}
substrates.append(s_obj)
#substrates.append(substrate.attributes['name'].value.split(':')[1]
# +'/'+substrate.attributes['id'].value)
products = []
for product in reaction.getElementsByTagName('product'):
p_obj = {'compound_ref': product.attributes['name'].value.split(':')[1],
'id': int(product.attributes['id'].value)}
products.append(p_obj)
#products.append(product.attributes['name'].value.split(':')[1]
# +'/'+product.attributes['id'].value)
break
#json_obj['arrows'].append(obj)
g_obj = obj.getElementsByTagName('graphics')[0].attributes
try:
link = e_obj['link'].value
except:
#sys.stderr.write("Warning: [Converting "+file_name+
# "] Could not find link for reaction: "+kegg_rxn+'\n')
pass
x = y = h = w = False
try:
x = int(g_obj['x'].value)
y = int(g_obj['y'].value)
h = int(g_obj['height'].value)
w = int(g_obj['width'].value)
except:
#sys.stderr.write("Warning: [Converting "+file_name+
# "] Could not find coordinates for reaction: "+kegg_rxn+'\n')
pass
obj = {"id": e_obj['id'].value,
"rxns": rxns,
"ec": e_obj['name'].value,
"shape": g_obj['type'].value,
"name": g_obj['name'].value,
}
try:
if substrates:
obj['substrate_refs'] = substrates
except:
obj['substrate_refs'] = []
#sys.stderr.write("Warning: [Converting "+file_name+
# "] Could not find substrates and products for: "+kegg_rxn+'\n')
try:
if products:
obj['product_refs'] = products
except:
obj['product_refs'] = []
#sys.stderr.write("Warning: [Converting "+file_name+
# "] Could not find substrates and products for: "+kegg_rxn+'\n')
try:
obj['reversible'] = reversible
except:
obj['reversible'] = 0
#sys.stderr.write("Warning: [Converting "+file_name+
# "] Could not find type (reversibility) for: "+kegg_rxn+'\n')
if link: obj['link'] = link
if x: obj['x'] = x
else: obj['x'] = 0
if y: obj['y'] = y
else: obj['y'] = 0
if h: obj['h'] = h
else: obj['h'] = 0
if w: obj['w'] = w
else: obj['w'] = 0
json_obj['reactions'].append(obj)
elif (obj.attributes['type'].value == 'compound'
and obj.getElementsByTagName('graphics') ):
e_obj = obj.attributes
kegg_cpds = e_obj['name'].value \
.replace('cpd:','').split(' ')
# for each kegg_cpd, get the seed cpd id, add to list
cpds = []
for kegg_cpd in kegg_cpds:
try:
# replace this with seed mapping result in future
cpd_id = cpd_mapping[kegg_cpd] # get seed_id
label = fba.get_compounds({'compounds': [cpd_id]})[0]['name']
#print label
except:
#sys.stderr.write("Warning: [Converting "+file_name+
# "] Could not find seed cpd id for KEGG id: "+kegg_cpd+'\n')
cpd_id = kegg_cpd
label = 'No Label Available'
cpds.append( cpd_id )
compound_ids.append(cpd_id)
g_obj = obj.getElementsByTagName('graphics')[0].attributes
obj = {"id": e_obj['id'].value,
"name": g_obj['name'].value,
"label": label,
"cpds": cpds,
"ec": e_obj['name'].value,
"link": e_obj['link'].value,
"shape": g_obj['type'].value,
"x": int(g_obj['x'].value),
"y": int(g_obj['y'].value),
"h": int(g_obj['height'].value),
"w": int(g_obj['width'].value),
"link_refs": [] #fixme: I don't know what this is...
}
json_obj['compounds'].append(obj)
#print json_obj
relations = xmldoc.getElementsByTagName('relation')
for obj in relations:
if (obj.attributes['type'].value == 'maplink'):
entry1 = obj.attributes['entry1'].value
entry2 = obj.attributes['entry2'].value
for maplink in json_obj['linkedmaps']:
maplink['connections'] = [entry1, entry2]
json_obj['groups'] = getGroups(json_obj['reactions'])
json_obj['reaction_ids'] = reaction_ids
json_obj['compound_ids'] = compound_ids
#print json_obj
json_file = file_name.replace('.xml', '.json')
outfile = open(json_file, 'w')
json.dump(json_obj, outfile)
print "Graph data converted to:", json_file
print
def getGroups(rxns):
groups = [];
grouped_ids = [];
#create groups
for rxn in rxns:
group = {'rxn_ids': []};
#skip any reaction that has already been grouped
if rxn['id'] in grouped_ids: continue
group['rxn_ids'].append(rxn['id'])
grouped_ids.append(rxn['id'])
#group['id'] = len(grouped_ids)
for rxn2 in rxns:
#skip the reaction in question already
if rxn2['id'] == rxn['id']: continue
#skip any reaction that has already been grouped
if rxn2['id'] in grouped_ids > 0: continue
#if reactions share same substrates and products, add to group
if (rxn['product_refs'] == rxn2['product_refs'] and
rxn['substrate_refs'] == rxn2['substrate_refs']):
group['rxn_ids'].append(rxn2['id'])
grouped_ids.append(rxn2['id'])
#group['id'] = len(grouped_ids)
groups.append(group)
# get mid points and unique id of groups
for group in groups:
xs = []
ys = []
for rxn_id in group['rxn_ids']:
for rxn in rxns:
if rxn['id'] == rxn_id:
xs.append(rxn['x'])
ys.append(rxn['y'])
x = (max(xs) + min(xs)) / 2
y = (max(ys) + min(ys)) / 2
group['x'] = x
group['y'] = y
return groups
def save_all():
print '\n\n*** Saving all json objects ***\n'
os.chdir(args.dir)
if (not args.kbase):
for name in os.listdir(os.getcwd()):
if name.endswith('.json'):
json_data = open(name)
data = json.load(json_data)
map_id = 'map'+name[2:7]
meta = {'reaction_ids': ','.join(data['reaction_ids']),
'compound_ids': ','.join(data['compound_ids']),
'name': data['name']}
print 'saving map: '+ws_path+map_id
test = ws.create({'objects': [[ws_path+map_id, 'json', meta, data]], 'overwrite': True})
print
if (args.kbase):
for name in os.listdir(os.getcwd()):
if name.endswith('.json'):
json_data=open(name)
data = json.load(json_data)
map_id = 'map'+name[2:7]
print 'saving map:', map_id, 'to', args.ws
test= kbws.save_objects({'workspace': args.ws,
'objects': [{
'name': map_id,
'data': data,
'meta': {'reaction_ids': ','.join(data['reaction_ids']),
'compound_ids': ','.join(data['compound_ids']),
'name': data['name']},
'type': 'KBaseBiochem.MetabolicMap'}
]})
print
# this function attempts to convert all .xml files in the current directory
def convert_all_data(dir_name):
os.chdir(dir_name)
print '\n\n*** Converting graph data ***\n'
for name in os.listdir(os.getcwd()):
if name.endswith('.xml'):
graph_to_json(name)
if __name__ == "__main__":
save_all()
#elif (sys.argv[1] == 'convert'):
# convert_all_data(sys.argv[2])
#ws = wsclient.Workspace()
# workspaces = ws.list_workspaces({})
# for arr in workspaces:
# if str(arr[1]) != 'nconrad': continue
# print str(arr) + '\n'
#convert_all_data()
# use one of these to convert a single file
#data_to_json(sys.argv[1])
#graph_to_json(sys.argv[1])
print 'Done.'