<jupyter_start><jupyter_text>cobrapycobrapy is a Python binding of the COBRA Toolbox (https://opencobra.github.io/cobratoolbox/latest/), a toolbox for constraint-based metabolic modelling. One of the key features of COBRA is its ability to load SBMLs and to run Flux Balanace Analyses on them.cobrapy's home page is https://opencobra.github.io/cobrapy/cobrapy's documentation can be found under https://cobrapy.readthedocs.io/en/stable/Most of this notebook's examples are based on cobrapy's documentation.If you use pip, you can install cobrapy by running "pip install cobrapy" and cobrapy's SBML functionalites with "pip install python-libsbml". 1. Loading a test modelhttps://cobrapy.readthedocs.io/en/stable/getting_started.html<jupyter_code>import cobra
import cobra.test

model = cobra.test.create_test_model("textbook")
# Note that cobrapy is partially integrated into Jupyter
model
print(len(model.reactions))
print(len(model.metabolites))
print(len(model.genes))
model.reactions[0]
model.reactions[0].reversibility
pgi = model.reactions.get_by_id("PGI")
pgi.add_metabolites({model.metabolites.get_by_id("h_c"): -1})
pgi.check_mass_balance()
pgi.subtract_metabolites({model.metabolites.get_by_id("h_c"): -1})
model.metabolites[-1]
model.genes.get_by_id("b0351")
# Efficient knock-outs on-the-fly, without copying the model
for reaction in model.reactions[:5]:
    with model as model:
        reaction.knock_out()
        model.optimize()
        print('%s blocked (bounds: %s), new growth rate %f' %
              (reaction.id, str(reaction.bounds), model.objective.value))<jupyter_output>ACALD blocked (bounds: (0, 0)), new growth rate 0.873922
ACALDt blocked (bounds: (0, 0)), new growth rate 0.873922
ACKr blocked (bounds: (0, 0)), new growth rate 0.873922
ACONTa blocked (bounds: (0, 0)), new growth rate -0.000000
ACONTb blocked (bounds: (0, 0)), new growth rate -0.000000<jupyter_text>2. Write and Load an SBMLhttps://cobrapy.readthedocs.io/en/stable/io.html<jupyter_code># Write the model of section 1 into an SBML
cobra.io.write_sbml_model(model, "./cobrapy/textbook.xml")
# Read the model great again
model = cobra.io.read_sbml_model("./cobrapy/textbook.xml")
model<jupyter_output><empty_output><jupyter_text>3. Performing FBAhttps://cobrapy.readthedocs.io/en/stable/simulating.html<jupyter_code>fba_solution = model.optimize()
# model.slim_optimize() if we are only interested in the objective value
print(fba_solution)
fba_solution.objective_value<jupyter_output><empty_output><jupyter_text>4. Analyzing FBA results<jupyter_code>model.summary()
# Redox balance
model.metabolites.nadh_c.summary()<jupyter_output>PRODUCING REACTIONS -- Nicotinamide adenine dinucleotide - reduced (nadh_c)
---------------------------------------------------------------------------
%       FLUX  RXN ID      REACTION
----  ------  ----------  --------------------------------------------------
42%    16     GAPD        g3p_c + nad_c + pi_c <=> 13dpg_c + h_c + nadh_c
24%     9.28  PDH         coa_c + nad_c + pyr_c --> accoa_c + co2_c + nadh_c
13%     5.06  AKGDH       akg_c + coa_c + nad_c --> co2_c + nadh_c + succ...
13%     5.06  MDH         mal__L_c + nad_c <=> h_c + nadh_c + oaa_c
8%      3.1   Biomass...  1.496 3pg_c + 3.7478 accoa_c + 59.81 atp_c + 0....

CONSUMING REACTIONS -- Nicotinamide adenine dinucleotide - reduced (nadh_c)
---------------------------------------------------------------------------
%       FLUX  RXN ID      REACTION
----  ------  ----------  --------------------------------------------------
100%   38.5   NADH16      4.0 h_c + nadh_c + q8_c --> 3.0 h_e + nad_c + q...<jupyter_text>5. Sections 3 and 4 together with a new objective functionhttps://cobrapy.readthedocs.io/en/stable/simulating.htmlChanging-the-Objectives<jupyter_code># Currently, a biomass function is the objective function...
from cobra.util.solver import linear_reaction_coefficients
linear_reaction_coefficients(model)
# ...which is set as a reaction
model.reactions.get_by_id("Biomass_Ecoli_core")
# Now, we set the objective to ATPM
model.objective = "ATPM"
model.reactions.get_by_id("ATPM")
linear_reaction_coefficients(model)
# Perform FBA with the new objective function
model.optimize()
model.summary()<jupyter_output>IN FLUXES     OUT FLUXES    OBJECTIVES
------------  ------------  ------------
o2_e      60  co2_e  60     ATPM  175
glc__D_e  10  h2o_e  60<jupyter_text>6. Loopless FBASome solutions may contain loops, which should not be possible in vivo. This can be solved by using solvers which use looplessness as a contstrain. However, loopless solutions are generally more computationally expensive.https://cobrapy.readthedocs.io/en/stable/loopless.htmlPaper: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3030201/<jupyter_code>from cobra.flux_analysis.loopless import add_loopless, loopless_solution

loopless = loopless_solution(model)
loopless[:5]<jupyter_output><empty_output><jupyter_text>7. Flux Variability AnalysisAs an FBA does not always return a single most optimal solution, FBA returns the bounds of all optimal solutions.https://cobrapy.readthedocs.io/en/stable/simulating.htmlRunning-FVA<jupyter_code>from cobra.flux_analysis import flux_variability_analysis
flux_variability_analysis(model, model.reactions[:5])
# With 90 % optimality
flux_variability_analysis(model, model.reactions[:5], fraction_of_optimum=0.9)
model.summary(fva=.95)
# Loops can be avoided by setting loopless=True
loop_reactions = [model.reactions.FRD7, model.reactions.SUCDi]
flux_variability_analysis(model, loop_reactions, loopless=False)
flux_variability_analysis(model, loop_reactions, loopless=True)<jupyter_output><empty_output><jupyter_text>8. Parsimoniuous FBAThis is an FBA with the additional contrains of miminal total fluxes.https://cobrapy.readthedocs.io/en/stable/simulating.htmlRunning-pFBA<jupyter_code>cobra.flux_analysis.pfba(model)[:5]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1532722956934554, 'fraction_numerical': 0.025940996948118005, 'mean_word_length': 3.6706254948535233, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 35, 'https://': 11, 'lorem ipsum': 0, 'www.': 1, 'xml': 2}, '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': '374708', 'license': 'MIT', 'n_tokens_mistral': 2156, 'n_tokens_neox': 1976, 'n_words': 472, 'path': 'python_module_tutorials/cobrapy.ipynb', 'repo_name': 'Paulocracy/python_tutorials', 'size': '5898'}