Lengthy example of creating SBML models presented in the SBML specification.
#include <iostream>
using namespace std;
LIBSBML_CPP_NAMESPACE_USE
bool writeExampleSBML(
const SBMLDocument *sbmlDoc,
const string& filename);
const static string ProgramName = "createExampleModels";
const static string ProgramVersion = "1.0.0";
const static unsigned int Level = 2;
const static unsigned int Version = 4;
int
main (int argc, char *argv[])
{
bool SBMLok = false;
try
{
sbmlDoc = createExampleEnzymaticReaction();
SBMLok = validateExampleSBML(sbmlDoc);
if (SBMLok) writeExampleSBML(sbmlDoc, "enzymaticreaction.xml");
delete sbmlDoc;
if (!SBMLok) return 1;
sbmlDoc = createExampleInvolvingUnits();
SBMLok = validateExampleSBML(sbmlDoc);
if (SBMLok) writeExampleSBML(sbmlDoc, "units.xml");
delete sbmlDoc;
if (!SBMLok) return 1;
sbmlDoc = createExampleInvolvingFunctionDefinitions();
SBMLok = validateExampleSBML(sbmlDoc);
if (SBMLok) writeExampleSBML(sbmlDoc, "functiondef.xml");
delete sbmlDoc;
if (!SBMLok) return 1;
}
catch (std::bad_alloc& e)
{
cerr << e.what() << ": Unable to allocate memory." << endl;
return 1;
}
catch (...)
{
cerr << "Unexpected exceptional condition encountered." << endl;
return 1;
}
return 0;
}
{
const unsigned int level = Level;
const unsigned int version = Version;
model->
setId(
"EnzymaticReaction");
unitdef->
setId(
"per_second");
unitdef->
setId(
"litre_per_mole_per_second");
const string compName = "cytosol";
delete astMath;
para->
setUnits(
"litre_per_mole_per_second");
string mathXMLString = "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">"
" <apply>"
" <times/>"
" <ci> cytosol </ci>"
" <ci> kcat </ci>"
" <ci> ES </ci>"
" </apply>"
"</math>";
delete astMath;
return sbmlDoc;
}
{
const unsigned int level = Level;
const unsigned int version = Version;
model->
setId(
"unitsExample");
unitdef->
setId(
"substance");
const string compName = "cell";
string notesString = "<xhtml:p> ((vm * s1)/(km + s1)) * cell </xhtml:p>";
delete astMath;
notesXMLNode.addChild(
XMLNode(
" ((vm * s2)/(km + s2)) * cell "));
string mathXMLString = "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">"
" <apply>"
" <times/>"
" <apply>"
" <divide/>"
" <apply>"
" <times/>"
" <ci> vm </ci>"
" <ci> s2 </ci>"
" </apply>"
" <apply>"
" <plus/>"
" <ci> km </ci>"
" <ci> s2 </ci>"
" </apply>"
" </apply>"
" <ci> cell </ci>"
" </apply>"
"</math>";
delete astMath;
notesString = "<xhtml:p> ((vm * x1)/(km + x1)) * cell </xhtml:p>";
mathXMLString = "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">"
" <apply>"
" <times/>"
" <apply>"
" <divide/>"
" <apply>"
" <times/>"
" <ci> vm </ci>"
" <ci> x1 </ci>"
" </apply>"
" <apply>"
" <plus/>"
" <ci> km </ci>"
" <ci> x1 </ci>"
" </apply>"
" </apply>"
" <ci> cell </ci>"
" </apply>"
"</math>";
delete astMath;
return sbmlDoc;
}
{
const unsigned int level = Level;
const unsigned int version = Version;
model->
setId(
"functionExample");
string mathXMLString = "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">"
" <lambda>"
" <bvar>"
" <ci> x </ci>"
" </bvar>"
" <apply>"
" <times/>"
" <ci> x </ci>"
" <cn> 2 </cn>"
" </apply>"
" </lambda>"
"</math>";
delete astMath;
const string compName = "compartmentOne";
reaction->
setId(
"reaction_1");
mathXMLString = "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">"
" <apply>"
" <divide/>"
" <apply>"
" <times/>"
" <apply>"
" <ci> f </ci>"
" <ci> S1 </ci>"
" </apply>"
" <ci> compartmentOne </ci>"
" </apply>"
" <ci> t </ci>"
" </apply>"
"</math>";
delete astMath;
return sbmlDoc;
}
{
if (!sbmlDoc)
{
cerr << "validateExampleSBML: given a null SBML Document" << endl;
return false;
}
string consistencyMessages;
string validationMessages;
bool noProblems = true;
unsigned int numCheckFailures = 0;
unsigned int numConsistencyErrors = 0;
unsigned int numConsistencyWarnings = 0;
unsigned int numValidationErrors = 0;
unsigned int numValidationWarnings = 0;
if ( numCheckFailures > 0 )
{
noProblems = false;
for (unsigned int i = 0; i < numCheckFailures; i++)
{
{
++numConsistencyErrors;
}
else
{
++numConsistencyWarnings;
}
}
ostringstream oss;
consistencyMessages = oss.str();
}
if (numConsistencyErrors > 0)
{
consistencyMessages += "Further validation aborted.";
}
else
{
if ( numCheckFailures > 0 )
{
noProblems = false;
for (unsigned int i = 0; i < numCheckFailures; i++)
{
{
++numValidationErrors;
}
else
{
++numValidationWarnings;
}
}
ostringstream oss;
validationMessages = oss.str();
}
}
if (noProblems)
return true;
else
{
if (numConsistencyErrors > 0)
{
cout << "ERROR: encountered " << numConsistencyErrors
<< " consistency error" << (numConsistencyErrors == 1 ? "" : "s")
<<
" in model '" << sbmlDoc->
getModel()->
getId() <<
"'." << endl;
}
if (numConsistencyWarnings > 0)
{
cout << "Notice: encountered " << numConsistencyWarnings
<< " consistency warning" << (numConsistencyWarnings == 1 ? "" : "s")
<<
" in model '" << sbmlDoc->
getModel()->
getId() <<
"'." << endl;
}
cout << endl << consistencyMessages;
if (numValidationErrors > 0)
{
cout << "ERROR: encountered " << numValidationErrors
<< " validation error" << (numValidationErrors == 1 ? "" : "s")
<<
" in model '" << sbmlDoc->
getModel()->
getId() <<
"'." << endl;
}
if (numValidationWarnings > 0)
{
cout << "Notice: encountered " << numValidationWarnings
<< " validation warning" << (numValidationWarnings == 1 ? "" : "s")
<<
" in model '" << sbmlDoc->
getModel()->
getId() <<
"'." << endl;
}
cout << endl << validationMessages;
return (numConsistencyErrors == 0 && numValidationErrors == 0);
}
}
bool writeExampleSBML(
const SBMLDocument* sbmlDoc,
const string& filename)
{
bool result = sbmlWriter.
writeSBML(sbmlDoc, filename);
if (result)
{
cout << "Wrote file \"" << filename << "\"" << endl;
return true;
}
else
{
cerr << "Failed to write \"" << filename << "\"" << endl;
return false;
}
}
@ AST_MINUS
Definition: ASTNodeType.h:85
@ AST_DIVIDE
Definition: ASTNodeType.h:87
@ AST_TIMES
Definition: ASTNodeType.h:86
@ AST_NAME
Definition: ASTNodeType.h:95
@ AST_PLUS
Definition: ASTNodeType.h:84
ASTNode_t * readMathMLFromString(const char *xml)
Reads the MathML from the given XML string, constructs a corresponding abstract syntax tree,...
Definition: MathML.cpp:2273
Include all SBML types in a single header file.
@ UNIT_KIND_LITRE
Definition: UnitKind.h:80
@ UNIT_KIND_MOLE
Definition: UnitKind.h:85
@ UNIT_KIND_SECOND
Definition: UnitKind.h:90
Definition: ASTNode.h:221
ASTNode * getLeftChild() const
Returns the left child of this node.
Definition: ASTNode.cpp:1031
int setName(const char *name)
Sets the value of this ASTNode to the given name.
Definition: ASTNode.cpp:2088
ASTNode * getRightChild() const
Returns the right child of this node.
Definition: ASTNode.cpp:1045
int addChild(ASTNode *disownedChild, bool inRead=false)
Adds the given node as a child of this ASTNode.
Definition: ASTNode.cpp:846
Definition: Compartment.h:490
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this Compartment object.
Definition: Compartment.cpp:479
int setSize(double value)
Sets the "size" attribute (or "volume" in SBML Level 1) of this Compartment object.
Definition: Compartment.cpp:603
Definition: FunctionDefinition.h:150
virtual int setMath(const ASTNode *math)
Sets the "math" subelement of this FunctionDefinition to the Abstract Syntax Tree given in math.
Definition: FunctionDefinition.cpp:272
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this FunctionDefinition.
Definition: FunctionDefinition.cpp:225
Definition: KineticLaw.h:203
virtual int setMath(const ASTNode *math)
Sets the mathematical expression of this KineticLaw instance to a copy of the given ASTNode.
Definition: KineticLaw.cpp:392
Parameter * createParameter()
Creates a new Parameter object, adds it to this KineticLaw's list of parameters, and returns the Para...
Definition: KineticLaw.cpp:613
FunctionDefinition * createFunctionDefinition()
Creates a new FunctionDefinition inside this Model and returns it.
Definition: Model.cpp:1431
UnitDefinition * createUnitDefinition()
Creates a new UnitDefinition inside this Model and returns it.
Definition: Model.cpp:1459
Reaction * createReaction()
Creates a new Reaction inside this Model and returns it.
Definition: Model.cpp:1782
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this Model.
Definition: Model.cpp:717
Species * createSpecies()
Creates a new Species inside this Model and returns it.
Definition: Model.cpp:1586
Parameter * createParameter()
Creates a new Parameter inside this Model and returns it.
Definition: Model.cpp:1614
Compartment * createCompartment()
Creates a new Compartment inside this Model and returns it.
Definition: Model.cpp:1558
virtual const std::string & getId() const
Returns the value of the "id" attribute of this Model.
Definition: Parameter.h:202
int setUnits(const std::string &units)
Sets the "units" attribute of this Parameter to a copy of the given units identifier units.
Definition: Parameter.cpp:394
int setValue(double value)
Sets the "value" attribute of this Parameter to the given double value and marks the attribute as set...
Definition: Parameter.cpp:382
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this Parameter.
Definition: Parameter.cpp:335
Definition: Reaction.h:224
SpeciesReference * createReactant()
Creates a new SpeciesReference, adds it to this Reaction's list of reactants, and returns it.
Definition: Reaction.cpp:954
int setReversible(bool value)
Sets the value of the "reversible" attribute of this Reaction.
Definition: Reaction.cpp:599
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this Reaction.
Definition: Reaction.cpp:517
SpeciesReference * createProduct()
Creates a new SpeciesReference, adds it to this Reaction's list of products, and returns it.
Definition: Reaction.cpp:983
KineticLaw * createKineticLaw()
Creates a new KineticLaw object, installs it as this Reaction's "kineticLaw" subelement,...
Definition: Reaction.cpp:1041
Definition: SBMLDocument.h:351
Model * createModel(const std::string sid="")
Creates a new Model inside this SBMLDocument, and returns a pointer to it.
Definition: SBMLDocument.cpp:627
unsigned int checkConsistency()
Performs consistency checking and validation on this SBML document.
Definition: SBMLDocument.cpp:699
virtual XMLNamespaces * getNamespaces() const
Returns a list of XML Namespaces associated with the XML content of this SBML document.
Definition: SBMLDocument.cpp:1334
const SBMLError * getError(unsigned int n) const
Returns the nth error or warning encountered during parsing, consistency checking,...
Definition: SBMLDocument.cpp:1152
void printErrors(std::ostream &stream=std::cerr) const
Prints all the errors or warnings encountered trying to parse, check, or translate this SBML document...
Definition: SBMLDocument.cpp:1192
unsigned int checkInternalConsistency()
Performs consistency checking on libSBML's internal representation of an SBML Model.
Definition: SBMLDocument.cpp:870
const Model * getModel() const
Returns the Model object stored in this SBMLDocument.
Definition: SBMLDocument.cpp:350
Definition: SBMLError.h:1100
Definition: SBMLWriter.h:115
bool writeSBML(const SBMLDocument *d, const std::string &filename)
Writes the given SBML document to filename.
Definition: SBMLWriter.cpp:140
int setNotes(const XMLNode *notes)
Sets the value of the "notes" subelement of this SBML object.
Definition: SBase.cpp:1775
int setSpecies(const std::string &sid)
Sets the "species" attribute of this SimpleSpeciesReference.
Definition: SimpleSpeciesReference.cpp:196
Definition: Species.h:429
int setCompartment(const std::string &sid)
Sets the "compartment" attribute of this Species object.
Definition: Species.cpp:661
int setInitialConcentration(double value)
Sets the "initialConcentration" attribute of this Species and marks the field as set.
Definition: Species.cpp:695
int setInitialAmount(double value)
Sets the "initialAmount" attribute of this Species and marks the field as set.
Definition: Species.cpp:680
virtual int setName(const std::string &name)
Sets the value of the "name" attribute of this Species.
Definition: Species.cpp:609
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this Species.
Definition: Species.cpp:591
Definition: SpeciesReference.h:281
Definition: UnitDefinition.h:282
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this UnitDefinition.
Definition: UnitDefinition.cpp:237
Unit * createUnit()
Creates a new and empty Unit, adds it to this UnitDefinition's list of units, and returns it.
Definition: UnitDefinition.cpp:682
int setKind(UnitKind_t kind)
Sets the "kind" attribute value of this Unit.
Definition: Unit.cpp:755
int setExponent(int value)
Sets the "exponent" attribute value of this Unit.
Definition: Unit.cpp:774
int setScale(int value)
Sets the "scale" attribute value of this Unit.
Definition: Unit.cpp:817
Definition: XMLAttributes.h:257
bool isError() const
Predicate returning true or false depending on whether this error is a significant error.
Definition: XMLError.cpp:661
bool isFatal() const
Predicate returning true or false depending on whether this error is a fatal run-time error.
Definition: XMLError.cpp:671
int add(const std::string &uri, const std::string prefix="")
Appends an XML namespace prefix and URI pair to this list of namespace declarations.
Definition: XMLNamespaces.cpp:119
Definition: XMLNode.h:178
Definition: XMLTriple.h:94