Skip to content

Commit

Permalink
Update growth_simulation
Browse files Browse the repository at this point in the history
#104 , #79
Update growth simulation + plotting model vs media (bars and heatmap)
  • Loading branch information
cb-Hades committed Dec 4, 2023
1 parent 5331494 commit 231e232
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 134 deletions.
2 changes: 1 addition & 1 deletion refinegems/database/medium.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def set_default_flux(self,flux:float =10.0, replace:bool =False, double_o2:bool
# keep already set fluxes
else:
self.substance_table['flux'] = self.substance_table['flux'].fillna(flux)
if self.is_aerobic() and double_o2 and self.substance_table.loc[self.substance_table['name']=='Dioxygen [O2]','flux'].isna():
if self.is_aerobic() and double_o2 and self.substance_table.loc[self.substance_table['name']=='Dioxygen [O2]','flux'].any():
self.substance_table.loc[self.substance_table['name']=='Dioxygen [O2]','flux'] = 2*flux

def set_oxygen_percentage(self, perc:float=1.0):
Expand Down
48 changes: 28 additions & 20 deletions refinegems/growth.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import re
from typing import Literal
import yaml
import matplotlib.pyplot as plt

__author__ = "Famke Baeuerle and Carolin Brune"

Expand Down Expand Up @@ -261,29 +262,28 @@ def get_metabs_essential_for_growth_wrapper(model: cobraModel, media: list[mediu


# @TEST
def growth_sim_single(model: cobraModel, m: medium.Medium, supplement:Literal[None,'std','min'] = None, anaerobic:bool = False) -> reports.SingleGrowthSimulationReport:
def growth_sim_single(model: cobraModel, m: medium.Medium, supplement:Literal[None,'std','min'] = None) -> reports.SingleGrowthSimulationReport:
"""Simulate the growth of a model on a given medium.
Args:
model (cobraModel): The model.
m (medium.Medium): The medium.
supplement (Literal[None,'std','min'], optional): Flag to add additvites to the model to ensure growth. Defaults to None (no supplements).
Further options include 'std' for standard uptake and 'min' for minimal uptake supplementation.
anaerobic (bool, optional): Set medium to anaerobic/aerobic. Defaults to False (aerobic growth).
Returns:
reports.SingleGrowthSimulationReport: Object with the simulation results
"""

with model:
# check for (an)aerobic conditions
if anaerobic and m.is_aerobic():
m.make_anaerobic()
if not anaerobic and not m.is_aerobic():
m.make_aerobic(flux=10.0)

# convert to a cobrapy medium
exported_m = medium.medium_to_model(medium=m, model=model, add=False)
exported_m = medium.medium_to_model(medium=m, model=model,
namespace='BiGG',
default_flux=10.0,
replace=False,
double_o2=False,
add=False)

# supplement, if tag is set
match supplement:
Expand Down Expand Up @@ -314,7 +314,6 @@ def growth_sim_single(model: cobraModel, m: medium.Medium, supplement:Literal[No
return report


# @TEST
def growth_sim_multi(models: cobraModel|list[cobraModel], media: medium.Medium|list[medium.Medium], supplement_modes:list[Literal['None','min','std']]|None|Literal['None','min','std']=None) -> reports.GrowthSimulationReport:
"""Simulate the growth of (at least one) models on (at least one) media.
Expand All @@ -341,14 +340,14 @@ def growth_sim_multi(models: cobraModel|list[cobraModel], media: medium.Medium|l
report = reports.GrowthSimulationReport()
for mod in models:
for med,supp in zip(media, supplement_modes):
o2_check = med.is_aerobic()
r = growth_sim_single(mod, med, anaerobic = not o2_check, supplement=supp)
r = growth_sim_single(mod, med, supplement=supp)
report.add_sim_results(r)

return report


# @IDEA: more options for fluxes
# @TODO/@IDEA: validity check nefore parsing
def read_media_config(yaml_path:str):

media_list = []
Expand Down Expand Up @@ -436,12 +435,15 @@ def read_media_config(yaml_path:str):
return (media_list,supplement_list)

# @TODO
# @IDEA : choose different namespaces for the media
# main objective: read in models and media from input (command line, YAML etc.) dict?
# -> compile a complete list media (load, add information about anaerobic, additives, fluxes and the like)
# -> run simulation
# -> compile a complete list media (load, add information about anaerobic, additives, fluxes and the like) : CHECK
# -> run simulation : CHECK
# -> visulise also here?
def growth_analysis(models:cobra.Model|str|list[str]|list[cobra.Model],
media:medium.Medium|list[medium.Medium]|str):
media:medium.Medium|list[medium.Medium]|str,
supplements:None|list[Literal[None,'std','min']]=None,
retrieve:Literal['report','plot','both']='plot') -> reports.GrowthSimulationReport|plt.Figure|tuple:

# read-in all models into list
# ----------------------------
Expand All @@ -452,7 +454,7 @@ def growth_analysis(models:cobra.Model|str|list[str]|list[cobra.Model],
if len(models) > 0:
# if list entries are paths
if all(isinstance(_, str) for _ in models):
mod_list = load_multiple_models(models, package='COBRApy')
mod_list = load_multiple_models(models, package='cobra')
# if list entries are already cobra.Models
elif all(isinstance(_, cobra.Model) for _ in models):
mod_list = models
Expand Down Expand Up @@ -487,20 +489,26 @@ def growth_analysis(models:cobra.Model|str|list[str]|list[cobra.Model],
raise TypeError('Unknown type found in media, should be list fo medium.Medium.')
# string - connection to YAML config file
case str():
# @TODO
media, supplements = read_media_config(media)
# unknown input
case _:
raise ValueError(f'Unknown input for media: {media}')

# run simulation
# --------------
report = growth_sim_multi(models, media)

return report
report = growth_sim_multi(mod_list, media, supplements)

# save / visualise report
pass
# -----------------------
match retrieve:
case 'report':
return report
case 'plot':
return report.plot_growth()
case 'both':
return (report, report.plot_growth)
case _:
raise ValueError(f'Unknown input for retrieve: {retrieve}')


# @RENAMED
Expand Down
6 changes: 3 additions & 3 deletions refinegems/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from libsbml import Model as libModel
from libsbml import SBMLReader, writeSBMLToFile, SBMLValidator, SBMLDocument
from datetime import date
from typing import Union
from typing import Union, Literal

__author__ = "Tobias Fehrenbach, Famke Baeuerle and Gwendolyn O. Döbel"

Expand Down Expand Up @@ -56,12 +56,12 @@ def load_model_libsbml(modelpath: str) -> libModel:
return mod


def load_multiple_models(models: list[str], package: str) -> list:
def load_multiple_models(models: list[str], package: Literal['cobra','libsbml']) -> list:
"""Loads multiple models into a list
Args:
- models (list): List of paths to models
- package (str): COBRApy|libSBML
- package (str): cobra|libsbml
Returns:
list: List of model objects loaded with COBRApy|libSBML
Expand Down
18 changes: 10 additions & 8 deletions refinegems/media_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ media:
aerobic: False
LB_o2:
base: LB
default_flux: 5.0
o2_percent: 2.0
SNM3:
supplement: min
# supplement: min
M9:
add:
- aa
- SNM3
PERS:
external_base: link
add_external:
- link
# add:
# - aa
# - SNM3
# PERS:
# external_base: link
# add_external:
# - link
Loading

0 comments on commit 231e232

Please sign in to comment.