コード例 #1
0
ファイル: builder.py プロジェクト: matthiaskoenig/sbmlutils
def create_top_replacements(model, model_fba, compartment_id):
    """ Create all the replacements in the top model.

    :param model:
    :param model_fba:
    :param compartment_id:
    :return:
    """

    # compartment
    comp.replace_elements(model, compartment_id, ref_type=comp.SBASE_REF_TYPE_PORT,
                          replaced_elements={
                              'update': ['{}_port'.format(compartment_id)],
                              'bounds': ['{}_port'.format(compartment_id)]})

    # dt
    comp.replace_elements(model, 'dt', ref_type=comp.SBASE_REF_TYPE_PORT,
                          replaced_elements={'bounds': ['dt_port']})

    # species dependent replacements
    ex_rids = utils.find_exchange_reactions(model_fba)
    for ex_rid, sid in ex_rids.items():

        # flux parameters
        comp.replace_elements(model, FLUX_PARAMETER_PREFIX + sid, ref_type=comp.SBASE_REF_TYPE_PORT,
                              replaced_elements={'update': ['{}_port'.format(FLUX_PARAMETER_PREFIX + sid)]})

        # dynamic species
        comp.replace_elements(model, sid, ref_type=comp.SBASE_REF_TYPE_PORT,
                              replaced_elements={
                                  'bounds': ['{}_port'.format(sid)],
                                  'update': ['{}_port'.format(sid)]})

        # bounds of exchange reactions
        for replace_id in [
                           UPPER_BOUND_PREFIX + EXCHANGE_REACTION_PREFIX + sid,
                           LOWER_BOUND_PREFIX + EXCHANGE_REACTION_PREFIX + sid]:

            comp.replace_elements(model, replace_id, ref_type=comp.SBASE_REF_TYPE_PORT,
                                  replaced_elements={
                                    'bounds': ['{}_port'.format(replace_id)],
                                    'fba': ['{}_port'.format(replace_id)]})

    # replace units
    for unit in model.getListOfUnitDefinitions():
        uid = unit.getId()
        comp.replace_element_in_submodels(model, uid, ref_type=comp.SBASE_REF_TYPE_UNIT,
                                          submodels=['bounds', 'fba', 'update'])
コード例 #2
0
def create_top_replacements(model, model_fba, compartment_id):
    """ Create all the replacements in the top model.

    :param model:
    :param model_fba:
    :param compartment_id:
    :return:
    """

    # compartment
    comp.replace_elements(model,
                          compartment_id,
                          ref_type=comp.SBASE_REF_TYPE_PORT,
                          replaced_elements={
                              'update': ['{}_port'.format(compartment_id)],
                              'bounds': ['{}_port'.format(compartment_id)]
                          })

    # dt
    comp.replace_elements(model,
                          'dt',
                          ref_type=comp.SBASE_REF_TYPE_PORT,
                          replaced_elements={'bounds': ['dt_port']})

    # species dependent replacements
    ex_rids = utils.find_exchange_reactions(model_fba)
    for ex_rid, sid in ex_rids.items():

        # flux parameters
        comp.replace_elements(
            model,
            FLUX_PARAMETER_PREFIX + sid,
            ref_type=comp.SBASE_REF_TYPE_PORT,
            replaced_elements={
                'update': ['{}_port'.format(FLUX_PARAMETER_PREFIX + sid)]
            })

        # dynamic species
        comp.replace_elements(model,
                              sid,
                              ref_type=comp.SBASE_REF_TYPE_PORT,
                              replaced_elements={
                                  'bounds': ['{}_port'.format(sid)],
                                  'update': ['{}_port'.format(sid)]
                              })

        # bounds of exchange reactions
        for replace_id in [
                UPPER_BOUND_PREFIX + EXCHANGE_REACTION_PREFIX + sid,
                LOWER_BOUND_PREFIX + EXCHANGE_REACTION_PREFIX + sid
        ]:

            comp.replace_elements(model,
                                  replace_id,
                                  ref_type=comp.SBASE_REF_TYPE_PORT,
                                  replaced_elements={
                                      'bounds': ['{}_port'.format(replace_id)],
                                      'fba': ['{}_port'.format(replace_id)]
                                  })

    # replace units
    for unit in model.getListOfUnitDefinitions():
        uid = unit.getId()
        comp.replace_element_in_submodels(
            model,
            uid,
            ref_type=comp.SBASE_REF_TYPE_UNIT,
            submodels=['bounds', 'fba', 'update'])
コード例 #3
0
def top_model(sbml_file, directory, emds, doc_fba, annotations=None):
    """ Create top comp model.

    Creates full comp model by combining fba, update and bounds
    model with additional kinetics in the top model.
    """
    top_notes = notes.format("""
        <h2>TOP model</h2>
        <p>Main comp DFBA model by combining fba, update and bounds
            model with additional kinetics in the top model.</p>
        """)
    working_dir = os.getcwd()
    os.chdir(directory)

    doc = builder.template_doc_top(settings.MODEL_ID, emds)
    model = doc.getModel()
    utils.set_model_info(model,
                         notes=top_notes,
                         creators=creators, units=units, main_units=main_units)

    # dt
    builder.create_dfba_dt(model, step_size=DT_SIM, time_unit=UNIT_TIME, create_port=False)

    # compartment
    compartment_id = "extern"
    builder.create_dfba_compartment(model, compartment_id=compartment_id, unit_volume=UNIT_VOLUME, create_port=False)

    # dynamic species
    model_fba = doc_fba.getModel()
    builder.create_dfba_species(model, model_fba, compartment_id=compartment_id, hasOnlySubstanceUnits=True,
                                unit_amount=UNIT_AMOUNT, create_port=False)
    # dummy species
    builder.create_dummy_species(model, compartment_id=compartment_id, hasOnlySubstanceUnits=True,
                                 unit_amount=UNIT_AMOUNT)

    # exchange flux bounds
    builder.create_exchange_bounds(model, model_fba=model_fba, unit_flux=UNIT_FLUX, create_ports=False)

    # dummy reactions & flux assignments
    builder.create_dummy_reactions(model, model_fba=model_fba, unit_flux=UNIT_FLUX)

    # replacedBy (fba reactions)
    builder.create_top_replacedBy(model, model_fba=model_fba)

    # replaced
    builder.create_top_replacements(model, model_fba, compartment_id=compartment_id)

    # initial concentrations for fba exchange species
    initial_c = {
        'A': 10.0,
        'C': 0.0,
    }
    for sid, value in initial_c.items():
        species = model.getSpecies(sid)
        species.setInitialConcentration(value)

    # kinetic model
    mc.create_objects(model, [
        # kinetic species
        mc.Species(sid='D', initialConcentration=0, substanceUnit=UNIT_AMOUNT,
                   hasOnlySubstanceUnits=True, compartment="extern"),

        # kinetic
        mc.Parameter(sid="k_R4", value=0.1, constant=True, unit="per_s", sboTerm="SBO:0000009"),

        # bounds parameter
        mc.Parameter(sid='ub_R1', value=1.0, unit=UNIT_FLUX, constant=False, sboTerm="SBO:0000625"),
    ])
    # kinetic reaction (MMK)
    mc.create_reaction(model, rid="R4", name="R4: C -> D", fast=False, reversible=False,
                       reactants={"C": 1}, products={"D": 1}, formula="k_R4*C", compartment="extern")

    # kinetic flux bounds
    comp.replace_elements(model, 'ub_R1', ref_type=comp.SBASE_REF_TYPE_PORT,
                          replaced_elements={'bounds': ['ub_R1_port'],
                                             'fba': ['ub_R1_port']})

    # write SBML file
    if annotations:
        annotation.annotate_sbml_doc(doc, annotations)
    sbmlio.write_sbml(doc, filepath=os.path.join(directory, sbml_file), validate=True)

    # change back the working dir
    os.chdir(working_dir)
コード例 #4
0
def top_model(sbml_file, directory, emds, doc_fba, annotations=None):
    """ Create top comp model.

    Creates full comp model by combining fba, update and bounds
    model with additional kinetics in the top model.
    """
    top_notes = notes.format("""
        <h2>TOP model</h2>
        <p>Main comp DFBA model by combining fba, update and bounds
            model with additional kinetics in the top model.</p>
        """)
    working_dir = os.getcwd()
    os.chdir(directory)

    doc = builder.template_doc_top(settings.MODEL_ID, emds)
    model = doc.getModel()
    utils.set_model_info(model,
                         notes=top_notes,
                         creators=creators,
                         units=units,
                         main_units=main_units)

    # dt
    builder.create_dfba_dt(model,
                           step_size=DT_SIM,
                           time_unit=UNIT_TIME,
                           create_port=False)

    # compartment
    compartment_id = "extern"
    builder.create_dfba_compartment(model,
                                    compartment_id=compartment_id,
                                    unit_volume=UNIT_VOLUME,
                                    create_port=False)

    # dynamic species
    model_fba = doc_fba.getModel()
    builder.create_dfba_species(model,
                                model_fba,
                                compartment_id=compartment_id,
                                hasOnlySubstanceUnits=True,
                                unit_amount=UNIT_AMOUNT,
                                create_port=False)
    # dummy species
    builder.create_dummy_species(model,
                                 compartment_id=compartment_id,
                                 hasOnlySubstanceUnits=True,
                                 unit_amount=UNIT_AMOUNT)

    # exchange flux bounds
    builder.create_exchange_bounds(model,
                                   model_fba=model_fba,
                                   unit_flux=UNIT_FLUX,
                                   create_ports=False)

    # dummy reactions & flux assignments
    builder.create_dummy_reactions(model,
                                   model_fba=model_fba,
                                   unit_flux=UNIT_FLUX)

    # replacedBy (fba reactions)
    builder.create_top_replacedBy(model, model_fba=model_fba)

    # replaced
    builder.create_top_replacements(model,
                                    model_fba,
                                    compartment_id=compartment_id)

    # initial concentrations for fba exchange species
    initial_c = {
        'A': 10.0,
        'C': 0.0,
    }
    for sid, value in initial_c.items():
        species = model.getSpecies(sid)
        species.setInitialConcentration(value)

    # kinetic model
    mc.create_objects(
        model,
        [
            # kinetic species
            mc.Species(sid='D',
                       initialConcentration=0,
                       substanceUnit=UNIT_AMOUNT,
                       hasOnlySubstanceUnits=True,
                       compartment="extern"),

            # kinetic
            mc.Parameter(sid="k_R4",
                         value=0.1,
                         constant=True,
                         unit="per_s",
                         sboTerm="SBO:0000009"),

            # bounds parameter
            mc.Parameter(sid='ub_R1',
                         value=1.0,
                         unit=UNIT_FLUX,
                         constant=False,
                         sboTerm="SBO:0000625"),
        ])
    # kinetic reaction (MMK)
    mc.create_reaction(model,
                       rid="R4",
                       name="R4: C -> D",
                       fast=False,
                       reversible=False,
                       reactants={"C": 1},
                       products={"D": 1},
                       formula="k_R4*C",
                       compartment="extern")

    # kinetic flux bounds
    comp.replace_elements(model,
                          'ub_R1',
                          ref_type=comp.SBASE_REF_TYPE_PORT,
                          replaced_elements={
                              'bounds': ['ub_R1_port'],
                              'fba': ['ub_R1_port']
                          })

    # write SBML file
    if annotations:
        annotator.annotate_sbml_doc(doc, annotations)
    sbml.write_sbml(doc,
                    filepath=os.path.join(directory, sbml_file),
                    validate=True)

    # change back the working dir
    os.chdir(working_dir)
コード例 #5
0
def create_top_level_model(sbml_file):
    """
    Creates the full comp model as combination of FBA and comp models.

    connections
    ------------
    [1] flux bounds
    kinetic reaction bounds => replace the FBA bounds
    comp_ode.submodel_bounds__ub_R1 => fba_R1.upper_bound

    [2] reaction rates (fluxes)
    FBA flux (all reactions) => replaces Reaction flux in kinetic model

    ** FBA **
    <listOfParameters>
        <parameter id="ub_R1" name="ub R1" value="1" units="item_per_s" constant="false"/>
        <parameter id="lb" name="lower bound" value="0" units="item_per_s" constant="true"/>
        <parameter id="ub" name="upper bound" value="1000" units="item_per_s" constant="true"/>
        <parameter id="v_R1" name="R1 flux" value="0" units="item_per_s" constant="false"/>
        <parameter id="v_R2" name="R2 flux" value="0" units="item_per_s" constant="false"/>
        <parameter id="v_R3" name="R3 flux" value="0" units="item_per_s" constant="false"/>
    </listOfParameters>
    """
    sbmlns = SBMLNamespaces(3, 1, "comp", 1)
    doc = SBMLDocument(sbmlns)
    doc.setPackageRequired("comp", True)
    doc.setPackageRequired("fbc", False)

    mdoc = doc.getPlugin("comp")

    # create listOfExternalModelDefinitions
    emd_bounds = comp.create_ExternalModelDefinition(mdoc,
                                                     "toy_ode_bounds",
                                                     sbml_file=ode_bounds_file)
    emd_fba = comp.create_ExternalModelDefinition(mdoc,
                                                  "toy_fba",
                                                  sbml_file=fba_file)
    emd_update = comp.create_ExternalModelDefinition(mdoc,
                                                     "toy_ode_update",
                                                     sbml_file=ode_update_file)
    emd_model = comp.create_ExternalModelDefinition(mdoc,
                                                    "toy_ode_model",
                                                    sbml_file=ode_model_file)

    # create models and submodels
    model = doc.createModel()
    model.setId("toy_top_level")
    model.setName("Top level model")
    model_factory.add_generic_info(model)
    mplugin = model.getPlugin("comp")
    model.setSBOTerm(comp.SBO_CONTINOUS_FRAMEWORK)

    # add submodel which references the external model definition
    comp.add_submodel_from_emd(mplugin, submodel_sid="bounds", emd=emd_bounds)
    comp.add_submodel_from_emd(mplugin, submodel_sid="fba", emd=emd_fba)
    comp.add_submodel_from_emd(mplugin, submodel_sid="update", emd=emd_update)
    comp.add_submodel_from_emd(mplugin, submodel_sid="model", emd=emd_model)

    # --- compartments ---
    create_compartments(model, [{
        A_ID: "extern",
        A_NAME: "external compartment",
        A_VALUE: 1.0,
        A_SPATIAL_DIMENSION: 3,
        A_UNIT: model_factory.UNIT_VOLUME
    }, {
        A_ID: 'cell',
        A_NAME: 'cell',
        A_VALUE: 1.0,
        A_SPATIAL_DIMENSION: 3,
        A_UNIT: model_factory.UNIT_VOLUME
    }])

    # --- species ---
    # replaced species
    # (fba species are not replaced, because they need their boundaryConditions for the FBA,
    #    and do not depend on the actual concentrations)
    create_species(model, [
        {
            A_ID: 'C',
            A_NAME: "C",
            A_VALUE: 0,
            A_UNIT: model_factory.UNIT_AMOUNT,
            A_HAS_ONLY_SUBSTANCE_UNITS: True,
            A_COMPARTMENT: "extern"
        },
    ])

    # --- parameters ---
    create_parameters(
        model,
        [
            # bounds
            {
                A_ID: 'ub_R1',
                A_VALUE: 1.0,
                A_UNIT: model_factory.UNIT_FLUX,
                A_NAME: 'ub_R1',
                A_CONSTANT: False
            },
            {
                A_ID: "vR3",
                A_NAME: "vR3 (FBA flux)",
                A_VALUE: 0.1,
                A_UNIT: model_factory.UNIT_FLUX,
                A_CONSTANT: False
            }
        ])

    # --- reactions ---
    # dummy reaction in top model
    r1 = create_reaction(model,
                         rid="R3",
                         name="R3 dummy",
                         fast=False,
                         reversible=True,
                         reactants={},
                         products={"C": 1},
                         compartment="extern")
    # assignment rule
    create_assignment_rules(model, [{A_ID: "vR3", A_VALUE: "R3"}])

    # --- replacements ---
    # replace compartments
    comp.replace_elements(model,
                          'extern',
                          ref_type=comp.SBASE_REF_TYPE_PORT,
                          replaced_elements={
                              'fba': ['extern_port'],
                              'update': ['extern_port'],
                              'model': ['extern_port']
                          })

    comp.replace_elements(model,
                          'cell',
                          ref_type=comp.SBASE_REF_TYPE_PORT,
                          replaced_elements={'fba': ['cell_port']})

    # replace parameters
    comp.replace_elements(model,
                          'ub_R1',
                          ref_type=comp.SBASE_REF_TYPE_PORT,
                          replaced_elements={
                              'bounds': ['ub_R1_port'],
                              'fba': ['ub_R1_port']
                          })
    comp.replace_elements(model,
                          'vR3',
                          ref_type=comp.SBASE_REF_TYPE_PORT,
                          replaced_elements={'update': ['vR3_port']})

    # replace species
    comp.replace_elements(model,
                          'C',
                          ref_type=comp.SBASE_REF_TYPE_PORT,
                          replaced_elements={
                              'fba': ['C_port'],
                              'update': ['C_port'],
                              'model': ['C_port']
                          })

    # replace reaction by fba reaction
    comp.replaced_by(model,
                     'R3',
                     ref_type=comp.SBASE_REF_TYPE_PORT,
                     submodel='fba',
                     replaced_by="R3_port")

    # replace units
    for uid in [
            's', 'kg', 'm3', 'm2', 'mM', 'item_per_m3', 'm', 'per_s',
            'item_per_s'
    ]:
        comp.replace_element_in_submodels(
            model,
            uid,
            ref_type=comp.SBASE_REF_TYPE_UNIT,
            submodels=['bounds', 'fba', 'update', 'model'])

    # write SBML file
    sbml_io.write_and_check(doc, sbml_file)