<jupyter_start><jupyter_text>Map from chemical names to KEGG IDs This notebook provides an example how to map from chemical names, e.g. '16-Hydroxypalmitate' to KEGG ID, e.g. C18218. The resulting dataframe can be used as input to GraphOmics analysis.<jupyter_code>import os
import pandas as pd
from bioservices import *<jupyter_output><empty_output><jupyter_text>Read CSV data<jupyter_code>base_dir = 'C:\\Users\\joewa\\Dropbox\\Analysis\\omics_integration\\covid19_data'
df = pd.read_csv(os.path.join(base_dir, 'compound_names.tsv'), sep='\t', index_col=0)
df.head()<jupyter_output><empty_output><jupyter_text>The names of the chemical compounds are stored in the index of the dataframe. For example:<jupyter_code>df.index.values[0:5]<jupyter_output><empty_output><jupyter_text>Print the total number of names<jupyter_code>len(df.index.values)<jupyter_output><empty_output><jupyter_text>Map Chemical Names to KEGG ID Iterate through names and query kegg to get the ID, if possible<jupyter_code>mapping = {}
for name in df.index.values:
    print('Processing', name)

    try:
        res = k.find('compound', name)    
        res = res.strip()
    except AttributeError:
        continue
        
    if len(res) == 0:
        continue
    
    tokens = res.split('\t')
    front = tokens[0].split(':')
    kegg_id = front[1]
    kegg_names = tokens[1]
    print(kegg_id, ':', kegg_names)    
    
    mapping[name] = kegg_id<jupyter_output><empty_output><jupyter_text>Print the total number of mapping we get<jupyter_code>print('Found %d/%d KEGG IDs' % (len(mapping), len(df.index.values)))<jupyter_output>Found 220/905 KEGG IDs<jupyter_text>Parsed dataframe: remove those with no mappings<jupyter_code>keep = set(mapping.keys())
drop = set([name for name in df.index.values if name not in keep])
len(drop), len(keep)
filtered_df = df.drop(drop)
filtered_df = filtered_df.rename(index=mapping)
filtered_df.head()<jupyter_output><empty_output><jupyter_text>Save output CSV<jupyter_code>filtered_df.to_csv(os.path.join(base_dir, 'compound_data.csv'))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1186359269932757, 'fraction_numerical': 0.010566762728146013, 'mean_word_length': 5.236526946107785, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '624715', 'license': 'MIT', 'n_tokens_mistral': 767, 'n_tokens_neox': 734, 'n_words': 165, 'path': 'graphomics/notebooks/mapping/chemical_names_to_kegg.ipynb', 'repo_name': 'joewandy/omics_integration', 'size': '2082'}