<jupyter_start><jupyter_text>Loading models and Cobrapy compatibilityLet's show how the COMETS Python toolbox deals with models, including compatibility with cobra. We first create a set of parameters by instantiating the params class with no arguments, meaning that all parameter values will be the default. We then also load a layout containing only an amount of glucose (plus unlimited minerals, ammonia, phosphate etc.) and we load in the Cobra SBML iJO1366 model from file. Each time the models in a layout are changed, we have to run the `update_models()` method.<jupyter_code>import comets as c
import os
import pandas as pd
import matplotlib.pyplot as plt
pd.options.display.max_rows = 10

# .. Create set of parameters with default values, and change timeStep
my_params = c.params()
my_params.all_params['timeStep'] = 0.25

# .. Load layout from file
my_layout = c.layout('test_models/layout_blueprint')
my_layout.models = ['test_models/iJO1366.xml']
my_layout.update_models()
my_layout.models<jupyter_output>Academic license - for non-commercial use only<jupyter_text>In the layout file, the model was specified as a path to a xml file. Internally, the `layout` class transformed this SBML model in a file readable by the `COMETS` Java engine. The field models will show just the model ID. Let's run the model and see the results.<jupyter_code># create comets object from the loaded parameters and layout, and run it
my_comets = c.comets(my_layout, my_params)
my_comets.run()
biomass1 = my_comets.total_biomass
plt.plot('cycle', 'iJO1366', data=biomass1)<jupyter_output><empty_output><jupyter_text>Adjusting COMETS parameters in modelsWhat if we want to change some parameters of the model? For instance, we might want to change the type of optimization of the model (objective style) to maximize biomass and then minimize the total flux (parsimonious FBA in cobra terms). For changing such parameters, we need to read the `COBRA` model, and add the corresponding fields for `COMETS`.<jupyter_code>import cobra as cb
md = cb.io.read_sbml_model('test_models/iJO1366.xml')
# add some parameters to the model
md.obj_style = 'MAX_OBJECTIVE_MIN_TOTAL'

# change the ID of the model
md.id = 'modified_iJO1366'<jupyter_output><empty_output><jupyter_text>If we also want to change $V_{max}$, $K_m$, and Hill coefficients of some uptake reaction, say glucose uptake, we have to add these as fields of the corresponding exchange reaction.  Moreover, if we change these parameters for any particular reaction, we also need to specify their default values for all the rest of uptake reactions in the model.<jupyter_code>md.reactions.EX_glc__D_e.Vmax = 6
md.reactions.EX_glc__D_e.Km = .1
md.reactions.EX_glc__D_e.Hill = 2

md.default_vmax = 10
md.default_km = 0.01
md.default_hill = 1<jupyter_output><empty_output><jupyter_text>Lets now include this cobra model object in our layout. Now, we are passing models to the layout as a list of `COBRA` models loaded into the current Python environment, which can also be done in addition to passing them as filenames.<jupyter_code>my_layout.models = [md]
my_layout.models<jupyter_output><empty_output><jupyter_text>As you see, now the layout has a list of models containing cobra models (only one model in this case). Note that the list of models in the layout can be either a list of model files (either cobra xml or comets models) or a list of cobra models loaded into the environment, but never a mix of both.  In order to make effective any changes in the layout models, we have to run `update_models`:<jupyter_code>my_layout.update_models()<jupyter_output><empty_output><jupyter_text>Now, lets run the new model and compare it to the previous simulation.<jupyter_code>my_comets = c.comets(my_layout, my_params)
my_comets.run()
biomass2 = my_comets.total_biomass
plt.plot('cycle', 'iJO1366', data=biomass1)
plt.plot('cycle', 'modified_iJO1366', data=biomass2)<jupyter_output>Running COMETS simulation ...
Done!<jupyter_text>Makes sense - we made the model worse in glucose by reducing $V_{max}$ and increasing $K_M$. Loading COMETS model filesThe `layout` class is also capable of loading model files in COMETS model format (`.cmd`, the format passed to the COMETS Java engine). This will be deprecated in the future to simplify things for the user, who will only have to deal with cobra type SBML models.For example, let's include a `COMETS` model in our previous layout, and run it.<jupyter_code>my_layout.models = ['test_models/iJO1366.cmd']
my_layout.update_models()
my_layout.models
my_comets = c.comets(my_layout, my_params)
my_comets.run()
print(my_comets.run_output)
plt.plot('cycle', 'iJO1366', data=my_comets.total_biomass)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07306711979609176, 'fraction_numerical': 0.011682242990654205, 'mean_word_length': 5.66053748231966, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '637340', 'license': 'MIT', 'n_tokens_mistral': 1500, 'n_tokens_neox': 1407, 'n_words': 582, 'path': '.ipynb_checkpoints/model_testing-checkpoint.ipynb', 'repo_name': 'jeremymchacon/COMETS-Python-Toolbox', 'size': '4708'}