Lengthy example of creating SBML models presented in the SBML specification.
#include <iostream>
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;
}
}