-
Notifications
You must be signed in to change notification settings - Fork 0
/
kegg_query.py
645 lines (542 loc) · 17.8 KB
/
kegg_query.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#!/usr/bin/env python3
"""Fetch hierarchical mappings and descriptions of a given list of KEGG
Orthology (KO) entries from the KEGG server.
Usage:
python me.py ko.list
Notes:
This script utilizes the official KEGG API (https://www.kegg.jp/kegg/rest/
keggapi.html) to query the KEGG database and retrieve relevant information
in the following categories: orthology (KO), module, pathway, reaction,
reaction class, compound, and disease.
Tested and working with KEGG release 105.0+.
Restrictions:
The official website states:
"KEGG API is provided for academic use by academic users belonging to
academic institutions." (https://www.kegg.jp/kegg/rest/)
"The maximum number of identifiers that can be given is 10 (per query)."
(https://www.kegg.jp/kegg/rest/keggapi.html)
References:
The latest KEGG paper (Kanehisa et al., 2021):
https://academic.oup.com/nar/article/49/D1/D545/5943834
"""
import sys
from time import sleep
from datetime import datetime
from functools import partial
from urllib.request import urlopen, HTTPError, URLError
__author__ = 'Qiyun Zhu'
__license__ = 'BSD-3-Clause'
__version__ = '0.0.2-dev'
__email__ = 'qiyunzhu@gmail.com'
# network connection parameters
server = 'http://rest.kegg.jp/'
step = 10 # no. of entries per query (max. 10 according to policy)
delay = 2 # time gap between two queries (sec)
retries = 5 # no. retries on failed query
timeout = 60 # waiting time before giving up (sec)
def fetch(api):
"""Fetch content from KEGG server.
Parameters
----------
api : str
RESTful API command.
Returns
-------
list of str
Lines of text.
"""
for i in range(retries):
if i:
print('Retrying...', end=' ', flush=True)
sleep(delay)
try:
with urlopen(server + api, timeout=timeout) as response:
return response.read().decode('utf-8').splitlines()
except (HTTPError, URLError) as e:
print(f'{e.code} {e.reason}.', end=' ', flush=True)
def kegg_info():
"""Return current KEGG release's version and statistics.
Returns
-------
list of str
KEGG release information.
"""
return [x[17:] for x in fetch('info/kegg')]
def batch_query(ids, cmd, f, name='entries', step=10):
"""Perform batch query and retrieve results.
Parameters
----------
ids : list of str
Entries to query.
cmd : str
API command ("get", "list", etc.)
f : function
Function to convert retrieved text into data.
name : str, optional
Task name to display.
step : int, optional
Number of queries to submit per time.
Returns
-------
dict of dict
Retrieved data.
"""
data = {}
print(f'Querying {len(ids)} {name}...', end=' ', flush=True)
counter = 0
ids = sorted(ids)
for i in range(0, len(ids), step):
batch = ids[i:i + step]
text = fetch(cmd + '/' + '+'.join(batch))
if text:
data = {**data, **f(text)}
counter += len(batch)
print(str(counter), end=' ', flush=True)
sleep(delay)
print('done.', flush=True)
return data
def parse_list(text, code=None):
"""Parse KEGG list files.
Parameters
----------
text : list of str
KEGG list text.
code : str, optional
Database code to strip from beginning.
Returns
-------
dict
Entry to description dictionary.
Raises
------
ValueError
Entry has unexpected format (e.g., different code).
Examples
--------
C00001 H2O
C00002 ATP
C00003 NAD+
C00004 NADH
C00005 NADPH
"""
if not code:
return dict(x.split('\t') for x in text)
ll = len(code)
res = {}
for line in text:
key, value = line.split('\t')
if not key.startswith(code) or not key[ll:].isnumeric():
raise ValueError(f'Unexpected entry: {key}.')
res[key] = value
return res
def parse_flat(text, skeys=(), mkeys=()):
"""Parse KEGG flat files.
Parameters
----------
text : list of str
KEGG flat text.
skeys : tuple of str, optional
Single keys to retrieve.
mkeys : tuple of str, optional
Multiple keys to retrieve.
Returns
-------
dict of dict
Processed data of each key under each entry.
Examples
--------
ENTRY K00699 KO
NAME UGT
DEFINITION glucuronosyltransferase [EC:2.4.1.17]
PATHWAY ko00040 Pentose and glucuronate interconversions
ko00053 Ascorbate and aldarate metabolism
ko00140 Steroid hormone biosynthesis
MODULE M00014 Glucuronate pathway (uronate pathway)
M00129 Ascorbate biosynthesis, animals, glucose-1P => ...
DISEASE H00208 Hyperbilirubinemia
H01593 Osteoporosis
"""
# data structure
data = {}
# current record
entry = None # current entry
mkey = None # current multi key
# line heads
sheads = tuple(x.upper().ljust(12) for x in skeys)
mheads = tuple(x.upper().ljust(12) for x in mkeys)
for line in text:
# record starts
if line.startswith('ENTRY '):
entry = line[12:].split()[0]
data[entry] = {}
continue
# record ends
if line == '///':
entry, mkey = None, None
# single keys
if line.startswith(sheads):
skey = skeys[sheads.index(line[:12])]
data[entry][skey] = line[12:].rstrip(';')
mkey = None
continue
# multi keys
if line.startswith(mheads):
mkey = mkeys[mheads.index(line[:12])]
data[entry][mkey] = []
# clear current key
elif not line.startswith(' '):
mkey = None
# append targets to a multi key
if mkey:
data[entry][mkey].append(line[12:])
return data
def extract_targets(data, keys):
"""Extract target entries and definitions from multiline terms.
Parameters
----------
data : dict of dict
Main data structure.
keys : list or tuple of str
Keys under which targets will be extracted.
Returns
-------
dict of dict
Definitions of individual targets under each key.
Examples
--------
K00699 glucuronosyltransferase [EC:2.4.1.17] [RN:R01383]
K01195,K14756 beta-glucuronidase [EC:3.2.1.31] [RN:R01478]
K00002 alcohol dehydrogenase (NADP+) [EC:1.1.1.2] [RN:R01481]
"""
names = {x: {} for x in keys}
for entry, datum in data.items():
for key in keys:
if key not in datum:
continue
res = []
for line in datum[key]:
# attempt to extract targets and names
left, found, right = line.partition(' ')
if not found:
continue
targets = []
for field in left.split(','):
# validate target entry format
if not field.isalnum():
targets = []
break
targets.append(field)
# add one or multiple targets
if targets:
res.extend(targets)
for target in targets:
names[key][target] = right
datum[key] = sorted(set(res))
return names
def extract_dblinks(data, dbs, key='dblinks'):
"""Extract database links from multiline terms.
Parameters
----------
data : dict of dict
Main data structure.
dbs : dict of str
Map of database codes to names.
key : str, optional
Key of database link terms.
Examples
--------
RN: R01478 R04979 R07818 R08127 R08260 R10830
COG: COG3250
GO: 0004566
"""
for entry, datum in data.items():
if key not in datum:
continue
for line in datum[key]:
try:
code, targets = line.split(': ', 1)
except IndexError:
continue
if code in dbs:
datum[dbs[code]] = targets.split()
del(datum[key])
def write_smap(data, key, fname):
"""Write one-to-one mapping to file.
Parameters
----------
data : dict of dict
Main data structure.
key : str
Key of data to write.
fname : str
Output file name.
"""
with open(fname, 'w') as f:
for entry, datum in data.items():
if key in datum:
print(entry, datum[key], sep='\t', file=f)
def write_mmap(data, key, fname):
"""Write one-to-many mapping to file.
Parameters
----------
data : dict of dict
Main data structure.
key : str
Key of data to write.
fname : str
Output file name.
"""
with open(fname, 'w') as f:
for entry, datum in data.items():
if key in datum:
targets = []
for value in datum[key]:
targets.extend(value.split(','))
print(entry, '\t'.join(targets), sep='\t', file=f)
def write_names(names, fname):
"""Write names / descriptions of entries to file.
Parameters
----------
names : dict
Name dictionary.
fname : str
Output file name.
"""
with open(fname, 'w') as f:
for key, name in sorted(names.items()):
print(key, name, sep='\t', file=f)
def write_all(name, data, skeys=[], mkeys=[]):
"""Write all data to file.
Parameters
----------
name : str
Name of current analysis.
data : dict of dict
Main data structure.
skeys : iterable of str
Single keys of data to write.
mkeys : iterable of str
Multiple keys of data to write.
"""
for key in skeys:
write_smap(data, key, f'{name}_{key}.txt')
for key in mkeys:
stem = 'ko' if key == 'orthology' else key
write_mmap(data, key, f'{name}-to-{stem}.txt')
def rename_paths(code, data, names=None):
"""Convert pathway entries using a given prefix.
Parameters
----------
code : str
New pathway code.
data : dict of dict
Main data structure.
names : dict of dict
Also rename pathways in name dictionary.
Raises
------
ValueError
Entry has unexpected format (e.g., different code).
Notes
-----
This function converts among different formats of KEGG pathway entries.
According to the KEGG website, available prefixes are:
map: manually drawn reference pathway
ko: reference pathway highlighting KOs
ec: reference metabolic pathway highlighting EC numbers
rn: reference metabolic pathway highlighting reactions
<org>: organism-specific pathway generated by converting KOs to gene
identifiers
For duplicate entries after conversion, only the first will be kept.
"""
# rename pathways in data
for entry, datum in data.items():
if 'pathway' in datum:
newpaths = {}
for path in datum['pathway']:
path = f'{code}{path[-5:]}'
if path not in newpaths:
newpaths[path] = ''
datum['pathway'] = list(newpaths.keys())
# rename pathways in names
if names and 'pathway' in names:
newnames = {}
for path, name in names['pathway'].items():
path = f'{code}{path[-5:]}'
if path not in newnames:
newnames[path] = name
names['pathway'] = newnames
def get_ecs(definition):
"""Extract EC numbers from a KO definition.
Parameters
----------
definition : str
KO definition.
Returns
-------
list of str
Extracted EC numbers.
Examples
--------
K00930 acetylglutamate kinase [EC:2.7.2.8]
K02618 oxepin-CoA hydrolase [EC:3.3.2.12 1.2.1.91]
K09866 aquaporin-4
"""
if definition.endswith(']'):
idx = definition.find(' [EC:')
if idx > 0:
return definition[idx + 5:-1].split()
def get_compounds(equation):
"""Extract compound entries from an equation.
Parameters
----------
equation : str
Equation string.
Returns
-------
list of str
Compounds extracted from the left side of equation.
list of str
Compounds extracted from the right side of equation.
Examples
--------
C00068 + C00001 <=> C01081 + C00009
C00029 + C00001 + 2 C00003 <=> C00167 + 2 C00004 + 2 C00080
G10481(n+1) + G10620 <=> G10481(n) + G11108
C17207(n) + (n-2) C00009 <=> C20861 + (n-2) C00636
"""
res = []
for side in equation.split(' <=> '):
cpds = []
for field in side.split(' + '):
idx = field.find('C')
if idx >= 0:
cpd = field[idx:idx + 6]
if len(cpd) == 6 and cpd[1:].isdigit():
cpds.append(cpd)
res.append(sorted(set(cpds)))
return res
def get_classes(data, key='class'):
"""Extract multiple classes from a single line.
Parameters
----------
data : dict of dict
Main data structure.
key : str, optional
Key under which classes will be extracted.
names
-----
A class line is delimited by "; ". Example:
Pathway modules; Carbohydrate metabolism
"""
for entry, datum in data.items():
if key in datum:
datum[key] = datum[key].split('; ')
def main():
if len(sys.argv) < 2:
sys.exit(__doc__)
print(f'Task started at {datetime.now()}.')
# get KEGG release info
text = kegg_info()
print('KEGG ' + text[1])
with open('kegg_info.txt', 'w') as f:
for line in text:
print(line, file=f)
# read query KOs
with open(sys.argv[1], 'r') as f:
kos = sorted(set(x.split('\t')[0] for x in f.read(
).splitlines() if not x.startswith('#')))
print(f'KO entries to query: {len(kos)}.')
# orthology (KO)
skeys = ('name',)
mkeys = ('module', 'pathway', 'disease', 'dblinks')
f = partial(parse_flat, skeys=skeys, mkeys=mkeys)
data = batch_query(kos, 'get', f, name='KOs')
for ko, datum in data.items():
if 'name' in datum:
ecs = get_ecs(datum['name'])
if ecs:
datum['ec'] = ecs
names = extract_targets(data, ('module', 'pathway', 'disease'))
extract_dblinks(data, {'RN': 'reaction', 'COG': 'cog', 'GO': 'go'})
mds = names['module'].keys()
paths = names['pathway'].keys()
dses = names['disease'].keys()
rns = set().union(*[x['reaction'] for x in data.values()
if 'reaction' in x])
mkeys = ('module', 'pathway', 'disease', 'ec', 'reaction', 'cog', 'go')
write_all('ko', data, skeys, mkeys)
# reaction
skeys = ('name', 'definition', 'equation', 'enzyme')
mkeys = ('orthology', 'module', 'pathway', 'rclass')
f = partial(parse_flat, skeys=skeys, mkeys=mkeys)
data = batch_query(rns, 'get', f, name='reactions')
names = extract_targets(data, mkeys)
for entry, datum in data.items():
if 'enzyme' in datum:
datum['enzyme'] = datum['enzyme'].split()
if 'equation' in datum:
left, right = get_compounds(datum['equation'])
if left:
datum['left_compound'] = left
if right:
datum['right_compound'] = right
both = sorted(set(left + right))
if both:
datum['compound'] = both
rename_paths('map', data, names)
skeys = ('name', 'definition', 'equation')
mkeys = ('orthology', 'module', 'pathway', 'rclass', 'enzyme', 'compound',
'left_compound', 'right_compound')
write_all('reaction', data, skeys, mkeys)
cpds = set().union(*[x['compound'] for x in data.values()
if 'compound' in x])
rcs = names['rclass'].keys()
mds = set(mds).union(names['module'].keys())
paths = set(paths).union(names['pathway'].keys())
# reaction class
f = partial(parse_list, code='RC')
names = batch_query(rcs, 'list', f, name='reaction classes')
write_names(names, 'rclass_name.txt')
# compound
f = partial(parse_list, code='C')
names = batch_query(cpds, 'list', f, name='compounds')
write_names(names, 'compound_name.txt')
# module
skeys = ('name', 'definition', 'class')
mkeys = ('orthology', 'pathway', 'reaction', 'compound')
f = partial(parse_flat, skeys=skeys, mkeys=mkeys)
data = batch_query(mds, 'get', f, name='modules')
get_classes(data)
names = extract_targets(data, mkeys)
skeys = ('name', 'definition')
mkeys = ('orthology', 'pathway', 'reaction', 'compound', 'class')
write_all('module', data, skeys, mkeys)
paths = set(paths).union(names['pathway'].keys())
# pathway (map)
skeys = ('name', 'class')
mkeys = ('module', 'disease')
f = partial(parse_flat, skeys=skeys, mkeys=mkeys)
data = batch_query(paths, 'get', f, name='pathways (map)')
get_classes(data)
names = extract_targets(data, mkeys)
skeys = ('name',)
mkeys = ('module', 'disease', 'class')
write_all('pathway', data, skeys, mkeys)
dses = set(dses).union(names['disease'].keys())
# pathway (ko)
mkeys = ('orthology', 'compound')
paths_ = set([f'ko{x[-5:]}' for x in paths])
f = partial(parse_flat, mkeys=mkeys)
data = batch_query(paths_, 'get', f, name='pathways (ko)')
get_classes(data)
extract_targets(data, mkeys)
data_ = {f'map{k[-5:]}': v for k, v in data.items()}
write_all('pathway', data_, (), mkeys)
# disease
f = partial(parse_list, code='H')
names = batch_query(dses, 'list', f, name='diseases')
write_names(names, 'disease_name.txt')
print(f'Task completed at {datetime.now()}.')
if __name__ == '__main__':
main()