Example #1
0
def test_create_exchange_reaction():
    print(libsbml.getLibSBMLDottedVersion())
    doc = create_fba_doc()
    model = doc.getModel()

    builder.create_exchange_reaction(model, species_id="A")
    builder.create_exchange_reaction(model, species_id="B")

    assert model.getNumReactions() == 2
    ex_A = model.getReaction(builder.EXCHANGE_REACTION_PREFIX + "A")
    assert ex_A
    ex_B = model.getReaction(builder.EXCHANGE_REACTION_PREFIX + "B")
    assert ex_B

    comp_model = model.getPlugin("comp")
    ports = comp_model.getListOfPorts()
    assert ports is not None
    assert comp_model.getNumPorts() == 6
    assert comp_model.getPort(builder.EXCHANGE_REACTION_PREFIX + "A_port")
    assert comp_model.getPort(builder.EXCHANGE_REACTION_PREFIX + "B_port")
    assert comp_model.getPort(builder.LOWER_BOUND_PREFIX + builder.EXCHANGE_REACTION_PREFIX + "A_port")
    assert comp_model.getPort(builder.LOWER_BOUND_PREFIX + builder.EXCHANGE_REACTION_PREFIX + "B_port")
    assert comp_model.getPort(builder.UPPER_BOUND_PREFIX + builder.EXCHANGE_REACTION_PREFIX + "A_port")
    assert comp_model.getPort(builder.UPPER_BOUND_PREFIX + builder.EXCHANGE_REACTION_PREFIX + "B_port")

    fbc_ex_A = ex_A.getPlugin("fbc")
    assert fbc_ex_A
    fbc_ex_B = ex_B.getPlugin("fbc")
    assert fbc_ex_B
Example #2
0
def test_create_exchange_reaction():
    print(libsbml.getLibSBMLDottedVersion())
    doc = create_fba_doc()
    model = doc.getModel()

    builder.create_exchange_reaction(model, species_id="A")
    builder.create_exchange_reaction(model, species_id="B")

    assert model.getNumReactions() == 2
    ex_A = model.getReaction(builder.EXCHANGE_REACTION_PREFIX + "A")
    assert ex_A
    ex_B = model.getReaction(builder.EXCHANGE_REACTION_PREFIX + "B")
    assert ex_B

    comp_model = model.getPlugin("comp")
    ports = comp_model.getListOfPorts()
    assert ports is not None
    assert comp_model.getNumPorts() == 6
    assert comp_model.getPort(builder.EXCHANGE_REACTION_PREFIX + "A_port")
    assert comp_model.getPort(builder.EXCHANGE_REACTION_PREFIX + "B_port")
    assert comp_model.getPort(builder.LOWER_BOUND_PREFIX +
                              builder.EXCHANGE_REACTION_PREFIX + "A_port")
    assert comp_model.getPort(builder.LOWER_BOUND_PREFIX +
                              builder.EXCHANGE_REACTION_PREFIX + "B_port")
    assert comp_model.getPort(builder.UPPER_BOUND_PREFIX +
                              builder.EXCHANGE_REACTION_PREFIX + "A_port")
    assert comp_model.getPort(builder.UPPER_BOUND_PREFIX +
                              builder.EXCHANGE_REACTION_PREFIX + "B_port")

    fbc_ex_A = ex_A.getPlugin("fbc")
    assert fbc_ex_A
    fbc_ex_B = ex_B.getPlugin("fbc")
    assert fbc_ex_B
Example #3
0
def test_create_directed_exchange_reaction():
    doc = create_fba_doc()
    model = doc.getModel()

    builder.create_exchange_reaction(model,
                                     species_id="A",
                                     exchange_type=builder.EXCHANGE_IMPORT)
    builder.create_exchange_reaction(model,
                                     species_id="B",
                                     exchange_type=builder.EXCHANGE_EXPORT)

    assert model.getNumReactions() == 2

    # import
    ex_A = model.getReaction(builder.EXCHANGE_REACTION_PREFIX + "A")
    assert ex_A
    fbc_ex_A = ex_A.getPlugin("fbc")
    assert fbc_ex_A
    pid_ub = fbc_ex_A.getUpperFluxBound()
    p_ub = model.getParameter(pid_ub)
    assert p_ub
    assert p_ub.value == builder.ZERO_BOUND
    pid_lb = fbc_ex_A.getLowerFluxBound()
    p_lb = model.getParameter(pid_lb)
    assert p_lb
    assert p_lb.value == builder.LOWER_BOUND_DEFAULT

    # export
    ex_B = model.getReaction(builder.EXCHANGE_REACTION_PREFIX + "B")
    assert ex_B
    fbc_ex_B = ex_B.getPlugin("fbc")
    assert fbc_ex_B
    pid_ub = fbc_ex_B.getUpperFluxBound()
    p_ub = model.getParameter(pid_ub)
    assert p_ub
    assert p_ub.value == builder.UPPER_BOUND_DEFAULT
    pid_lb = fbc_ex_B.getLowerFluxBound()
    p_lb = model.getParameter(pid_lb)
    assert p_lb
    assert p_lb.value == builder.ZERO_BOUND
Example #4
0
def test_create_directed_exchange_reaction():
    doc = create_fba_doc()
    model = doc.getModel()

    builder.create_exchange_reaction(model, species_id="A", exchange_type=builder.EXCHANGE_IMPORT)
    builder.create_exchange_reaction(model, species_id="B", exchange_type=builder.EXCHANGE_EXPORT)

    assert model.getNumReactions() == 2

    # import
    ex_A = model.getReaction(builder.EXCHANGE_REACTION_PREFIX + "A")
    assert ex_A
    fbc_ex_A = ex_A.getPlugin("fbc")
    assert fbc_ex_A
    pid_ub = fbc_ex_A.getUpperFluxBound()
    p_ub = model.getParameter(pid_ub)
    assert p_ub
    assert p_ub.value == builder.ZERO_BOUND
    pid_lb = fbc_ex_A.getLowerFluxBound()
    p_lb = model.getParameter(pid_lb)
    assert p_lb
    assert p_lb.value == builder.LOWER_BOUND_DEFAULT

    # export
    ex_B = model.getReaction(builder.EXCHANGE_REACTION_PREFIX + "B")
    assert ex_B
    fbc_ex_B = ex_B.getPlugin("fbc")
    assert fbc_ex_B
    pid_ub = fbc_ex_B.getUpperFluxBound()
    p_ub = model.getParameter(pid_ub)
    assert p_ub
    assert p_ub.value == builder.UPPER_BOUND_DEFAULT
    pid_lb = fbc_ex_B.getLowerFluxBound()
    p_lb = model.getParameter(pid_lb)
    assert p_lb
    assert p_lb.value == builder.ZERO_BOUND
Example #5
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
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:
        annotation.annotate_sbml_doc(doc, annotations)
    sbmlio.write_sbml(doc, filepath=os.path.join(directory, sbml_file), validate=True)
    return doc
Example #7
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
Example #8
0
def fba_model(sbml_file, directory):
    """ Create FBA submodel.
    """
    # Read the model
    fba_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                            'data/ecoli_fba.xml')
    doc_fba = sbml.read_sbml(fba_path)

    # add comp
    doc_fba.enablePackage(
        "http://www.sbml.org/sbml/level3/version1/comp/version1", "comp", True)
    doc_fba.setPackageRequired("comp", True)

    # add notes
    model = doc_fba.getModel()
    fba_notes = notes.format("""DFBA FBA submodel.""")
    utils.set_model_info(model,
                         notes=fba_notes,
                         creators=None,
                         units=units,
                         main_units=main_units)

    # clip R_ reaction and M_ metabolite prefixes
    utils.clip_prefixes_in_model(model)

    # set id & framework
    model.setId('ecoli_fba')
    model.setName('ecoli (FBA)')
    model.setSBOTerm(comp.SBO_FLUX_BALANCE_FRAMEWORK)

    # add units and information
    for species in model.getListOfSpecies():
        species.setInitialConcentration(0.0)
        species.setHasOnlySubstanceUnits(False)
        species.setUnits(UNIT_AMOUNT)
    for compartment in model.getListOfCompartments():
        compartment.setUnits(UNIT_VOLUME)

    # The ATPM (atp maintainance reactions creates many problems in the DFBA)
    # mainly resulting in infeasible solutions when some metabolites run out.
    # ATP -> ADP is part of the biomass, so we set the lower bound to zero
    r_ATPM = model.getReaction('ATPM')
    r_ATPM_fbc = r_ATPM.getPlugin(builder.SBML_FBC_NAME)
    lb_id = r_ATPM_fbc.getLowerFluxBound()
    model.getParameter(lb_id).setValue(0.0)  # 8.39 before

    # make unique upper and lower bounds for exchange reaction
    if not biomass_weighting:
        builder.update_exchange_reactions(model=model, flux_unit=UNIT_FLUX)
    else:
        builder.update_exchange_reactions(model=model,
                                          flux_unit=UNIT_FLUX_PER_G)

    # add exchange reaction for biomass (X)
    # we are adding the biomass component to the biomass function and create an
    # exchange reaction for it
    r_biomass = model.getReaction('BIOMASS_Ecoli_core_w_GAM')

    # FIXME: refactor in function
    # FIXME: annotate biomass species (SBO for biomass missing)
    mc.create_objects(model, [
        mc.Parameter(sid='cf_X',
                     value=1.0,
                     unit="g_per_mmol",
                     name="biomass conversion factor",
                     constant=True),
        mc.Species(sid='X',
                   initialAmount=0.001,
                   compartment='c',
                   name='biomass',
                   substanceUnit='g',
                   hasOnlySubstanceUnits=True,
                   conversionFactor='cf_X')
    ])

    pr_biomass = r_biomass.createProduct()
    pr_biomass.setSpecies('X')
    pr_biomass.setStoichiometry(1.0)
    pr_biomass.setConstant(True)
    # FIXME: the flux units must fit to the species units.
    if not biomass_weighting:
        builder.create_exchange_reaction(model,
                                         species_id='X',
                                         flux_unit=UNIT_FLUX,
                                         exchange_type=builder.EXCHANGE_EXPORT)
    else:
        builder.create_exchange_reaction(model,
                                         species_id='X',
                                         flux_unit=UNIT_FLUX_PER_G,
                                         exchange_type=builder.EXCHANGE_EXPORT)
    # write SBML file
    sbml.write_sbml(doc_fba,
                    filepath=pjoin(directory, sbml_file),
                    validate=True)

    # Set kinetic laws to zero for kinetic simulation
    '''
    for reaction in model.getListOfReactions():
        ast_node = mc.ast_node_from_formula(model=model, formula='0 {}'.format(UNIT_FLUX))
        law = reaction.createKineticLaw()
        law.setMath(ast_node)
    '''

    return doc_fba
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
        mc.Compartment(sid='bioreactor', value=1.0, unit=UNIT_VOLUME, constant=True, name='bioreactor',
                       spatialDimensions=3),

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

        # bounds
        mc.Parameter(sid="zero", name="zero bound", value=0.0, unit=UNIT_FLUX_PER_G, constant=True, sboTerm="SBO:0000612"),
        mc.Parameter(sid="ub_default", name="default upper bound", value=builder.UPPER_BOUND_DEFAULT, unit=UNIT_FLUX_PER_G,
                     constant=True, sboTerm="SBO:0000612"),
    ]
    mc.create_objects(model, objects)

    # reactions
    r_v1 = mc.create_reaction(model, rid="v1", name="v1 (39.43 Ac + 35 O2 -> X)", reversible=False,
                              reactants={"Ac": 39.43, "O2": 35}, products={"X": 1}, compartment='bioreactor')
    r_v2 = mc.create_reaction(model, rid="v2", name="v2 (9.46 Glcxt + 12.92 O2 -> X)", reversible=False,
                              reactants={"Glcxt": 9.46, "O2": 12.92}, products={"X": 1}, compartment='bioreactor')
    r_v3 = mc.create_reaction(model, rid="v3", name="v3 (9.84 Glcxt + 12.73 O2 -> 1.24 Ac + X)", reversible=False,
                              reactants={"Glcxt": 9.84, "O2": 12.73}, products={"Ac": 1.24, "X": 1},
                              compartment='bioreactor')
    r_v4 = mc.create_reaction(model, rid="v4", name="v4 (19.23 Glcxt -> 12.12 Ac + X)", reversible=False,
                              reactants={"Glcxt": 19.23}, products={"Ac": 12.12, "X": 1}, compartment='bioreactor')

    # flux bounds: internal fluxes
    fbc.set_flux_bounds(r_v1, lb="zero", ub="ub_default")
    fbc.set_flux_bounds(r_v2, lb="zero", ub="ub_default")
    fbc.set_flux_bounds(r_v3, lb="zero", ub="ub_default")
    fbc.set_flux_bounds(r_v4, lb="zero", ub="ub_default")

    # 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:
        annotation.annotate_sbml_doc(doc, annotations)
    sbmlio.write_sbml(doc, filepath=pjoin(directory, sbml_file), validate=True)

    return doc
Example #10
0
def fba_model(sbml_file, directory):
    """ Create FBA submodel.
    """
    # Read the model
    fba_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data/ecoli_fba.xml')
    doc_fba = sbmlio.read_sbml(fba_path)

    # add comp
    doc_fba.enablePackage("http://www.sbml.org/sbml/level3/version1/comp/version1", "comp", True)
    doc_fba.setPackageRequired("comp", True)

    # add notes
    model = doc_fba.getModel()
    fba_notes = notes.format("""DFBA FBA submodel.""")
    utils.set_model_info(model, notes=fba_notes,
                         creators=None, units=units, main_units=main_units)

    # clip R_ reaction and M_ metabolite prefixes
    utils.clip_prefixes_in_model(model)

    # set id & framework
    model.setId('ecoli_fba')
    model.setName('ecoli (FBA)')
    model.setSBOTerm(comp.SBO_FLUX_BALANCE_FRAMEWORK)

    # add units and information
    for species in model.getListOfSpecies():
        species.setInitialConcentration(0.0)
        species.setHasOnlySubstanceUnits(False)
        species.setUnits(UNIT_AMOUNT)
    for compartment in model.getListOfCompartments():
        compartment.setUnits(UNIT_VOLUME)


    # The ATPM (atp maintainance reactions creates many problems in the DFBA)
    # mainly resulting in infeasible solutions when some metabolites run out.
    # ATP -> ADP is part of the biomass, so we set the lower bound to zero
    r_ATPM = model.getReaction('ATPM')
    r_ATPM_fbc = r_ATPM.getPlugin(builder.SBML_FBC_NAME)
    lb_id = r_ATPM_fbc.getLowerFluxBound()
    model.getParameter(lb_id).setValue(0.0)  # 8.39 before

    # make unique upper and lower bounds for exchange reaction
    if not biomass_weighting:
        builder.update_exchange_reactions(model=model, flux_unit=UNIT_FLUX)
    else:
        builder.update_exchange_reactions(model=model, flux_unit=UNIT_FLUX_PER_G)

    # add exchange reaction for biomass (X)
    # we are adding the biomass component to the biomass function and create an
    # exchange reaction for it
    r_biomass = model.getReaction('BIOMASS_Ecoli_core_w_GAM')

    # FIXME: refactor in function
    # FIXME: annotate biomass species (SBO for biomass missing)
    mc.create_objects(model, [
        mc.Parameter(sid='cf_X', value=1.0, unit="g_per_mmol", name="biomass conversion factor", constant=True),
        mc.Species(sid='X', initialAmount=0.001, compartment='c', name='biomass', substanceUnit='g', hasOnlySubstanceUnits=True,
                   conversionFactor='cf_X')
    ])


    pr_biomass = r_biomass.createProduct()
    pr_biomass.setSpecies('X')
    pr_biomass.setStoichiometry(1.0)
    pr_biomass.setConstant(True)
    # FIXME: the flux units must fit to the species units.
    if not biomass_weighting:
        builder.create_exchange_reaction(model, species_id='X', flux_unit=UNIT_FLUX, exchange_type=builder.EXCHANGE_EXPORT)
    else:
        builder.create_exchange_reaction(model, species_id='X', flux_unit=UNIT_FLUX_PER_G, exchange_type=builder.EXCHANGE_EXPORT)
    # write SBML file
    sbmlio.write_sbml(doc_fba, filepath=pjoin(directory, sbml_file), validate=True)

    # Set kinetic laws to zero for kinetic simulation
    '''
    for reaction in model.getListOfReactions():
        ast_node = mc.ast_node_from_formula(model=model, formula='0 {}'.format(UNIT_FLUX))
        law = reaction.createKineticLaw()
        law.setMath(ast_node)
    '''

    return doc_fba
Example #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
Example #12
0
# reactions
r1 = factory.create_reaction(model, rid="R1", name="A import (R1)", fast=False, reversible=True,
                        reactants={"A": 1}, products={"B1": 1}, compartment='membrane')
r2 = factory.create_reaction(model, rid="R2", name="B1 <-> B2 (R2)", fast=False, reversible=True,
                        reactants={"B1": 1}, products={"B2": 1}, compartment='cell')
r3 = factory.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)

# write SBML file
import tempfile
sbml_file = tempfile.NamedTemporaryFile(suffix=".xml")
sbmlio.write_sbml(doc=doc, filepath=sbml_file.name)


# In[ ]:
Example #13
0
def test_modelcreator_notebook():
    """
    If this test fails the respective notebook must be updated:
    :return:
    """

    import libsbml
    from libsbml import (UNIT_KIND_SECOND, UNIT_KIND_ITEM, UNIT_KIND_MOLE,
                         UNIT_KIND_KILOGRAM, UNIT_KIND_METRE, UNIT_KIND_LITRE)

    from sbmlutils import comp
    from sbmlutils import fbc
    from sbmlutils import sbmlio
    from sbmlutils import factory as fac
    from sbmlutils.dfba import builder, utils

    main_units = {
        'time': 's',
        'extent': UNIT_KIND_ITEM,
        'substance': UNIT_KIND_ITEM,
        'length': 'm',
        'area': 'm2',
        'volume': 'm3',
    }
    units = [
        fac.Unit('s', [(UNIT_KIND_SECOND, 1.0)]),
        fac.Unit('item', [(UNIT_KIND_ITEM, 1.0)]),
        fac.Unit('kg', [(UNIT_KIND_KILOGRAM, 1.0)]),
        fac.Unit('m', [(UNIT_KIND_METRE, 1.0)]),
        fac.Unit('m2', [(UNIT_KIND_METRE, 2.0)]),
        fac.Unit('m3', [(UNIT_KIND_METRE, 3.0)]),
        fac.Unit('mM', [(UNIT_KIND_MOLE, 1.0, 0),
                        (UNIT_KIND_METRE, -3.0)]),
        fac.Unit('per_s', [(UNIT_KIND_SECOND, -1.0)]),
        fac.Unit('item_per_s', [(UNIT_KIND_ITEM, 1.0),
                                (UNIT_KIND_SECOND, -1.0)]),
        fac.Unit('item_per_m3', [(UNIT_KIND_ITEM, 1.0),
                                 (UNIT_KIND_METRE, -3.0)]),
    ]

    UNIT_TIME = 's'
    UNIT_AMOUNT = 'item'
    UNIT_AREA = 'm2'
    UNIT_VOLUME = 'm3'
    UNIT_CONCENTRATION = 'item_per_m3'
    UNIT_FLUX = 'item_per_s'

    # Create SBMLDocument with fba
    doc = builder.template_doc_fba(model_id="toy")
    model = doc.getModel()

    utils.set_units(model, units)
    utils.set_main_units(model, main_units)

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

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

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

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

    # reactions
    r1 = fac.create_reaction(model, rid="R1", name="A import (R1)", fast=False, reversible=True,
                             reactants={"A": 1}, products={"B1": 1}, compartment='membrane')
    r2 = fac.create_reaction(model, rid="R2", name="B1 <-> B2 (R2)", fast=False, reversible=True,
                             reactants={"B1": 1}, products={"B2": 1}, compartment='cell')
    r3 = fac.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")
    fac.create_objective(model_fbc, oid="R3_maximize", otype="maximize",
                         fluxObjectives={"R3": 1.0}, active=True)

    # write SBML file
    import tempfile
    sbml_file = tempfile.NamedTemporaryFile(suffix=".xml")
    sbmlio.write_sbml(doc=doc, filepath=sbml_file.name)
Example #14
0
    # reactions
    Reaction(sid="R1", name="A import (R1)",
             equation="A <-> B1", compartment='membrane', lowerFluxBound="zero", upperFluxBound="ub_R1"),
    Reaction(sid="R2", name="B1 <-> B2 (R2)",
             equation="B1 <-> B2", compartment='cell',
             lowerFluxBound="zero", upperFluxBound="ub_default"),
    Reaction(sid="R3", name="B2 export (R3)", fast=False, reversible=True,
             equation="B2 <-> C", compartment='membrane',
             lowerFluxBound="zero", upperFluxBound="ub_default"
    ),
    
]
factory.create_objects(model, objects)

# 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)

# write SBML file
import tempfile
sbml_file = tempfile.NamedTemporaryFile(suffix=".xml")
sbmlio.write_sbml(doc=doc, filepath=sbml_file.name)


# In[ ]:
Example #15
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:
        annotation.annotate_sbml_doc(doc, annotations)

    # write SBML
    sbmlio.write_sbml(doc, filepath=os.path.join(directory, sbml_file), validate=True)
    return doc