Skip to content

Commit

Permalink
Merge pull request #236 from CovertLab/cofactors
Browse files Browse the repository at this point in the history
Cofactors updates
  • Loading branch information
cyrus-bio authored Aug 21, 2024
2 parents f8fed4e + 1420a34 commit 3e8b756
Show file tree
Hide file tree
Showing 30 changed files with 40,065 additions and 31,185 deletions.
287 changes: 131 additions & 156 deletions ecoli/experiments/metabolism_redux_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Metabolism using user-friendly FBA
==================================
"""

import argparse

# vivarium-core imports
Expand All @@ -19,39 +18,38 @@
import datetime
import dill


def run_ecoli_with_metabolism_redux(
filename="metabolism_redux",
total_time=5000,
divide=True,
initial_state_file="wcecoli_t0",
progress_bar=True,
log_updates=False,
emitter="timeseries",
name="metabolism-redux",
raw_output=False,
save=False,
# save_times=4,
filename='metabolism_redux',
total_time=5000,
divide=True,
initial_state_file='wcecoli_t0',
progress_bar=True,
log_updates=False,
emitter='timeseries',
name='metabolism-redux',
raw_output=False,
save=False,
# save_times=4,
):
# filename = 'default'
sim = EcoliSim.from_file(CONFIG_DIR_PATH + filename + ".json")
sim = EcoliSim.from_file(CONFIG_DIR_PATH + filename + '.json')
sim.total_time = total_time
sim.divide = divide
sim.progress_bar = progress_bar
sim.log_updates = log_updates
sim.emitter = emitter
sim.initial_state = get_state_from_file(path=f"data/{initial_state_file}.json")
sim.initial_state = get_state_from_file(path=f'data/{initial_state_file}.json')
sim.raw_output = raw_output
sim.save = save


sim.build_ecoli()
sim.run()

query = []
folder = f"out/fbagd/{name}_{total_time}_{datetime.date.today()}/"
folder = f'out/fbagd/{name}_{total_time}_{datetime.date.today()}/'
save_sim_output(folder, query, sim, save_model=True)


# disables growth rate control
def run_ecoli_with_metabolism_redux_classic(
filename="metabolism_redux_classic",
Expand Down Expand Up @@ -104,62 +102,61 @@ def run_ecoli_with_metabolism_redux_classic(
sim.run()

query = []
folder = f"out/cofactors/{name}_{total_time}_{datetime.date.today()}/"
folder = f'out/cofactors/{name}_{total_time}_{datetime.date.today()}/'
save_sim_output(folder, query, sim, save_model=True)

def run_colony(
filename='metabolism_redux_classic',
total_time=1500,
divide=True,
# initial_state_file='wcecoli_t0', # 'met_division_test_state',
progress_bar=True,
log_updates=False,
emitter='timeseries', # 'timeseries',
name='metabolism-redux-classic-rich',
raw_output=False,
save=True,
save_times=[1, 200, 400, 1300],
condition = "with_aa", # basal, with_aa
fixed_media = "minimal_plus_amino_acids" # minimal, minimal_plus_amino_acids
):
# filename = 'default'
sim = EcoliSim.from_file(CONFIG_DIR_PATH + filename + ".json")
sim.total_time = total_time
sim.divide = divide
sim.progress_bar = progress_bar
sim.log_updates = log_updates
sim.emitter = emitter
# sim.initial_state = get_state_from_file(path=f'data/{initial_state_file}.json')
sim.raw_output = raw_output
sim.save = save
sim.save_times = save_times

# def run_ecoli_with_metabolism_redux_classic(
# filename="metabolism_redux_classic",
# total_time=1500,
# divide=True,
# # initial_state_file='wcecoli_t0', # 'met_division_test_state',
# progress_bar=True,
# log_updates=False,
# emitter="timeseries", # 'timeseries',
# name="metabolism-redux-classic-rich",
# raw_output=False,
# save=True,
# save_times=[1, 200, 400, 1300],
# condition="with_aa", # basal, with_aa
# fixed_media="minimal_plus_amino_acids", # minimal, minimal_plus_amino_acids
# ):
# # filename = 'default'
# sim = EcoliSim.from_file(CONFIG_DIR_PATH + filename + ".json")
# sim.total_time = total_time
# sim.divide = divide
# sim.progress_bar = progress_bar
# sim.log_updates = log_updates
# sim.emitter = emitter
# # sim.initial_state = get_state_from_file(path=f'data/{initial_state_file}.json')
# sim.raw_output = raw_output
# sim.save = save
# sim.save_times = save_times

# sim.condition = condition
# sim.fixed_media = fixed_media
sim.condition = condition
sim.fixed_media = fixed_media

# for seed in [i for i in range(20, 29, 1)]:
# sim.seed = seed
for seed in [i for i in range(20, 29, 1)]:
sim.seed = seed

# sim.build_ecoli()
sim.build_ecoli()

# sim.run()
sim.run()

# query = []
# folder = f"out/cofactors/rich-{seed}/"
# save_sim_output(folder, query, sim, save_model=False)
query = []
folder = f'out/cofactors/rich-{seed}/'
save_sim_output(folder, query, sim, save_model=False)


@pytest.mark.slow
def test_ecoli_with_metabolism_redux(
filename="metabolism_redux",
total_time=4,
divide=False,
progress_bar=True,
log_updates=False,
emitter="timeseries",
filename='metabolism_redux',
total_time=4,
divide=False,
progress_bar=True,
log_updates=False,
emitter='timeseries',
):
sim = EcoliSim.from_file(CONFIG_DIR_PATH + filename + ".json")
sim = EcoliSim.from_file(CONFIG_DIR_PATH + filename + '.json')
sim.total_time = total_time
sim.divide = divide
sim.progress_bar = progress_bar
Expand All @@ -170,56 +167,45 @@ def test_ecoli_with_metabolism_redux(
# run simulation and add asserts to output
sim.run()


@pytest.mark.slow
def test_ecoli_with_metabolism_redux_div(
filename="metabolism_redux",
total_time=4,
divide=True,
emitter="timeseries",
filename='metabolism_redux',
total_time=4,
divide=True,
emitter='timeseries',
):
# TODO (Cyrus) - Add test that affirms structure of output query.
sim = EcoliSim.from_file(CONFIG_DIR_PATH + filename + ".json")
sim = EcoliSim.from_file(CONFIG_DIR_PATH + filename + '.json')
sim.total_time = total_time
sim.divide = divide
sim.emitter = emitter
sim.build_ecoli()

sim.run()


query = []
agents = sim.query()["agents"].keys()
agents = sim.query()['agents'].keys()
for agent in agents:
query.extend(
[
("agents", agent, "listeners", "fba_results"),
("agents", agent, "listeners", "mass"),
("agents", agent, "bulk"),
]
)
query.extend([('agents', agent, 'listeners', 'fba_results'),
('agents', agent, 'listeners', 'mass'),
('agents', agent, 'bulk')])
output = sim.query(query)

# test that water is being used (model is running)
assert (
sum(
output["agents"][agent]["listeners"]["fba_results"][
"estimated_exchange_dmdt"
]["WATER"]
)
!= 0
)
assert sum(output['agents'][agent]['listeners']['fba_results']['estimated_exchange_dmdt']['WATER']) != 0


@pytest.mark.slow
def test_ecoli_with_metabolism_classic(
filename="metabolism_redux_classic",
total_time=4,
divide=False,
progress_bar=True,
log_updates=False,
emitter="timeseries",
filename='metabolism_redux_classic',
total_time=4,
divide=False,
progress_bar=True,
log_updates=False,
emitter='timeseries',
):
sim = EcoliSim.from_file(CONFIG_DIR_PATH + filename + ".json")
sim = EcoliSim.from_file(CONFIG_DIR_PATH + filename + '.json')
sim.total_time = total_time
sim.divide = divide
sim.progress_bar = progress_bar
Expand All @@ -231,44 +217,41 @@ def test_ecoli_with_metabolism_classic(
sim.run()


@pytest.mark.slow
def test_ecoli_with_metabolism_classic_div(
filename="metabolism_redux_classic",
total_time=10,
divide=True,
emitter="timeseries",
initial_state_file="vivecoli_t2527",
):
# TODO (Cyrus) - Add test that affirms structure of output query.
sim = EcoliSim.from_file(CONFIG_DIR_PATH + filename + ".json")
sim.total_time = total_time
sim.initial_state = get_state_from_file(path=f"data/{initial_state_file}.json")

sim.divide = divide
sim.emitter = emitter

# this means that sims will not create conflicting random indices
sim.seed += int(sim.initial_state["agents"]["0"]["global_time"])

sim.build_ecoli()

sim.run()

# assert division occured
assert (
len(sim.query()["agents"]) == 3
), "Cell did not divide in metabolism division test"

# @pytest.mark.slow
# def test_ecoli_with_metabolism_classic_div(
# filename='metabolism_redux_classic',
# total_time=10,
# divide=True,
# emitter='timeseries',
# initial_state_file='met_division_test_state',
# ):
# # TODO (Cyrus) - Add test that affirms structure of output query.
# sim = EcoliSim.from_file(CONFIG_DIR_PATH + filename + '.json')
# sim.total_time = total_time
# sim.initial_state = get_state_from_file(path=f'data/{initial_state_file}.json')
#
# sim.divide = divide
# sim.emitter = emitter
#
# # this means that sims will not create conflicting random indices
# sim.seed += int(sim.initial_state['agents']['0']['global_time'])
#
# sim.build_ecoli()
#
# sim.run()
#
# # assert division occured
# assert len(sim.query()['agents']) == 3, "Cell did not divide in metabolism division test"

def run_ecoli_with_default_metabolism(
filename="default",
total_time=10,
divide=False,
progress_bar=True,
log_updates=False,
emitter="timeseries",
filename='default',
total_time=10,
divide=False,
progress_bar=True,
log_updates=False,
emitter='timeseries',
):
sim = EcoliSim.from_file(CONFIG_DIR_PATH + filename + ".json")
sim = EcoliSim.from_file(CONFIG_DIR_PATH + filename + '.json')
sim.total_time = total_time
sim.divide = divide
sim.progress_bar = progress_bar
Expand All @@ -280,54 +263,46 @@ def run_ecoli_with_default_metabolism(
# output = sim.query()
output = sim.ecoli_experiment.emitter.get_timeseries()

folder = f"out/fbagd/{total_time}/{datetime.datetime.now()}/"
pathlib.Path(folder).mkdir(parents=True, exist_ok=True)
np.save(folder + "fba_results.npy", output["listeners"]["fba_results"])
np.save(folder + "mass.npy", output["listeners"]["mass"])
np.save(folder + "bulk.npy", output["bulk"])
np.save(
folder + "stoichiometry.npy",
sim.ecoli_experiment.steps["ecoli-metabolism"].model.stoichiometry,
)

folder = f'out/fbagd/{total_time}/{datetime.datetime.now()}/'
pathlib.Path(folder).mkdir(parents=True, exist_ok=True)
np.save(folder + 'fba_results.npy', output['listeners']['fba_results'])
np.save(folder + 'mass.npy', output['listeners']['mass'])
np.save(folder + 'bulk.npy', output['bulk'])
np.save(folder + 'stoichiometry.npy', sim.ecoli_experiment.steps['ecoli-metabolism'].model.stoichiometry)

experiment_library = {
"2": run_ecoli_with_metabolism_redux,
"2a": run_ecoli_with_metabolism_redux_classic,
"3": test_ecoli_with_metabolism_redux,
"3a": test_ecoli_with_metabolism_classic,
"4": test_ecoli_with_metabolism_redux_div,
"4a": test_ecoli_with_metabolism_classic_div,
"5": run_ecoli_with_default_metabolism,
'2': run_ecoli_with_metabolism_redux,
'2a': run_ecoli_with_metabolism_redux_classic,
'2b': run_colony,
'3': test_ecoli_with_metabolism_redux,
'3a': test_ecoli_with_metabolism_classic,
'4': test_ecoli_with_metabolism_redux_div,
'5': run_ecoli_with_default_metabolism,
}


def save_sim_output(folder, query, sim, save_model=False):
agents = sim.query()["agents"].keys()
agents = sim.query()['agents'].keys()
for agent in agents:
query = []
query.extend(
[
("agents", agent, "listeners", "fba_results"),
("agents", agent, "listeners", "mass"),
("agents", agent, "listeners", "unique_molecule_counts"),
("agents", agent, "bulk"),
]
)
query.extend([('agents', agent, 'listeners', 'fba_results'),
('agents', agent, 'listeners', 'mass'),
('agents', agent, 'listeners', 'unique_molecule_counts'),
('agents', agent, 'bulk')])
output = sim.query(query)
pathlib.Path(folder).mkdir(parents=True, exist_ok=True)
np.save(folder + f"{agent}_output.npy", output)
np.save(folder + f'{agent}_output.npy', output)

if save_model:
f = open(folder + "agent_steps.pkl", "wb")
dill.dump(sim.ecoli_experiment.steps["agents"][agent], f)
f = open(folder + 'agent_steps.pkl', 'wb')
dill.dump(sim.ecoli_experiment.steps['agents'][agent], f)
f.close()


# run experiments with command line arguments: python ecoli/experiments/metabolism_redux_sim.py -n exp_id
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="user-friendly metabolism")
parser.add_argument("--name", "-n", default=[], nargs="+", help="test ids to run")
parser = argparse.ArgumentParser(description='user-friendly metabolism')
parser.add_argument('--name', '-n', default=[], nargs='+', help='test ids to run')
args = parser.parse_args()
run_all = not args.name

Expand Down
Empty file.
Loading

0 comments on commit 3e8b756

Please sign in to comment.