<p>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</p>
<pre><code>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')
</code></pre>

CC BY-SA 4.0
2022-09-26T04:10:26.130Z
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.
gringer
CC BY-SA 4.0
2022-09-23T21:28:36.233Z
2022-09-23T21:28:36.233Z
D.A.
<p>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</p>
<pre><code>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)
</code></pre>
<p>and now I have the protein to term relationship but not sure how to get the hierarchy between terms.
Any idea?</p>

<python><kegg>
KEGG retrieve hierarchy for organism in biopython
# KEGG retrieve hierarchy for organism in biopython

(Asked by: username_0 on 2022-09-23)

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?

**Tags:** python, kegg

---

## 1 answers

### Answer by username_0 on 2022-09-23. Score: 2

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')

> **Comments:**
> **username_1**: 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.  

---

