<jupyter_start><jupyter_text>Genome-scale metabolic reconstruction<jupyter_code>from sympy import Matrix, Eq, var
from cameo import models
model = models.bigg.iJO1366
len(model.reactions)
len(model.metabolites)
len(model.genes)
model.medium
print(model.objective)<jupyter_output>Maximize
-1.0*BIOMASS_Ec_iJO1366_core_53p95M_reverse_5c8b1 + 1.0*BIOMASS_Ec_iJO1366_core_53p95M<jupyter_text>The stoichiometric matrix S<jupyter_code>S = model.to_array_based_model().S.todense()
S<jupyter_output><empty_output><jupyter_text>Steady-state mass balances: $\mathbf{S} \mathbf{v} = \mathbf{0}$<jupyter_code>S = Matrix()
print(S)
v = Matrix([var(reaction.id) for reaction in model.reactions])
print(v)<jupyter_output>Matrix([[ACALD], [ACALDt], [ACKr], [ACONTa], [ACONTb], [ACt2r], [ADK1], [AKGDH], [AKGt2r], [ALCD2x], [ATPM], [ATPS4r], [BIOMASS_Ecoli_core_w_GAM], [CO2t], [CS], [CYTBD], [D_LACt2], [ENO], [ETOHt2r], [EX_ac_e], [EX_acald_e], [EX_akg_e], [EX_co2_e], [EX_etoh_e], [EX_for_e], [EX_fru_e], [EX_fum_e], [EX_glc__D_e], [EX_gln__L_e], [EX_glu__L_e], [EX_h_e], [EX_h2o_e], [EX_lac__D_e], [EX_mal__L_e], [EX_nh4_e], [EX_o2_e], [EX_pi_e], [EX_pyr_e], [EX_succ_e], [FBA], [FBP], [FORt2], [FORti], [FRD7], [FRUpts2], [FUM], [FUMt2_2], [G6PDH2r], [GAPD], [GLCpts], [GLNS], [GLNabc], [GLUDy], [GLUN], [GLUSy], [GLUt2r], [GND], [H2Ot], [ICDHyr], [ICL], [LDH_D], [MALS], [MALt2_2], [MDH], [ME1], [ME2], [NADH16], [NADTRHD], [NH4t], [O2t], [PDH], [PFK], [PFL], [PGI], [PGK], [PGL], [PGM], [PIt2r], [PPC], [PPCK], [PPS], [PTAr], [PYK], [PYRt2], [RPE], [RPI], [SUCCt2_2], [SUCCt3], [SUCDi], [SUCOAS], [TALA], [THD2], [TKT1], [TKT2], [TPI]])<jupyter_text>The stoichiometry matrix S has the following dimensions.<jupyter_code>S.shape
v.shape
S.dot(v)<jupyter_output><empty_output><jupyter_text>Gene-Protein-Reaction Rules (GPRs)<jupyter_code>import pandas
pandas.DataFrame([[reaction.id, reaction.gene_name_reaction_rule] for reaction in model.reactions], columns=['reaction', 'GPR'])<jupyter_output><empty_output><jupyter_text>Exercises This is how the the stoichiometry matrix S looks like when visualized as a matrix plot.<jupyter_code>import matplotlib.pyplot as plt
plt.spy(S, precision=0.01, markersize=.1)<jupyter_output><empty_output><jupyter_text>1. What do you think the first diagonal blocks means?1. What do you think the long horizontal lines mean?  Solution  Solution 1 The diagonal pattern from column 0 to 330 corresponds to exchange reactions.<jupyter_code>len(model.exchanges)
model.reactions[0]<jupyter_output><empty_output><jupyter_text>Solution 2 The horizontal lines correspond to metabolites (mostly co-factors) that appear in many reactions.<jupyter_code>import networkx as nx
def reactions_to_network(reactions):
    edges = list()
    for reaction in reactions:
        substrates = list(reaction.reactants)
        for substrate in substrates:
            products = list(reaction.products)
            for product in products:
                if reaction.reversibility:
                    edges.append((product, substrate, dict(reaction=reaction)))
                    edges.append((substrate, product, dict(reaction=reaction)))
                elif reaction.lower_bound > 0:
                    edges.append((substrate, product, dict(reaction=reaction)))
                else:
                    edges.append((product, substrate, dict(reaction=reaction)))

    multi_graph = nx.MultiDiGraph(edges)
    return multi_graph
network = reactions_to_network(model.reactions)<jupyter_output><empty_output><jupyter_text>These metabolites are the reason metabolic network look like giant hairballs.<jupyter_code>nx.draw_spring(network)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15036774720784526, 'fraction_numerical': 0.02152002179242713, 'mean_word_length': 5.876404494382022, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 36, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '244036', 'license': 'Apache-2.0', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1405, 'n_words': 281, 'path': 'Advanced-SynBio-for-Cell-Factories-Course/Genome-scale metabolic reconstruction.ipynb', 'repo_name': 'biosustain/cameo-notebooks', 'size': '3671'}