Skip to content

Commit

Permalink
Add substances + specific fluxes to medium
Browse files Browse the repository at this point in the history
#104 , #99
- function to add a substance from db to medium
- changes to media_config to allow adding substances
- small docs additions
  • Loading branch information
cb-Hades committed Dec 19, 2023
1 parent 935f6f0 commit c0a0c69
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
29 changes: 28 additions & 1 deletion refinegems/database/medium.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,39 @@ def __init__(self, name:str, substance_table:pd.DataFrame=pd.DataFrame(columns=[

# possible @TODO
# ..............
# add compound
# has compound
# remove compound
# set source of
# ------------------------------------

def add_substance_from_db(self, name:str, flux:float=10.0):
"""Add a substance from the database to the medium.
Args:
name (str): Name of the substance. Should be part of the database substance.name column.
flux (float, optional): Sets the flux value of the new substance. Defaults to 10.0.
"""
# build connection to DB
connection = sqlite3.connect(PATH_TO_DB)
cursor = connection.cursor()
# fetch substance
result = cursor.execute(""" SELECT substance.name, substance.formula, substance2db.db_id, substance2db.db_type
FROM substance, substance2db
WHERE substance.name = ? AND substance.id = substance2db.substance_id""", (name,))
substance = result.fetchall()

# check if fetch was successful
if len(substance) == 0:
warnings.warn(f'Could not fetch substance {name} from DB.')
return

# add substance to table
substance_table = pd.DataFrame(substance, columns=['name','formula','db_id','db_type'])
substance_table.insert(2,'flux',flux)
substance_table.insert(3,'source',None)
self.substance_table = pd.concat([self.substance_table, substance_table], ignore_index=True)


def get_source(self, element:str) -> list[str]:
"""Get the source of a given element for the medium.
Expand Down
40 changes: 39 additions & 1 deletion refinegems/growth.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,15 @@ def growth_sim_multi(models: cobraModel|list[cobraModel], media: medium.Medium|l
# @IDEA: more options for fluxes
# @TODO/@IDEA: validity check nefore parsing
# @ASK: maybe something for the io module or does it fit here?
def read_media_config(yaml_path:str):
def read_media_config(yaml_path:str) -> tuple[list[medium.Medium],list[str,None]]:
"""Read the information from a media configuration file.
Args:
yaml_path (str): The path to a media configuration file in YAML-format.
Returns:
tuple[list[medium.Medium],list[str,None]]: Tuple of A) list of the loaded media and B) list of supplement modes.
"""

media_list = []
supplement_list = []
Expand Down Expand Up @@ -428,6 +436,36 @@ def read_media_config(yaml_path:str):
elif params and 'o2_percemt' in params.keys():
new_medium.set_oxygen_percentage(params['o2_percent'])

# add additional substances from DB
if p and 'add_substance' in p.keys():
for s,f in p['add_substance'].items():
# read in flux
if not f:
if p and 'default_flux' in p.keys():
f = p['default_flux']
elif params and 'default_flux' in params.keys():
f = params['default_flux']
elif isinstance(f,str):
if '%' in f:
f = float(f[:f.index('%')])/100
if p and 'default_flux' in p.keys():
f = f * p['default_flux']
elif params and 'default_flux' in params.keys():
f = f * params['default_flux']
else:
f = f * 10.0
else:
warn_string = f'Could not read in flux for {s}: {f}. \nWill be using default 10.0 instead.'
warnings.warn(warn_string)
f = 10.0
# change medium
if s in new_medium.substance_table['name'].tolist():
# change fluxes only
new_medium.substance_table.loc[new_medium.substance_table['name']==s,'flux'] = f
else:
# add substance to medium
new_medium.add_substance_from_db(s,f)

# supplement settings
if p and 'supplement' in p.keys():
supplement_list.append(p['supplement'])
Expand Down
9 changes: 9 additions & 0 deletions refinegems/media_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ params:
# supplement
# default_flux
# o2_percent
# add_substance: Null|float|str in format 'X.X%' with X being [0-9]*
# Add further substances to the medium and/or add specific fluxes to
# to substances. Flux settings here overwrite all previously set fluxes.
# Null uses the most specific set default flux.
# When a percentage is set uses this percentage of the most specific set default flux.
# --------------------------------------------------------------------

media:
Expand All @@ -72,6 +77,10 @@ media:
# add:
# - aa
# - SNM3
add_substance:
Glycine: Null
Glycerol: 0.87
Guanosine: '20%'
# PERS:
# external_base: link
# add_external:
Expand Down

0 comments on commit c0a0c69

Please sign in to comment.