Skip to content

Commit

Permalink
some cleanup of dFBA
Browse files Browse the repository at this point in the history
  • Loading branch information
eagmon committed Nov 6, 2024
1 parent 7db34eb commit 676b7e5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
5 changes: 0 additions & 5 deletions spatio_flux/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ def apply_non_negative(schema, current, update, core):
'exchange': 'map[float]', # {mol_id: delta_value}
}

particle_with_spin = {
'_type': 'spin_particle',
'_inherit': 'particle',
'spin': 'float'
}

def register_types(core):
core.register('positive_float', positive_float)
Expand Down
40 changes: 20 additions & 20 deletions spatio_flux/processes/dfba.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
warnings.filterwarnings("ignore", category=UserWarning, module="cobra.util.solver")
warnings.filterwarnings("ignore", category=FutureWarning, module="cobra.medium.boundary_types")

# TODO -- can set lower and upper bounds by config instead of hardcoding
MODEL_FOR_TESTING = load_model('textbook')


class DynamicFBA(Process):
"""
Expand All @@ -27,21 +24,16 @@ class DynamicFBA(Process):
Parameters:
- model: The metabolic model for the simulation.
- kinetic_params: Kinetic parameters (Km and Vmax) for each substrate.
- biomass_reaction: The identifier for the biomass reaction in the model.
- substrate_update_reactions: A dictionary mapping substrates to their update reactions.
- biomass_identifier: The identifier for biomass in the current state.
- bounds: A dictionary of bounds for any reactions in the model.
TODO -- check units
"""

config_schema = {
'model_file': 'string',
# 'model': 'any',
'kinetic_params': 'map[tuple[float,float]]',
'biomass_reaction': {
'_type': 'string',
'_default': 'Biomass_Ecoli_core'
},
'substrate_update_reactions': 'map[string]',
'biomass_identifier': 'string',
'bounds': 'map[bounds]',
Expand All @@ -50,9 +42,7 @@ class DynamicFBA(Process):
def __init__(self, config, core):
super().__init__(config, core)

if self.config['model_file'] == 'TESTING':
self.model = MODEL_FOR_TESTING
elif not 'xml' in self.config['model_file']:
if not 'xml' in self.config['model_file']:
# use the textbook model if no model file is provided
self.model = load_model(self.config['model_file'])
elif isinstance(self.config['model_file'], str):
Expand All @@ -77,9 +67,15 @@ def outputs(self):
'substrates': 'map[positive_float]'
}

# def interface(self):
# return {
# 'inputs': {'substrates': 'map[positive_float]'},
# 'outputs': {'substrates': 'map[positive_float]'},
# }

# TODO -- can we just put the inputs/outputs directly in the function?
def update(self, state, interval):
substrates_input = state['substrates']
def update(self, inputs, interval):
substrates_input = inputs['substrates']

for substrate, reaction_id in self.config['substrate_update_reactions'].items():
Km, Vmax = self.config['kinetic_params'][substrate]
Expand All @@ -92,13 +88,13 @@ def update(self, state, interval):
solution = self.model.optimize()
if solution.status == 'optimal':
current_biomass = substrates_input[self.config['biomass_identifier']]
biomass_growth_rate = solution.fluxes[self.config['biomass_reaction']]
biomass_growth_rate = solution.objective_value
substrate_update[self.config['biomass_identifier']] = biomass_growth_rate * current_biomass * interval

for substrate, reaction_id in self.config['substrate_update_reactions'].items():
flux = solution.fluxes[reaction_id] * current_biomass * interval
old_concentration = substrates_input[substrate]
new_concentration = max(old_concentration + flux, 0) # keep above 0
new_concentration = max(old_concentration + flux, 0) # keep above 0 -- TODO this should not happen
substrate_update[substrate] = new_concentration - old_concentration
# TODO -- assert not negative?
else:
Expand All @@ -116,7 +112,6 @@ def update(self, state, interval):
def dfba_config(
model_file='textbook',
kinetic_params=None,
biomass_reaction='Biomass_Ecoli_core',
substrate_update_reactions=None,
biomass_identifier='biomass',
bounds=None
Expand All @@ -136,14 +131,19 @@ def dfba_config(
return {
'model_file': model_file,
'kinetic_params': kinetic_params,
'biomass_reaction': biomass_reaction,
'substrate_update_reactions': substrate_update_reactions,
'biomass_identifier': biomass_identifier,
'bounds': bounds
}


def get_single_dfba_spec(mol_ids=None, path=None, i=None, j=None):
def get_single_dfba_spec(
model_file='textbook',
mol_ids=None,
path=None,
i=None,
j=None,
):
"""
Constructs a configuration dictionary for a dynamic FBA process with optional path indices.
Expand Down Expand Up @@ -178,7 +178,7 @@ def build_path(mol_id):
return {
'_type': 'process',
'address': 'local:DynamicFBA',
'config': dfba_config(),
'config': dfba_config(model_file=model_file),
'inputs': {
'substrates': {mol_id: build_path(mol_id) for mol_id in mol_ids}
},
Expand Down
4 changes: 2 additions & 2 deletions spatio_flux/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,9 @@ def run_particles_dfba(
core = ProcessTypes()
core = register_types(core)

# run_dfba_single(core=core)
run_dfba_single(core=core)
# run_dfba_spatial(core=core, n_bins=(4,4), total_time=60)
# run_diffusion_process(core=core)
# run_particles(core)
# run_particle_comets(core)
run_particles_dfba(core)
# run_particles_dfba(core)

0 comments on commit 676b7e5

Please sign in to comment.