<jupyter_start><jupyter_text>Community modelingIn this notebook we will implement a method to create community models of two or more species specific metabolic models using cobrapy.<jupyter_code>model_DP = cobra.io.read_sbml_model("models/consistent_DP_SNM.xml")
model_SA = cobra.io.read_sbml_model("models/consistent_iYS854_SNM.xml")
print("Growth: ", model_DP.slim_optimize())
print("Growth: ", model_SA.slim_optimize())
for rec in model_SA.reactions:
    rec.lower_bound = max(rec.lower_bound, -1000)
    rec.upper_bound = min(rec.upper_bound, 1000)
snm3 = pd.read_csv("SNM3.csv", sep =";")
snm3.head()
BIOMASS_DP = "Growth" 
BIOMASS_SA = "BIOMASS_iYS_wild_type"
models = [model_DP.copy(), model_SA.copy()]
from community_models import *
import json
compm_SA = json.loads(open("compm_SA.json").read())
compm_DP = json.loads(open("compm_DP.json").read())

model_DP.medium = compm_DP 
model_SA.medium = compm_SA
model1 = Model(model_DP, BIOMASS_DP)
model2 = Model(model_SA, BIOMASS_SA)

community_model1 = model1 + model2 
community_model2 = MIP_community_model(model1, model2)
community_model3_1_1 = create_bag_of_react_model([model_DP, model_SA],[BIOMASS_DP, BIOMASS_SA], [1,1])
community_model3_10_1 = create_bag_of_react_model([model_DP, model_SA],[BIOMASS_DP, BIOMASS_SA], [10,1])
community_model1.set_weights([1,1])
print("MBR Weights 1:1: ", community_model1.slim_optimize())
single_growth = community_model1.optimize().x[community_model1.objective_c != 0]
print("DP growth: ", single_growth[0])
print("SA growth: ", single_growth[1])
community_model1.set_weights([10,1])
print("MBR Weights 10:1: ", community_model1.slim_optimize())
single_growth = community_model1.optimize().x[community_model1.objective_c != 0]
print("DP growth: ", single_growth[0])
print("SA growth: ", single_growth[1])
community_model2.weights = [1,1]
print("MBR Weights 1:1: ", community_model2.optimize())
print("SA growth: ", community_model2.x2[community_model2.obj2].x)
print("DP growth: ", community_model2.x1[community_model2.obj1].x)
community_model2.weights = [10,1]
print("MBR Weights 10:1: ", community_model2.optimize())
print("SA growth: ", community_model2.x2[community_model2.obj2].x)
print("DP growth: ", community_model2.x1[community_model2.obj1].x)
print("MBR Weights 1:1: ", community_model3_1_1.slim_optimize())
print("SA growth: " + str(community_model3_1_1.reactions.get_by_id(BIOMASS_SA).flux))
print("DP growth: " + str(community_model3_1_1.reactions.get_by_id(BIOMASS_DP).flux))
print("MBR Weights 10:1: ", community_model3_10_1.slim_optimize())
print("SA growth: " + str(community_model3_10_1.reactions.get_by_id(BIOMASS_SA).flux))
print("DP growth: " + str(community_model3_10_1.reactions.get_by_id(BIOMASS_DP).flux))
coopm = community_model2.compute_coopm()
coopm2 = optimize_coopm_community(community_model3_1_1, community_model3_1_1.slim_optimize(), [BIOMASS_DP, BIOMASS_SA], [1,1])
coopm
coopm2
community_model2.set_medium(coopm)
community_model2.weights = [1,1]
print("MBR Weights 1:1: ", community_model2.optimize())
print("SA growth: ", community_model2.x2[community_model2.obj2].x)
print("DP growth: ", community_model2.x1[community_model2.obj1].x)
community_model2.weights = [10,1]
print("MBR Weights 10:1: ", community_model2.optimize())
print("SA growth: ", community_model2.x2[community_model2.obj2].x)
print("DP growth: ", community_model2.x1[community_model2.obj1].x)
community_model3_1_1.medium = coopm
print("MBR Weights 1:1: ", community_model3_1_1.slim_optimize())
print("SA growth: " + str(community_model3_1_1.reactions.get_by_id(BIOMASS_SA).flux))
print("DP growth: " + str(community_model3_1_1.reactions.get_by_id(BIOMASS_DP).flux))
community_model3_10_1.medium = coopm
print("MBR Weights 10:1: ", community_model3_10_1.slim_optimize())
print("SA growth: " + str(community_model3_10_1.reactions.get_by_id(BIOMASS_SA).flux))
print("DP growth: " + str(community_model3_10_1.reactions.get_by_id(BIOMASS_DP).flux))
community_model2.set_medium(coopm2)
community_model2.weights = [1,1]
print("MBR Weights 1:1: ", community_model2.optimize())
print("SA growth: ", community_model2.x2[community_model2.obj2].x)
print("DP growth: ", community_model2.x1[community_model2.obj1].x)
community_model2.weights = [10,1]
print("MBR Weights 10:1: ", community_model2.optimize())
print("SA growth: ", community_model2.x2[community_model2.obj2].x)
print("DP growth: ", community_model2.x1[community_model2.obj1].x)
community_model3_1_1.medium = coopm2
print("MBR Weights 1:1: ", community_model3_1_1.slim_optimize())
print("SA growth: " + str(community_model3_1_1.reactions.get_by_id(BIOMASS_SA).flux))
print("DP growth: " + str(community_model3_1_1.reactions.get_by_id(BIOMASS_DP).flux))
community_model3_10_1.medium = coopm2
print("MBR Weights 10:1: ", community_model3_10_1.slim_optimize())
print("SA growth: " + str(community_model3_10_1.reactions.get_by_id(BIOMASS_SA).flux))
print("DP growth: " + str(community_model3_10_1.reactions.get_by_id(BIOMASS_DP).flux))<jupyter_output>MBR Weights 1:1:  0.5117389225411352
SA growth: 0.2558694612705676
DP growth: 0.2558694612705676
MBR Weights 10:1:  2.8145640739762436
SA growth: 0.2558694612705676
DP growth: 0.2558694612705676<jupyter_text>COOPM alphas modelHere is a collection of COOPM medias for different alpha values<jupyter_code>community_model2 = MIP_community_model(model1, model2)
alphas = [0.,0.01,0.1,0.2,0.5,0.8,0.9,0.99,1.]
coopms = []
for alpha in alphas:
    coopms.append(community_model2.compute_alpha_coopm(alpha))
df = pd.DataFrame(coopms)
df.index = alphas
df.T.plot.bar(figsize=(20,10))
plt.yscale("log")
plt.ylabel("COOPM medium flux")
plt.xlabel("COOPM medium")
plt.savefig("COOPM_alpha_plot.pdf")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14163165266106442, 'fraction_numerical': 0.06810224089635854, 'mean_word_length': 8.490033222591363, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '62452', 'license': 'MIT', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2260, 'n_words': 319, 'path': 'community_model_approaches_26.ipynb', 'repo_name': 'Biomathsys/community-D-pigrum-S-aureus-human-nose', 'size': '5712'}