libSBML C++ API  libSBML 5.18.0 C++ API
fbc_example1.cpp

Simple example of writing a model that uses the SBML Level 3 Flux Balance Constraints package.

/**
* @file example1.cpp
* @brief SBML FBC example
* @author Frank T. Bergmann
*
* <!--------------------------------------------------------------------------
* This sample program is distributed under a different license than the rest
* of libSBML. This program uses the open-source MIT license, as follows:
*
* Copyright (c) 2013-2018 by the California Institute of Technology
* (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
* and the University of Heidelberg (Germany), with support from the National
* Institutes of Health (USA) under grant R01GM070923. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Neither the name of the California Institute of Technology (Caltech), nor
* of the European Bioinformatics Institute (EMBL-EBI), nor of the University
* of Heidelberg, nor the names of any contributors, may be used to endorse
* or promote products derived from this software without specific prior
* written permission.
* ------------------------------------------------------------------------ -->
*/
#include <sbml/SBMLTypes.h>
int main(int argc,char** argv)
{
SBMLNamespaces sbmlns(3,1,"fbc",1);
// create the document
SBMLDocument *document = new SBMLDocument(&sbmlns);
document->setPackageRequired("fbc", false);
// create the Model
Model* model=document->createModel();
// create the Compartment
Compartment* compartment = model->createCompartment();
compartment->setId("compartment");
compartment->setConstant(true);
compartment->setSize(1);
// create the Species
Species* species = model->createSpecies();
species->setId("Node1");
species->setCompartment("compartment");
species->setBoundaryCondition(false);
species->setConstant(false);
species->setHasOnlySubstanceUnits(false);
species = model->createSpecies();
species->setId("Node2");
species->setCompartment("compartment");
species->setBoundaryCondition(false);
species->setConstant(false);
species->setHasOnlySubstanceUnits(false);
species = model->createSpecies();
species->setId("Node3");
species->setCompartment("compartment");
species->setBoundaryCondition(false);
species->setConstant(false);
species->setHasOnlySubstanceUnits(false);
species = model->createSpecies();
species->setId("Node4");
species->setCompartment("compartment");
species->setBoundaryCondition(false);
species->setConstant(false);
species->setHasOnlySubstanceUnits(false);
species = model->createSpecies();
species->setId("Node5");
species->setCompartment("compartment");
species->setBoundaryCondition(false);
species->setConstant(false);
species->setHasOnlySubstanceUnits(false);
species = model->createSpecies();
species->setId("Node6");
species->setCompartment("compartment");
species->setBoundaryCondition(false);
species->setConstant(false);
species->setHasOnlySubstanceUnits(false);
species = model->createSpecies();
species->setId("Node7");
species->setCompartment("compartment");
species->setBoundaryCondition(false);
species->setConstant(false);
species->setHasOnlySubstanceUnits(false);
species = model->createSpecies();
species->setId("Node8");
species->setCompartment("compartment");
species->setBoundaryCondition(false);
species->setConstant(false);
species->setHasOnlySubstanceUnits(false);
species = model->createSpecies();
species->setId("Node0");
species->setCompartment("compartment");
species->setBoundaryCondition(true);
species->setConstant(false);
species->setHasOnlySubstanceUnits(false);
species = model->createSpecies();
species->setId("Node9");
species->setCompartment("compartment");
species->setBoundaryCondition(true);
species->setConstant(false);
species->setHasOnlySubstanceUnits(false);
Reaction* reaction = model->createReaction();
reaction->setId("J0");
reaction->setReversible(false);
reaction->setFast(false);
SpeciesReference* reactant = reaction->createReactant();
reactant->setSpecies("Node0");
reactant->setStoichiometry(1);
reactant->setConstant(true);
SpeciesReference* product = reaction->createProduct();
product->setSpecies("Node1");
product->setStoichiometry(1);
product->setConstant(true);
reaction = model->createReaction();
reaction->setId("J1");
reaction->setReversible(false);
reaction->setFast(false);
reactant = reaction->createReactant();
reactant->setSpecies("Node1");
reactant->setStoichiometry(1);
reactant->setConstant(true);
product = reaction->createProduct();
product->setSpecies("Node2");
product->setStoichiometry(1);
product->setConstant(true);
reaction = model->createReaction();
reaction->setId("J2");
reaction->setReversible(false);
reaction->setFast(false);
reactant = reaction->createReactant();
reactant->setSpecies("Node2");
reactant->setStoichiometry(1);
reactant->setConstant(true);
product = reaction->createProduct();
product->setSpecies("Node3");
product->setStoichiometry(1);
product->setConstant(true);
reaction = model->createReaction();
reaction->setId("J3");
reaction->setReversible(false);
reaction->setFast(false);
reactant = reaction->createReactant();
reactant->setSpecies("Node1");
reactant->setStoichiometry(1);
reactant->setConstant(true);
product = reaction->createProduct();
product->setSpecies("Node4");
product->setStoichiometry(1);
product->setConstant(true);
reaction = model->createReaction();
reaction->setId("J4");
reaction->setReversible(false);
reaction->setFast(false);
reactant = reaction->createReactant();
reactant->setSpecies("Node4");
reactant->setStoichiometry(1);
reactant->setConstant(true);
product = reaction->createProduct();
product->setSpecies("Node3");
product->setStoichiometry(1);
product->setConstant(true);
reaction = model->createReaction();
reaction->setId("J5");
reaction->setReversible(false);
reaction->setFast(false);
reactant = reaction->createReactant();
reactant->setSpecies("Node3");
reactant->setStoichiometry(1);
reactant->setConstant(true);
product = reaction->createProduct();
product->setSpecies("Node5");
product->setStoichiometry(1);
product->setConstant(true);
reaction = model->createReaction();
reaction->setId("J6");
reaction->setReversible(false);
reaction->setFast(false);
reactant = reaction->createReactant();
reactant->setSpecies("Node5");
reactant->setStoichiometry(1);
reactant->setConstant(true);
product = reaction->createProduct();
product->setSpecies("Node6");
product->setStoichiometry(1);
product->setConstant(true);
reaction = model->createReaction();
reaction->setId("J7");
reaction->setReversible(false);
reaction->setFast(false);
reactant = reaction->createReactant();
reactant->setSpecies("Node6");
reactant->setStoichiometry(1);
reactant->setConstant(true);
product = reaction->createProduct();
product->setSpecies("Node7");
product->setStoichiometry(1);
product->setConstant(true);
reaction = model->createReaction();
reaction->setId("J8");
reaction->setReversible(false);
reaction->setFast(false);
reactant = reaction->createReactant();
reactant->setSpecies("Node5");
reactant->setStoichiometry(1);
reactant->setConstant(true);
product = reaction->createProduct();
product->setSpecies("Node8");
product->setStoichiometry(1);
product->setConstant(true);
reaction = model->createReaction();
reaction->setId("J9");
reaction->setReversible(false);
reaction->setFast(false);
reactant = reaction->createReactant();
reactant->setSpecies("Node8");
reactant->setStoichiometry(1);
reactant->setConstant(true);
product = reaction->createProduct();
product->setSpecies("Node7");
product->setStoichiometry(1);
product->setConstant(true);
reaction = model->createReaction();
reaction->setId("J10");
reaction->setReversible(false);
reaction->setFast(false);
reactant = reaction->createReactant();
reactant->setSpecies("Node7");
reactant->setStoichiometry(1);
reactant->setConstant(true);
product = reaction->createProduct();
product->setSpecies("Node9");
product->setStoichiometry(1);
product->setConstant(true);
//
// Get a FbcModelPlugin object plugged in the model object.
//
// The type of the returned value of SBase::getPlugin() function is
// SBasePlugin*, and thus the value needs to be casted for the
// corresponding derived class.
//
FbcModelPlugin* mplugin
= static_cast<FbcModelPlugin*>(model->getPlugin("fbc"));
FluxBound* bound= mplugin->createFluxBound();
bound->setId("bound1");
bound->setReaction("J0");
bound->setOperation("equal");
bound->setValue(10);
Objective* objective = mplugin->createObjective();
objective->setId("obj1");
objective->setType("maximize");
// mark obj1 as active objective
mplugin->setActiveObjectiveId("obj1");
FluxObjective* fluxObjective = objective->createFluxObjective();
fluxObjective->setReaction("J8");
fluxObjective->setCoefficient(1);
writeSBML(document,"fbc_example1.xml");
delete document;
}
SpeciesReference
A reference to an SBML species in a reaction.
Definition: SpeciesReference.h:275
FluxObjective
An objective function for a flux.
Definition: FluxObjective.h:100
Compartment::setConstant
int setConstant(bool value)
Sets the value of the "constant" attribute of this Compartment object.
Definition: Compartment.cpp:681
Reaction
An SBML reaction between species in an SBML model.
Definition: Reaction.h:218
FbcModelPlugin
Extension of Model.
Definition: FbcModelPlugin.h:156
SBMLTypes.h
Include all SBML types in a single header file.
Model::createReaction
Reaction * createReaction()
Creates a new Reaction inside this Model and returns it.
Definition: Model.cpp:1779
Reaction::setReversible
int setReversible(bool value)
Sets the value of the "reversible" attribute of this Reaction.
Definition: Reaction.cpp:607
SBMLDocument::createModel
Model * createModel(const std::string sid="")
Creates a new Model inside this SBMLDocument, and returns a pointer to it.
Definition: SBMLDocument.cpp:643
Reaction::setFast
int setFast(bool value)
Sets the value of the "fast" attribute of this Reaction.
Definition: Reaction.cpp:620
Model::createSpecies
Species * createSpecies()
Creates a new Species inside this Model and returns it.
Definition: Model.cpp:1590
FluxObjective::setReaction
virtual int setReaction(const std::string &reaction)
Sets the value of the "reaction" attribute of this FluxObjective.
Definition: FluxObjective.cpp:247
Compartment::setId
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this Compartment object.
Definition: Compartment.cpp:474
FbcModelPlugin::createFluxBound
FluxBound * createFluxBound()
Creates a new FluxBound object and adds it to the list of FluxBound objects and returns it.
Definition: FbcModelPlugin.cpp:827
Reaction::createReactant
SpeciesReference * createReactant()
Creates a new SpeciesReference, adds it to this Reaction's list of reactants, and returns it.
Definition: Reaction.cpp:955
FluxBound::setId
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this FluxBound.
Definition: FluxBound.cpp:170
Objective::setType
int setType(ObjectiveType_t type)
Sets the value of the "type" attribute of this Objective.
Definition: Objective.cpp:252
Species::setConstant
int setConstant(bool value)
Sets the "constant" attribute of this Species object.
Definition: Species.cpp:824
Compartment
An SBML compartment, where species are located.
Definition: Compartment.h:484
Objective
An objective function.
Definition: Objective.h:142
SimpleSpeciesReference::setSpecies
int setSpecies(const std::string &sid)
Sets the "species" attribute of this SimpleSpeciesReference.
Definition: SimpleSpeciesReference.cpp:192
Model
An SBML model.
Definition: Model.h:479
Species::setId
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this Species.
Definition: Species.cpp:586
Objective::setId
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this Objective.
Definition: Objective.cpp:229
FbcModelPlugin::setActiveObjectiveId
int setActiveObjectiveId(const std::string &objectiveId)
Sets the id of the active objective.
Definition: FbcModelPlugin.cpp:1323
Reaction::createProduct
SpeciesReference * createProduct()
Creates a new SpeciesReference, adds it to this Reaction's list of products, and returns it.
Definition: Reaction.cpp:983
FbcExtensionTypes.h
Includes all SBML types of the fbc package extension in a single header file.
FbcModelPlugin::createObjective
Objective * createObjective()
Creates a new Objective object, adds it to this FbcModelPlugin's ListOfObjectives and returns the Obj...
Definition: FbcModelPlugin.cpp:1092
SBMLDocument::setPackageRequired
int setPackageRequired(const std::string &package, bool flag)
Sets the required attribute value of the given package extension.
Definition: SBMLDocument.cpp:1439
Reaction::setId
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this Reaction.
Definition: Reaction.cpp:516
SpeciesReference::setStoichiometry
int setStoichiometry(double value)
Sets the value of the "stoichiometry" attribute of this SpeciesReference.
Definition: SpeciesReference.cpp:307
FluxBound
Max or min value for a reaction flux.
Definition: FluxBound.h:131
SpeciesReference::setConstant
int setConstant(bool flag)
Sets the "constant" attribute of this SpeciesReference to the given boolean flag.
Definition: SpeciesReference.cpp:395
SBMLNamespaces
Set of SBML Level + Version + namespace triples.
Definition: SBMLNamespaces.h:139
Species::setCompartment
int setCompartment(const std::string &sid)
Sets the "compartment" attribute of this Species object.
Definition: Species.cpp:665
Model::createCompartment
Compartment * createCompartment()
Creates a new Compartment inside this Model and returns it.
Definition: Model.cpp:1563
Objective::createFluxObjective
FluxObjective * createFluxObjective()
Creates a new FluxObjective object.
Definition: Objective.cpp:466
Species
An SBML species – a pool of entities.
Definition: Species.h:423
FluxObjective::setCoefficient
virtual int setCoefficient(double coefficient)
Sets the value of the "coefficient" attribute of this FluxObjective.
Definition: FluxObjective.cpp:257
LIBSBML_CPP_NAMESPACE_USE
#define LIBSBML_CPP_NAMESPACE_USE
Definition: libsbml-namespace.h:67
SBMLDocument
Overall SBML container object.
Definition: SBMLDocument.h:342
Compartment::setSize
int setSize(double value)
Sets the "size" attribute (or "volume" in SBML Level&#160;1) of this Compartment object.
Definition: Compartment.cpp:605
SBase::getPlugin
SBasePlugin * getPlugin(const std::string &package)
Returns a plug-in object (extension interface) for an SBML Level&#160;3 package extension with the given p...
Definition: SBase.cpp:3460
Species::setBoundaryCondition
int setBoundaryCondition(bool value)
Sets the "boundaryCondition" attribute of this Species object.
Definition: Species.cpp:791
Species::setHasOnlySubstanceUnits
int setHasOnlySubstanceUnits(bool value)
Sets the "hasOnlySubstanceUnits" attribute of this Species object.
Definition: Species.cpp:771
writeSBML
int writeSBML(const SBMLDocument_t *d, const char *filename)
Writes the given SBML document d to the file named by filename.