{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Metabolic network modeling with COBRA\n", "\n", "## Databases of models\n", "1. Biomodels https://www.ebi.ac.uk/biomodels-main/publmodels\n", "2. BiGG http://bigg.ucsd.edu/\n", "3. HMA http://www.metabolicatlas.org\n", "\n", "## Documentation of cobrapy and cobra toolbox\n", "1. https://cobrapy.readthedocs.io/en/stable/index.html\n", "2. https://opencobra.github.io/cobratoolbox/stable/\n", "\n", "## Solvers\n", "1. Gurobi http://www.gurobi.com\n", "2. cplex https://www.ibm.com/products/ilog-cplex-optimization-studio\n", "3. glpk\n", "\n", "To install `cobrapy`, run the following code below (requires pip):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# install cobrapy\n", "# !pip install cobra\n", "# !pip install pytest" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# load modules\n", "import cobra\n", "from cobra.io import load_model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# check cobra version\n", "cobra.__version__" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model = load_model(\"textbook\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### You can also load a exist model by using io functions like load_matlab_model, read_sbml_model ..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## io functions\n", "\n", "# cobra.io.read_sbml_model\n", "# cobra.io.load_json_model\n", "# cobra.io.load_matlab_model\n", "# cobra.io.load_yaml_model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Components of model\n", "### Model (`cobra.core.model.Model`)\n", "\n", "-------------\n", "\n", "#### attribute:\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
AttributeTypeDescription
reactionsDictListA DictList where the key is the reaction identifier and the value a Reaction
metabolitesDictListA DictList where the key is the metabolite identifier and the value a Metabolite
genesDictListA DictList where the key is the gene identifier and the value a Gene
solutioncobra.SolutionThe last obtained solution from optimizing the model.
boundaryDictListBoundary reactions in the model. Reactions that either have no substrate or product.
exchangesDictListExchange reactions in model. Reactions that exchange mass with the exterior. Uses annotations and heuristics to exclude non-exchanges such as sink reactions.
demandsDictListDemand reactions in model. Irreversible reactions that accumulate or consume a metabolite in the inside of the model.
sinksDictListSink reactions in model. Reversible reactions that accumulate or consume a metabolite in the inside of the model.
objectiveoptlang.interface.ObjectiveBefore introduction of the optlang based problems, this function returned the objective reactions as a list. With optlang, the objective is not limited a simple linear summation of individual reaction fluxes, making that return value ambiguous. Henceforth, use cobra.util.solver.linear_reaction_coefficients to get a dictionary of reactions with their linear coefficients (empty if there are none)
\n", "The set value can be dictionary (reactions as keys, linear coefficients as values), string (reaction identifier), int (reaction index), Reaction or problem.Objective or sympy expression directly interpreted as objectives.
\n", "\n", "#### methods:\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
MethodParametersReturn typeDescription
copyNonecobra.ModelProvides a partial ‘deepcopy’ of the Model. All of the Metabolite, Gene, and Reaction objects are created anew but in a faster fashion than deepcopy
add_metabolitesmetabolite_list (list)NoneWill add a list of metabolites to the model object and add new constraints accordingly.
remove_metabolitesmetabolite_list (list)
\n", " destructive (bool)\n", "
NoneRemove a list of metabolites from the the object.
add_boundarymetabolite (cobra.Metabolite)
\n", " type (str, {\"exchange\", \"demand\", \"sink\"})
\n", " reaction_id (str, optional)
\n", " lb (float, optional)
\n", " ub (float, optional)
\n", " sbo_term (str, optional)
cobra.ReactionAdd a boundary reaction for a given metabolite.
\n", "There are three different types of pre-defined boundary reactions: exchange, demand, and sink reactions. An exchange reaction is a reversible, unbalanced reaction that adds to or removes an extracellular metabolite from the extracellular compartment. A demand reaction is an irreversible reaction that consumes an intracellular metabolite. A sink is similar to an exchange but specifically for intracellular metabolites.
\n", "If you set the reaction type to something else, you must specify the desired identifier of the created reaction along with its upper and lower bound. The name will be given by the metabolite name and the given type.
add_reactionsreaction_list (list) NoneAdd reactions to the model.
\n", " Reactions with identifiers identical to a reaction already in the model are ignored.
remove_reactionsreaction_list (list)
\n", " remove_orphans (bool)\n", "
NoneRemove reactions from the model.
slim_optimizeerror_value (float, None)
\n", " message (string)\n", "
floatOptimize model without creating a solution object.
optimizeobjective_sense ({None, 'maximize' 'minimize'}, optional)
\n", " raise_error (bool)\n", "
cobra.SolutionOptimize the model using flux balance analysis.
summarysolution (cobra.Solution, optional)
\n", " threshold (float, optional)
\n", " fva (pandas.DataFrame or float, optional)
\n", " names (bool, optional)
\n", " float_format (callable, optional)\n", "
cobra.ModelSummaryCreate a summary of the exchange fluxes of the model.
\n", "\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# components of a cobra Model\n", "\n", "print(f\"{len(model.reactions)} Reactions in the model\\n------------------\")\n", "for r in model.reactions[:10]:\n", " print(f\"{r.id}: {r.reaction}, associated with {r.gene_name_reaction_rule}\")\n", "print()\n", "\n", "\n", "print(f\"{len(model.metabolites)} Metabolites in the model\\n------------------\")\n", "for m in model.metabolites[:10]:\n", " print(f\"{m.id}: {m.name}\")\n", "print()\n", "\n", "\n", "print(f\"{len(model.genes)} Genes in the model\\n------------------\")\n", "for g in model.genes[:10]:\n", " print(f\"{g.id}: {g.name}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# deepcopy a cobra model\n", "model_copy = model.copy()\n", "\n", "new_reaction = cobra.Reaction(\"new_rxn_1\")\n", "model_copy.add_reactions([new_reaction])\n", "\n", "len(model_copy.reactions), len(model.reactions)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# model objective\n", "print(model.objective)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# check the reaction in the objective function\n", "model.reactions.get_by_id(\"Biomass_Ecoli_core\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reactions (`cobra.core.reaction.Reaction`)\n", "\n", "-------------\n", "\n", "#### attribute:\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
AttributeTypeDescription
idstringThe identifier to associate with this reaction
namestringA human readable name for the reaction
reactionstringHuman readable reaction string
lower_boundfloatThe lower flux bound
upper_boundfloatThe upper flux bound
boundstuppleThe upper and the lower flux bounds
subsystemstringSubsystem where the reaction is meant to occur
objective_coefficientfloatThe coefficient for this reaction in a linear objective
flux_expressionsympy expressionForward flux expression
fluxfloatThe flux value in the most recent solution.
reduced_costfloatThe reduced cost in the most recent solution.
metabolitesdict (cobra.metabolite: float)The dict of metabolites involved in the reaction.
genesfrozenset (cobra.gene)The frozenset of genes involved in the reaction.
gene_reaction_rulestringThe boolean representation of the gene requirements for the reaction to be active.
\n", "\n", "#### methods:\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
MethodParametersReturn typeDescription
remove_from_model remove_orphans=False (bool) None Removes the reaction from a model.
get_coefficient metabolite_id (str or cobra.Metabolite) float Return the stoichiometric coefficient of a metabolite.
get_coefficients metabolite_ids (iterable) map Return the stoichiometric coefficients for a list of metabolites.
add_metabolites metabolites_to_add (dict)\n", "
combine (bool)\n", "
reversibly (bool)
None Add metabolites and stoichiometric coefficients to the reaction. If the final coefficient for a metabolite is 0 then it is removed from the reaction.
subtract_metabolites metabolites (dict)
\n", " combine (bool)
\n", " reversibly (bool)
None Subtract metabolites from a reaction.
\n", " That means add the metabolites with -1*coefficient. If the final coefficient for a metabolite is 0 then the metabolite is removed from the reaction.
build_reaction_string use_metabolite_names=False (bool) str Generate a human readable reaction string
build_reaction_from_string reaction_str (string)
\n", " verbose (bool)
\n", " fwd_arrow (re.compile)
\n", " rev_arrow (re.compile)
\n", " reversible_arrow (re.compile)
\n", " term_split (string)\n", "
None Builds reaction from reaction equation reaction_str using parser
\n", " Takes a string and using the specifications supplied in the optional arguments infers a set of metabolites, metabolite compartments and stoichiometries for the reaction. It also infers the reversibility of the reaction from the reaction arrow.
knock_out None None Knockout reaction by setting its bounds to zero.
\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create new reactions\n", "# cobra.Reaction(id=...)\n", "new_rxns = [cobra.Reaction(id=f\"R{i}\", name=f\"reaction No.{i}\", lower_bound=0, upper_bound=1000) for i in range(1, 10)]\n", "\n", "# add reactions to a model\n", "model_copy.add_reactions(new_rxns)\n", "print(model_copy.reactions[-10:])\n", "print(f\"Now the model has {len(model_copy.reactions)} reactions\")\n", "\n", "# remove reactions from a model\n", "model_copy.remove_reactions(new_rxns)\n", "print(f\"Now the model has {len(model_copy.reactions)} reactions\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get a reaction from a model\n", "print(model.reactions[0])\n", "print(model.reactions.get_by_id(\"ACALDt\"),\"\\n\")\n", "\n", "\n", "# reactions attributes\n", "rxn = model.reactions.get_by_id(\"ACALD\")\n", "print(f\"ID: {rxn.id}\\n\\\n", " Name: {rxn.name}\\n\\\n", " Lower bound: {rxn.lower_bound}\\n\\\n", " Upper bound: {rxn.upper_bound}\\n\\\n", " Subsystem: {rxn.subsystem}\\n\\\n", " Metabolites: {rxn.metabolites}\\n\\\n", " Genes: {rxn.genes}\\n\\\n", " Gene-reaction rule: {rxn.gene_reaction_rule}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rxn.build_reaction_string()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "new_rxn = cobra.Reaction(\"dummy_rxn\")\n", "model_copy.add_reactions([new_rxn])\n", "new_rxn" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# build a reaction by a human readable reaction string\n", "new_rxn.build_reaction_from_string(\"acald_c + coa_c + nad_c --> accoa_c + h_c + nadh_c\")\n", "new_rxn" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# assign new values to the reaction attributes\n", "new_rxn.name = \"R123\"\n", "new_rxn.lower_bound, new_rxn.upper_bound = -1, 30\n", "new_rxn.subsystem = \"new subsystem\"\n", "new_rxn" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# knock out a reaction\n", "new_rxn.knock_out()\n", "new_rxn.bounds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Metabolites (`cobra.core.metabolite.Metabolite`)\n", "\n", "-------------\n", "\n", "#### attribute:\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
AttributeTypeDescription
idstrthe identifier to associate with the metabolite
formulastrChemical formula (e.g. H2O)
namestrA human readable name.
chargefloatThe charge number of the metabolite
compartmentstr or NoneCompartment of the metabolite.
elementsdictDictionary of elements as keys and their count in the metabolite as integer.
shadow_pricefloatThe shadow price in the most recent solution.
\n", "\n", "#### method:\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
MethodParametersReturn typeDescription
remove_from_modeldestructive (bool)NoneRemoves the association from self.model
summarysolution=None (cobra.Solution)
\n", " threshold=0.01 (float)
\n", " fva=None (pandas.DataFrame or float)
\n", " names=False (bool)
\n", " float_format='{:.3g}'.format (callable)
cobra.MetaboliteSummaryCreate a summary of the producing and consuming fluxes.
\n", " This method requires the model for which this metabolite is a part to be solved.
\n", "\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create a new metabolite\n", "new_met = cobra.Metabolite(id=\"met_c\", name=\"new metabolite\", formula=\"C100H200\", compartment=\"c\")\n", "print(new_met.elements)\n", "new_met" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# add a metabolite to a reaction\n", "new_rxn.add_metabolites({\n", " new_met: 1\n", "})\n", "new_rxn" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# remove a metabolite from a reaction\n", "new_rxn.subtract_metabolites({\n", " new_met: 1\n", "})\n", "new_rxn" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# remove metabolite from a model\n", "model_copy.remove_metabolites([model_copy.metabolites.get_by_id(\"met_c\")])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get a metabolite from a model\n", "print(model.metabolites[0])\n", "print(model.metabolites.get_by_id(\"mal__L_c\"),\"\\n\")\n", "\n", "# metabolites property\n", "met = model.metabolites.get_by_id(\"mal__L_c\")\n", "print(f\"ID: {met.id}\\n\\\n", " Name: {met.name}\\n\\\n", " Formula: {met.formula}\\n\\\n", " Charge: {met.charge}\\n\\\n", " Compartment: {met.compartment}\\n\\\n", " Reactions: {met.reactions}\\n\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "met.summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Genes (`cobra.core.gene.Gene`)\n", "\n", "-------------\n", "\n", "#### attribute:\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
AttributeTypeDescription
idstrThe identifier to associate the gene with
namestrA longer human readable name for the gene
functionalboolIndicates whether the gene is functional. If it is not functional then it cannot be used in an enzyme complex nor can its products be used.
\n", "\n", "#### method:\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
MethodParametersReturn typeDescription
knock_outNoneNoneKnockout gene by marking it as non-functional and setting all associated reactions bounds to zero.
\n", "\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create a new gene\n", "new_gen = cobra.Gene(id=\"404\", name=\"new gene\")\n", "print(new_gen.id, new_gen.name)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get a gene\n", "gene = model_copy.genes.get_by_id(\"b1241\")\n", "print(gene.id)\n", "\n", "gene = model_copy.genes[10]\n", "gene" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# related rxn\n", "gene.reactions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# check boolean relationship\n", "list(gene.reactions)[0].gene_reaction_rule" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# add a gene to a model\n", "print(f\"Number of genes : {len(model_copy.genes)}\")\n", "list(gene.reactions)[0].gene_reaction_rule = f\"{list(gene.reactions)[0].gene_reaction_rule} and 404\"\n", "\n", "print(f\"Number of genes : {len(model_copy.genes)}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# They are not the same object!!\n", "model_copy.genes.get_by_id(\"404\") == new_gen\n", "\n", "print(id(model_copy.genes.get_by_id(\"404\")), id(new_gen))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "model_copy.reactions[0].gene_reaction_rule = f\"{model_copy.reactions[0].gene_reaction_rule} or 404\"\n", "\n", "print(model_copy.genes.get_by_id(\"404\").reactions)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# knocking out a gene\n", "model_copy.genes.get_by_id(\"404\").knock_out()\n", "\n", "# The related reactions might be influenced after you knock out a gene\n", "for i in model_copy.genes.get_by_id(\"404\").reactions:\n", " print(i)\n", " print(i.gene_reaction_rule)\n", " print(f\"{i.id} is functional? {i.functional}\")\n", " print()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Configuration\n", "\n", "reference: https://cobrapy.readthedocs.io/en/stable/configuration.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The object has the following attributes which you can inspect but also change as desired.\n", "\n", "cobra_config = cobra.Configuration()\n", "cobra_config.bounds" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# change default bounds\n", "cobra_config.bounds = -10, 30\n", "cobra.Reaction(\"R1\", lower_bound=None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# change default solver\n", "cobra_config.solver = \"glpk_exact\"\n", "new_model = load_model(\"textbook\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using context manager" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with model:\n", " model.reactions[10].bounds = (-2000, 2000)\n", " model.reactions[9].knock_out()\n", "\n", " print(model.reactions[10].bounds)\n", " print(model.reactions[9].bounds)\n", " \n", " new_rxn2 = cobra.Reaction(\"r2\")\n", " new_met2 = cobra.Metabolite(\"m2\")\n", " model.add_metabolites([new_met2])\n", " model.add_reactions([new_rxn2])\n", " new_rxn2.build_reaction_from_string(\"m2 -->\")\n", " \n", " print(model.reactions.r2)\n", " \n", " print(f\"The model has {len(model.reactions)} reactions\")\n", " print(f\"The model has {len(model.metabolites)} metabolites\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# all the changes are reverted\n", "print(model.reactions[10].bounds)\n", "print(model.reactions[9].bounds)\n", "\n", "try:\n", " print(model.reactions.new_rxn2)\n", "except:\n", " print(\"Cannot find new_rxn2 in the model\")\n", "\n", "print(f\"The model has {len(model.reactions)} reactions\")\n", "print(f\"The model has {len(model.metabolites)} metabolites\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Flux balance analysis(FBA)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The LP problem of the model\n", "print(model.objective)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get a dict of {Reaction: objective_coefficient}\n", "from cobra.util.solver import linear_reaction_coefficients\n", "linear_reaction_coefficients(model)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# modify the model objective\n", "with model:\n", " model.objective = model.reactions.TPI\n", " print(f\"obj value: {model.slim_optimize()}\\n\")\n", "\n", " model.objective = {model.reactions.TPI: 2, \n", " model.reactions.ACALD: 1}\n", " \n", " # after running FBA, the flux of reaction can be obtained by:\n", " model.optimize()\n", " print(f\"TPI Flux: {model.reactions.get_by_id('TPI').flux}\\nACALD Flux: {model.reactions.get_by_id('ACALD').flux}\\nobj value: {model.slim_optimize()}\\n\")\n", "\n", " model.objective = \"ACONTa\"\n", "\n", " print(f\"obj value: {model.slim_optimize()}\\n\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Running FBA\n", "sol = model.optimize()\n", "sol" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# attributes (analysis results) of a cobra.Solution object\n", "sol.objective_value, sol.status" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# attributes (analysis results) of a cobra.Solution object\n", "sol.to_frame().fluxes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "plt.figure(figsize=(12, 7))\n", "with model:\n", " X = np.arange(0, 1000)\n", " Y = np.zeros(X.shape)\n", " for i, x in enumerate(X):\n", " THD2 = model.reactions.get_by_id(\"THD2\")\n", " THD2.lower_bound = x\n", " Y[i] = model.slim_optimize()\n", " plt.xlabel(\"Lower bound of THD2\")\n", " plt.ylabel(\"Objective value\")\n", " plt.plot(X, Y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%timeit\n", "model.slim_optimize()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%timeit\n", "model.optimize()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.summary()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.metabolites.get_by_id(\"atp_c\").summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Flux variability analysis(FVA)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from cobra.flux_analysis import flux_variability_analysis as fva\n", "\n", "sol = fva(model, fraction_of_optimum=0.8)\n", "sol" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.summary(fva=0.8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.metabolites.get_by_id(\"atp_c\").summary(fva=0.8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "### pFBA\n", "from cobra.flux_analysis import pfba\n", "\n", "sol = pfba(model)\n", "sol" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sol.objective_value" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# the objective value is the sum of fluxes\n", "sol.fluxes.abs().sum()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.4 64-bit", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.4" }, "vscode": { "interpreter": { "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1" } } }, "nbformat": 4, "nbformat_minor": 2 }