From d03d24e9cd530e126249938e82ee21b48e23dc92 Mon Sep 17 00:00:00 2001 From: Eran Date: Wed, 19 Jul 2023 15:16:32 -0400 Subject: [PATCH] initial commit --- .../experiments/glucose_phosphorylation.py | 105 ---------------- template/processes/glucose_phosphorylation.py | 119 ------------------ {template => vivarium_tellurium}/__init__.py | 0 .../composites/__init__.py | 0 .../injected_glc_phosphorylation.py | 0 .../experiments/__init__.py | 0 .../library/__init__.py | 0 .../processes/__init__.py | 0 .../processes/tellurium_process.py | 100 ++++++--------- 9 files changed, 35 insertions(+), 289 deletions(-) delete mode 100644 template/experiments/glucose_phosphorylation.py delete mode 100644 template/processes/glucose_phosphorylation.py rename {template => vivarium_tellurium}/__init__.py (100%) rename {template => vivarium_tellurium}/composites/__init__.py (100%) rename {template => vivarium_tellurium}/composites/injected_glc_phosphorylation.py (100%) rename {template => vivarium_tellurium}/experiments/__init__.py (100%) rename {template => vivarium_tellurium}/library/__init__.py (100%) rename {template => vivarium_tellurium}/processes/__init__.py (100%) rename template/processes/template_process.py => vivarium_tellurium/processes/tellurium_process.py (50%) diff --git a/template/experiments/glucose_phosphorylation.py b/template/experiments/glucose_phosphorylation.py deleted file mode 100644 index ba4f007..0000000 --- a/template/experiments/glucose_phosphorylation.py +++ /dev/null @@ -1,105 +0,0 @@ -""" -====================================== -Toy Glucose Phosphorylation Experiment -====================================== - -This is a toy example referred to by the documentation. -""" - -# TODO: Delete this file before publishing your project. - -import os - -from vivarium.core.composition import ( - simulate_experiment, - EXPERIMENT_OUT_DIR, -) -from vivarium.plots.simulation_output import plot_simulation_output -from vivarium.core.emitter import ( - path_timeseries_from_data, - timeseries_from_data, -) -from vivarium.core.engine import Engine - -from template.composites.injected_glc_phosphorylation import ( - InjectedGlcPhosphorylation, -) - - -NAME = 'glucose_phosphorylation' -OUT_DIR = os.path.join(EXPERIMENT_OUT_DIR, NAME) - - -def glucose_phosphorylation_experiment(config=None): - if config is None: - config = {} - default_config = { - 'injected_glc_phosphorylation': {}, - 'emitter': { - 'type': 'timeseries', - }, - 'initial_state': {}, - } - default_config.update(config) - config = default_config - compartment = InjectedGlcPhosphorylation( - config['injected_glc_phosphorylation']) - compartment_dict = compartment.generate() - experiment = Engine( - processes=compartment_dict['processes'], - topology=compartment_dict['topology'], - emitter=config['emitter'], - initial_state=config['initial_state'], - ) - return experiment - - -def run_experiment(): - experiment = glucose_phosphorylation_experiment() - settings = { - 'return_raw_data': True, - } - data = simulate_experiment(experiment, settings) - experiment.end() - return data - - -def test_experiment(): - data = run_experiment() - path_ts = path_timeseries_from_data(data) - # At every timestep, the changes in ADP and G6P should be equal, and - # the change in GLC should be the same but with the opposite sign. - atp = path_ts[('cell', 'ATP')] - adp = path_ts[('cell', 'ADP')] - g6p = path_ts[('cell', 'G6P')] - glc = path_ts [('cell', 'GLC')] - - assert len(atp) == len(adp) == len(g6p) == len(glc) - - for i in range(len(atp) - 1): - delta_adp = adp[i + 1] - adp[i] - delta_g6p = g6p[i + 1] - g6p[i] - delta_glc = glc[i + 1] - glc[i] - delta_atp = atp[i + 1] - atp[i] - - assert delta_adp == delta_g6p, 'index: {}'.format(i) - assert delta_atp > 0, 'index: {}'.format(i) - assert delta_glc < 0, 'index: {}'.format(i) - - -def main(): - test_experiment() - data = run_experiment() - agents_plot_settings = { - 'agents_key': 'agents', - } - plot_simulation_output( - timeseries_from_data(data), - agents_plot_settings, - OUT_DIR, - 'simulation', - ) - - -if __name__ == '__main__': - main() diff --git a/template/processes/glucose_phosphorylation.py b/template/processes/glucose_phosphorylation.py deleted file mode 100644 index 546508f..0000000 --- a/template/processes/glucose_phosphorylation.py +++ /dev/null @@ -1,119 +0,0 @@ -""" -=================================== -Toy Glucose Phosphorylation Process -=================================== - -This is a toy example referenced in the documentation. -""" - -# TODO: Delete this file before publishing your project. - -from vivarium.core.process import Process -from vivarium.core.composition import simulate_process -from vivarium.plots.simulation_output import plot_simulation_output -from vivarium.library.units import units - - -class GlucosePhosphorylation(Process): - - name = 'glucose_phosphorylation' - defaults = { - 'k_cat': 2e-3, - 'K_ATP': 5e-2, - 'K_GLC': 4e-2, - } - - def __init__(self, parameters=None): - super().__init__(parameters) - - def next_update(self, timestep, states): - # Get concentrations from state - cytoplasm = states['cytoplasm'] - nucleoside_phosphates = states['nucleoside_phosphates'] - hk = cytoplasm['HK'] - glc = cytoplasm['GLC'] - atp = nucleoside_phosphates['ATP'] - - # Get kinetic parameters - k_cat = self.parameters['k_cat'] - k_atp = self.parameters['K_ATP'] - k_glc = self.parameters['K_GLC'] - - # Compute reaction rate with michaelis-menten equation - rate = k_cat * hk * glc * atp / ( - k_glc * k_atp + k_glc * atp + k_atp * glc + glc * atp) - - # Compute concentration changes from rate and timestep - delta_glc = -rate * timestep - delta_atp = -rate * timestep - delta_g6p = rate * timestep - delta_adp = rate * timestep - - # Compile changes into an update - update = { - 'cytoplasm': { - 'GLC': delta_glc, - 'G6P': delta_g6p, - # We exclude HK because it doesn't change - }, - 'nucleoside_phosphates': { - 'ATP': delta_atp, - 'ADP': delta_adp, - }, - } - - return update - - def ports_schema(self): - return { - 'cytoplasm': { - 'GLC': { - # accumulate means to add the updates - '_updater': 'accumulate', - '_default': 1.0, - '_properties': { - 'mw': 1.0 * units.g / units.mol, - }, - '_emit': True, - }, - # accumulate is the default, so we don't need to specify - # updaters for the rest of the variables - 'G6P': { - '_default': 0.0, - '_properties': { - 'mw': 1.0 * units.g / units.mol, - }, - '_emit': True, - }, - 'HK': { - '_default': 0.1, - '_properties': { - 'mw': 1.0 * units.g / units.mol, - }, - }, - }, - 'nucleoside_phosphates': { - 'ATP': { - '_default': 2.0, - '_emit': True, - }, - 'ADP': { - '_default': 0.0, - '_emit': True, - } - }, - } - - -if __name__ == '__main__': - parameters = { - 'k_cat': 1.5, - 'time_step': 0.1, - } - my_process = GlucosePhosphorylation(parameters) - - settings = { - 'total_time': 10, - } - timeseries = simulate_process(my_process, settings) - plot_simulation_output(timeseries, {}, './') diff --git a/template/__init__.py b/vivarium_tellurium/__init__.py similarity index 100% rename from template/__init__.py rename to vivarium_tellurium/__init__.py diff --git a/template/composites/__init__.py b/vivarium_tellurium/composites/__init__.py similarity index 100% rename from template/composites/__init__.py rename to vivarium_tellurium/composites/__init__.py diff --git a/template/composites/injected_glc_phosphorylation.py b/vivarium_tellurium/composites/injected_glc_phosphorylation.py similarity index 100% rename from template/composites/injected_glc_phosphorylation.py rename to vivarium_tellurium/composites/injected_glc_phosphorylation.py diff --git a/template/experiments/__init__.py b/vivarium_tellurium/experiments/__init__.py similarity index 100% rename from template/experiments/__init__.py rename to vivarium_tellurium/experiments/__init__.py diff --git a/template/library/__init__.py b/vivarium_tellurium/library/__init__.py similarity index 100% rename from template/library/__init__.py rename to vivarium_tellurium/library/__init__.py diff --git a/template/processes/__init__.py b/vivarium_tellurium/processes/__init__.py similarity index 100% rename from template/processes/__init__.py rename to vivarium_tellurium/processes/__init__.py diff --git a/template/processes/template_process.py b/vivarium_tellurium/processes/tellurium_process.py similarity index 50% rename from template/processes/template_process.py rename to vivarium_tellurium/processes/tellurium_process.py index 8494362..50cfa13 100644 --- a/template/processes/template_process.py +++ b/vivarium_tellurium/processes/tellurium_process.py @@ -1,33 +1,15 @@ -''' -Execute by running: ``python template/processes/template_process.py`` - -TODO: Replace the template code to implement your own process. -''' - -import os - from vivarium.core.process import Process -from vivarium.core.composition import ( - simulate_process, - PROCESS_OUT_DIR, -) -from vivarium.plots.simulation_output import plot_simulation_output - +from vivarium.core.engine import Engine, pp -# a global NAME is used for the output directory and for the process name -NAME = 'template' +# TODO import tellurium -class Template(Process): - ''' - This mock process provides a basic template that can be used for a new process - ''' - - # give the process a name, so that it can register in the process_repository - name = NAME +class TelluriumProcess(Process): + ''' Vivarium Process for Tellurium ''' # declare default parameters as class variables defaults = { + 'model_path': '', 'uptake_rate': 0.1, } @@ -50,6 +32,7 @@ def ports_schema(self): * `_serializer` ''' + # TODO this needs to be done automatically by reading the tellurium model. Maybe study vivarium-biosimulators return { 'internal': { 'A': { @@ -67,36 +50,35 @@ def ports_schema(self): }, } - def next_update(self, timestep, states): + def next_update(self, interval, states): - # get the states + # get the states from the tellurium model internal_A = states['internal']['A'] external_A = states['external']['A'] - # calculate timestep-dependent updates - internal_update = self.parameters['uptake_rate'] * external_A * timestep + # run tellurium here + internal_update = self.parameters['uptake_rate'] * external_A * interval external_update = -1 * internal_update - # return an update that mirrors the ports structure - return { + # get the results from tellurium + update = { 'internal': { 'A': internal_update}, 'external': { 'A': external_update} } + # return an update that mirrors the ports structure + return update -# functions to configure and run the process -def run_template_process(): - '''Run a simulation of the process. - Returns: - The simulation output. - ''' - # initialize the process by passing in parameters - parameters = {} - template_process = Template(parameters) +def test_tellurium(): + totaltime = 10.0 + + # initialize the process + config = {} + tellurium_process = TelluriumProcess(config) # declare the initial state, mirroring the ports structure initial_state = { @@ -108,38 +90,26 @@ def run_template_process(): }, } - # run the simulation - sim_settings = { - 'total_time': 10, - 'initial_state': initial_state} - output = simulate_process(template_process, sim_settings) - - return output - + ports = tellurium_process.ports_schema() + print('PORTS') + print(ports) -def test_template_process(): - '''Test that the process runs correctly. + # make the simulation + sim = Engine( + processes={'tellurium_process': tellurium_process}, + topology={'tellurium_process': {port_id: (port_id,) for port_id in ports.keys()}}, + initial_state=initial_state + ) - This will be executed by pytest. - ''' - output = run_template_process() - # TODO: Add assert statements to ensure correct performance. - - -def main(): - '''Simulate the process and plot results.''' - # make an output directory to save plots - out_dir = os.path.join(PROCESS_OUT_DIR, NAME) - if not os.path.exists(out_dir): - os.makedirs(out_dir) + # run the simulation + sim.update(totaltime) - output = run_template_process() + # get the results + data = sim.emitter.get_data() - # plot the simulation output - plot_settings = {} - plot_simulation_output(output, plot_settings, out_dir) + print(pp(data)) # run module with python template/processes/template_process.py if __name__ == '__main__': - main() + test_tellurium()