# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.8.5 64-bit (''base'': conda)'
#     name: python3
# ---

# # Setup
#
# An example of what the model setup can do is shown here. This will be demonstrated on five metabolic models: Moraxella, H. Influenza, S. aureus, S. epidermidis and D. pigrum. 
#
# Hence, let's import some of the methods we will use.

from ncmw.utils import get_models
from ncmw.setup_models import set_default_configs_and_snm3_medium, create_consistent_model, gapfill_medium, gapfill_model
models = get_models("models")
for model in models:
    print(f"Growth {model.id}: {model.slim_optimize()}")

# Notice that the growth values differ strongly. This is because the bounds and the medium differ. So let's first standardize these!

# ## Set default configurations
#
# This package is devoted to the work on nasal microbiomes expressed through the **SNM3** medium. Unfortunately, most metabolic models are created for the gut and thus often fail to achieve growth on SNM3. Here, 3 of the 5 models fail to achieve growth.
#
# As a result, we cannot analyze them. However, it must be noted that when analyzing a community, this may be less relevant, as other members can provide missing metabolites through interactions. On the other hand, it may be well known that this species can grow on the medium hence this indicates some problems with the metabolic model.

standardized_models =[]
for model in models:
    standardized_models.append(set_default_configs_and_snm3_medium(model))

for model in standardized_models:
    print(f"Growth {model.id}: {model.slim_optimize()}")

# In order to allow analysis and to fix errors, we implement several "gap filling" strategies. This adds metabolites to the medium or reactions to the model so that it can grow.

# ## Gapfilling
#
# To achieve growth on a general predefined medium, we deploy two strategies:
# * **Gapfill medium**: Since other members of the community could provide different metabolites that are currently unmodeled, the medium is less strict from the point of view of the community. Hence we may extend the medium by the minimal number of metabolites such that the organism can grow.
# * **Gapfill model:** We can add reactions to the model such that it can grow on the given medium i.e. reactions such that the model can synthesize the missing metabolites.

for model in standardized_models:
    old_medium = model.medium
    m2, extension = gapfill_medium(model)
    print(f"We need to add following metabolites to the medium: {extension}")
    print(f"Growth {m2.id}: {m2.slim_optimize()}")
    m2.medium = old_medium # Rest for next step

# Nice, this worked. It also provides us with information about missing metabolites. Note that the solution to this problem is not unique e.g. for the first result an equivalent result would be obtained by adding EX_fe3_e. 

for model in standardized_models:
    m2, extension = gapfill_model(model)
    print(f"We need to add following reactions to the model: {[e for e in extension]}")
    print(f"Growth {m2.id}: {m2.slim_optimize()}")

# Nice, this worked too! Notice that the results are quite similar but slightly different. Just adding reactions to a model is, of course, questionable i.e. these reactions should have some empirical evidence. One can look e.g. in KEGG if genes for certain reactions are present for a given species.

# ## Cleaning the model
#
# There are typically several blocked reactions or mass charge violations within a metabolic model. Thus before checking the quality we may want to remove these e.g. using the FastCC algorithm.

consistent_models =[]
reports = []
for model in standardized_models:
    consistent_model, report = create_consistent_model(model)
    consistent_models.append(consistent_model)
    reports.append(report)
    

# Lets look at one of the report returned.

reports[0]

# As expected we can see some improvements.

for model in consistent_models:
    print(f"Growth {model.id}: {model.slim_optimize()}")

# And that's it. Now we may first check the model's quality i.e. using **memote**. Next, we can start analyzing and comparing results for the models, or continue to create a community model from these. This also serves as a small overview of what the "ncmw_setup" can do.

StarcoderdataJupyterScriptsDedupFiltered