def test_write_parameter(self): model = Model(id='model', version='0.0.1', wc_lang_version='0.0.1') submodel = model.submodels.create(id='submodel') species_type = model.species_types.create(id='st', structure=ChemicalStructure( molecular_weight=1., charge=0)) compartment = model.compartments.create(id='c') species = model.species.create(species_type=species_type, compartment=compartment) species.id = species.gen_id() reaction = submodel.reactions.create(id='reaction', model=model) reaction.participants.create(species=species, coefficient=1.) rate_law = reaction.rate_laws.create( model=model, direction=RateLawDirection.forward) rate_law.id = rate_law.gen_id() rate_law_eq = rate_law.expression = RateLawExpression( expression='parameter') parameter = rate_law_eq.parameters.create( id='parameter', value=1., units=unit_registry.parse_units('dimensionless'), model=model) filename = os.path.join(self.tempdir, 'model.xlsx') Writer().run(filename, model, data_repo_metadata=False) parameter.model = Model(id='model2', version='0.0.1', wc_lang_version='0.0.1') with self.assertRaisesRegex(ValueError, 'must be set to the instance of `Model`'): Writer().run(filename, model, data_repo_metadata=False)
def test_merge_models(self): in_paths = [ path.join(self.tempdir, 'in-0.xlsx'), path.join(self.tempdir, 'in-1.xlsx'), ] out_path = path.join(self.tempdir, 'out.xlsx') # write models model_0 = Model(id='model', name='test model', version='0.0.1a', wc_lang_version='0.0.1') model_0.species_types.create(id='a') model_1 = Model(id='model', name='test model', version='0.0.1a', wc_lang_version='0.0.1') model_1.species_types.create(id='b') Writer().run(in_paths[0], model_0, data_repo_metadata=False) Writer().run(in_paths[1], model_1, data_repo_metadata=False) # merge models with __main__.App(argv=[ 'merge-models', '-p', in_paths[0], '-s', in_paths[1], '-o', out_path ]) as app: app.run() # read merged model merged_model = Reader().run(out_path)[Model][0] # verify merged model self.assertEqual(len(merged_model.species_types), 2) self.assertNotEqual(merged_model.species_types.get_one(id='a'), None) self.assertNotEqual(merged_model.species_types.get_one(id='b'), None)
def test_difference(self): now = datetime.datetime.now().replace(microsecond=0) model1 = Model(id='model', name='test model', version='0.0.1a', wc_lang_version='0.0.0', created=now, updated=now) filename1 = path.join(self.tempdir, 'model1.xlsx') Writer().run(filename1, model1, data_repo_metadata=False) model2 = Model(id='model', name='test model', version='0.0.1a', wc_lang_version='0.0.0', created=now, updated=now) filename2 = path.join(self.tempdir, 'model2.xlsx') Writer().run(filename2, model2, data_repo_metadata=False) model3 = Model(id='model', name='test model', version='0.0.1a', wc_lang_version='0.0.1', created=now, updated=now) filename3 = path.join(self.tempdir, 'model3.xlsx') Writer().run(filename3, model3, data_repo_metadata=False) with CaptureOutput(relay=False) as capturer: with __main__.App( argv=['difference', filename1, filename2]) as app: app.run() self.assertEqual(capturer.get_text(), 'Models are identical') with CaptureOutput(relay=False) as capturer: with __main__.App(argv=[ 'difference', filename1, filename2, '--compare-files' ]) as app: app.run() self.assertEqual(capturer.get_text(), 'Models are identical') with CaptureOutput(relay=False) as capturer: with __main__.App( argv=['difference', filename1, filename3]) as app: app.run() diff = ( 'Objects (Model: "model", Model: "model") have different attribute values:\n ' '`wc_lang_version` are not equal:\n 0.0.0 != 0.0.1') self.assertEqual(capturer.get_text(), diff) with CaptureOutput(relay=False) as capturer: with __main__.App(argv=[ 'difference', filename1, filename3, '--compare-files' ]) as app: app.run() diff = 'Sheet !!Model:\n Row 7:\n Cell B: 0.0.0 != 0.0.1' self.assertEqual(capturer.get_text(), diff)
def test(self): model1 = Model(id='model1', name='test model', version='0.0.1a', wc_lang_version='0.0.1') model2 = Model(id='model2', name='test model', version='0.0.1a', wc_lang_version='0.0.1') filename = os.path.join(self.tempdir, 'model.xlsx') obj_tables.io.WorkbookWriter().run(filename, [model1, model2], models=Writer.MODELS, include_all_attributes=False) with self.assertRaisesRegex(ValueError, ' should define one model$'): Reader().run(filename)
def test(self): test_model = Model(id='model', version='0.0.1', wc_lang_version='0.0.1') submodel = test_model.submodels.create(id='submodel') filename_yaml = os.path.join(self.tempdir, 'model.yaml') Writer().run(filename_yaml, test_model) model = Reader().run(filename_yaml)[Model][0] self.assertEqual(model.validate(), None) self.assertTrue(model.is_equal(test_model)) self.assertEqual(test_model.difference(model), '')
def test(self): model = Model() c_1 = model.compartments.create(id='c_1') c_2 = model.compartments.create(id='c_2') st_1 = model.species_types.create(id='st_1') st_2 = model.species_types.create(id='st_2') st_1_c_1 = model.species.create(species_type=st_1, compartment=c_1) st_1_c_2 = model.species.create(species_type=st_1, compartment=c_2) st_2_c_1 = model.species.create(species_type=st_2, compartment=c_1) st_2_c_2 = model.species.create(species_type=st_2, compartment=c_2) st_1_c_1.id = st_1_c_1.gen_id() st_1_c_2.id = st_1_c_2.gen_id() st_2_c_1.id = st_2_c_1.gen_id() st_2_c_2.id = st_2_c_2.gen_id() model.distribution_init_concentrations.create(species=st_1_c_1, mean=1., std=2.) model.distribution_init_concentrations.create(species=st_2_c_2, mean=1., std=2.) transform = CreateImplicitDistributionZeroInitConcentrationsTransform() transform.run(model) self.assertEqual(len(model.distribution_init_concentrations), 4) self.assertEqual(st_1_c_1.distribution_init_concentration.mean, 1.) self.assertEqual(st_1_c_2.distribution_init_concentration.mean, 0.) self.assertEqual(st_2_c_1.distribution_init_concentration.mean, 0.) self.assertEqual(st_2_c_2.distribution_init_concentration.mean, 1.) self.assertEqual(st_1_c_1.distribution_init_concentration.std, 2.) self.assertEqual(st_1_c_2.distribution_init_concentration.std, 0.) self.assertEqual(st_2_c_1.distribution_init_concentration.std, 0.) self.assertEqual(st_2_c_2.distribution_init_concentration.std, 2.)
def test_normalize(self): filename_xls_1 = path.join(self.tempdir, 'model-1.xlsx') filename_xls_2 = path.join(self.tempdir, 'model-2.xlsx') model = Model(id='model', name='test model', version='0.0.1a', wc_lang_version='0.0.0') Writer().run(filename_xls_1, model, data_repo_metadata=False) # with same destination with __main__.App(argv=['normalize', filename_xls_1]) as app: app.run() model2 = Reader().run(filename_xls_1)[Model][0] self.assertTrue(model2.is_equal(model)) # with different destination with __main__.App( argv=['normalize', filename_xls_1, '--dest', filename_xls_2 ]) as app: app.run() model2 = Reader().run(filename_xls_2)[Model][0] self.assertTrue(model2.is_equal(model))
def test_species_concentration_units(self): model = Model() st_1 = model.species_types.create(id='st_1') st_2 = model.species_types.create(id='st_2') c_1 = model.compartments.create(id='c_1') c_2 = model.compartments.create(id='c_2') st_1_c_1 = model.species.create(species_type=st_1, compartment=c_1) st_1_c_2 = model.species.create(species_type=st_1, compartment=c_2) st_2_c_1 = model.species.create(species_type=st_2, compartment=c_1) st_2_c_2 = model.species.create(species_type=st_2, compartment=c_2) st_1_c_1.id = st_1_c_1.gen_id() st_1_c_2.id = st_1_c_2.gen_id() st_2_c_1.id = st_2_c_1.gen_id() st_2_c_2.id = st_2_c_2.gen_id() st_1_c_1.distribution_init_concentration = DistributionInitConcentration(units=unit_registry.parse_units('gram')) st_1_c_2.distribution_init_concentration = DistributionInitConcentration(units=unit_registry.parse_units('hour')) st_2_c_1.distribution_init_concentration = DistributionInitConcentration(units=unit_registry.parse_units('liter')) st_2_c_2.distribution_init_concentration = DistributionInitConcentration(units=unit_registry.parse_units('second')) st_1_c_1.distribution_init_concentration.id = st_1_c_1.distribution_init_concentration.gen_id() st_1_c_2.distribution_init_concentration.id = st_1_c_2.distribution_init_concentration.gen_id() st_2_c_1.distribution_init_concentration.id = st_2_c_1.distribution_init_concentration.gen_id() st_2_c_2.distribution_init_concentration.id = st_2_c_2.distribution_init_concentration.gen_id() ChangeValueTransform((('species', {'id': 'st_1[c_1]'}), 'distribution_init_concentration', 'units'), unit_registry.parse_units('meter')).run(model) self.assertEqual(st_1_c_1.distribution_init_concentration.units, unit_registry.parse_units('meter')) self.assertEqual(st_1_c_2.distribution_init_concentration.units, unit_registry.parse_units('hour')) self.assertEqual(st_2_c_1.distribution_init_concentration.units, unit_registry.parse_units('liter')) self.assertEqual(st_2_c_2.distribution_init_concentration.units, unit_registry.parse_units('second'))
def test_parameter_value(self): model = Model() p_1 = model.parameters.create(id='p_1', value=1) p_2 = model.parameters.create(id='p_2', value=3) ChangeValueTransform((('parameters', {'id': 'p_1'}), 'value'), 2).run(model) self.assertEqual(p_1.value, 2) self.assertEqual(p_2.value, 3)
def test_parameter_units(self): model = Model() p_1 = model.parameters.create(id='p_1', units=unit_registry.parse_units('M')) p_2 = model.parameters.create(id='p_2', units=unit_registry.parse_units('l')) ChangeValueTransform((('parameters', {'id': 'p_1'}), 'units'), unit_registry.parse_units('m^2')).run(model) self.assertEqual(p_1.units, unit_registry.parse_units('m^2')) self.assertEqual(p_2.units, unit_registry.parse_units('l'))
def test_function_expression(self): model = Model() f = model.functions.create(id='f', expression=FunctionExpression(expression='x')) g = model.functions.create(id='g', expression=FunctionExpression(expression='y')) ChangeValueTransform((('functions', {'id': 'g'}), 'expression', 'expression'), 'z').run(model) self.assertEqual(f.expression.expression, 'x') self.assertEqual(g.expression.expression, 'z')
def test_compartment_density(self): model = Model() c = model.compartments.create(id='c', init_volume=InitVolume(mean=1.)) e = model.compartments.create(id='e', init_volume=InitVolume(mean=1.)) ChangeValueTransform((('compartments', {'id': 'c'}), ('init_volume',), ('mean',)), 2.).run(model) self.assertEqual(c.init_volume.mean, 2.) self.assertEqual(e.init_volume.mean, 1.)
def setUp(self): self.model = model = Model(id='test', version='0.1') c = model.compartments.create(id='comp') c.init_density = model.parameters.create(id='density_compartment_1', value=1100, units=unit_registry.parse_units('g l^-1')) t0 = model.species_types.create(id='s0', type=onto['WC:metabolite']) t1 = model.species_types.create(id='s1', type=onto['WC:metabolite']) t2 = model.species_types.create(id='s2', type=onto['WC:metabolite']) s0 = model.species.create(id='s0[comp]', species_type=t0, compartment=c) s1 = model.species.create(id='s1[comp]', species_type=t1, compartment=c) s2 = model.species.create(id='s2[comp]', species_type=t2, compartment=c) self.submodel = submodel = model.submodels.create(id='submodel', framework=onto['WC:stochastic_simulation_algorithm']) self.r0 = r0 = model.reactions.create(id='r0', reversible=True, submodel=submodel) r0.participants.create(species=s0, coefficient=-2) r0.participants.create(species=s1, coefficient=3) r0_f = r0.rate_laws.create(id='r0-forward', direction=RateLawDirection.forward, model=model) a = model.parameters.create(id='a', value=1., units=unit_registry.parse_units('s^-1')) r0_f.expression, error = RateLawExpression.deserialize('a', {Parameter: {'a': a}}) assert error is None, str(error) r0_b = r0.rate_laws.create(id='r0-backward', direction=RateLawDirection.backward, model=model) b = model.parameters.create(id='b', value=1., units=unit_registry.parse_units('s^-1')) r0_b.expression, error = RateLawExpression.deserialize('b', {Parameter: {'b': b}}) assert error is None, str(error) r0.references.create(id='ref_0', model=model) r0.identifiers.create(namespace='x', id='y') self.r1 = r1 = model.reactions.create(id='r1', reversible=False, submodel=submodel) r1.participants.create(species=s1, coefficient=-3) r1.participants.create(species=s2, coefficient=4) r1_f = r1.rate_laws.create(id='r1-forward', direction=RateLawDirection.forward, model=model) c = model.parameters.create(id='c', value=1., units=unit_registry.parse_units('s^-1')) r1_f.expression, error = RateLawExpression.deserialize('c', {Parameter: {'c': c}}) assert error is None, str(error) r1.references.create(id='ref_1', model=model) r1.identifiers.create(namespace='xx', id='yy') self.tempdir = tempfile.mkdtemp() # check model's integrity by writing and reading with validate=True filename = os.path.join(self.tempdir, 'model_for_tranformation.xlsx') Writer().run(filename, model, data_repo_metadata=False) # turn off validate_element_charge_balance validation so that simple species and reactions validate with EnvironUtils.temp_config_env([(['wc_lang', 'validation', 'validate_element_charge_balance'], 'False')]): model_read = Reader().run(filename, validate=True)[Model][0] self.assertTrue(model_read.is_equal(model))
def test_write_other(self): model = Model(id='model', version='0.0.1', wc_lang_version='0.0.1') species_type = model.species_types.create(id='species_type') compartment = model.compartments.create(id='compartment') species = Species( model=model, species_type=species_type, compartment=compartment) species.id = species.gen_id() s_id = species.serialize() obs_expr, _ = ObservableExpression.deserialize(s_id, {Species: {s_id: species}}) observable = model.observables.create(id='observable', expression=obs_expr) filename = os.path.join(self.tempdir, 'model.xlsx') Writer().run(filename, model, data_repo_metadata=False) model2 = Model(id='model2', version='0.0.1', wc_lang_version='0.0.1') observable.model = model2 with self.assertRaisesRegex(ValueError, 'must be set to the instance of `Model`'): Writer().run(filename, model, data_repo_metadata=False)
def test_reaction_expression(self): model = Model() s_1 = model.submodels.create(id='s_1') r_1_1 = model.reactions.create(submodel=s_1, id='r_1_1') rl_f = model.rate_laws.create(reaction=r_1_1, direction=RateLawDirection.forward, expression=RateLawExpression(expression='x')) rl_b = model.rate_laws.create(reaction=r_1_1, direction=RateLawDirection.backward, expression=RateLawExpression(expression='y')) ChangeValueTransform((('reactions', {'id': 'r_1_1'}), ('rate_laws', {'direction': RateLawDirection.forward}), 'expression', 'expression'), 'z').run(model) self.assertEqual(rl_f.expression.expression, 'z') self.assertEqual(rl_b.expression.expression, 'y')
def test_validate(self): model = Model(id='model', name='test model', version='0.0.1a', wc_lang_version='0.0.1') self.assertEqual(Validator().run(model, get_related=True), None) filename = path.join(self.tempdir, 'model.xlsx') Writer().run(filename, model, data_repo_metadata=False) with CaptureOutput(relay=False) as capturer: with __main__.App(argv=['validate', filename]) as app: app.run() self.assertEqual(capturer.get_text(), 'Model is valid')
def test_transform_exception(self): source = path.join(self.tempdir, 'source.xlsx') model = Model(id='model', name='test model', version='0.0.1a', wc_lang_version='0.0.0') Writer().run(source, model, data_repo_metadata=False) dest = path.join(self.tempdir, 'dest.xlsx') with self.assertRaisesRegex(SystemExit, 'Please select at least one transform'): with __main__.App(argv=['transform', source, dest]) as app: app.run()
def test_reaction_reversible(self): model = Model() s_1 = model.submodels.create(id='s_1') s_2 = model.submodels.create(id='s_2') r_1_1 = model.reactions.create(submodel=s_1, id='r_1_1', reversible=False) r_1_2 = model.reactions.create(submodel=s_1, id='r_1_2', reversible=False) r_2_1 = model.reactions.create(submodel=s_2, id='r_2_1', reversible=True) r_2_2 = model.reactions.create(submodel=s_2, id='r_2_2', reversible=True) ChangeValueTransform((('reactions', {'id': 'r_2_1'}), 'reversible'), False).run(model) self.assertEqual(r_1_1.reversible, False) self.assertEqual(r_1_2.reversible, False) self.assertEqual(r_2_1.reversible, False) self.assertEqual(r_2_2.reversible, True)
def test_reaction_flux_bounds_min(self): model = Model() s_1 = model.submodels.create(id='s_1') s_2 = model.submodels.create(id='s_2') r_1_1 = model.reactions.create(submodel=s_1, id='r_1_1', flux_bounds=FluxBounds(min=1, units=unit_registry.parse_units('M s^-1'))) r_1_2 = model.reactions.create(submodel=s_1, id='r_1_2', flux_bounds=FluxBounds(min=2, units=unit_registry.parse_units('M s^-1'))) r_2_1 = model.reactions.create(submodel=s_2, id='r_2_1', flux_bounds=FluxBounds(min=3, units=unit_registry.parse_units('M s^-1'))) r_2_2 = model.reactions.create(submodel=s_2, id='r_2_2', flux_bounds=FluxBounds(min=4, units=unit_registry.parse_units('M s^-1'))) ChangeValueTransform((('reactions', {'id': 'r_1_2'}), ('flux_bounds',), ('min',)), 0).run(model) self.assertEqual(r_1_1.flux_bounds.min, 1) self.assertEqual(r_1_2.flux_bounds.min, 0) self.assertEqual(r_2_1.flux_bounds.min, 3) self.assertEqual(r_2_2.flux_bounds.min, 4)
def test_reaction_flux_bounds_max(self): model = Model() s_1 = model.submodels.create(id='s_1') s_2 = model.submodels.create(id='s_2') r_1_1 = model.reactions.create(submodel=s_1, id='r_1_1', flux_bounds=FluxBounds(max=1)) r_1_2 = model.reactions.create(submodel=s_1, id='r_1_2', flux_bounds=FluxBounds(max=2)) r_2_1 = model.reactions.create(submodel=s_2, id='r_2_1', flux_bounds=FluxBounds(max=3)) r_2_2 = model.reactions.create(submodel=s_2, id='r_2_2', flux_bounds=FluxBounds(max=4)) ChangeValueTransform((('reactions', {'id': 'r_2_2'}), ('flux_bounds',), ('max',)), 0).run(model) self.assertEqual(r_1_1.flux_bounds.max, 1) self.assertEqual(r_1_2.flux_bounds.max, 2) self.assertEqual(r_2_1.flux_bounds.max, 3) self.assertEqual(r_2_2.flux_bounds.max, 0)
def test_convert(self): filename_xls = path.join(self.tempdir, 'model.xlsx') filename_csv = path.join(self.tempdir, 'model-*.csv') model = Model(id='model', name='test model', version='0.0.1a', wc_lang_version='0.0.0') Writer().run(filename_xls, model, data_repo_metadata=False) with __main__.App(argv=['convert', filename_xls, filename_csv]) as app: app.run() self.assertTrue(path.isfile(path.join(self.tempdir, 'model-Model.csv')))
def test_transform(self): source = path.join(self.tempdir, 'source.xlsx') model = Model(id='model', name='test model', version='0.0.1a', wc_lang_version='0.0.0') Writer().run(source, model, data_repo_metadata=False) dest = path.join(self.tempdir, 'dest.xlsx') with __main__.App(argv=[ 'transform', source, dest, '--transform', 'MergeAlgorithmicallyLikeSubmodels' ]) as app: app.run() self.assertTrue(path.isfile(dest))
def test_update_version_metadata(self): filename = path.join(self.tempdir, 'model.xlsx') model = Model(id='model', name='test model', version='0.0.1a', wc_lang_version='0.0.0') self.assertNotEqual(model.wc_lang_version, wc_lang.__version__) Writer().run(filename, model, data_repo_metadata=False) with __main__.App(argv=[ 'update-version-metadata', filename, '--ignore-repo-metadata' ]) as app: app.run() model = Reader().run(filename)[Model][0] self.assertEqual(model.wc_lang_version, wc_lang.__version__)
def test_reaction_k_cat(self): model = Model() s_1 = model.submodels.create(id='s_1') r_1_1 = model.reactions.create(submodel=s_1, id='r_1_1') k_cat_f = model.parameters.create(id='k_cat_f', value=1) k_cat_b = model.parameters.create(id='k_cat_b', value=2) rl_f = model.rate_laws.create(reaction=r_1_1, direction=RateLawDirection.forward, expression=RateLawExpression( parameters=[k_cat_f])) rl_b = model.rate_laws.create(reaction=r_1_1, direction=RateLawDirection.backward, expression=RateLawExpression( parameters=[k_cat_b])) rl_f.id = rl_f.gen_id() rl_b.id = rl_b.gen_id() ChangeValueTransform((('parameters', {'id': 'k_cat_b'}), 'value'), 0).run(model) self.assertEqual(k_cat_f.value, 1) self.assertEqual(k_cat_b.value, 0)
def make_objects(self): model = Model() objects = { Observable: {}, Parameter: {}, Function: {}, StopCondition: {} } self.param_value = 4 objects[Parameter]['param'] = param = \ model.parameters.create(id='param', value=self.param_value, units=unit_registry.parse_units('dimensionless')) self.fun_expr = expr = 'param - 2 + max(param, 10)' fun1 = Expression.make_obj(model, Function, 'fun1', expr, objects) fun2 = Expression.make_obj(model, Function, 'fun2', 'log(2) - param', objects) return model, param, fun1, fun2
def test_validate_exception(self): model = Model(id='model', name='test model', version='0.0.1a', wc_lang_version='0.0.1') model.parameters.append( Parameter(id='param_1', value=1., units=unit_registry.parse_units('dimensionless'))) model.parameters.append( Parameter(id='param_1', value=1., units=unit_registry.parse_units('dimensionless'))) self.assertNotEqual(Validator().run(model, get_related=True), None) filename = path.join(self.tempdir, 'model.xlsx') Writer().run(filename, model, data_repo_metadata=False) with self.assertRaisesRegex(SystemExit, '^Model is invalid: '): with __main__.App(argv=['validate', filename]) as app: app.run()
def test_species_concentration_value(self): model = Model() st_1 = model.species_types.create(id='st_1') st_2 = model.species_types.create(id='st_2') c_1 = model.compartments.create(id='c_1') c_2 = model.compartments.create(id='c_2') st_1_c_1 = model.species.create(species_type=st_1, compartment=c_1) st_1_c_2 = model.species.create(species_type=st_1, compartment=c_2) st_2_c_1 = model.species.create(species_type=st_2, compartment=c_1) st_2_c_2 = model.species.create(species_type=st_2, compartment=c_2) st_1_c_1.id = st_1_c_1.gen_id() st_1_c_2.id = st_1_c_2.gen_id() st_2_c_1.id = st_2_c_1.gen_id() st_2_c_2.id = st_2_c_2.gen_id() st_1_c_1.distribution_init_concentration = DistributionInitConcentration(mean=1, std=2) st_1_c_2.distribution_init_concentration = DistributionInitConcentration(mean=2, std=2) st_2_c_1.distribution_init_concentration = DistributionInitConcentration(mean=3, std=2) st_2_c_2.distribution_init_concentration = DistributionInitConcentration(mean=4, std=2) st_1_c_1.distribution_init_concentration.id = st_1_c_1.distribution_init_concentration.gen_id() st_1_c_2.distribution_init_concentration.id = st_1_c_2.distribution_init_concentration.gen_id() st_2_c_1.distribution_init_concentration.id = st_2_c_1.distribution_init_concentration.gen_id() st_2_c_2.distribution_init_concentration.id = st_2_c_2.distribution_init_concentration.gen_id() ChangeValueTransform((('species', {'id': st_2_c_1.id}), 'distribution_init_concentration', 'mean'), 0).run(model) self.assertEqual(st_1_c_1.distribution_init_concentration.mean, 1) self.assertEqual(st_1_c_2.distribution_init_concentration.mean, 2) self.assertEqual(st_2_c_1.distribution_init_concentration.mean, 0) self.assertEqual(st_2_c_2.distribution_init_concentration.mean, 4) ChangeValueTransform((('species', {'id': st_2_c_1.id}), 'distribution_init_concentration', 'std'), 0).run(model) self.assertEqual(st_1_c_1.distribution_init_concentration.std, 2) self.assertEqual(st_1_c_2.distribution_init_concentration.std, 2) self.assertEqual(st_2_c_1.distribution_init_concentration.std, 0) self.assertEqual(st_2_c_2.distribution_init_concentration.std, 2)
def setup_submodel_params(self, model_type, num_species, vol_mean, vol_std): # make Model model = Model(id='test_model', name=model_type, version='0.0.0', wc_lang_version='0.0.1') # make SpeciesTypes species_types = [] for i in range(num_species): spec_type = model.species_types.create(id='spec_type_{}'.format(i)) species_types.append(spec_type) initial_volume = InitVolume(mean=vol_mean, std=vol_std, units=unit_registry.parse_units('l')) comp = model.compartments.create( id='compt_1', biological_type=onto['WC:cellular_compartment'], init_volume=initial_volume) comp.init_density = model.parameters.create( id='density_compt_1', value=1100, units=unit_registry.parse_units('g l^-1')) return (model, species_types, comp)
def setUp(self): self.model = mdl = Model(id='model', name='test model', version='0.0.1a', wc_lang_version='0.0.1b') mdl.taxon = Taxon(id='taxon', name='test taxon', rank=TaxonRank.species) self.comp_0 = comp_0 = mdl.compartments.create(id='comp_0', name='compartment 0') self.comp_1 = comp_1 = mdl.compartments.create(id='comp_1', name='compartment 1') self.compartments = compartments = [comp_0, comp_1] density_comp_0 = mdl.parameters.create(id='density_comp_0', value=1100, units=unit_registry.parse_units('g l^-1')) density_comp_1 = mdl.parameters.create(id='density_comp_1', value=1000, units=unit_registry.parse_units('g l^-1')) compartments[0].init_density = density_comp_0 compartments[1].init_density = density_comp_1 self.species_types = species_types = [] self.species = species = [] for i in range(8): spec_type = mdl.species_types.create( id='spec_type_{}'.format(i), name='species type {}'.format(i), type=onto['WC:metabolite'], structure=ChemicalStructure( empirical_formula=EmpiricalFormula('C' + str(i + 1)), molecular_weight=12 * (i + 1), charge=i + 1)) species_types.append(spec_type) if i != 3: spec = Species(species_type=spec_type, compartment=comp_0) else: spec = Species(species_type=spec_type, compartment=comp_1) spec.id = spec.gen_id() spec.model = mdl species.append(spec) conc = DistributionInitConcentration(model=mdl, species=spec, mean=3 * i, units=unit_registry.parse_units('M')) conc.id = conc.gen_id() species_coefficients = {} def get_or_create_species_coefficient(species=None, coefficient=None): part_serialized = SpeciesCoefficient._serialize(species, coefficient) if part_serialized not in species_coefficients: species_coefficients[part_serialized] = SpeciesCoefficient(species=species, coefficient=coefficient) return species_coefficients[part_serialized] self.observables = observables = [] objects = {Species: {}, Observable: {}} for i in range(8): expr_parts = [] for j in range(i + 1): objects[Species][species[j].id] = species[j] expr_parts.append("{} * {}".format(j + 1, species[j].id)) obs_expr, _ = ObservableExpression.deserialize(' + '.join(expr_parts), objects) id = 'obs_{}'.format(i) obs = mdl.observables.create(id=id, expression=obs_expr) observables.append(obs) objects[Observable][id] = obs for i in range(3): expr_parts = [] for j in range(i + 1): expr_parts.append("{} * {}".format(j + 1, observables[j].id)) obs_expr, _ = ObservableExpression.deserialize(' + '.join(expr_parts), objects) obs = mdl.observables.create(id='obs_{}'.format(i + 8), expression=obs_expr) observables.append(obs) objects = {Observable: {o.id: o for o in observables}} self.functions = functions = [] for i in range(8): obs_expr = ' + 2 * '.join(o.id for o in observables[0:i + 1]) func_expr, _ = FunctionExpression.deserialize(obs_expr, objects) func = mdl.functions.create(id='func_{}'.format(i), expression=func_expr, units=unit_registry.parse_units('molecule')) functions.append(func) self.submdl_0 = submdl_0 = mdl.submodels.create( id='submodel_0', name='submodel 0', framework=onto['WC:stochastic_simulation_algorithm']) self.submdl_1 = submdl_1 = mdl.submodels.create( id='submodel_1', name='submodel 1', framework=onto['WC:stochastic_simulation_algorithm']) self.submdl_2 = submdl_2 = mdl.submodels.create( id='submodel_2', name='submodel 2', framework=onto['WC:dynamic_flux_balance_analysis']) self.submodels = submodels = [submdl_0, submdl_1, submdl_2] self.rxn_0 = rxn_0 = submdl_0.reactions.create( id='rxn_0', name='reaction 0', model=mdl) rxn_0.participants.append(get_or_create_species_coefficient(species=species[0], coefficient=-3)) rxn_0.participants.append(get_or_create_species_coefficient(species=species[1], coefficient=-3)) rxn_0.participants.append(get_or_create_species_coefficient(species=species[2], coefficient=3)) k_cat_0 = mdl.parameters.create(id='k_cat_0', value=2, units=unit_registry.parse_units('s^-1')) k_m_0 = mdl.parameters.create(id='k_m_0', value=1, units=unit_registry.parse_units('molecule')) expression, _ = RateLawExpression.deserialize('k_cat_0 * {0} / (k_m_0 + {0})'.format(species[5].id), { Species: { species[5].id: species[5], }, Parameter: { 'k_cat_0': k_cat_0, 'k_m_0': k_m_0, }, }) rate_law_0 = rxn_0.rate_laws.create( model=mdl, direction=RateLawDirection.forward, expression=expression, units=unit_registry.parse_units('s^-1')) rate_law_0.id = rate_law_0.gen_id() self.rxn_1 = rxn_1 = submdl_1.reactions.create( id='rxn_1', name='reaction 1', model=mdl) rxn_1.participants.append(get_or_create_species_coefficient(species=species[0], coefficient=-2)) rxn_1.participants.append(get_or_create_species_coefficient(species=species[1], coefficient=-3)) rxn_1.participants.append(get_or_create_species_coefficient(species=species[3], coefficient=2)) k_cat_1 = mdl.parameters.create(id='k_cat_1', value=2, units=unit_registry.parse_units('s^-1')) k_m_1 = mdl.parameters.create(id='k_m_1', value=1, units=unit_registry.parse_units('molecule')) expression, _ = RateLawExpression.deserialize('k_cat_1 * {0} / (k_m_1 + {0})'.format(species[6].id), { Species: { species[6].id: species[6], }, Parameter: { 'k_cat_1': k_cat_1, 'k_m_1': k_m_1, }, }) rate_law_1 = rxn_1.rate_laws.create( model=mdl, direction=RateLawDirection.forward, expression=expression, units=unit_registry.parse_units('s^-1')) rate_law_1.id = rate_law_1.gen_id() self.rxn_2 = rxn_2 = submdl_2.reactions.create( id='rxn_2', name='reaction 2', model=mdl) rxn_2.participants.append(get_or_create_species_coefficient(species=species[0], coefficient=-2)) rxn_2.participants.append(get_or_create_species_coefficient(species=species[1], coefficient=-3)) rxn_2.participants.append(get_or_create_species_coefficient(species=species[7], coefficient=1)) k_cat_2 = mdl.parameters.create(id='k_cat_2', value=2, units=unit_registry.parse_units('s^-1')) k_m_2 = mdl.parameters.create(id='k_m_2', value=1, units=unit_registry.parse_units('molecule')) expression, _ = RateLawExpression.deserialize('k_cat_2 * {0} / (k_m_2 + {0})'.format(species[7].id), { Species: { species[7].id: species[7], }, Parameter: { 'k_cat_2': k_cat_2, 'k_m_2': k_m_2, }, }) rate_law_2 = rxn_2.rate_laws.create( model=mdl, direction=RateLawDirection.forward, expression=expression, units=unit_registry.parse_units('s^-1')) rate_law_2.id = rate_law_2.gen_id() submdl_2.dfba_obj = DfbaObjective(model=mdl) submdl_2.dfba_obj.id = submdl_2.dfba_obj.gen_id() submdl_2.dfba_obj.expression = DfbaObjectiveExpression(expression='rxn_2', reactions=[rxn_2]) self.reactions = [rxn_0, rxn_1, rxn_2] self.rate_laws = [rate_law_0, rate_law_1, rate_law_2] self.parameters = parameters = [] self.references = references = [] self.identifiers = identifiers = [] for i in range(3): param = mdl.parameters.create( id='param_{}'.format(i), name='parameter {}'.format(i), value=i * 4, units=unit_registry.parse_units('dimensionless')) param.submodels = submodels[i:i + 1] parameters.append(param) ref = param.references.create( id='ref_{}'.format(i), name='reference {}'.format(i), type=None) ref.model = mdl references.append(ref) x_ref = ref.identifiers.create(namespace='x', id='y' * (i + 1)) identifiers.append(x_ref) param = mdl.parameters.create( id='param_stop_cond', name='parameter - stop condition', value=1., units=unit_registry.parse_units('molecule')) parameters.append(param) objects[Parameter] = {param.id: param} self.stop_conditions = stop_conditions = [] for i in range(3): cond = mdl.stop_conditions.create(id='stop_cond_{}'.format(i)) expr = '({}) > {}'.format(' + '.join(o.id for o in observables[0:i+1]), param.id) cond.expression, error = StopConditionExpression.deserialize(expr, objects) self.stop_conditions.append(cond) self.tempdir = tempfile.mkdtemp()
def test(self): """ Test that algorithmically-like submodels are correctly merged """ """ Construct model with 3 submodels: two SSA and one FBA """ mdl = Model() cmp = mdl.compartments.create(id='comp_0', name='compartment 0') species = [] for i in range(5): st = mdl.species_types.create(id='spec_type_{}'.format(i), type=onto['WC:metabolite']) s = mdl.species.create(species_type=st, compartment=cmp) s.id = s.gen_id() species.append(s) submdl_0 = mdl.submodels.create( id='submdl_0', framework=onto['WC:stochastic_simulation_algorithm']) submdl_1 = mdl.submodels.create( id='submdl_1', framework=onto['WC:stochastic_simulation_algorithm']) submdl_2 = mdl.submodels.create( id='submdl_2', framework=onto['WC:dynamic_flux_balance_analysis']) submdl_3 = mdl.submodels.create( id='submdl_3', framework=onto['WC:dynamic_flux_balance_analysis']) rxn_0_0 = mdl.reactions.create(id='rxn_0_0', submodel=submdl_0) rxn_0_0.participants.add( SpeciesCoefficient(species=species[0], coefficient=-1)) rxn_0_0.participants.add( SpeciesCoefficient(species=species[1], coefficient=-1)) rxn_0_0.participants.add( SpeciesCoefficient(species=species[2], coefficient=1)) rxn_0_1 = mdl.reactions.create(id='rxn_0_1', submodel=submdl_0) rxn_0_1.participants.add( SpeciesCoefficient(species=species[0], coefficient=-1)) rxn_0_1.participants.add( SpeciesCoefficient(species=species[1], coefficient=-1)) rxn_0_1.participants.add( SpeciesCoefficient(species=species[2], coefficient=1)) rxn_1_0 = mdl.reactions.create(id='rxn_1_0', submodel=submdl_1) rxn_1_0.participants.add( SpeciesCoefficient(species=species[0], coefficient=-1)) rxn_1_0.participants.add( SpeciesCoefficient(species=species[1], coefficient=-1)) rxn_1_0.participants.add( SpeciesCoefficient(species=species[3], coefficient=1)) rxn_1_1 = mdl.reactions.create(id='rxn_1_1', submodel=submdl_1) rxn_1_1.participants.add( SpeciesCoefficient(species=species[0], coefficient=-1)) rxn_1_1.participants.add( SpeciesCoefficient(species=species[1], coefficient=-1)) rxn_1_1.participants.add( SpeciesCoefficient(species=species[3], coefficient=1)) rxn_2_0 = mdl.reactions.create(id='rxn_2_0', submodel=submdl_2) rxn_2_0.participants.add( SpeciesCoefficient(species=species[0], coefficient=-1)) rxn_2_0.participants.add( SpeciesCoefficient(species=species[1], coefficient=-1)) rxn_2_0.participants.add( SpeciesCoefficient(species=species[4], coefficient=1)) rxn_2_1 = mdl.reactions.create(id='rxn_2_1', submodel=submdl_2) rxn_2_1.participants.add( SpeciesCoefficient(species=species[0], coefficient=-1)) rxn_2_1.participants.add( SpeciesCoefficient(species=species[1], coefficient=-1)) rxn_2_1.participants.add( SpeciesCoefficient(species=species[4], coefficient=1)) dfba_obj_rxn_2_0 = mdl.dfba_obj_reactions.create(id='dfba_obj_rxn_2_0', submodel=submdl_2) dfba_obj_rxn_2_1 = mdl.dfba_obj_reactions.create(id='dfba_obj_rxn_2_1', submodel=submdl_2) dfba_obj_rxn_3_0 = mdl.dfba_obj_reactions.create(id='dfba_obj_rxn_3_0', submodel=submdl_3) dfba_obj_rxn_3_1 = mdl.dfba_obj_reactions.create(id='dfba_obj_rxn_3_1', submodel=submdl_3) dfba_obj_2 = mdl.dfba_objs.create( id='dfba_obj_2', submodel=submdl_2, expression=DfbaObjectiveExpression.deserialize( 'rxn_2_0 + 2 * rxn_2_1 + 3 * dfba_obj_rxn_2_0 + dfba_obj_rxn_2_1', { Reaction: { 'rxn_2_0': rxn_2_0, 'rxn_2_1': rxn_2_1, }, DfbaObjReaction: { 'dfba_obj_rxn_2_0': dfba_obj_rxn_2_0, 'dfba_obj_rxn_2_1': dfba_obj_rxn_2_1, }, })[0]) dfba_obj_3 = mdl.dfba_objs.create( id='dfba_obj_3', submodel=submdl_3, expression=DfbaObjectiveExpression.deserialize( 'dfba_obj_rxn_3_0 + dfba_obj_rxn_3_1', { Reaction: {}, DfbaObjReaction: { 'dfba_obj_rxn_3_0': dfba_obj_rxn_3_0, 'dfba_obj_rxn_3_1': dfba_obj_rxn_3_1, } })[0]) mdl.parameters.create(id='param_0') mdl.parameters.create(id='param_1') mdl.parameters.create(id='param_2') mdl.observations.create(id='obs_0') mdl.observations.create(id='obs_1') mdl.observations.create(id='obs_2') mdl.observations.create(id='obs_3') mdl.observations.create(id='obs_4') mdl.observations.create(id='obs_5') mdl.observations.create(id='obs_6') mdl.observations.create(id='obs_7') ev_0 = Evidence(observation=mdl.observations[0], type=onto['WC:supporting_evidence']) ev_1 = Evidence(observation=mdl.observations[1], type=onto['WC:supporting_evidence']) ev_2 = Evidence(observation=mdl.observations[2], type=onto['WC:supporting_evidence']) ev_3 = Evidence(observation=mdl.observations[3], type=onto['WC:supporting_evidence']) submdl_0.evidence.extend([ev_0, ev_1]) submdl_1.evidence.extend([ev_0, ev_2, ev_3]) submdl_2.evidence.extend([ev_1, ev_2]) submdl_3.evidence.extend([ev_3]) dfba_obj_2.evidence.append(ev_2) dfba_obj_3.evidence.append(ev_3) mdl.conclusions.create(id='conclusion_0', submodels=[submdl_0, submdl_1], evidence=[ Evidence( observation=mdl.observations[4], type=onto['WC:supporting_evidence']) ]) mdl.conclusions.create(id='conclusion_1', submodels=[submdl_0, submdl_2], evidence=[ Evidence( observation=mdl.observations[5], type=onto['WC:supporting_evidence']) ]) mdl.conclusions.create(id='conclusion_2', submodels=[submdl_1, submdl_2], dfba_objs=[dfba_obj_2], evidence=[ Evidence( observation=mdl.observations[6], type=onto['WC:supporting_evidence']) ]) mdl.conclusions.create(id='conclusion_2', submodels=[submdl_1, submdl_3], dfba_objs=[dfba_obj_3], evidence=[ Evidence( observation=mdl.observations[7], type=onto['WC:supporting_evidence']) ]) mdl.references.create(id='ref_0', submodels=[submdl_0]) mdl.references.create(id='ref_1', submodels=[submdl_1]) mdl.references.create(id='ref_2', submodels=[submdl_2], dfba_objs=[dfba_obj_2]) mdl.references.create(id='ref_2', submodels=[submdl_3], dfba_objs=[dfba_obj_3]) submdl_0.identifiers.create(id='xref_0') submdl_1.identifiers.create(id='xref_1') submdl_2.identifiers.create(id='xref_2', dfba_objs=[dfba_obj_2]) submdl_3.identifiers.create(id='xref_3', dfba_objs=[dfba_obj_3]) """ Merge algorithmically-like submodels """ merged_mdl = mdl.copy() MergeAlgorithmicallyLikeSubmodelsTransform().run(merged_mdl) merged_submdl_ssa = merged_mdl.submodels.get_one( framework=onto['WC:stochastic_simulation_algorithm']) merged_submdl_fba = merged_mdl.submodels.get_one( framework=onto['WC:dynamic_flux_balance_analysis']) """ Test submodels merged corrected """ self.assertEqual(len(merged_mdl.compartments), len(mdl.compartments)) self.assertEqual(len(merged_mdl.species_types), len(mdl.species_types)) self.assertEqual(len(merged_mdl.submodels), 2) self.assertEqual(len(merged_mdl.parameters), len(mdl.parameters)) self.assertEqual(len(merged_mdl.reactions), len(mdl.reactions)) self.assertEqual(len(merged_mdl.dfba_objs), 1) self.assertEqual(len(merged_mdl.dfba_obj_reactions), len(mdl.dfba_obj_reactions)) self.assertEqual(len(merged_mdl.observations), len(mdl.observations)) self.assertEqual(len(merged_mdl.conclusions), len(mdl.conclusions)) self.assertEqual(len(merged_mdl.references), len(mdl.references)) self.assertIn(merged_submdl_ssa.id, [ '{0}_{1}'.format(submdl_0.id, submdl_1.id), '{1}_{0}'.format(submdl_0.id, submdl_1.id), ]) self.assertIn(merged_submdl_fba.id, [ '{0}_{1}'.format(submdl_2.id, submdl_3.id), '{1}_{0}'.format(submdl_2.id, submdl_3.id), ]) self.assertEqual(merged_submdl_ssa.framework, onto['WC:stochastic_simulation_algorithm']) self.assertEqual(merged_submdl_fba.framework, onto['WC:dynamic_flux_balance_analysis']) self.assertEqual( len(merged_submdl_ssa.get_children(kind='submodel', __type=Species)), len( set( submdl_0.get_children(kind='submodel', __type=Species) + submdl_1.get_children(kind='submodel', __type=Species)))) self.assertEqual( len(merged_submdl_fba.get_children(kind='submodel', __type=Species)), len( set( submdl_2.get_children(kind='submodel', __type=Species) + submdl_3.get_children(kind='submodel', __type=Species)))) self.assertEqual( len(merged_submdl_ssa.reactions), len(set(submdl_0.reactions) | set(submdl_1.reactions))) self.assertEqual( len(merged_submdl_fba.reactions), len(set(submdl_2.reactions) | set(submdl_3.reactions))) self.assertEqual(merged_submdl_ssa.dfba_obj, None) self.assertEqual(merged_submdl_fba.dfba_obj.id, merged_submdl_fba.dfba_obj.gen_id()) self.assertEqual(len(merged_submdl_fba.dfba_obj.expression.reactions), len(submdl_2.reactions) + len(submdl_3.reactions)) self.assertEqual( len(merged_submdl_fba.dfba_obj.expression.dfba_obj_reactions), len(submdl_2.dfba_obj_reactions) + len(submdl_3.dfba_obj_reactions)) self.assertEqual(merged_submdl_ssa.dfba_obj_reactions, []) self.assertEqual( len(merged_submdl_fba.dfba_obj_reactions), len(submdl_2.dfba_obj_reactions) + len(submdl_3.dfba_obj_reactions)) self.assertEqual(len(set(submdl_0.evidence) | set(submdl_1.evidence)), len(merged_submdl_ssa.evidence)) self.assertEqual(len(merged_submdl_fba.evidence), len(set(submdl_2.evidence) | set(submdl_3.evidence))) self.assertEqual( len(set(submdl_0.conclusions) | set(submdl_1.conclusions)), len(merged_submdl_ssa.conclusions)) self.assertEqual( len(merged_submdl_fba.conclusions), len(set(submdl_2.conclusions) | set(submdl_3.conclusions))) self.assertEqual( len(set(submdl_0.references) | set(submdl_1.references)), len(merged_submdl_ssa.references)) self.assertEqual( len(merged_submdl_fba.references), len(set(submdl_2.references) | set(submdl_3.references))) self.assertEqual( len(set(submdl_0.identifiers) | set(submdl_1.identifiers)), len(merged_submdl_ssa.identifiers)) self.assertEqual( len(merged_submdl_fba.identifiers), len(set(submdl_2.identifiers) | set(submdl_3.identifiers)))