Example #1
0
def test_convert_all():
    network = kegg_tools.pathway_id_to_network('hsa00040')
    network = convert_all(network, verbose=True)
    print(sorted(network.nodes))
    ans = {
        'chemName': 'D-Ribulose 5-phosphate',
        'keggName': 'C00199',
        'hmdbNames': 'HMDB0000618',
        'databaseSource': 'KEGG',
        'speciesType': 'compound'
    }
    print(network.node['HMDB0000618'])
    print(network.node['DHDH'])
Example #2
0
def download_kegg(species='hsa', verbose=False):
    """
    Downloads every KEGG pathway to provided directory

    """
    if species == 'hsa':
        from magine.mappings.maps import convert_all
    kegg.organism = species
    list_of_kegg_pathways = [i[5:] for i in kegg.pathwayIds]
    if verbose:
        print("Number of pathways  = {}".format(len(list_of_kegg_pathways)))

    # keys are KEGG pathways ids, values are kegg pathways
    kegg_dict = dict()

    for pathway_id in list_of_kegg_pathways:
        pathway = kegg.get(pathway_id, "kgml")

        if pathway == 404:
            print("%s ended with 404 error" % pathway_id)
            continue

        graph, pathway_name = kgml_to_nx(pathway, species=species)
        if species == 'hsa':
            graph = convert_all(graph, species)
        kegg_dict[pathway_id] = graph
        if verbose:
            print('{} has {} nodes and {} edges'.format(pathway_name,
                                                        len(graph.nodes),
                                                        len(graph.edges)))

    # create a dictionary mapping species to pathways
    node_to_path = dict()
    for i in kegg_dict:
        for node in kegg_dict[i].nodes:
            if node not in node_to_path:
                node_to_path[node] = set()
            node_to_path[node].add(i)

    # save kegg pathway its to networks
    n = '{}_kegg_path_ids_to_networks.p.gz'.format(species)
    save_path_id_to_graph = os.path.join(network_data_dir, n)
    save_gzip_pickle(save_path_id_to_graph, kegg_dict)

    # save nodes to pathways
    n = '{}_kegg_node_to_pathway.p.gz'.format(species)
    save_node_to_path = os.path.join(network_data_dir, n)
    save_gzip_pickle(save_node_to_path, node_to_path)
Example #3
0
def load_all_of_kegg(species='hsa', fresh_download=False, verbose=False):
    """
    Combines all KEGG pathways into a single network

    Parameters
    ----------
    species : species
        Default 'hsa'
    fresh_download : bool
        Download kegg new
    verbose : bool

    Returns
    -------

    """
    p_name = os.path.join(network_data_dir,
                          '{}_all_of_kegg.p.gz'.format(species))
    # load in network if it exists
    if os.path.exists(p_name) and not fresh_download:
        if verbose:
            print('Reading in KEGG network')
        all_of_kegg = nx.read_gpickle(p_name)
    else:
        # create the network
        path_to_graph, _ = load_kegg_mappings(species,
                                              verbose=verbose,
                                              fresh_download=fresh_download)

        # merge into single networks
        all_of_kegg = utils.compose_all(path_to_graph.values())

        # relabel notes
        if species == 'hsa':
            from magine.mappings.maps import convert_all
            all_of_kegg = convert_all(all_of_kegg, species=species)

        # save network
        nx.write_gpickle(all_of_kegg, p_name)
    print("KEGG network {} nodes and {} edges".format(len(all_of_kegg.nodes),
                                                      len(all_of_kegg.edges)))
    return all_of_kegg
Example #4
0
def test_convert_all():
    network = kegg_tools.pathway_id_to_network('hsa00040')
    convert_all(network, verbose=True)