# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="B_MFvPmfdb5j"
# # Constraint-based modelling using CobraPy

# + [markdown] id="QlAAXbiMdU_o"
# ## Installing CobraPy

# + colab={"base_uri": "https://localhost:8080/"} id="evRZ2qL7PrfK" executionInfo={"status": "ok", "timestamp": 1638870037208, "user_tz": -330, "elapsed": 11484, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="1f6c7cb8-47fd-47b3-bc17-1e0aff8342ee"
# !pip install cobra

# + colab={"base_uri": "https://localhost:8080/", "height": 792} id="Ro0Ob5kdR5iX" executionInfo={"status": "ok", "timestamp": 1638870291867, "user_tz": -330, "elapsed": 8816, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="a573bcf0-b9f5-4d9a-dcec-c0c058610305"
# !pip install pytest_benchmark

# + id="1Dbv--J5Ro8x" executionInfo={"status": "ok", "timestamp": 1638870356976, "user_tz": -330, "elapsed": 1730, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}}
import cobra

# + [markdown] id="_FALKHh2dodi"
# ### Testing all the requirements are satisified

# + colab={"base_uri": "https://localhost:8080/"} id="Sql57Q9gQeHk" executionInfo={"status": "ok", "timestamp": 1638870362895, "user_tz": -330, "elapsed": 3344, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="3ce1753f-a541-4398-b0cd-074853e957b6"
import cobra.test
cobra.test.test_all()

# + [markdown] id="DblshRbudvQ6"
# ### Reading a constraint-based model

# + [markdown] id="IJwujD5pycsK"
# If you have a SBML model

# + id="e4oYpk82RuQ4" executionInfo={"status": "ok", "timestamp": 1638872247055, "user_tz": -330, "elapsed": 435, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "<KEY>", "userId": "01062516875061334592"}}
ecoli = cobra.io.read_sbml_model("e_coli_core.xml")

# + [markdown] id="x0I5wxLhyiwK"
# If you have a JSON model

# + id="yJWoZr4gTJyV"
ecoli = cobra.io.load_json_model(filename)

# + [markdown] id="uZXVPgQHylv8"
# If you have a MAT model

# + id="YxO1fykdTQyz"
ecoli =  cobra.io.load_matlab_model("e_coli_core.mat")

# + [markdown] id="VAjZmbzwd5S0"
# ### How a model object looks like

# + colab={"base_uri": "https://localhost:8080/", "height": 169} id="iakZ4-_BStFL" executionInfo={"status": "ok", "timestamp": 1638870475978, "user_tz": -330, "elapsed": 387, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "<KEY>Y=s64", "userId": "01062516875061334592"}} outputId="d8b14e4a-6552-4e55-ebe7-1f03e0cb5a47"
ecoli

# + [markdown] id="hC5Q_v1Ed_2a"
# ### Different attributes of this model object

# + colab={"base_uri": "https://localhost:8080/"} id="8Esy8pB3UtOt" executionInfo={"status": "ok", "timestamp": 1638871008697, "user_tz": -330, "elapsed": 408, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="df6f4069-62f4-45ed-afb6-61133e7f1c53"
vars(ecoli).keys()

# + colab={"base_uri": "https://localhost:8080/"} id="pnOg-zaopP47" executionInfo={"status": "ok", "timestamp": 1638876392300, "user_tz": -330, "elapsed": 441, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="61203855-876e-4305-9784-3d05ac8a08af"
ecoli.genes[:10]

# + [markdown] id="TsexJZoseHQ6"
# Looking at active exchange reactions

# + colab={"base_uri": "https://localhost:8080/"} id="xXK6HQmeb37D" executionInfo={"status": "ok", "timestamp": 1638872917952, "user_tz": -330, "elapsed": 403, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="ba0e24a5-7fdf-4410-a984-464145a87da1"
ecoli.medium

# + [markdown] id="poqJX95GeZjh"
# Looking at all echange reactions

# + colab={"base_uri": "https://localhost:8080/"} id="RoBaw4iQdNTI" executionInfo={"status": "ok", "timestamp": 1638873234360, "user_tz": -330, "elapsed": 395, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="321fd4ed-0cde-48c0-898a-e446e2b2eb70"
ecoli.exchanges

# + [markdown] id="VEqi378LiTMV"
# ### Accessing the metabolites and reactions in the model

# + [markdown] id="bGtmHwDyijCj"
# Metabolites

# + colab={"base_uri": "https://localhost:8080/"} id="YjZSfl4giH3b" executionInfo={"status": "ok", "timestamp": 1638874536921, "user_tz": -330, "elapsed": 460, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="6ebd7464-a928-47f5-bbe7-d9942f17d8c1"
ecoli.metabolites[:10]

# + colab={"base_uri": "https://localhost:8080/", "height": 285} id="trRDKRImiYWk" executionInfo={"status": "ok", "timestamp": 1638874610977, "user_tz": -330, "elapsed": 374, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="f85ca224-7c05-4b1a-e556-798e4c775dbc"
ecoli.metabolites.glc__D_e.summary()

# + colab={"base_uri": "https://localhost:8080/"} id="BibjTCQSnN_X" executionInfo={"status": "ok", "timestamp": 1638875882963, "user_tz": -330, "elapsed": 382, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="d23fdd29-f4d8-4c3f-f7ab-324f9fdba7ab"
vars(ecoli.metabolites.glc__D_e).keys()

# + [markdown] id="cqrq8_JPimVR"
# Reactions

# + colab={"base_uri": "https://localhost:8080/"} id="spyyo5IaiPKe" executionInfo={"status": "ok", "timestamp": 1638874556264, "user_tz": -330, "elapsed": 740, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="c10c9bc8-2d9f-4a25-859e-3d7f40251e25"
ecoli.reactions[:10]

# + colab={"base_uri": "https://localhost:8080/", "height": 130} id="wJ8U2cwAio0F" executionInfo={"status": "ok", "timestamp": 1638874678576, "user_tz": -330, "elapsed": 381, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="9b4d806d-f105-477f-9b22-f11582377c2c"
ecoli.reactions.ATPM.summary()

# + colab={"base_uri": "https://localhost:8080/"} id="7LUAolSii1Xo" executionInfo={"status": "ok", "timestamp": 1638874725179, "user_tz": -330, "elapsed": 428, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="7607449d-e1df-4da4-9d56-6e00e5cfcbe8"
vars(ecoli.reactions.ATPM).keys()

# + colab={"base_uri": "https://localhost:8080/", "height": 36} id="L0BheJcrjIOX" executionInfo={"status": "ok", "timestamp": 1638874862005, "user_tz": -330, "elapsed": 900, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="b1cd8e95-3f1b-4268-d309-9af86353c91c"
ecoli.reactions.ATPM.name

# + [markdown] id="tThzRsvtfGHo"
# ### Simulating the model using flux balance analysis

# + colab={"base_uri": "https://localhost:8080/"} id="Tzn-xZkISt_f" executionInfo={"status": "ok", "timestamp": 1638870988984, "user_tz": -330, "elapsed": 403, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="0eb9bfcc-1962-4625-bf6d-a2d6f6a2f685"
solution = ecoli.optimize()
vars(solution).keys()

# + colab={"base_uri": "https://localhost:8080/"} id="lQHB8yyIS1kc" executionInfo={"status": "ok", "timestamp": 1638871088434, "user_tz": -330, "elapsed": 385, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="ed5743af-19af-4979-952f-6aa0d5674855"
print('Satus: ',solution.status)
print('Growth rate: %.2f' % solution.objective_value)

# + [markdown] id="C3ZkEZmEfWiT"
# Using the `model.summary()` function for FBA

# + colab={"base_uri": "https://localhost:8080/", "height": 442} id="j2F8gq5-VDlj" executionInfo={"status": "ok", "timestamp": 1638871206114, "user_tz": -330, "elapsed": 390, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="6b0b545a-1fef-44ad-aa58-e468a66d022b"
ecoli.summary()

# + [markdown] id="yZQrUa-nfgWB"
# ### Flux variability analysis (FVA)

# + colab={"base_uri": "https://localhost:8080/", "height": 363} id="IvPMb9wPWpWx" executionInfo={"status": "ok", "timestamp": 1638871799542, "user_tz": -330, "elapsed": 388, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="97c1af6b-3835-40d5-bae6-059a71c65056"
from cobra.flux_analysis import flux_variability_analysis
flux_variability_analysis(ecoli, ecoli.reactions[:10])

# + [markdown] id="8Bxyhu3SfmpG"
# Using `model.summary()` function for FVA

# + colab={"base_uri": "https://localhost:8080/", "height": 724} id="LM9LPamMXyVX" executionInfo={"status": "ok", "timestamp": 1638872209477, "user_tz": -330, "elapsed": 406, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="091e8d20-dd83-47d4-b7ae-4ad6e6ae2498"
ecoli.summary(fva=0.95)

# + [markdown] id="DQZgrbapfztR"
# ### Simulating deletions

# + id="KgK6BVqnYRl5" executionInfo={"status": "ok", "timestamp": 1638871936302, "user_tz": -330, "elapsed": 392, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}}
from cobra.flux_analysis import (
    single_gene_deletion, single_reaction_deletion, double_gene_deletion,
    double_reaction_deletion)

# + [markdown] id="YcVtVhIjgMrS"
# Reaction deletions

# + colab={"base_uri": "https://localhost:8080/"} id="qz6lDxJpYfX2" executionInfo={"status": "ok", "timestamp": 1638872254242, "user_tz": -330, "elapsed": 383, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="dd429318-772c-48ff-cc74-7d3d8174e10e"
print('complete model: ', ecoli.optimize())
with ecoli:
    ecoli.reactions.PFK.knock_out()
    print('pfk knocked out: ', ecoli.optimize())

# + colab={"base_uri": "https://localhost:8080/", "height": 677} id="Jng1pxMaZ8cZ" executionInfo={"status": "ok", "timestamp": 1638872473614, "user_tz": -330, "elapsed": 366, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="2b0cd97a-27a2-4d76-fbf3-71dde162b26c"
single_reaction_deletion(ecoli, ecoli.reactions[:20])

# + [markdown] id="vMzcglPSgRCl"
# Gene deletions

# + colab={"base_uri": "https://localhost:8080/"} id="mOyus5gYY5jY" executionInfo={"status": "ok", "timestamp": 1638872256985, "user_tz": -330, "elapsed": 360, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="72c61739-ec6a-452a-ddf1-2179c9a06efd"
print('complete model: ', ecoli.optimize())
with ecoli:
    ecoli.genes.b1723.knock_out()
    print('pfkA knocked out: ', ecoli.optimize())
    ecoli.genes.b3916.knock_out()
    print('pfkB knocked out: ', ecoli.optimize())

# + id="VT3I0GT7ZkM1" executionInfo={"status": "ok", "timestamp": 1638872744500, "user_tz": -330, "elapsed": 367, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}}
sin_gene_deletion_results = single_gene_deletion(ecoli)

# + colab={"base_uri": "https://localhost:8080/"} id="woHAitf9bPc9" executionInfo={"status": "ok", "timestamp": 1638872768328, "user_tz": -330, "elapsed": 564, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="d2fbaac3-6e22-462e-c03c-382f6fef1ea7"
print(sin_gene_deletion_results.knockout["b1723"])

# + colab={"base_uri": "https://localhost:8080/", "height": 424} id="1_iC_uc4ZtkA" executionInfo={"status": "ok", "timestamp": 1638872315084, "user_tz": -330, "elapsed": 423, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="6343bea1-fc2a-4644-f243-58ddc874b0d3"
sin_deletion_results

# + [markdown] id="bvxvhy1aghZE"
# Double deletions

# + colab={"base_uri": "https://localhost:8080/", "height": 520} id="wvJcmJ8dZxMn" executionInfo={"status": "ok", "timestamp": 1638872477448, "user_tz": -330, "elapsed": 465, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="f7fcf065-679d-4016-d252-98b140c8051a"
double_gene_deletion(ecoli, ecoli.genes[-5:]).round(4)

# + colab={"base_uri": "https://localhost:8080/", "height": 520} id="kyLlSPCEaZ7A" executionInfo={"status": "ok", "timestamp": 1638872518030, "user_tz": -330, "elapsed": 355, "user": {"displayName": "<NAME> bt19s011", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgOtaoZrJ-HKPOJi03yar_EU3gkXoR07v0WUp1Y=s64", "userId": "01062516875061334592"}} outputId="f39b2a88-6d11-46cf-c62f-8145a72a9cf7"
double_reaction_deletion(ecoli, ecoli.reactions[-5:]).round(4)

StarcoderdataJupyterScriptsDedupFiltered