# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import bio2bel_reactome
import itertools as itt
import pandas as pd

manager = bio2bel_reactome.Manager()

top_level_pathways = manager.get_all_top_hierarchy_pathways()

top_level = [
    pathway
    for pathway in top_level_pathways
    if 'HSA' in pathway.resource_id
]


def traverse(parent_pathway):
    
    if not parent_pathway.children:
        return []
    
    result = [
        {'Source Resource': 'reactome', 
         'Source ID': pathway.resource_id, 
         'Source Name': pathway.name, 
         'Mapping Type': 'isPartOf', 
         'Target Resource': 'reactome',
         'Target ID': parent_pathway.resource_id, 
         'Target Name': parent_pathway.name,
        }
        for pathway in parent_pathway.children
    ]
        
    for pathway in parent_pathway.children:
        result.extend(
            traverse(pathway)
        )
    
    return result
    


reactome_hierarchy = list(itt.chain.from_iterable([
    traverse(pathway)
    for pathway in top_level
]))

len(reactome_hierarchy)

df = pd.DataFrame(reactome_hierarchy)

df.to_csv('reactome_hierarchy.tsv', index=False, sep='\t')



StarcoderdataJupyterScriptsDedupFiltered