<jupyter_start><jupyter_text>MetaCyc Compounds dataThis notebook intends to re-organize compounds database of MetaCyc obtained from Pathway Tools data structureIn the original MetaCyc platform, the data is organized in the way that Pathway Tools API can retrieve data with heirrachical structure. We need those data in python platform in order to use them to connect with our previous generated model<jupyter_code>import pandas as pd
import numpy as np<jupyter_output><empty_output><jupyter_text>df = pd.read_csv('compounds.dat') , sep='\s+', header=None, skiprows=1) df = pd.read_table('compounds.dat', sep='\s+', header=None, skiprows=1)<jupyter_code>cpd = open('compounds.dat')
cpd
txt = []
for line in cpd:
    txt += [line.split()]<jupyter_output><empty_output><jupyter_text>txt<jupyter_code># I hack a .dat file by opening in excel and save it as .csv file which I'm more confident in
csv = pd.read_csv('compounds.csv', skiprows = 103, names=['Col'])
#csv.columns('Col')
csv_header = pd.read_csv('compounds.csv', skiprows = 23)
header = csv_header[1:80]
csv.head(30)
k1 = 0
ID = [] # 'UNIQUE-ID - '
ID_L = len('UNIQUE-ID - ')

k2 = 0
CName = [] # COMMON-NAME - 
CName_L = len('COMMON-NAME - ')

k3 = 0
Gibb = [] # GIBBS-0 - 
Gibb_L = len('GIBBS-0 - ')

k4 = 0
InChI = [] # INCHI - or NON-STANDARD-INCHI - 
InChI_L = len('INCHI - ')
NonInChI_L = len('NON-STANDARD-INCHI - ')

k5 = 0
SMILES = [] # SMILES - 
SMILES_L = len('SMILES - ')

k6 = 0
PubChemID = [] # DBLINKS - (PUBCHEM "id" NIL
PubChemID_L = len('DBLINKS - (PUBCHEM "')
PubChemID_After = len('" NIL')
for i in range(csv.shape[0]):
    
    data = csv['Col'][i]
    if data[:ID_L] == 'UNIQUE-ID - ':
        ID.append(data[(ID_L):])
        k1 += 1
    elif data[:CName_L] == 'COMMON-NAME - ':
        CName.append(data[(CName_L):])
        k2 += 1
    elif data[:Gibb_L] == 'GIBBS-0 - ':
        Gibb.append(data[(Gibb_L):])
        k3 += 1
    elif data[:InChI_L] == 'INCHI - ':
        InChI.append(data[(InChI_L):])
        k4 += 1
    elif data[:SMILES_L] == 'SMILES - ':
        SMILES.append(data[(SMILES_L):])
        k5 += 1
    # in case that there is no InChI available
    elif data[:NonInChI_L] == 'NON-STANDARD-INCHI - ' and k4 < k1:
        InChI.append(data[(NonInChI_L):])
        k4 += 1
    elif data[:PubChemID_L] == 'DBLINKS - (PUBCHEM "':
        CID = data[PubChemID_L:]
        for i in range(len(CID)):
            if CID[i:i+PubChemID_After] == '" NIL':
                PubChemID.append(CID[:i])
                k6 += 1
            else:
                pass
    else:
        pass
    
    if k2 < k1-1:
        CName.append('N/A')
        k2 += 1
    if k3 < k1-1:
        Gibb.append('N/A')
        k3 += 1
    if k4 < k1-1:
        InChI.append('N/A')
        k4 += 1
    if k5 < k1-1:
        SMILES.append('N/A')
        k5 += 1
    if k6 < k1-1:
        PubChemID.append('N/A')
        k6 += 1

print ('Number of compounds count is', k1, k2, k3, k4, k5, 'and', k6)
df_cpd = pd.DataFrame({'UNIQUE-ID' : ID,
                       'COMMON-NAME' : CName,
                       'GIBBS-0' : Gibb,
                       'INCHI' : InChI,
                       'SMILES' : SMILES,
                       'PubChemID' : PubChemID})
df_cpd.to_csv('df_cpd.csv')
df_cpd
# this approach takes longer to run and seem to be wrong in some step, 
# let's do the listing approach shown above

k1 = 0
ID = [] # 'UNIQUE-ID - '
ID_L = len('UNIQUE-ID - ')

k2 = 0
CName = [] # COMMON-NAME - 
CName_L = len('COMMON-NAME - ')

k3 = 0
Gibb = [] # GIBBS-0 - 
Gibb_L = len('GIBBS-0 - ')

k4 = 0
InChI = [] # INCHI - 
InChI_L = len('INCHI - ')

k5 = 0
SMILES = [] # SMILES - 
SMILES_L = len('SMILES - ')

df_cpd = pd.DataFrame(names = ['UNIQUE-ID',
                      'COMMON-NAME',
                      'GIBBS-0',
                      'INCHI',
                      'SMILES'])

k = 0

for i in range(csv.shape[0]):
    
    data = csv['Col'][i]
    if data[:ID_L] == 'UNIQUE-ID - ':
        df_cpd['UNIQUE-ID'][k] = (data[(ID_L):])
        k += 1

    elif data[:CName_L] == 'COMMON-NAME - ':
        df_cpd['COMMON-NAME'][k-1] = (data[(CName_L):])
        
    elif data[:Gibb_L] == 'GIBBS-0 - ':
        df_cpd['GIBBS-0'][k-1] = (data[(Gibb_L):])
        
    elif data[:InChI_L] == 'INCHI - ':
        df_cpd['INCHI'][k-1] = (data[(InChI_L):])
        
    elif data[:SMILES_L] == 'SMILES - ':
        df_cpd['SMILES'][k-1] = (data[(SMILES_L):])
        
    else:
        pass
    
print ('Number of compounds count is', k)<jupyter_output>Number of compounds count is 15263
{'fraction_non_alphanumeric': 0.16362837689435536, 'fraction_numerical': 0.022622446738414233, 'mean_word_length': 2.236673773987207, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '596225', 'license': 'MIT', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1822, 'n_words': 400, 'path': 'notebooks/MetaCyc_Compounds_Parsing_Draft.ipynb', 'repo_name': 'metamoles/metamoles', 'size': '4553'}