<jupyter_start><jupyter_text>ModelSeed universal model- 2022-04-14- <NAME><jupyter_code>import requests
import json
import os
import pandas as pd
try:
    os.mkdir("../output/ModelSeed/")
except:
    None
try:
    os.mkdir("../input/ModelSeed/")
except:
    None
# r = requests.get("https://github.com/ModelSEED/ModelSEEDDatabase/blob/master/Biochemistry/Pathways/HopeScenarios.txt?raw=True")
# with open("../input/ModelSeed/HopeScenarios.txt", "w") as f:
#     f.write(r.text)

# r = requests.get("https://github.com/ModelSEED/ModelSEEDDatabase/blob/master/Biochemistry/Pathways/KEGG.pathways?raw=True")
# with open("../input/ModelSeed/KEGG.pathways", "w") as f:
#     f.write(r.text)

# r = requests.get("https://github.com/ModelSEED/ModelSEEDDatabase/blob/master/Biochemistry/Pathways/plantdefault.pathways.tsv?raw=True")
# with open("../input/ModelSeed/plantdefault.pathways.tsv", "w") as f:
#     f.write(r.text)<jupyter_output><empty_output><jupyter_text>pathway request<jupyter_code>r = requests.get("https://github.com/ModelSEED/ModelSEEDDatabase/blob/master/Biochemistry/Pathways/ModelSEED_Subsystems.tsv?raw=True")
with open("../input/ModelSeed/ModelSEED_Subsystems.tsv", "w") as f:
    f.write(r.text)<jupyter_output><empty_output><jupyter_text>compounds request<jupyter_code>r = requests.get("https://github.com/ModelSEED/ModelSEEDDatabase/blob/master/Biochemistry/compounds.tsv?raw=True")
with open("../input/ModelSeed/compounds.tsv", "w") as f:
    f.write(r.text)
r = requests.get("https://github.com/ModelSEED/ModelSEEDDatabase/blob/master/Biochemistry/compounds.json?raw=True")
with open("../input/ModelSeed/compounds.json", "w") as f:
    f.write(r.text)<jupyter_output><empty_output><jupyter_text>reactions request<jupyter_code>r = requests.get("https://github.com/ModelSEED/ModelSEEDDatabase/blob/master/Biochemistry/reactions.tsv?raw=True")
with open("../input/ModelSeed/reactions.tsv", "w") as f:
    f.write(r.text)
r = requests.get("https://github.com/ModelSEED/ModelSEEDDatabase/blob/master/Biochemistry/reactions.json?raw=True")
with open("../input/ModelSeed/reactions.json", "w") as f:
    f.write(r.text)
cpd_df = pd.read_csv("../input/ModelSeed/compounds.tsv", sep = "\t") # using json formatted compound table for parsing
rxn_df = pd.read_csv("../input/ModelSeed/reactions.tsv", sep = '\t')
pwy_df = pd.read_csv("../input/ModelSeed/ModelSEED_Subsystems.tsv", sep = '\t')
cpd_df.shape # matched with the json format! So using json is fine!
rxn_df.shape # matched with the json format! So using json is fine!
with open("../input/ModelSeed/compounds.json", "r") as f:
    cpd_list = json.load(f)
with open("../input/ModelSeed/reactions.json", "r") as f:
    rxn_list = json.load(f)
len(rxn_list)<jupyter_output><empty_output><jupyter_text>----<jupyter_code>import sys
from metDataModel.core import Compound, Reaction, Pathway, MetabolicModel
sys.path.append("/Users/gongm/Documents/projects/mass2chem/")
sys.path.append("/Users/gongm/Documents/projects/JMS/JMS/jms/")
import mass2chem
from jms import formula<jupyter_output><empty_output><jupyter_text>Porting compounds<jupyter_code>cpd.__dict__
myCpdlist = []
for seedCpd in cpd_list:
    cpd = Compound()
    cpd.abbreviation = seedCpd['abbreviation']
    cpd.id = seedCpd['id']
    cpd.internal_id = cpd.id
    cpd.db_ids = []
    try:
        for item in seedCpd['aliases']:
            record = tuple(item.split(": "))
            if record[0] == "Name":
                cpd.name = record[1]
            else:
                cpd.db_ids.append(record)
    except:
        None
    cpd.db_ids.append(('inchikey',seedCpd['inchikey']))
    cpd.charge = seedCpd['charge']
    cpd.charged_formula = seedCpd['formula']
    cpd.SMILES = seedCpd['smiles']
    
    try:
        cpd.neutral_formula = formula.adjust_charge_in_formula(cpd.charged_formula,cpd.charge)
    except:
        cpd.neutral_formula = ''
    try:
        cpd.neutral_mono_mass = round(mass2chem.formula.calculate_formula_mass(cpd.neutral_formula),6)
    except:
        cpd.neutral_mono_mass = 0
    myCpdlist.append(cpd)
len(myCpdlist)
[x.__dict__ for x in myCpdlist[:2]]<jupyter_output><empty_output><jupyter_text>Porting reactions<jupyter_code>rxn = Reaction()
rxn.__dict__
rxn_list[0]
rxn_list[0].keys()
test = rxn_list[10]['code']
rxn_list[10]['code']
import re
def extract_compounds(equation_code):
    [reactant_str, product_str] = equation_code.split("=")
    reactants = re.findall(r'\) (cpd\d+)\[',reactant_str)
    products = re.findall(r'\) (cpd\d+)\[',product_str)
    return(reactants,products)

reactants, products = extract_compounds(rxn_list[10]['code'])
def port_reaction(R):
    new = Reaction()
    new.id = R['id']
    reactants, products = extract_compounds(R['code'])
    new.reactants = reactants 
    new.products = products
    if R['ec_numbers']:
        if "|" in R['ec_numbers']:
            new.enzymes = ecs.split("|")
        else:
            new.enzymes = R['ec_numbers']       # this version of human-GEM may have it as string
    return new

myRxnlist = []
for R in rxn_list: 
    if R['is_transport']==0: # remove the transportation reactions
        myRxnlist.append(port_reaction(R))
print(f'There are {len(myRxnlist)} true reactions without considering transport in {len(rxn_list)} reactions')<jupyter_output>There are 38269 true reactions without considering transport in 43774 reactions<jupyter_text>----  Port pathways<jupyter_code>pwy = Pathway()
pwy.__dict__
subsys2reactions_dict = pwy_df.groupby('Sub-class')['Reaction'].apply(list).to_dict()
def generateMSPwy(number):
    res = "MSPwy" + str(number).zfill(4)
    return(res)
path_test = Pathway();path_test.__dict__
mysubsyslist = []
i = 1
for k,v in subsys2reactions_dict.items():
    new = Pathway()
    new.id = generateMSPwy(i)
    new.name = k
    new.source = "ModelSeed"
    new.list_of_reactions = v
    new.status: "testing"

    i +=1
    mysubsyslist.append(new)
len(mysubsyslist)<jupyter_output><empty_output><jupyter_text>Collect data and output<jupyter_code>note = """ModelSeed Universal Model. Add all reactions and compounds, """

## metabolicModel to export
MM = MetabolicModel()
MM.id = '' #
MM.meta_data = {
            'species': 'universal',
            'version': '',
            'sources': ['https://github.com/ModelSEED/ModelSEEDDatabase/blob/master/Biochemistry/, retrieved 2022-04-13'], #
            'status': '',
            'last_update': '20220413',  #
            'note': note,
        }
MM.list_of_pathways = [P.serialize() for P in mysubsyslist]
MM.list_of_reactions = [R.serialize() for R in  myRxnlist]
MM.list_of_compounds = [C.serialize() for C in myCpdlist]
# check output
[
MM.list_of_pathways[2],
MM.list_of_reactions[:2],
MM.list_of_compounds[100:102],
]
with open('../output/ModelSeed/Universal_ModelSeed.json', "w") as f:
    json.dump(MM.serialize(),f)
with open('../output/ModelSeed/Universal_ModelSeed.json', "r") as f:
    test = json.load(f)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15321654221711659, 'fraction_numerical': 0.009190120620333142, 'mean_word_length': 4.2368421052631575, 'pattern_counts': {'":': 2, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 9, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '64649', 'license': 'MIT', 'n_tokens_mistral': 2642, 'n_tokens_neox': 2527, 'n_words': 409, 'path': 'docs/ModelSeed_Universal_model.ipynb', 'repo_name': 'gmhhope/JMS', 'size': '6970'}