示例#1
0
def update_model(sbml_file, directory, doc_fba=None, annotations=None):
    """ Update model.
    """
    update_notes = notes.format("""
        <h2>UPDATE submodel</h2>
        <p>Submodel for dynamically updating the metabolite count.
        This updates the ode model based on the FBA fluxes.</p>
        """)
    doc = builder.template_doc_update(settings.MODEL_ID)
    model = doc.getModel()
    utils.set_model_info(model,
                         notes=update_notes,
                         creators=creators,
                         units=units, main_units=main_units)

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

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

    # update reactions
    builder.create_update_reactions(model, model_fba=model_fba, formula="-{}", unit_flux=UNIT_FLUX,
                                    modifiers=[])


    # write SBML file
    if annotations:
        annotator.annotate_sbml_doc(doc, annotations)
    sbmlio.write_sbml(doc, filepath=os.path.join(directory, sbml_file), validate=True)
示例#2
0
def bounds_model(sbml_file, directory, doc_fba, annotations=None):
    """"
    Bounds model.
    """
    bounds_notes = notes.format("""
    <h2>BOUNDS submodel</h2>
    <p>Submodel for dynamically calculating the flux bounds.
    The dynamically changing flux bounds are the input to the
    FBA model.</p>
    """)
    doc = builder.template_doc_bounds(settings.MODEL_ID)
    model = doc.getModel()
    utils.set_model_info(model,
                         notes=bounds_notes,
                         creators=creators,
                         units=units, main_units=main_units)

    builder.create_dfba_dt(model, step_size=DT_SIM, time_unit=UNIT_TIME, create_port=True)

    # compartment
    compartment_id = 'extern'
    builder.create_dfba_compartment(model, compartment_id=compartment_id, unit_volume=UNIT_VOLUME,
                                    create_port=True)

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

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


    objects = [
        # exchange bounds
        # FIXME: readout the FBA network bounds
        mc.Parameter(sid="lb_default", value=builder.LOWER_BOUND_DEFAULT, unit=UNIT_FLUX, constant=True),

        # kinetic bound parameter & calculation
        mc.Parameter(sid='ub_R1', value=1.0, unit=UNIT_FLUX, constant=False, sboTerm="SBO:0000625"),
        mc.Parameter(sid='k1', value=-0.2, unit="per_s", name="k1", constant=False),
        mc.RateRule(sid="ub_R1", value="k1*ub_R1"),

        # bound assignment rules
        mc.AssignmentRule(sid="lb_EX_A", value='max(lb_default, -A/dt)'),
        mc.AssignmentRule(sid="lb_EX_C", value='max(lb_default, -C/dt)'),
    ]
    mc.create_objects(model, objects)

    # ports
    comp.create_ports(model, portType=comp.PORT_TYPE_PORT,
                      idRefs=["ub_R1"])
    if annotations:
        annotator.annotate_sbml_doc(doc, annotations)
    sbmlio.write_sbml(doc, filepath=os.path.join(directory, sbml_file), validate=True)
示例#3
0
def bounds_model(sbml_file, directory, doc_fba, annotations=None):
    """"
    Bounds model.
    """
    bounds_notes = notes.format("""
    <h2>BOUNDS submodel</h2>
    <p>Submodel for dynamically calculating the flux bounds.
    The dynamically changing flux bounds are the input to the
    FBA model.</p>
    """)
    doc = builder.template_doc_bounds(settings.MODEL_ID)
    model = doc.getModel()
    utils.set_model_info(model,
                         notes=bounds_notes,
                         creators=creators,
                         units=units,
                         main_units=main_units)
    builder.create_dfba_dt(model,
                           step_size=DT_SIM,
                           time_unit=UNIT_TIME,
                           create_port=True)

    # compartment
    compartment_id = 'cell'
    builder.create_dfba_compartment(model,
                                    compartment_id=compartment_id,
                                    unit_volume=UNIT_VOLUME,
                                    create_port=True)

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

    # exchange bounds
    builder.create_exchange_bounds(model_bounds=model,
                                   model_fba=model_fba,
                                   unit_flux=UNIT_FLUX,
                                   create_ports=True)
    builder.create_dynamic_bounds(model_bounds=model,
                                  model_fba=model_fba,
                                  unit_flux=UNIT_FLUX)

    # annotations
    if annotations:
        annotator.annotate_sbml_doc(doc, annotations)

    sbmlio.write_sbml(doc,
                      filepath=os.path.join(directory, sbml_file),
                      validate=True)
示例#4
0
def update_model(sbml_file, directory, doc_fba=None, annotations=None):
    """
        Submodel for dynamically updating the metabolite count/concentration.
        This updates the ode model based on the FBA fluxes.
    """
    update_notes = notes.format("""
        <h2>UPDATE submodel</h2>
        <p>Submodel for dynamically updating the metabolite count.
        This updates the ode model based on the FBA fluxes.</p>
        """)
    doc = builder.template_doc_update(settings.MODEL_ID)
    model = doc.getModel()
    utils.set_model_info(model,
                         notes=update_notes,
                         creators=creators,
                         units=units,
                         main_units=main_units)

    # compartment
    compartment_id = "bioreactor"
    builder.create_dfba_compartment(model,
                                    compartment_id=compartment_id,
                                    unit_volume=UNIT_VOLUME,
                                    create_port=True)

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

    # update reactions
    # FIXME: weight with X (biomass)
    builder.create_update_reactions(model,
                                    model_fba=model_fba,
                                    formula="-{} * X * 1 l_per_mmol",
                                    unit_flux=UNIT_FLUX,
                                    modifiers=["X"])

    # write SBML file
    if annotations:
        annotator.annotate_sbml_doc(doc, annotations)
    sbml.write_sbml(doc, filepath=pjoin(directory, sbml_file), validate=True)
示例#5
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 = "cell"
    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=False,
                                unit_amount=UNIT_AMOUNT,
                                create_port=False)
    # dummy species
    builder.create_dummy_species(model,
                                 compartment_id=compartment_id,
                                 hasOnlySubstanceUnits=False,
                                 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)

    objects = [
        # kinetic parameters
        mc.Parameter(sid="Vmax_RATP", value=1, unit=UNIT_FLUX, constant=True),
        mc.Parameter(sid='k_RATP',
                     value=0.1,
                     unit=UNIT_CONCENTRATION,
                     constant=True),

        # balancing rules
        mc.AssignmentRule(sid="atp_tot",
                          value="atp + adp",
                          unit=UNIT_CONCENTRATION),
        mc.AssignmentRule(sid="c3_tot",
                          value="2 dimensionless * glc + pyr",
                          unit="mM")
    ]
    mc.create_objects(model, objects)

    ratp = mc.create_reaction(model,
                              rid="RATP",
                              name="atp -> adp",
                              fast=False,
                              reversible=False,
                              reactants={"atp": 1},
                              products={"adp": 1},
                              compartment=compartment_id,
                              formula='Vmax_RATP * atp/(k_RATP + atp)')

    # initial concentrations for fba exchange species
    initial_c = {'atp': 2.0, 'adp': 1.0, 'glc': 5.0, 'pyr': 0.0}
    for sid, value in initial_c.items():
        species = model.getSpecies(sid)
        species.setInitialConcentration(value)

    # write SBML file
    if annotations:
        annotator.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)
示例#6
0
def fba_model(sbml_file, directory, annotations=None):
    """ FBA model
    
    :param sbml_file: output file name 
    :param directory: output directory
    :return: SBMLDocument
    """
    fba_notes = notes.format("""
    <h2>FBA submodel</h2>
    <p>DFBA fba submodel. Unbalanced metabolites are encoded via exchange fluxes.</p>
    """)
    doc = builder.template_doc_fba(settings.MODEL_ID)
    model = doc.getModel()
    utils.set_model_info(model,
                         notes=fba_notes,
                         creators=creators,
                         units=units,
                         main_units=main_units)

    objects = [
        # compartments
        mc.Compartment(sid='cell',
                       value=1.0,
                       unit=UNIT_VOLUME,
                       constant=True,
                       name='cell',
                       spatialDimensions=3),

        # exchange species
        mc.Species(sid='atp',
                   name="ATP",
                   initialConcentration=0,
                   substanceUnit=UNIT_AMOUNT,
                   hasOnlySubstanceUnits=False,
                   compartment="cell"),
        mc.Species(sid='adp',
                   name="ADP",
                   initialConcentration=0,
                   substanceUnit=UNIT_AMOUNT,
                   hasOnlySubstanceUnits=False,
                   compartment="cell"),
        mc.Species(sid='glc',
                   name="Glucose",
                   initialConcentration=0,
                   substanceUnit=UNIT_AMOUNT,
                   hasOnlySubstanceUnits=False,
                   compartment="cell"),
        mc.Species(sid='pyr',
                   name='Pyruvate',
                   initialConcentration=0,
                   substanceUnit=UNIT_AMOUNT,
                   hasOnlySubstanceUnits=False,
                   compartment="cell"),

        # internal species
        mc.Species(sid='fru16bp',
                   name='Fructose 1,6-bisphospate',
                   initialConcentration=0,
                   substanceUnit=UNIT_AMOUNT,
                   hasOnlySubstanceUnits=False,
                   compartment="cell"),
        mc.Species(sid='pg2',
                   name='2-Phosphoglycerate',
                   initialConcentration=0,
                   substanceUnit=UNIT_AMOUNT,
                   hasOnlySubstanceUnits=False,
                   compartment="cell"),

        # bounds
        mc.Parameter(sid="ub_R3",
                     value=1.0,
                     unit=UNIT_FLUX,
                     constant=True,
                     sboTerm=builder.FLUX_BOUND_SBO),
        mc.Parameter(sid="zero",
                     value=0.0,
                     unit=UNIT_FLUX,
                     constant=True,
                     sboTerm=builder.FLUX_BOUND_SBO),
        mc.Parameter(sid="ub_default",
                     value=builder.UPPER_BOUND_DEFAULT,
                     unit=UNIT_FLUX,
                     constant=True,
                     sboTerm=builder.FLUX_BOUND_SBO),
    ]
    mc.create_objects(model, objects)

    # reactions
    r1 = mc.create_reaction(model,
                            rid="R1",
                            name="glu + 2 atp -> fru16bp + 2 adp",
                            fast=False,
                            reversible=False,
                            reactants={
                                "glc": 1,
                                "atp": 2
                            },
                            products={
                                "fru16bp": 1,
                                'adp': 2
                            },
                            compartment='cell')
    r2 = mc.create_reaction(model,
                            rid="R2",
                            name="fru16bp -> 2 pg2",
                            fast=False,
                            reversible=False,
                            reactants={"fru16bp": 1},
                            products={"pg2": 2},
                            compartment='cell')
    r3 = mc.create_reaction(model,
                            rid="R3",
                            name="pg2 + adp -> pyr + atp",
                            fast=False,
                            reversible=False,
                            reactants={
                                "pg2": 1,
                                "adp": 2
                            },
                            products={
                                "pyr": 1,
                                "atp": 2
                            },
                            compartment='cell')

    # flux bounds
    fbc.set_flux_bounds(r1, lb="zero", ub="ub_default")
    fbc.set_flux_bounds(r2, lb="zero", ub="ub_default")
    fbc.set_flux_bounds(r3, lb="zero", ub="ub_R3")
    # fbc.set_flux_bounds(ratp, lb="zero", ub="ub_RATP")

    # exchange reactions
    for sid in ['atp', 'adp', 'glc', 'pyr']:
        builder.create_exchange_reaction(model,
                                         species_id=sid,
                                         flux_unit=UNIT_FLUX)

    # objective function
    model_fbc = model.getPlugin("fbc")
    fbc.create_objective(model_fbc,
                         oid="RATP_maximize",
                         otype="maximize",
                         fluxObjectives={"R3": 1.0},
                         active=True)

    if annotations:
        annotator.annotate_sbml_doc(doc, annotations)

    # write SBML
    sbmlio.write_sbml(doc,
                      filepath=os.path.join(directory, sbml_file),
                      validate=True)
    return doc
示例#7
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)
示例#8
0
def fba_model(sbml_file, directory, annotations=None):
    """ FBA model
    
    :param sbml_file: output file name 
    :param directory: output directory
    :return: SBMLDocument
    """
    fba_notes = notes.format("""
    <h2>FBA submodel</h2>
    <p>DFBA fba submodel. Unbalanced metabolites are encoded via exchange fluxes.</p>
    """)
    doc = builder.template_doc_fba(settings.MODEL_ID)
    model = doc.getModel()
    utils.set_model_info(model,
                         notes=fba_notes,
                         creators=creators,
                         units=units,
                         main_units=main_units)

    objects = [
        # compartments
        mc.Compartment(sid='extern',
                       value=1.0,
                       unit=UNIT_VOLUME,
                       constant=True,
                       name='external compartment',
                       spatialDimensions=3),
        mc.Compartment(sid='cell',
                       value=1.0,
                       unit=UNIT_VOLUME,
                       constant=True,
                       name='cell',
                       spatialDimensions=3),
        mc.Compartment(sid='membrane',
                       value=1.0,
                       unit=UNIT_AREA,
                       constant=True,
                       name='membrane',
                       spatialDimensions=2),

        # exchange species
        mc.Species(sid='A',
                   name="A",
                   initialConcentration=0,
                   substanceUnit=UNIT_AMOUNT,
                   hasOnlySubstanceUnits=True,
                   compartment="extern"),
        mc.Species(sid='C',
                   name="C",
                   initialConcentration=0,
                   substanceUnit=UNIT_AMOUNT,
                   hasOnlySubstanceUnits=True,
                   compartment="extern"),

        # internal species
        mc.Species(sid='B1',
                   name="B1",
                   initialConcentration=0,
                   substanceUnit=UNIT_AMOUNT,
                   hasOnlySubstanceUnits=True,
                   compartment="cell"),
        mc.Species(sid='B2',
                   name="B2",
                   initialConcentration=0,
                   substanceUnit=UNIT_AMOUNT,
                   hasOnlySubstanceUnits=True,
                   compartment="cell"),

        # bounds
        mc.Parameter(sid="ub_R1",
                     value=1.0,
                     unit=UNIT_FLUX,
                     constant=True,
                     sboTerm=builder.FLUX_BOUND_SBO),
        mc.Parameter(sid="zero",
                     value=0.0,
                     unit=UNIT_FLUX,
                     constant=True,
                     sboTerm=builder.FLUX_BOUND_SBO),
        mc.Parameter(sid="ub_default",
                     value=builder.UPPER_BOUND_DEFAULT,
                     unit=UNIT_FLUX,
                     constant=True,
                     sboTerm=builder.FLUX_BOUND_SBO),
    ]
    mc.create_objects(model, objects)

    # reactions
    r1 = mc.create_reaction(model,
                            rid="R1",
                            name="A import (R1)",
                            fast=False,
                            reversible=True,
                            reactants={"A": 1},
                            products={"B1": 1},
                            compartment='membrane')
    r2 = mc.create_reaction(model,
                            rid="R2",
                            name="B1 <-> B2 (R2)",
                            fast=False,
                            reversible=True,
                            reactants={"B1": 1},
                            products={"B2": 1},
                            compartment='cell')
    r3 = mc.create_reaction(model,
                            rid="R3",
                            name="B2 export (R3)",
                            fast=False,
                            reversible=True,
                            reactants={"B2": 1},
                            products={"C": 1},
                            compartment='membrane')

    # flux bounds
    fbc.set_flux_bounds(r1, lb="zero", ub="ub_R1")
    fbc.set_flux_bounds(r2, lb="zero", ub="ub_default")
    fbc.set_flux_bounds(r3, lb="zero", ub="ub_default")

    # exchange reactions
    builder.create_exchange_reaction(model,
                                     species_id="A",
                                     flux_unit=UNIT_FLUX)
    builder.create_exchange_reaction(model,
                                     species_id="C",
                                     flux_unit=UNIT_FLUX)

    # objective function
    model_fbc = model.getPlugin("fbc")
    fbc.create_objective(model_fbc,
                         oid="R3_maximize",
                         otype="maximize",
                         fluxObjectives={"R3": 1.0},
                         active=True)

    # create ports for kinetic bounds
    comp.create_ports(model, portType=comp.PORT_TYPE_PORT, idRefs=["ub_R1"])

    # write SBML
    if annotations:
        annotator.annotate_sbml_doc(doc, annotations)
    sbml.write_sbml(doc,
                    filepath=os.path.join(directory, sbml_file),
                    validate=True)
    return doc
示例#9
0
def top_model(sbml_file, directory, emds, doc_fba=None, annotations=None):
    """
    Create diauxic comp model.
    Test script for working with the comp extension in SBML.

    One model composition combines all the kinetic models,
    in addition the higher level comp model is created which combines everything (i.e. the FBA & ODE models).
    For the simulation of the full combined model the tools have to figure out the subparts which are
    simulated with which simulation environment.
    Creates the full comp model as combination of FBA and comp models.

    The submodels must already exist in the given directory
    """
    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>
    """)
    # Necessary to change into directory with submodel files
    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, time_unit=UNIT_TIME, create_port=False)

    # compartment
    compartment_id = "bioreactor"
    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,
                                unit_amount=UNIT_AMOUNT,
                                create_port=False)

    # dummy species
    builder.create_dummy_species(model,
                                 compartment_id=compartment_id,
                                 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 kinetic concentrations
    initial_c = {
        'Glcxt': 10.8,
        'Ac': 0.4,
        'O2': 0.21,
        'X': 0.001,
    }
    for sid, value in initial_c.items():
        species = model.getSpecies(sid)
        species.setInitialConcentration(value)

    objects = [
        # biomass conversion factor
        # Parameter(sid="Y", name="biomass [g_per_l]", value=1.0, unit="g_per_l"),
        # oxygen exchange parameters
        Parameter(sid="O2_ref",
                  name="O2 reference",
                  value=0.21,
                  unit=UNIT_CONCENTRATION),
        Parameter(sid="kLa", name="O2 mass transfer", value=7.5, unit='per_h'),
    ]
    factory.create_objects(model, objects)

    # oxygen transfer reaction
    create_reaction(model,
                    rid="vO2_transfer",
                    name="oxygen transfer",
                    reversible=True,
                    reactants={},
                    products={"O2": 1},
                    formula="kLa * (O2_ref-O2) * bioreactor",
                    compartment="bioreactor")

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

    # change back into working dir
    os.chdir(working_dir)
示例#10
0
def bounds_model(sbml_file, directory, doc_fba=None, annotations=None):
    """"
    Submodel for dynamically calculating the flux bounds.

    The dynamically changing flux bounds are the input to the
    FBA model.
    """
    # TODO: the bounds model should be created based on the FBA model (i.e. use the exchange reactions
    # to create the bounds info.

    bounds_notes = notes.format("""
    <h2>BOUNDS submodel</h2>
    <p>Submodel for dynamically calculating the flux bounds.
    The dynamically changing flux bounds are the input to the
    FBA model.</p>
    """)
    doc = builder.template_doc_bounds(settings.MODEL_ID)
    model = doc.getModel()
    utils.set_model_info(model,
                         notes=bounds_notes,
                         creators=creators,
                         units=units,
                         main_units=main_units)

    # dt
    compartment_id = "bioreactor"
    builder.create_dfba_dt(model, time_unit=UNIT_TIME, create_port=True)

    # compartment
    builder.create_dfba_compartment(model,
                                    compartment_id=compartment_id,
                                    unit_volume=UNIT_VOLUME,
                                    create_port=True)

    # dynamic species
    model_fba = doc_fba.getModel()
    builder.create_dfba_species(model,
                                model_fba,
                                compartment_id=compartment_id,
                                unit_amount=UNIT_AMOUNT,
                                create_port=True)
    # bounds
    builder.create_exchange_bounds(model,
                                   model_fba=model_fba,
                                   unit_flux=UNIT_FLUX,
                                   create_ports=True)

    # bounds
    fba_infix = "fba_"
    model_fba = doc_fba.getModel()
    objects = []
    ex_rids = utils.find_exchange_reactions(model_fba)
    for ex_rid, sid in ex_rids.items():
        r = model_fba.getReaction(ex_rid)

        # lower & upper bound parameters
        r_fbc = r.getPlugin(builder.SBML_FBC_NAME)
        lb_id = r_fbc.getLowerFluxBound()
        fba_lb_id = builder.LOWER_BOUND_PREFIX + fba_infix + ex_rid
        lb_value = model_fba.getParameter(lb_id).getValue()

        objects.extend([
            # default bounds from fba
            Parameter(sid=fba_lb_id,
                      value=lb_value,
                      unit=UNIT_FLUX,
                      constant=False),
        ])
    factory.create_objects(model, objects)

    objects = [

        # kinetic lower bounds
        Parameter(sid="lb_kin_EX_Glcxt",
                  value=builder.LOWER_BOUND_DEFAULT,
                  unit=UNIT_FLUX,
                  constant=False,
                  sboTerm="SBO:0000612"),
        Parameter(sid="lb_kin_EX_O2",
                  value=builder.LOWER_BOUND_DEFAULT,
                  unit=UNIT_FLUX,
                  constant=False,
                  sboTerm="SBO:0000612"),

        # parameters for kinetic bounds
        Parameter(sid='Vmax_EX_O2', value=15, unit=UNIT_FLUX, constant=True),
        Parameter(sid='Vmax_EX_Glcxt', value=10, unit=UNIT_FLUX,
                  constant=True),
        Parameter(sid='Km_EX_Glcxt',
                  value=0.015,
                  unit=UNIT_CONCENTRATION,
                  name="Km_vGlcxt",
                  constant=True),

        # kinetic bounds (unintuitive direction due to the identical concentrations in bioreactor and model)
        AssignmentRule(sid="lb_kin_EX_Glcxt",
                       value="-Vmax_EX_Glcxt * Glcxt/(Km_EX_Glcxt + Glcxt)"),
        AssignmentRule(sid="lb_kin_EX_O2", value="-Vmax_EX_O2"),

        # exchange reaction bounds
        # uptake bounds (lower bound)
        # TODO: FIXME the X hack
        # the bounds for the fba model have to be in mmol/h/gdw
        AssignmentRule(
            sid="lb_EX_Ac",
            value="max(lb_fba_EX_Ac, -Ac/X/1 l_per_mmol*bioreactor/dt)"),
        AssignmentRule(
            sid="lb_EX_X",
            value="max(lb_fba_EX_X, -X/X/1 l_per_mmol*bioreactor/dt)"),
        AssignmentRule(
            sid="lb_EX_Glcxt",
            value="max(lb_kin_EX_Glcxt, -Glcxt/X/1 l_per_mmol*bioreactor/dt)"),
        AssignmentRule(
            sid="lb_EX_O2",
            value="max(lb_kin_EX_O2, -O2/X/1 l_per_mmol*bioreactor/dt)"),
    ]
    factory.create_objects(model, objects)

    if annotations:
        annotator.annotate_sbml_doc(doc, annotations)
    sbmlio.write_sbml(doc, filepath=pjoin(directory, sbml_file), validate=True)
示例#11
0
def fba_model(sbml_file, directory, annotations=None):
    """ Create FBA submodel.

    FBA submodel in sbml:fbc-version 2.
    """
    fba_notes = notes.format("""
    <h2>FBA submodel</h2>
    <p>DFBA fba submodel. Unbalanced metabolites are encoded via exchange fluxes.</p>
    """)
    doc = builder.template_doc_fba(settings.MODEL_ID)
    model = doc.getModel()
    utils.set_model_info(model,
                         notes=fba_notes,
                         creators=creators,
                         units=units,
                         main_units=main_units)

    objects = [
        # compartments
        Compartment(sid='bioreactor',
                    value=1.0,
                    unit=UNIT_VOLUME,
                    constant=True,
                    name='bioreactor',
                    spatialDimensions=3),

        # species
        Species(sid='Glcxt',
                name="glucose",
                initialConcentration=0.0,
                substanceUnit=UNIT_AMOUNT,
                hasOnlySubstanceUnits=False,
                compartment="bioreactor"),
        Species(sid='Ac',
                name="acetate",
                initialConcentration=0.0,
                substanceUnit=UNIT_AMOUNT,
                hasOnlySubstanceUnits=False,
                compartment="bioreactor"),
        Species(sid='O2',
                name="oxygen",
                initialConcentration=0.0,
                substanceUnit=UNIT_AMOUNT,
                hasOnlySubstanceUnits=False,
                compartment="bioreactor"),
        Species(sid='X',
                name="biomass",
                initialConcentration=0.0,
                substanceUnit=UNIT_AMOUNT,
                hasOnlySubstanceUnits=False,
                compartment="bioreactor"),

        # bounds
        Parameter(sid="zero",
                  name="zero bound",
                  value=0.0,
                  unit=UNIT_FLUX_PER_G,
                  constant=True,
                  sboTerm="SBO:0000612"),
        Parameter(sid="ub_default",
                  name="default upper bound",
                  value=builder.UPPER_BOUND_DEFAULT,
                  unit=UNIT_FLUX_PER_G,
                  constant=True,
                  sboTerm="SBO:0000612"),
        Reaction(sid="v1",
                 name="v1 (39.43 Ac + 35 O2 -> X)",
                 reversible=False,
                 equation="39.43 Ac + 35 O2 -> X",
                 compartment='bioreactor',
                 lowerFluxBound="zero",
                 upperFluxBound="ub_default"),
        Reaction(sid="v2",
                 name="v2 (9.46 Glcxt + 12.92 O2 -> X)",
                 reversible=False,
                 equation="9.46 Glcxt + 12.92 O2 -> X",
                 compartment='bioreactor',
                 lowerFluxBound="zero",
                 upperFluxBound="ub_default"),
        Reaction(sid="v3",
                 name="v3 (9.84 Glcxt + 12.73 O2 -> 1.24 Ac + X)",
                 reversible=False,
                 equation="9.84 Glcxt + 12.73 O2 -> 1.24 Ac + X",
                 compartment='bioreactor',
                 lowerFluxBound="zero",
                 upperFluxBound="ub_default"),
        Reaction(sid="v4",
                 name="v4 (19.23 Glcxt -> 12.12 Ac + X)",
                 reversible=False,
                 equation="19.23 Glcxt -> 12.12 Ac + X",
                 compartment='bioreactor',
                 lowerFluxBound="zero",
                 upperFluxBound="ub_default"),
    ]
    factory.create_objects(model, objects)

    # reactions: exchange reactions (this species can be changed by the FBA)
    for sid in ['Ac', 'Glcxt', 'O2', 'X']:
        builder.create_exchange_reaction(model,
                                         species_id=sid,
                                         flux_unit=UNIT_FLUX_PER_G,
                                         exchange_type=builder.EXCHANGE)
    # set bounds for the exchange reactions
    p_lb_O2 = model.getParameter("lb_EX_O2")
    p_lb_O2.setValue(
        -15.0)  # FIXME: this is in mmol/gdw/h (biomass weighting of FBA)
    p_lb_Glcxt = model.getParameter("lb_EX_Glcxt")
    p_lb_Glcxt.setValue(-10.0)  # FIXME: this is in mmol/gdw/h

    # objective function
    model_fba = model.getPlugin(builder.SBML_FBC_NAME)
    fbc.create_objective(model_fba,
                         oid="biomass_max",
                         otype="maximize",
                         fluxObjectives={
                             "v1": 1.0,
                             "v2": 1.0,
                             "v3": 1.0,
                             "v4": 1.0
                         })

    # write SBML file
    if annotations:
        annotator.annotate_sbml_doc(doc, annotations)
    sbmlio.write_sbml(doc, filepath=pjoin(directory, sbml_file), validate=True)

    return doc