Пример #1
0
    def test_gen_models_with_1_submodel(self):
        model = Model(id='model')
        model.submodels.create(id='submodel')

        core, submodels = model.submodels.gen_models()
        self.assertTrue(model.is_equal(core))
        self.assertEqual(submodels, [])
Пример #2
0
    def test_Compartment_from_sbml(self):
        model = Model()
        init_volume = InitVolume(mean=1.2)
        c_1 = model.compartments.create(id='c_1',
                                        name='cytosol',
                                        geometry=onto['WC:3D_compartment'],
                                        init_volume=init_volume)
        c_2 = model.compartments.create(id='c_2',
                                        name='cytosol',
                                        geometry=onto['WC:3D_compartment'],
                                        init_volume=init_volume)

        sbml_doc = sbml_util.LibSbmlInterface.create_doc()
        sbml_model = sbml_util.LibSbmlInterface.create_model(sbml_doc)

        sbml_c_1 = c_1.export_to_sbml(sbml_model)
        sbml_c_2 = c_2.export_to_sbml(sbml_model)

        model_2 = Model()
        c_1_b = model_2.compartments.create()
        c_1_b.import_from_sbml(sbml_c_1)
        c_2_b = model_2.compartments.create()
        c_2_b.import_from_sbml(sbml_c_2)

        self.assertTrue(c_1_b.is_equal(c_1))
        self.assertTrue(c_2_b.is_equal(c_2))
Пример #3
0
    def test_SbmlExporter_error(self):
        model = Model(id='model')
        model.submodels.create(
            id='Metabolism',
            framework=onto['WC:dynamic_flux_balance_analysis'])
        model.submodels.create(
            id='Metabolism_2',
            framework=onto['WC:dynamic_flux_balance_analysis'])
        with self.assertRaisesRegex(ValueError,
                                    'Only 1 submodel can be encoded'):
            sbml_io.SbmlExporter.run(model)

        model = Model(id='model')
        submodel = model.submodels.create(
            id='Metabolism',
            framework=onto['WC:dynamic_flux_balance_analysis'])
        model.reactions.create(
            id='rxn_1',
            submodel=submodel,
            flux_bounds=FluxBounds(units=unit_registry.parse_units('M s^-1')))
        model.reactions.create(
            id='rxn_1',
            submodel=submodel,
            flux_bounds=FluxBounds(units=unit_registry.parse_units('M s^-1')))
        with self.assertRaisesRegex(sbml_util.LibSbmlError,
                                    'Document is invalid'):
            with self.assertWarnsRegex(WcLangWarning, 'Model is invalid'):
                sbml_io.SbmlExporter.run(model)
Пример #4
0
    def test_export_import_comments(self):
        for comments in [
                '', 'My comments', 'My\ncomments', 'My<br/>comments',
                '<p>My</p>\n<p>comments</p>'
        ]:
            sbml_doc = LibSbmlInterface.create_doc()
            sbml_model = LibSbmlInterface.create_model(sbml_doc)

            model = Model(comments=comments)
            LibSbmlInterface.set_commments(model, sbml_model)

            model_2 = Model()
            LibSbmlInterface.get_commments(model_2, sbml_model)

            self.assertEqual(model_2.comments, comments)
Пример #5
0
    def test_set_get_math(self):
        model = Model(version='1.2.3', wc_lang_version='4.5.6')

        spec_1_c = Species(id='spec_1[c]')
        spec_2_c = Species(id='spec_2[c]')
        model_objs = {Species: {spec_1_c.id: spec_1_c, spec_2_c.id: spec_2_c}}

        expression, error = ObservableExpression.deserialize(
            'spec_1[c] + spec_2[c]', model_objs)
        assert error is None, str(error)

        sbml_doc = LibSbmlInterface.create_doc()
        sbml_model = LibSbmlInterface.init_model(model, sbml_doc)
        rule = LibSbmlInterface.call_libsbml(sbml_model.createAssignmentRule)
        LibSbmlInterface.set_math(rule.setMath, expression)

        self.assertEqual(
            libsbml.formulaToL3String(
                LibSbmlInterface.call_libsbml(rule.getMath)),
            'Species__spec_1__RB__c__LB__ + Species__spec_2__RB__c__LB__')

        expression_2 = LibSbmlInterface.get_math(rule.getMath,
                                                 ObservableExpression,
                                                 model_objs)
        self.assertTrue(expression_2.is_equal(expression))
        self.assertEqual(expression_2._parsed_expression._obj_tables_tokens,
                         expression._parsed_expression._obj_tables_tokens)
Пример #6
0
    def test_writer_errors(self):
        model = Model(version=None)
        with self.assertWarnsRegex(WcLangWarning, 'Model is invalid:'):
            sbml_io.SbmlWriter().run(model, self.dirname)

        with mock.patch('libsbml.writeSBMLToFile', return_value=False):
            with self.assertRaisesRegex(ValueError,
                                        ' could not be written to '):
                sbml_io.SbmlWriter().run(self.model, self.dirname)
Пример #7
0
    def test_Species_from_sbml(self):
        model = Model(id='mdl', created=None, updated=None)

        c = model.compartments.create(id='c')

        st_1 = model.species_types.create(
            id='st_1',
            structure=ChemicalStructure(
                value='AAA',
                format=ChemicalStructureFormat.BpForms,
                alphabet=ChemicalStructureAlphabet.dna))
        st_1.structure.empirical_formula = st_1.structure.get_structure(
        ).get_formula()
        st_1.structure.molecular_weight = st_1.structure.get_structure(
        ).get_mol_wt()
        st_1.structure.charge = st_1.structure.get_structure().get_charge()
        st_2 = model.species_types.create(id='st_2')

        s_1 = model.species.create(species_type=st_1, compartment=c)
        s_2 = model.species.create(species_type=st_2, compartment=c)
        s_1.id = s_1.gen_id()
        s_2.id = s_2.gen_id()

        sbml_doc = sbml_util.LibSbmlInterface.create_doc()
        sbml_model = sbml_util.LibSbmlInterface.create_model(sbml_doc)

        s_1_sbml = s_1.export_to_sbml(sbml_model)
        s_2_sbml = s_2.export_to_sbml(sbml_model)
        s_1.export_relations_to_sbml(sbml_model, s_1_sbml)
        s_2.export_relations_to_sbml(sbml_model, s_2_sbml)

        model_2 = Model(id='mdl', created=None, updated=None)
        c_b = model_2.compartments.create(id='c')
        s_1_b = model_2.species.create()
        s_2_b = model_2.species.create()
        s_1_b.import_from_sbml(s_1_sbml)
        s_2_b.import_from_sbml(s_2_sbml)
        objs = {Compartment: {c_b.id: c_b}}
        s_1_b.import_relations_from_sbml(s_1_sbml, objs=objs)
        s_2_b.import_relations_from_sbml(s_2_sbml, objs=objs)

        self.assertTrue(s_1_b.is_equal(s_1))
        self.assertTrue(s_2_b.is_equal(s_2))
        self.assertTrue(model_2.is_equal(model))
Пример #8
0
def gen_submodel_1():
    timestamp = datetime.datetime(2018, 1, 1, 12, 0, 0)
    model = Model(id='model', version='0.0.1', wc_lang_version='0.0.2', created=timestamp, updated=timestamp)

    submodel_1 = model.submodels.create(id='submodel_1')

    model.compartments.create(id='c_0')
    model.compartments.create(id='c_1')
    structure = ChemicalStructure(charge=0)
    model.species_types.create(id='s_0', structure=structure)
    model.species.create(compartment=model.compartments[0], species_type=model.species_types[0])
    model.species.create(compartment=model.compartments[1], species_type=model.species_types[0])
    for species in model.species:
        species.id = species.gen_id()

    model.reactions.create(id='rxn_10', submodel=submodel_1)
    model.reactions.create(id='rxn_11', submodel=submodel_1)
    model.reactions[0].participants.create(species=model.species[0], coefficient=-1)
    model.reactions[0].participants.create(species=model.species[1], coefficient=1)
    model.reactions[1].participants.add(model.reactions[0].participants[0])

    model.parameters.create(id='p_0', value=1., units=unit_registry.parse_units('molecule'))
    model.parameters.create(id='p_2', value=1., units=unit_registry.parse_units('molecule'))
    model.parameters.create(id='p_3', value=1., units=unit_registry.parse_units('g l^-1'))
    model.parameters.create(id='p_4', value=1., units=unit_registry.parse_units('g l^-1'))
    model.compartments[0].init_density = model.parameters[2]
    model.compartments[1].init_density = model.parameters[3]
    model.parameters.create(id='p_7', value=1., units=unit_registry.parse_units('s^-1'))
    model.parameters.create(id='p_8', value=1., units=unit_registry.parse_units('s^-1'))
    rl = model.rate_laws.create(reaction=model.reactions[0])
    rl.expression, error = RateLawExpression.deserialize(f'{model.parameters[4].id}',
                                                         {Parameter: {model.parameters[4].id: model.parameters[4]}})
    rl = model.rate_laws.create(reaction=model.reactions[1])
    rl.expression, error = RateLawExpression.deserialize(f'{model.parameters[5].id}',
                                                         {Parameter: {model.parameters[5].id: model.parameters[5]}})
    for rl in model.rate_laws:
        rl.id = rl.gen_id()

    objs = {
        Species: {s.id: s for s in model.species},
        Parameter: {p.id: p for p in model.parameters},
    }
    model.stop_conditions.create(id='sc_0')
    model.stop_conditions.create(id='sc_2')
    model.stop_conditions[0].expression, error = StopConditionExpression.deserialize(
        f'{model.species[0].id} > {model.parameters[0].id}', objs)
    assert error is None, str(error)
    model.stop_conditions[1].expression, error = StopConditionExpression.deserialize(
        f'{model.species[1].id} > {model.parameters[1].id}', objs)
    assert error is None, str(error)

    model.references.create(id='ref_0', submodels=[submodel_1])
    model.references.create(id='ref_2', submodels=[submodel_1])

    return model
Пример #9
0
    def __eq__(self, other, tol=0.):
        """ Compare two :obj:`ChangeValueTransform` objects

        Args:
            other (:obj:`Object`): other object
            tol (:obj:`float`, optional): equality tolerance

        Returns:
            :obj:`bool`: true if :obj:`ChangeValueTransform` objects are semantically equal
        """
        if other.__class__ is not self.__class__:
            return False

        if not Model.are_attr_paths_equal(self.attr_path, other.attr_path):
            return False

        attr = Model.get_nested_attr(self.attr_path)
        if not attr.value_equal(self.value, other.value, tol=tol):
            return False

        return True
Пример #10
0
    def test_export_import_annotations(self):
        model = Model()
        sbml_doc = LibSbmlInterface.create_doc()
        sbml_model = LibSbmlInterface.init_model(model, sbml_doc)
        LibSbmlInterface.set_annotations(model, {
            'version': 'version',
            'wc_lang_version': 'wc_lang_version'
        }, sbml_model)

        model_2 = Model()
        LibSbmlInterface.get_annotations(model_2, {
            'version': 'version',
            'wc_lang_version': 'wc_lang_version'
        }, sbml_model)
        self.assertEqual(model_2.version, model.version)
        self.assertEqual(model_2.wc_lang_version, model.wc_lang_version)

        model_3 = Model()
        LibSbmlInterface.get_annotations(model_3, {
            'version': [('version', )],
            'wc_lang_version': [('wc_lang_version', )]
        }, sbml_model)
        self.assertEqual(model_3.version, model.version)
        self.assertEqual(model_3.wc_lang_version, model.wc_lang_version)
Пример #11
0
    def test_get_obj_units(self):
        model = Model()
        units = set([model.time_units])
        self.assertEqual(set(obj_tables.sci.units.get_obj_units(model)), units)

        model.compartments.create()
        model.compartments.create()
        for c in model.compartments:
            units.add(c.mass_units)
            if c.init_volume:
                units.add(c.init_volume.units)
            if c.ph:
                units.add(c.ph.units)
        self.assertEqual(set(obj_tables.sci.units.get_obj_units(model)), units)

        model.species_types.create()
        model.species_types.create()
        self.assertEqual(set(obj_tables.sci.units.get_obj_units(model)), units)

        for c in model.compartments:
            for s in model.species_types:
                model.species.create(compartment=c, species_type=s)
        for s in model.species:
            units.add(s.units)
        self.assertEqual(set(obj_tables.sci.units.get_obj_units(model)), units)

        model.distribution_init_concentrations.create(
            species=model.species[0], units=unit_registry.parse_units('M'))
        model.distribution_init_concentrations.create(
            species=model.species[1],
            units=unit_registry.parse_units('molecule'))
        for o in model.distribution_init_concentrations:
            units.add(o.units)
        self.assertEqual(set(obj_tables.sci.units.get_obj_units(model)), units)

        model.parameters.create(units=unit_registry.parse_units('g'))
        model.parameters.create(units=unit_registry.parse_units('l'))
        model.parameters.create(units=unit_registry.parse_units('s'))
        for p in model.parameters:
            units.add(p.units)
        self.assertEqual(set(obj_tables.sci.units.get_obj_units(model)), units)

        model.functions.create(units=unit_registry.parse_units('g / l'))
        model.functions.create(units=unit_registry.parse_units('g / s'))
        model.functions.create(units=unit_registry.parse_units('l / s'))
        for f in model.functions:
            units.add(f.units)
        self.assertEqual(set(obj_tables.sci.units.get_obj_units(model)), units)
Пример #12
0
    def test(self):
        model = Model()
        submodel = model.submodels.create(framework=onto['WC:dynamic_flux_balance_analysis'])
        rxn_1 = model.reactions.create(submodel=submodel, reversible=True,
                                       flux_bounds=None)
        rxn_2 = model.reactions.create(submodel=submodel, reversible=False,
                                       flux_bounds=FluxBounds(min=None, max=None))
        rxn_3 = model.reactions.create(submodel=submodel, reversible=True,
                                       flux_bounds=FluxBounds(min=float('nan'), max=float('nan')))
        rxn_4 = model.reactions.create(submodel=submodel, reversible=False,
                                       flux_bounds=FluxBounds(min=float('nan'), max=float('nan')))
        rxn_5 = model.reactions.create(submodel=submodel, reversible=True,
                                       flux_bounds=FluxBounds(min=-1e3, max=1e3, units=unit_registry.parse_units('M s^-1')))
        rxn_6 = model.reactions.create(submodel=submodel, reversible=False,
                                       flux_bounds=FluxBounds(min=-1e3, max=1e3, units=unit_registry.parse_units('M s^-1')))
        rxn_7 = model.reactions.create(submodel=submodel, reversible=True,
                                       flux_bounds=FluxBounds(min=-1e1, max=1e1, units=unit_registry.parse_units('M s^-1')))
        rxn_8 = model.reactions.create(submodel=submodel, reversible=False,
                                       flux_bounds=FluxBounds(min=-1e1, max=1e1, units=unit_registry.parse_units('M s^-1')))

        transform = SetFiniteDfbaFluxBoundsTransform()
        env = EnvironmentVarGuard()
        env.set('CONFIG__DOT__wc_lang__DOT__dfba__DOT__flux_bounds__DOT__min_reversible', '-2e2')
        env.set('CONFIG__DOT__wc_lang__DOT__dfba__DOT__flux_bounds__DOT__min_irreversible', '0.')
        env.set('CONFIG__DOT__wc_lang__DOT__dfba__DOT__flux_bounds__DOT__max', '1e2')
        with env:
            transform.run(model)

        self.assertEqual(rxn_1.flux_bounds.min, -2e2)
        self.assertEqual(rxn_1.flux_bounds.max, 1e2)
        self.assertEqual(rxn_2.flux_bounds.min, 0)
        self.assertEqual(rxn_2.flux_bounds.max, 1e2)
        self.assertEqual(rxn_3.flux_bounds.min, -2e2)
        self.assertEqual(rxn_3.flux_bounds.max, 1e2)
        self.assertEqual(rxn_4.flux_bounds.min, 0)
        self.assertEqual(rxn_4.flux_bounds.max, 1e2)
        self.assertEqual(rxn_5.flux_bounds.min, -2e2)
        self.assertEqual(rxn_5.flux_bounds.max, 1e2)
        self.assertEqual(rxn_6.flux_bounds.min, 0)
        self.assertEqual(rxn_6.flux_bounds.max, 1e2)
        self.assertEqual(rxn_7.flux_bounds.min, -1e1)
        self.assertEqual(rxn_7.flux_bounds.max, 1e1)
        self.assertEqual(rxn_8.flux_bounds.min, 0)
        self.assertEqual(rxn_8.flux_bounds.max, 1e1)
Пример #13
0
def gen_core_model(extra_species=True):
    timestamp = datetime.datetime(2018, 1, 1, 12, 0, 0)
    model = Model(id='model', version='0.0.1', wc_lang_version='0.0.2', created=timestamp, updated=timestamp)

    model.compartments.create(id='c_0')
    model.compartments.create(id='c_1')
    structure = ChemicalStructure(charge=0)
    model.species_types.create(id='s_0', structure=structure)
    model.species_types.create(id='s_1', structure=structure)
    model.species.create(compartment=model.compartments[0], species_type=model.species_types[0])
    model.species.create(compartment=model.compartments[0], species_type=model.species_types[1])
    model.species.create(compartment=model.compartments[1], species_type=model.species_types[0])
    if extra_species:
        model.species.create(compartment=model.compartments[1], species_type=model.species_types[1])
    for species in model.species:
        species.id = species.gen_id()

    model.parameters.create(id='p_0', value=1., units=unit_registry.parse_units('molecule'))
    model.parameters.create(id='p_1', value=1., units=unit_registry.parse_units('molecule'))
    model.parameters.create(id='p_2', value=1., units=unit_registry.parse_units('molecule'))
    model.parameters.create(id='p_3', value=1., units=unit_registry.parse_units('g l^-1'))
    model.parameters.create(id='p_4', value=1., units=unit_registry.parse_units('g l^-1'))
    model.compartments[0].init_density = model.parameters[3]
    model.compartments[1].init_density = model.parameters[4]

    objs = {
        Species: {s.id: s for s in model.species},
        Parameter: {p.id: p for p in model.parameters},
    }
    model.stop_conditions.create(id='sc_0')
    model.stop_conditions.create(id='sc_1')
    model.stop_conditions.create(id='sc_2')
    model.stop_conditions[0].expression, error = StopConditionExpression.deserialize(
        f'{model.species[0].id} > {model.parameters[0].id}', objs)
    assert error is None, str(error)
    model.stop_conditions[1].expression, error = StopConditionExpression.deserialize(
        f'{model.species[1].id} > {model.parameters[1].id}', objs)
    assert error is None, str(error)
    model.stop_conditions[2].expression, error = StopConditionExpression.deserialize(
        f'{model.species[2].id} > {model.parameters[2].id}', objs)
    assert error is None, str(error)

    return model
Пример #14
0
 def test_gen_ids(self):
     model = Model()
     model.compartments.create(id='c_1')
     model.compartments.create(id='c_2')
     model.species_types.create(id='st_1')
     model.species_types.create(id='st_2')
     model.species.create(species_type=model.species_types[0],
                          compartment=model.compartments[0])
     model.species.create(species_type=model.species_types[0],
                          compartment=model.compartments[1])
     model.species.create(species_type=model.species_types[1],
                          compartment=model.compartments[0])
     model.species.create(species_type=model.species_types[1],
                          compartment=model.compartments[1])
     util.gen_ids(model)
     self.assertEqual(model.species[0].id, 'st_1[c_1]')
     self.assertEqual(model.species[1].id, 'st_1[c_2]')
     self.assertEqual(model.species[2].id, 'st_2[c_1]')
     self.assertEqual(model.species[3].id, 'st_2[c_2]')
Пример #15
0
    def test_init_model(self):
        model = Model()
        model.parameters.create(units=unit_registry.parse_units('s^-1'))
        model.parameters.create(units=unit_registry.parse_units('g / l'))
        model.parameters.create(units=unit_registry.parse_units('m'))
        sbml_model = LibSbmlInterface.init_model(model,
                                                 self.document,
                                                 packages={'fbc': 2})

        # check the SBML document
        LibSbmlInterface.verify_doc(self.document)
        return_val = LibSbmlInterface.call_libsbml(
            self.document.checkConsistency, returns_int=True)
        self.assertEqual(return_val, 0)
        LibSbmlInterface.verify_doc_is_compatible(self.document)

        # check seconds unit
        unit_def = LibSbmlInterface.call_libsbml(sbml_model.getUnitDefinition,
                                                 'unit_1_per_second')
        units = LibSbmlInterface.call_libsbml(UnitDefinition.printUnits,
                                              unit_def, True)
        self.assertEqual(units, '(1 second)^-1')
Пример #16
0
    def test_write_error(self):
        model = Model(id='test_model', version='0.0.0')
        comp = model.compartments.create(id='compartment_1')
        comp.init_density = model.parameters.create(
            id='density_compartment_1',
            value=1100,
            units=unit_registry.parse_units('g l^-1'))
        species_type_1 = model.species_types.create(
            id='species_type_1',
            structure=ChemicalStructure(
                empirical_formula=EmpiricalFormula('CHO'), charge=1))
        species_type_2 = model.species_types.create(
            id='species_type_2',
            structure=ChemicalStructure(
                empirical_formula=EmpiricalFormula('C2H2O2'), charge=2))
        species_1 = comp.species.create(species_type=species_type_1,
                                        id='',
                                        model=model)
        species_2 = comp.species.create(species_type=species_type_2,
                                        id='',
                                        model=model)
        submdl = model.submodels.create(id='submodel_1')

        # create a DistributionInitConcentration so that Species are provided to ExpressionAttribute.deserialize()
        species_1.distribution_init_concentration = DistributionInitConcentration(
            model=model, mean=1, units=unit_registry.parse_units('M'))
        species_1.distribution_init_concentration.id = species_1.distribution_init_concentration.gen_id(
        )
        objects = {Species: {}}
        objects[Species][species_1.id] = species_1
        observable_1 = Expression.make_obj(model, Observable, 'observable_1',
                                           species_1.id, objects)

        rxn_species_coeffs = [
            species_1.species_coefficients.create(coefficient=-2.),
            species_2.species_coefficients.create(coefficient=1.),
        ]
        rxn = submdl.reactions.create(id='reaction_1', model=model)
        rxn.participants.extend(rxn_species_coeffs)
        rl = rxn.rate_laws.create(direction=RateLawDirection.forward,
                                  units=unit_registry.parse_units('s^-1'),
                                  model=model)
        rl.id = rl.gen_id()
        param_1 = model.parameters.create(
            id='param_1', value=1., units=unit_registry.parse_units('s^-1'))
        rl.expression, error = RateLawExpression.deserialize(
            'param_1', {Parameter: {
                'param_1': param_1
            }})
        self.assertEqual(error, None)

        errors = obj_tables.Validator().run(model, get_related=True)
        self.assertNotEqual(errors, None)

        filename = os.path.join(self.tmp_dir, 'model.xlsx')
        with pytest.warns(obj_tables.io.IoWarning,
                          match='objects are not valid'):
            Writer().run(filename, model, data_repo_metadata=False)
        with self.assertRaisesRegex(ValueError,
                                    re.escape('contains error(s)')):
            Reader().run(filename)
Пример #17
0
    def test_successful_roundtrip(self):
        """
        Args:
            species_coefficient_creation_method (:obj:`str`): name of method to use to get or create
                an instance of :obj:`wc_lang.SpeciesCoefficient`
        """
        model = Model(id='test_model', version='0.0.0')
        comp = model.compartments.create(id='compartment_1')
        comp.init_density = model.parameters.create(
            id='density_compartment_1',
            value=1100,
            units=unit_registry.parse_units('g l^-1'))
        species_type_1 = model.species_types.create(
            id='species_type_1',
            structure=ChemicalStructure(
                empirical_formula=EmpiricalFormula('CHO'), charge=1))
        species_type_2 = model.species_types.create(
            id='species_type_2',
            structure=ChemicalStructure(
                empirical_formula=EmpiricalFormula('C3H3O3'), charge=3))
        species_1 = comp.species.create(species_type=species_type_1,
                                        model=model)
        species_2 = comp.species.create(species_type=species_type_2,
                                        model=model)
        species_1.id = species_1.gen_id()
        species_2.id = species_2.gen_id()
        submdl = model.submodels.create(id='submodel_1')

        # create a DistributionInitConcentration so that Species are provided to ExpressionAttribute.deserialize()
        species_1.distribution_init_concentration = DistributionInitConcentration(
            model=model, mean=1, units=unit_registry.parse_units('M'))
        species_1.distribution_init_concentration.id = species_1.distribution_init_concentration.gen_id(
        )
        objects = {Species: {}}
        objects[Species][species_1.id] = species_1
        observable_1 = Expression.make_obj(model, Observable, 'observable_1',
                                           species_1.id, objects)
        objects = {Observable: {'observable_1': observable_1}}
        observable_2 = Expression.make_obj(model, Observable, 'observable_2',
                                           'obs_1', objects)

        param_1 = model.parameters.create(
            id='param_1',
            value=1.,
            units=unit_registry.parse_units('dimensionless'))
        param_2 = model.parameters.create(
            id='param_2', value=1., units=unit_registry.parse_units('s^-1'))
        objects = {
            Parameter: {
                'param_1': param_1,
                'param_2': param_2,
            },
        }
        func_1 = Expression.make_obj(model, Function, 'func_1', 'param_1',
                                     objects)
        func_1.units = unit_registry.parse_units('dimensionless')

        rxn_species_coeffs = [
            species_1.species_coefficients.get_or_create(coefficient=-3.),
            species_2.species_coefficients.get_or_create(coefficient=1.),
        ]
        rxn = submdl.reactions.create(id='reaction_1', model=model)
        rxn.participants.extend(rxn_species_coeffs)
        rl = rxn.rate_laws.create(direction=RateLawDirection.forward,
                                  units=unit_registry.parse_units('s^-1'),
                                  model=model)
        rl.id = rl.gen_id()
        rl.expression, error = RateLawExpression.deserialize(
            'param_2', objects)
        self.assertEqual(error, None)

        errors = obj_tables.Validator().run(model, get_related=True)
        self.assertEqual(errors, None, str(errors))

        filename = os.path.join(self.tmp_dir, 'model.xlsx')
        Writer().run(filename, model, data_repo_metadata=False)
        model_2 = Reader().run(filename)[Model][0]

        self.assertEqual(model.difference(model_2), '')
        self.assertTrue(model_2.is_equal(model))
        self.assertTrue(model.is_equal(model_2))
Пример #18
0
    def setUp(self):
        self.model = mdl = Model()

        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]

        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'])
            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(species=spec, mean=1)
            conc.id = conc.gen_id()
            conc.model = mdl

        self.submdl_0 = submdl_0 = mdl.submodels.create(
            id='submdl_0',
            framework=onto['WC:stochastic_simulation_algorithm'])
        self.submdl_1 = submdl_1 = mdl.submodels.create(
            id='submdl_1',
            framework=onto['WC:stochastic_simulation_algorithm'])
        self.submdl_2 = submdl_2 = mdl.submodels.create(
            id='submdl_2', framework=onto['WC:dynamic_flux_balance_analysis'])
        self.submodels = [submdl_0, submdl_1, submdl_2]

        self.rxn_0 = rxn_0 = submdl_0.reactions.create(id='rxn_0', model=mdl)
        rxn_0.participants.create(species=species[0], coefficient=-2)
        rxn_0.participants.create(species=species[1], coefficient=-3)
        rxn_0.participants.create(species=species[2], coefficient=1)
        expression = RateLawExpression(
            expression='k_cat_0 * {0} / (k_m_0 + {0})'.format(
                species[5].get_primary_attribute()),
            species=species[5:6])
        expression.parameters.create(id='k_cat_0', value=2, model=mdl)
        expression.parameters.create(id='k_m_0', value=1, model=mdl)
        rate_law_0 = rxn_0.rate_laws.create(expression=expression, model=mdl)

        self.rxn_1 = rxn_1 = submdl_1.reactions.create(id='rxn_1', model=mdl)
        rxn_1.participants.create(species=species[0], coefficient=-2)
        rxn_1.participants.create(species=species[1], coefficient=-3)
        rxn_1.participants.create(species=species[3], coefficient=2)
        expression = RateLawExpression(
            expression='k_cat_1 * {0} / (k_m_1 + {0})'.format(
                species[6].get_primary_attribute()),
            species=species[6:7])
        expression.parameters.create(id='k_cat_1', value=2, model=mdl)
        expression.parameters.create(id='k_m_1', value=1, model=mdl)
        rate_law_1 = rxn_1.rate_laws.create(expression=expression, model=mdl)

        self.rxn_2 = rxn_2 = submdl_2.reactions.create(id='rxn_2', model=mdl)
        rxn_2.participants.create(species=species[0], coefficient=-2)
        rxn_2.participants.create(species=species[1], coefficient=-3)
        rxn_2.participants.create(species=species[4], coefficient=1)
        expression = RateLawExpression(
            expression='k_cat_2 * {0} / (k_m_2 + {0})'.format(
                species[7].get_primary_attribute()),
            species=species[7:8])
        expression.parameters.create(id='k_cat_2', value=2, model=mdl)
        expression.parameters.create(id='k_m_2', value=1, model=mdl)
        rate_law_2 = rxn_2.rate_laws.create(expression=expression, model=mdl)

        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))
            parameters.append(param)

            ref = param.references.create(id='ref_{}'.format(i), type=None)
            ref.model = mdl
            references.append(ref)

            x_ref = ref.identifiers.create(namespace='Y', id='x')
            identifiers.append(x_ref)