Skip to content

Commit

Permalink
updating for wc_lang.ChemicalStructure
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrkarr committed Apr 2, 2019
1 parent 6b1a9f3 commit 9c7dbdc
Show file tree
Hide file tree
Showing 23 changed files with 57 additions and 43 deletions.
1 change: 1 addition & 0 deletions .circleci/requirements.txt
Expand Up @@ -2,6 +2,7 @@
git+https://github.com/KarrLab/log.git#egg=log

# Karr Lab
git+https://github.com/KarrLab/bpforms.git#egg=bpforms
git+https://github.com/KarrLab/wc_onto.git#egg=wc_onto
git+https://github.com/KarrLab/wc_utils.git#egg=wc_utils
git+https://github.com/KarrLab/obj_model.git#egg=obj_model
Expand Down
Binary file modified examples/transcription_translation_hybrid_model/model.xlsx
Binary file not shown.
Binary file modified examples/translation_metabolism_hybrid_model/model.xlsx
Binary file not shown.
5 changes: 3 additions & 2 deletions tests/log/test_checkpoint.py
Expand Up @@ -167,8 +167,9 @@ def build_mock_model():
compartment_c = model.compartments.create(id='c', init_volume=init_volume)
compartment_e = model.compartments.create(id='e', init_volume=init_volume)

species_type_L = model.species_types.create(id='L', molecular_weight=10)
species_type_R = model.species_types.create(id='R', molecular_weight=10)
structure = wc_lang.ChemicalStructure(molecular_weight=10.)
species_type_L = model.species_types.create(id='L', structure=structure)
species_type_R = model.species_types.create(id='R', structure=structure)

species_L = wc_lang.Species(id='L[c]', species_type=species_type_L, compartment=compartment_c)
species_R = wc_lang.Species(id='R[c]', species_type=species_type_R, compartment=compartment_c)
Expand Down
Binary file modified tests/multialgorithm/fixtures/2_species_1_reaction.xlsx
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/multialgorithm/fixtures/MetabolismAndGeneExpression.xlsx
Binary file not shown.
Binary file modified tests/multialgorithm/fixtures/test_dry_model.xlsx
Binary file not shown.
Binary file not shown.
Binary file modified tests/multialgorithm/fixtures/test_model.xlsx
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/multialgorithm/fixtures/test_new_features_model.xlsx
Binary file not shown.
Binary file modified tests/multialgorithm/submodels/fixtures/test_submodel.xlsx
Binary file not shown.
Binary file not shown.
50 changes: 25 additions & 25 deletions tests/multialgorithm/test_dynamic_mass.py
Expand Up @@ -49,8 +49,8 @@ def gen_model(self):
model = model = wc_lang.Model(id='model', version='0.0.1', wc_lang_version='0.0.1')

# compartments and species
st_constant = model.species_types.create(id='st_constant', charge=0., molecular_weight=1.)
st_dynamic = model.species_types.create(id='st_dynamic', charge=0., molecular_weight=0.)
st_constant = model.species_types.create(id='st_constant', structure=wc_lang.ChemicalStructure(charge=0., molecular_weight=1.))
st_dynamic = model.species_types.create(id='st_dynamic', structure=wc_lang.ChemicalStructure(charge=0., molecular_weight=0.))
comp = model.compartments.create(id='comp', init_volume=wc_lang.InitVolume(std=0.))
spec_constant = model.species.create(species_type=st_constant, compartment=comp)
spec_dynamic = model.species.create(species_type=st_dynamic, compartment=comp)
Expand Down Expand Up @@ -178,12 +178,12 @@ def test_exponentially_increase_to_steady_state_count_with_constant_mass(self):

# set quantitative values
comp.init_volume.mean = 100e-15
st_constant.molecular_weight = 1.
st_dynamic.molecular_weight = 0.
st_constant.structure.molecular_weight = 1.
st_dynamic.structure.molecular_weight = 0.
spec_constant.distribution_init_concentration.mean = 1e6
spec_dynamic.distribution_init_concentration.mean = 10.
init_mass = (spec_constant.distribution_init_concentration.mean * st_constant.molecular_weight +
spec_dynamic.distribution_init_concentration.mean * st_dynamic.molecular_weight
init_mass = (spec_constant.distribution_init_concentration.mean * st_constant.structure.molecular_weight +
spec_dynamic.distribution_init_concentration.mean * st_dynamic.structure.molecular_weight
) / scipy.constants.Avogadro
init_density = density.value = init_mass / comp.init_volume.mean
model.parameters.get_one(id='k_syn_dynamic').value = 1.
Expand All @@ -195,7 +195,7 @@ def test_exponentially_increase_to_steady_state_count_with_constant_mass(self):
# verify results
numpy.testing.assert_equal(pop_constant, numpy.full((201,), spec_constant.distribution_init_concentration.mean))
numpy.testing.assert_equal(comp_mass, numpy.full((201,), spec_constant.distribution_init_concentration.mean
* st_constant.molecular_weight
* st_constant.structure.molecular_weight
/ scipy.constants.Avogadro))
self.assertEqual(density.value, init_density)

Expand All @@ -215,12 +215,12 @@ def test_exponentially_increase_to_steady_state_count_and_mass(self):

# set quantitative values
comp.init_volume.mean = 100e-15
st_constant.molecular_weight = 0.
st_dynamic.molecular_weight = 1.
st_constant.structure.molecular_weight = 0.
st_dynamic.structure.molecular_weight = 1.
spec_constant.distribution_init_concentration.mean = 0.
spec_dynamic.distribution_init_concentration.mean = 10.
init_mass = (spec_constant.distribution_init_concentration.mean * st_constant.molecular_weight +
spec_dynamic.distribution_init_concentration.mean * st_dynamic.molecular_weight
init_mass = (spec_constant.distribution_init_concentration.mean * st_constant.structure.molecular_weight +
spec_dynamic.distribution_init_concentration.mean * st_dynamic.structure.molecular_weight
) / scipy.constants.Avogadro
init_density = density.value = init_mass / comp.init_volume.mean
model.parameters.get_one(id='k_syn_dynamic').value = 5.
Expand All @@ -239,8 +239,8 @@ def test_exponentially_increase_to_steady_state_count_and_mass(self):
self.assertGreater(numpy.mean(pop_dynamic[101:]), spec_dynamic_ss - 3 * std_spec_dynamic_ss)
self.assertLess(numpy.mean(pop_dynamic[101:]), spec_dynamic_ss + 3 * std_spec_dynamic_ss)

mass_ss = spec_dynamic_ss * st_dynamic.molecular_weight / scipy.constants.Avogadro
std_mass_ss = std_spec_dynamic_ss * st_dynamic.molecular_weight / scipy.constants.Avogadro
mass_ss = spec_dynamic_ss * st_dynamic.structure.molecular_weight / scipy.constants.Avogadro
std_mass_ss = std_spec_dynamic_ss * st_dynamic.structure.molecular_weight / scipy.constants.Avogadro
self.assertGreater(numpy.mean(comp_mass[101:]), mass_ss - 3 * std_mass_ss)
self.assertLess(numpy.mean(comp_mass[101:]), mass_ss + 3 * std_mass_ss)

Expand All @@ -255,12 +255,12 @@ def test_exponentially_descend_to_steady_state_count_and_mass(self):

# set quantitative values
comp.init_volume.mean = 100e-15
st_constant.molecular_weight = 0.
st_dynamic.molecular_weight = 1.
st_constant.structure.molecular_weight = 0.
st_dynamic.structure.molecular_weight = 1.
spec_constant.distribution_init_concentration.mean = 0.
spec_dynamic.distribution_init_concentration.mean = 100.
init_mass = (spec_constant.distribution_init_concentration.mean * st_constant.molecular_weight +
spec_dynamic.distribution_init_concentration.mean * st_dynamic.molecular_weight
init_mass = (spec_constant.distribution_init_concentration.mean * st_constant.structure.molecular_weight +
spec_dynamic.distribution_init_concentration.mean * st_dynamic.structure.molecular_weight
) / scipy.constants.Avogadro
init_density = density.value = init_mass / comp.init_volume.mean
model.parameters.get_one(id='k_syn_dynamic').value = 1.
Expand All @@ -279,8 +279,8 @@ def test_exponentially_descend_to_steady_state_count_and_mass(self):
self.assertGreater(numpy.mean(pop_dynamic[101:]), spec_dynamic_ss - 3 * std_spec_dynamic_ss)
self.assertLess(numpy.mean(pop_dynamic[101:]), spec_dynamic_ss + 3 * std_spec_dynamic_ss)

mass_ss = spec_dynamic_ss * st_dynamic.molecular_weight / scipy.constants.Avogadro
std_mass_ss = std_spec_dynamic_ss * st_dynamic.molecular_weight / scipy.constants.Avogadro
mass_ss = spec_dynamic_ss * st_dynamic.structure.molecular_weight / scipy.constants.Avogadro
std_mass_ss = std_spec_dynamic_ss * st_dynamic.structure.molecular_weight / scipy.constants.Avogadro
self.assertGreater(numpy.mean(comp_mass[101:]), mass_ss - 3 * std_mass_ss)
self.assertLess(numpy.mean(comp_mass[101:]), mass_ss + 3 * std_mass_ss)

Expand Down Expand Up @@ -314,21 +314,21 @@ def test_exponentially_increase_to_steady_state_mass_and_descend_to_concentratio

# set quantitative values
comp.init_volume.mean = 100e-15
st_constant.molecular_weight = 1.
st_dynamic.molecular_weight = 0.
st_constant.structure.molecular_weight = 1.
st_dynamic.structure.molecular_weight = 0.
spec_constant.distribution_init_concentration.mean = 10.
spec_dynamic.distribution_init_concentration.mean = 100.
init_mass = (spec_constant.distribution_init_concentration.mean * st_constant.molecular_weight +
spec_dynamic.distribution_init_concentration.mean * st_dynamic.molecular_weight
init_mass = (spec_constant.distribution_init_concentration.mean * st_constant.structure.molecular_weight +
spec_dynamic.distribution_init_concentration.mean * st_dynamic.structure.molecular_weight
) / scipy.constants.Avogadro
init_density = density.value = init_mass / comp.init_volume.mean
model.parameters.get_one(id='k_syn_constant').value = 1.
model.parameters.get_one(id='k_deg_constant').value = 2e-2

spec_constant_ss = model.parameters.get_one(id='k_syn_constant').value / model.parameters.get_one(id='k_deg_constant').value
std_spec_constant_ss = numpy.sqrt(spec_constant_ss)
mass_ss = spec_constant_ss * st_constant.molecular_weight / scipy.constants.Avogadro
std_mass_ss = spec_constant_ss * st_constant.molecular_weight / scipy.constants.Avogadro
mass_ss = spec_constant_ss * st_constant.structure.molecular_weight / scipy.constants.Avogadro
std_mass_ss = spec_constant_ss * st_constant.structure.molecular_weight / scipy.constants.Avogadro
vol_ss = mass_ss / density.value
std_vol_ss = std_mass_ss / density.value

Expand Down
6 changes: 4 additions & 2 deletions tests/multialgorithm/test_model_utilities.py
Expand Up @@ -78,15 +78,17 @@ def test_concentration_to_molecules(self):

compartment_c = model.compartments.create(id='c', init_volume=wc_lang.InitVolume(mean=1.))

structure = wc_lang.ChemicalStructure(molecular_weight=10.)

species_types = {}
cus_species_types = {}
for cu in wc_lang.DistributionInitConcentration.units.choices:
id = str(cu).replace(' ', '_')
species_types[id] = model.species_types.create(id=id, molecular_weight=10)
species_types[id] = model.species_types.create(id=id, structure=structure)
cus_species_types[id] = cu

for other in ['no_units', 'no_concentration', 'no_such_concentration_unit']:
species_types[other] = model.species_types.create(id=other, molecular_weight=10)
species_types[other] = model.species_types.create(id=other, structure=structure)

species = {}
for key, species_type in species_types.items():
Expand Down
3 changes: 2 additions & 1 deletion tests/multialgorithm/test_multialgorithm_simulation.py
Expand Up @@ -373,7 +373,8 @@ def setUp(self):

@unittest.skip('Disable temporarily, while A finishes "incomplete-updates" branch')
def test_nan_propensities(self):
self.model.species_types.get_one(id='spec_type_0').molecular_weight = float('NaN')
st_0 = self.model.species_types.get_one(id='spec_type_0')
st_0.structure.molecular_weight = float('NaN')
multialgorithm_simulation = MultialgorithmSimulation(self.model, {})
simulation_engine, _ = multialgorithm_simulation.build_simulation()
with self.assertRaisesRegex(AssertionError, "total propensities is 'NaN'"):
Expand Down
2 changes: 1 addition & 1 deletion tests/multialgorithm/test_species_populations.py
Expand Up @@ -778,7 +778,7 @@ def setUp(self):
self.simulator = SimulationEngine()
self.species_ids = 's1 s2 s3'.split()
self.initial_population = dict(zip(self.species_ids, range(3)))
self.molecular_weight = dict(zip(self.species_ids, [10]*3))
self.molecular_weight = dict(zip(self.species_ids, [10.]*3))
self.test_species_pop_sim_obj = SpeciesPopSimObject('test_name', self.initial_population,
self.molecular_weight,
random_state=RandomStateManager.instance())
Expand Down
19 changes: 10 additions & 9 deletions wc_sim/multialgorithm/make_models.py
Expand Up @@ -14,7 +14,7 @@
Observable, Function, FunctionExpression,
Reaction, RateLawDirection, RateLawExpression, Parameter,
DistributionInitConcentration,
Validator, InitVolume)
Validator, InitVolume, ChemicalStructure)
from wc_lang.transform import PrepForWcSimTransform
from wc_utils.util.enumerate import CaseInsensitiveEnum
from wc_onto import onto
Expand Down Expand Up @@ -67,7 +67,7 @@ def convert_pop_conc(species_copy_number, vol):

@classmethod
def add_test_submodel(cls, model, model_type, submodel_num, init_vol, species_types,
default_species_copy_number, default_species_std,
default_species_copy_number, default_species_std,
species_copy_numbers, species_stds, expressions):
""" Create a test submodel
Expand Down Expand Up @@ -121,8 +121,8 @@ def add_test_submodel(cls, model, model_type, submodel_num, init_vol, species_ty
init_volume=init_volume)
objects[Compartment][comp.id] = comp

density = comp.init_density = model.parameters.create(id='density_compt_{}'.format(submodel_num),
value=1100, units=unit_registry.parse_units('g l^-1'))
density = comp.init_density = model.parameters.create(id='density_compt_{}'.format(submodel_num),
value=1100, units=unit_registry.parse_units('g l^-1'))
objects[Parameter][density.id] = density

volume = model.functions.create(id='volume_compt_{}'.format(submodel_num), units=unit_registry.parse_units('l'))
Expand Down Expand Up @@ -245,7 +245,7 @@ def add_test_submodel(cls, model, model_type, submodel_num, init_vol, species_ty
@classmethod
def make_test_model(cls, model_type,
init_vols=None,
molecular_weight=10,
molecular_weight=10.,
charge=0,
num_submodels=1,
default_species_copy_number=1000000,
Expand Down Expand Up @@ -294,14 +294,15 @@ def make_test_model(cls, model_type,
model = Model(id='test_model', name='{} with {} submodels'.format(model_type, num_submodels),
version='0.0.0', wc_lang_version='0.0.1')

structure = ChemicalStructure(molecular_weight=molecular_weight, charge=charge)

# SpeciesTypes
species_types = []
for i in range(num_species):
spec_type = model.species_types.create(
id='spec_type_{}'.format(i),
type=onto['WC:protein'], # protein
molecular_weight=molecular_weight,
charge=charge)
type=onto['WC:protein'], # protein
structure=structure)
species_types.append(spec_type)

# make submodels
Expand All @@ -311,7 +312,7 @@ def make_test_model(cls, model_type,
cls.add_test_submodel(model, model_type, submodel_num, init_vols[i],
species_types, default_species_copy_number=default_species_copy_number,
default_species_std=default_species_std,
species_copy_numbers=species_copy_numbers, species_stds=species_stds,
species_copy_numbers=species_copy_numbers, species_stds=species_stds,
expressions=expressions)

if transform_prep_and_check:
Expand Down
14 changes: 11 additions & 3 deletions wc_sim/multialgorithm/multialgorithm_simulation.py
Expand Up @@ -185,8 +185,12 @@ def molecular_weights_for_species(self, species):
"""
species_weights = {}
for species_id in species:
(species_type_id, _) = ModelUtilities.parse_species_id(species_id)
species_weights[species_id] = self.model.species_types.get_one(id=species_type_id).molecular_weight
species_type_id, _ = ModelUtilities.parse_species_id(species_id)
species_type = self.model.species_types.get_one(id=species_type_id)
if species_type.structure:
species_weights[species_id] = species_type.structure.molecular_weight
else:
species_weights[species_id] = float('nan')
return species_weights

def create_shared_species_pop_objs(self):
Expand Down Expand Up @@ -310,7 +314,11 @@ def make_local_species_pop(model, random_state, retain_history=True):
for specie in model.get_species():
(species_type_id, _) = ModelUtilities.parse_species_id(specie.id)
# TODO(Arthur): make get_one more robust, or do linear search
molecular_weights[specie.id] = model.species_types.get_one(id=species_type_id).molecular_weight
species_type = model.species_types.get_one(id=species_type_id)
if species_type.structure:
molecular_weights[specie.id] = species_type.structure.molecular_weight
else:
molecular_weights[specie.id] = float('nan')

# Species used by continuous time submodels (like DFBA and ODE) need initial fluxes
# which indicate that the species is modeled by a continuous time submodel.
Expand Down

0 comments on commit 9c7dbdc

Please sign in to comment.