Lengthy example of creating SBML models presented in the SBML specification.
using System.Collections.Generic;
public class CreateExampleSBML
{
private static int Level = 2;
private static int Version = 4;
public static int Main(string[] args)
{
SBMLDocument sbmlDoc = null;
bool SBMLok = false;
try
{
sbmlDoc = createExampleEnzymaticReaction();
SBMLok = validateExampleSBML(sbmlDoc);
if (SBMLok) writeExampleSBML(sbmlDoc, "enzymaticreaction.xml");
if (!SBMLok) return 1;
sbmlDoc = createExampleInvolvingUnits();
SBMLok = validateExampleSBML(sbmlDoc);
if (SBMLok) writeExampleSBML(sbmlDoc, "units.xml");
if (!SBMLok) return 1;
sbmlDoc = createExampleInvolvingFunctionDefinitions();
SBMLok = validateExampleSBML(sbmlDoc);
if (SBMLok) writeExampleSBML(sbmlDoc, "functiondef.xml");
if (!SBMLok) return 1;
}
catch
{
Console.Error.WriteLine("Unexpected exceptional condition encountered.");
return 1;
}
return 0;
}
private static SBMLDocument createExampleEnzymaticReaction()
{
int level = Level;
int version = Version;
SBMLDocument sbmlDoc = new SBMLDocument(level, version);
Model model = sbmlDoc.createModel();
model.setId("EnzymaticReaction");
UnitDefinition unitdef;
Unit unit;
unitdef = model.createUnitDefinition();
unitdef.setId("per_second");
unit = unitdef.createUnit();
unit.setKind(libsbml.UNIT_KIND_SECOND);
unit.setExponent(-1);
unitdef = model.createUnitDefinition();
unitdef.setId("litre_per_mole_per_second");
unit = unitdef.createUnit();
unit.setKind(libsbml.UNIT_KIND_MOLE);
unit.setExponent(-1);
unit = unitdef.createUnit();
unit.setKind(libsbml.UNIT_KIND_LITRE);
unit.setExponent(1);
unit = unitdef.createUnit();
unit.setKind(libsbml.UNIT_KIND_SECOND);
unit.setExponent(-1);
Compartment comp;
string compName = "cytosol";
comp = model.createCompartment();
comp.setId(compName);
comp.setSize(1e-14);
Species sp;
sp = model.createSpecies();
sp.setId("ES");
sp.setName("ES");
sp.setCompartment(compName);
sp.setInitialAmount(0);
sp = model.createSpecies();
sp.setCompartment(compName);
sp.setId("P");
sp.setName("P");
sp.setInitialAmount(0);
sp = model.createSpecies();
sp.setCompartment(compName);
sp.setId("S");
sp.setName("S");
sp.setInitialAmount(1e-20);
sp = model.createSpecies();
sp.setCompartment(compName);
sp.setId("E");
sp.setName("E");
sp.setInitialAmount(5e-21);
Reaction reaction;
SpeciesReference spr;
KineticLaw kl;
reaction = model.createReaction();
reaction.setId("veq");
spr = reaction.createReactant();
spr.setSpecies("E");
spr = reaction.createReactant();
spr.setSpecies("S");
spr = reaction.createProduct();
spr.setSpecies("ES");
kl = reaction.createKineticLaw();
ASTNode astCytosol = new ASTNode(libsbml.AST_NAME);
astCytosol.setName("cytosol");
ASTNode astKon = new ASTNode(libsbml.AST_NAME);
astKon.setName("kon");
ASTNode astKoff = new ASTNode(libsbml.AST_NAME);
astKoff.setName("koff");
ASTNode astE = new ASTNode(libsbml.AST_NAME);
astE.setName("E");
ASTNode astS = new ASTNode(libsbml.AST_NAME);
astS.setName("S");
ASTNode astES = new ASTNode(libsbml.AST_NAME);
astES.setName("ES");
ASTNode astTimes1 = new ASTNode(libsbml.AST_TIMES);
astTimes1.addChild(astKoff);
astTimes1.addChild(astES);
ASTNode astTimes2 = new ASTNode(libsbml.AST_TIMES);
astTimes2.addChild(astE);
astTimes2.addChild(astS);
ASTNode astTimes = new ASTNode(libsbml.AST_TIMES);
astTimes.addChild(astKon);
astTimes.addChild(astTimes2);
ASTNode astMinus = new ASTNode(libsbml.AST_MINUS);
astMinus.addChild(astTimes);
astMinus.addChild(astTimes1);
ASTNode astMath = new ASTNode(libsbml.AST_TIMES);
astMath.addChild(astCytosol);
astMath.addChild(astMinus);
kl.setMath(astMath);
Parameter para = kl.createParameter();
para.setId("kon");
para.setValue(1000000);
para.setUnits("litre_per_mole_per_second");
para = kl.createParameter();
para.setId("koff");
para.setValue(0.2);
para.setUnits("per_second");
reaction = model.createReaction();
reaction.setId("vcat");
reaction.setReversible(false);
spr = reaction.createReactant();
spr.setSpecies("ES");
spr = reaction.createProduct();
spr.setSpecies("E");
spr = reaction.createProduct();
spr.setSpecies("P");
kl = reaction.createKineticLaw();
string mathXMLString = "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">"
+ " <apply>"
+ " <times/>"
+ " <ci> cytosol </ci>"
+ " <ci> kcat </ci>"
+ " <ci> ES </ci>"
+ " </apply>"
+ "</math>";
astMath = libsbml.readMathMLFromString(mathXMLString);
kl.setMath(astMath);
para = kl.createParameter();
para.setId("kcat");
para.setValue(0.1);
para.setUnits("per_second");
return sbmlDoc;
}
private static SBMLDocument createExampleInvolvingUnits()
{
int level = Level;
int version = Version;
SBMLDocument sbmlDoc = new SBMLDocument(level, version);
sbmlDoc.getNamespaces().add("http://www.w3.org/1999/xhtml", "xhtml");
Model model = sbmlDoc.createModel();
model.setId("unitsExample");
UnitDefinition unitdef;
Unit unit;
unitdef = model.createUnitDefinition();
unitdef.setId("substance");
unit = unitdef.createUnit();
unit.setKind(libsbml.UNIT_KIND_MOLE);
unit.setScale(-3);
unitdef = model.createUnitDefinition();
unitdef.setId("mmls");
unit = unitdef.createUnit();
unit.setKind(libsbml.UNIT_KIND_MOLE);
unit.setScale(-3);
unit = unitdef.createUnit();
unit.setKind(libsbml.UNIT_KIND_LITRE);
unit.setExponent(-1);
unit = unitdef.createUnit();
unit.setKind(libsbml.UNIT_KIND_SECOND);
unit.setExponent(-1);
unitdef = model.createUnitDefinition();
unitdef.setId("mml");
unit = unitdef.createUnit();
unit.setKind(libsbml.UNIT_KIND_MOLE);
unit.setScale(-3);
unit = unitdef.createUnit();
unit.setKind(libsbml.UNIT_KIND_LITRE);
unit.setExponent(-1);
Compartment comp;
string compName = "cell";
comp = model.createCompartment();
comp.setId(compName);
comp.setSize(1);
Species sp;
sp = model.createSpecies();
sp.setId("x0");
sp.setCompartment(compName);
sp.setInitialConcentration(1);
sp = model.createSpecies();
sp.setId("x1");
sp.setCompartment(compName);
sp.setInitialConcentration(1);
sp = model.createSpecies();
sp.setCompartment(compName);
sp.setId("s1");
sp.setInitialConcentration(1);
sp = model.createSpecies();
sp.setCompartment(compName);
sp.setId("s2");
sp.setInitialConcentration(1);
Parameter para;
para = model.createParameter();
para.setId("vm");
para.setValue(2);
para.setUnits("mmls");
para = model.createParameter();
para.setId("km");
para.setValue(2);
para.setUnits("mml");
Reaction reaction;
SpeciesReference spr;
KineticLaw kl;
reaction = model.createReaction();
reaction.setId("v1");
spr = reaction.createReactant();
spr.setSpecies("x0");
spr = reaction.createProduct();
spr.setSpecies("s1");
kl = reaction.createKineticLaw();
string notesString = "<xhtml:p> ((vm * s1)/(km + s1)) * cell </xhtml:p>";
kl.setNotes(notesString);
ASTNode astMath = new ASTNode(libsbml.AST_TIMES);
astMath.addChild(new ASTNode(libsbml.AST_DIVIDE));
ASTNode astDivide = astMath.getLeftChild();
astDivide.addChild(new ASTNode(libsbml.AST_TIMES));
ASTNode astTimes = astDivide.getLeftChild();
astTimes.addChild(new ASTNode(libsbml.AST_NAME));
astTimes.getLeftChild().setName("vm");
astTimes.addChild(new ASTNode(libsbml.AST_NAME));
astTimes.getRightChild().setName("s1");
astDivide.addChild(new ASTNode(libsbml.AST_PLUS));
ASTNode astPlus = astDivide.getRightChild();
astPlus.addChild(new ASTNode(libsbml.AST_NAME));
astPlus.getLeftChild().setName("km");
astPlus.addChild(new ASTNode(libsbml.AST_NAME));
astPlus.getRightChild().setName("s1");
astMath.addChild(new ASTNode(libsbml.AST_NAME));
astMath.getRightChild().setName("cell");
kl.setMath(astMath);
reaction = model.createReaction();
reaction.setId("v2");
spr = reaction.createReactant();
spr.setSpecies("s1");
spr = reaction.createProduct();
spr.setSpecies("s2");
kl = reaction.createKineticLaw();
XMLNode notesXMLNode = new XMLNode(
new XMLTriple("p", "", "xhtml"),
new XMLAttributes());
notesXMLNode.addChild(new XMLNode(" ((vm * s2)/(km + s2)) * cell "));
kl.setNotes(notesXMLNode);
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>";
astMath = libsbml.readMathMLFromString(mathXMLString);
kl.setMath(astMath);
reaction = model.createReaction();
reaction.setId("v3");
spr = reaction.createReactant();
spr.setSpecies("s2");
spr = reaction.createProduct();
spr.setSpecies("x1");
kl = reaction.createKineticLaw();
notesString = "<xhtml:p> ((vm * x1)/(km + x1)) * cell </xhtml:p>";
kl.setNotes(notesString);
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>";
astMath = libsbml.readMathMLFromString(mathXMLString);
kl.setMath(astMath);
return sbmlDoc;
}
private static SBMLDocument createExampleInvolvingFunctionDefinitions()
{
int level = Level;
int version = Version;
SBMLDocument sbmlDoc = new SBMLDocument(level, version);
Model model = sbmlDoc.createModel();
model.setId("functionExample");
FunctionDefinition fdef = model.createFunctionDefinition();
fdef.setId("f");
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>";
ASTNode astMath = libsbml.readMathMLFromString(mathXMLString);
fdef.setMath(astMath);
Compartment comp;
const string compName = "compartmentOne";
comp = model.createCompartment();
comp.setId(compName);
comp.setSize(1);
Species sp;
sp = model.createSpecies();
sp.setId("S1");
sp.setCompartment(compName);
sp.setInitialConcentration(1);
sp = model.createSpecies();
sp.setId("S2");
sp.setCompartment(compName);
sp.setInitialConcentration(0);
Parameter para;
para = model.createParameter();
para.setId("t");
para.setValue(1);
para.setUnits("second");
Reaction reaction;
SpeciesReference spr;
KineticLaw kl;
reaction = model.createReaction();
reaction.setId("reaction_1");
reaction.setReversible(false);
spr = reaction.createReactant();
spr.setSpecies("S1");
spr = reaction.createProduct();
spr.setSpecies("S2");
kl = reaction.createKineticLaw();
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>";
astMath = libsbml.readMathMLFromString(mathXMLString);
kl.setMath(astMath);
return sbmlDoc;
}
private static bool validateExampleSBML(SBMLDocument sbmlDoc)
{
if (sbmlDoc == null)
{
Console.Error.WriteLine("validateExampleSBML: given a null SBML Document");
return false;
}
string consistencyMessages = "";
string validationMessages = "";
bool noProblems = true;
int numCheckFailures = 0;
int numConsistencyErrors = 0;
int numConsistencyWarnings = 0;
int numValidationErrors = 0;
int numValidationWarnings = 0;
numCheckFailures = (int)sbmlDoc.checkInternalConsistency();
if (numCheckFailures > 0)
{
noProblems = false;
for (int i = 0;
i < numCheckFailures;
i++)
{
SBMLError sbmlErr = sbmlDoc.getError(i);
if (sbmlErr.isFatal() || sbmlErr.isError())
{
++numConsistencyErrors;
}
else
{
++numConsistencyWarnings;
}
}
consistencyMessages = sbmlDoc.getErrorLog().toString();
}
if (numConsistencyErrors > 0)
{
consistencyMessages += "Further validation aborted.";
}
else
{
numCheckFailures = (int)sbmlDoc.checkConsistency();
if (numCheckFailures > 0)
{
noProblems = false;
for (int i = 0;
i < numCheckFailures;
i++)
{
SBMLError sbmlErr = sbmlDoc.getError(i);
if (sbmlErr.isFatal() || sbmlErr.isError())
{
++numValidationErrors;
}
else
{
++numValidationWarnings;
}
}
validationMessages = sbmlDoc.getErrorLog().toString();
}
}
if (noProblems)
return true;
else
{
if (numConsistencyErrors > 0)
{
Console.WriteLine("ERROR: encountered " + numConsistencyErrors
+ " consistency error" + (numConsistencyErrors == 1 ? "" : "s")
+ " in model '" + sbmlDoc.getModel().getId() + "'.");
}
if (numConsistencyWarnings > 0)
{
Console.WriteLine("Notice: encountered " + numConsistencyWarnings
+ " consistency warning" + (numConsistencyWarnings == 1 ? "" : "s")
+ " in model '" + sbmlDoc.getModel().getId() + "'.");
}
Console.WriteLine();
Console.WriteLine(consistencyMessages);
if (numValidationErrors > 0)
{
Console.WriteLine("ERROR: encountered " + numValidationErrors
+ " validation error" + (numValidationErrors == 1 ? "" : "s")
+ " in model '" + sbmlDoc.getModel().getId() + "'.");
}
if (numValidationWarnings > 0)
{
Console.WriteLine("Notice: encountered " + numValidationWarnings
+ " validation warning" + (numValidationWarnings == 1 ? "" : "s")
+ " in model '" + sbmlDoc.getModel().getId() + "'.");
}
Console.WriteLine();
Console.WriteLine(validationMessages);
return (numConsistencyErrors == 0 && numValidationErrors == 0);
}
}
private static bool writeExampleSBML(SBMLDocument sbmlDoc, string filename)
{
int result = libsbml.writeSBML(sbmlDoc, filename);
if (result == 1)
{
Console.WriteLine("Wrote file \"" + filename + "\"");
return true;
}
else
{
Console.WriteLine("Failed to write \"" + filename + "\"");
return false;
}
}
}