<jupyter_start><jupyter_text>Visualizing equivalent pathways in PathMe ViewerAs Reactome is hierarchically structured, subpathways of pathways must be accessed. Thus, here URLs for all merged networks of all equivalent pathways (including those Reactome subpathways) are generated.<jupyter_code>import collections
import pandas as pd

from bio2bel_reactome import Manager
reactome_manager = Manager()
prefix = 'http://0.0.0.0:5000/pathme/viewer?'
reactome_suffix = 'resources[]=reactome&pathways[]=%s'
kegg_suffix = 'resources[]=kegg&pathways[]=%s'
wikipathways_suffix = 'resources[]=wikipathways&pathways[]=%s'
reactome_ids = ['R-HSA-5358508','R-HSA-209968','R-HSA-195721','R-HSA-5683057','R-HSA-71336','R-HSA-1257604','R-HSA-168898','R-HSA-983705','R-HSA-157118','R-HSA-109581','R-HSA-428157','R-HSA-5358351','R-HSA-71403','R-HSA-69306','R-HSA-5693571','R-HSA-1640170','R-HSA-9006936','R-HSA-165159','R-HSA-448424','R-HSA-74182','R-HSA-1170546']               
kegg_ids = ['hsa03430','hsa04918','hsa04310','hsa04010','hsa00030','hsa04151','hsa04620','hsa04662','hsa04330','hsa04210','hsa00600','hsa04340','hsa00020','hsa03030','hsa03450','hsa04110','hsa04350','hsa04150','hsa04657','hsa00072','hsa04917']
wikipathways_ids = ['WP531','WP1981','WP363','WP382','WP134','WP4172','WP75','WP23','WP268','WP254','WP1422','WP47','WP78','WP466','WP438','WP179','WP366','WP1471','WP2112','WP311','WP2037']

# Reactome pathways not contained in RDF file
REACTOME_BLACK_LIST = ['R-HSA-2025928','R-HSA-9604323', 'R-HSA-9013700','R-HSA-9017802','R-HSA-168927','R-HSA-9006936', 'R-HSA-9014325', 'R-HSA-9013508', 'R-HSA-9013973', 'R-HSA-9013957', 'R-HSA-9013695']
def flatten(l):
    for el in l:
        if isinstance(el, collections.Iterable) and not isinstance(el, (str, bytes)):
            yield from flatten(el)
        else:
            yield el
def get_all_pathway_children_by_id(manager, reactome_id):
    
    pathway = manager.get_pathway_by_id(reactome_id)

    if not pathway.children:
        return pathway.reactome_id
    
    children = []
    
    for child in pathway.children:

        children.append(get_all_pathway_children_by_id(manager, child.reactome_id))
    
    return children
parent_to_child = dict()

for reactome_id in reactome_ids:
    
    all_children = get_all_pathway_children_by_id(reactome_manager, reactome_id)

    if isinstance(all_children, str):
        continue
        
    flattened_children = flatten(all_children)
    parent_to_child[reactome_id] = [pathway for pathway in flattened_children]<jupyter_output><empty_output><jupyter_text>16 Reactome pathways contain subpathways and therefore we concatenate each subpathway into the url<jupyter_code>len(parent_to_child)
merged_pathway_urls = []

for counter, reactome_id in enumerate(reactome_ids):
        
    base_url = prefix + kegg_suffix % (kegg_ids[counter])+'&'+wikipathways_suffix % (wikipathways_ids[counter])
    
    # Some of the main reactome pathways are not contained in the RDF file (~50 pathways in total)
    if reactome_id not in REACTOME_BLACK_LIST:
        base_url += '&' + reactome_suffix % reactome_id

    if reactome_id in parent_to_child:
        
        pathway_children = parent_to_child[reactome_id]
        
        base_url += '&'+'&'.join(
            [reactome_suffix % reactome_id
             for reactome_id in pathway_children
             # Check that the children are not in the black list (pathways not in the RDF)
            if reactome_id not in REACTOME_BLACK_LIST]
        )
        
    merged_pathway_urls.append(base_url)<jupyter_output><empty_output><jupyter_text>Table with database identifiers and URLS to visualize merged pathways in the PathMe Viewer<jupyter_code>def pandas_df_to_markdown_table(df):
    from IPython.display import Markdown, display
    fmt = ['---' for i in range(len(df.columns))]
    df_fmt = pd.DataFrame([fmt], columns=df.columns)
    df_formatted = pd.concat([df_fmt, df])
    display(Markdown(df_formatted.to_csv(sep="|", index=False)))
    
df = pd.DataFrame(
    {'KEGG': kegg_ids, 
     'Reactome': reactome_id, 
     'WikiPathways': wikipathways_ids,
     'url to visualize merged pathways': merged_pathway_urls
    },
)
pandas_df_to_markdown_table(df)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13428974600188145, 'fraction_numerical': 0.09148635936030103, 'mean_word_length': 4.6706666666666665, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '472698', 'license': 'Apache-2.0', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1530, 'n_words': 264, 'path': 'notebooks/case_scenarios/visualizing_similarity_equivalent_pathways.ipynb', 'repo_name': 'PathwayMerger/PathMe-Resources', 'size': '4252'}