1
$\begingroup$

I want to obtain a direct graph from KEGG as well as the genes present in a single term. While it is pretty easy to get list of genes/compounds that are part of a specific category, I am kinda stucked to obtain the hierarchical relationships between terms. The docs on biopython.KGML are not really helpful. This is what I have so far

from Bio import SeqIO
import Bio.KEGG.REST as kegg
from Bio.KEGG.KGML import KGML_parser
from Bio.Graphics.KGML_vis import KGMLCanvas
from Bio.Graphics.ColorSpiral import ColorSpiral
from itertools import chain

k =  kegg.kegg_list('pathway', 'pae').read()
todf = []
for x in k.split('\n'):
    if x=='':
        next
    dd = x.split(' - ')
    dd = [v.split('\t') if '\t' in v else v for v in dd]
    dd = list(chain.from_iterable(dd))[:2]
    try:
        ll= kegg.kegg_get(dd[0]).read().split('GENE')[-1]
        ll = [x.lstrip() for x in ll.split('COMPOUND')[0].split('\n')]
        ll= [x.rstrip().split('\t') for x in ll]
        for gn in ll:
            gn.extend(dd)
        todf.append(pd.DataFrame(ll))
    except IndexError:
        pass
todf = pd.concat(todf)
# remove compounds and random strings
todf= todf[todf[0].str.startswith('PA')]
todf[0] = [x.split(';')[0] if ';' in x else x for x in todf[0]]
todf[0] = todf[0].str.split('  ')
todf=todf.explode(0)

and now I have the protein to term relationship but not sure how to get the hierarchy between terms. Any idea?

$\endgroup$

1 Answer 1

2
$\begingroup$

Ok realized it was much easier than expected. On KEGG it is possible to DL a json file, which is intrinsically ordered. So this parses it into a df

import pandas as pd 
import json

todf = []

kegg = json.load(open('meta/KEGG_pae00001.json'))
for main in kegg['children']:
    for broad in main['children']:
        for sub in broad['children']:
            for gn in sub.get('children', [None]):
                if gn:
                     nm = gn['name'].split(';')[0].split(' ')
                     todf.append([nm[0], nm[1], sub['name'], broad['name'], main['name']])
                else:
                     # some terms have no children
                     pass
                     #print(sub)
            
            
            
todf = pd.DataFrame(todf)
todf.columns=['Locus', 'GeneName', 'Cat1', 'Cat2', 'Cat3']
todf.to_csv('meta/kegg_parsed.csv')
$\endgroup$
1
  • $\begingroup$ Hi, assuming this answer works for you, could you please mark this as the accepted answer? That will help the automatic answer-finding algorithms do the right thing. $\endgroup$
    – gringer
    Commented Sep 26, 2022 at 4:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.