The libsbml C# bindings are now included with the official libsbml release. So the code below should be seen only as example. Please download the official version from the SBML Homepage
30th Apr. Update: updated prototype, added memory handling, OStreams, inlining of enums, replaced uints in signatures with int's
I'm currently working on libsbml C# bindings, to provide an alternative way of SBML support (that is other than via SBW) for the .NET languages. I currently have a working prototype (relying on SWIG generated code) that is available here:
libsbml C# bindings (prototype)
The archive contains:
libsbml C# bindings
===================
The archive contains the following files:
- libSBMLCSharp.dll
The C# Assembly you would include in your project
- libsbml_csharp-wrap.dll
The SWIG generated C++ backend that is used by the C# assembly over P/Invoke
- SBMLTest.exe
A small test program demonstrating that the C# assembly above is actually working.
It is a command line application that expects a filename (of an SBML file) and will
print Species (and initialconcentration) as well as the reaction scheme.
- ReadMe.txt
This document
- src/SBMLTest.cs
An example of how to use the C# assembly. (SourceCode to SBMLTest.exe)
For questions and comments contact Frank Bergmann (fbergman@u.washington.edu)
Known Issues:
=============
- The SWIG generated bindings are not thread safe, so use all communications with
a model on the same thread
- I'm currently in the progress of testing the library more thoroughly.
The library obviously needs more testing and currently I'm trying to generate test cases automatically from the libsbml tests.
— Frank Bergmann 2008/04/29 12:44
Here an example of how to use the above prototype.
using System;
using libsbml;
namespace SBMLTest
{
/// <summary>
/// SBML Test is intended to provide a test of basic SBML reading capabilities using the SWIG wrapped libsbml-csharp bindings
///
/// Frank Bergmann (fbergman@u.washington.edu)
/// </summary>
class SBMLTest
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("usage: SBMLTest <fileName>");
System.Environment.Exit(-1);
}
// read the given SBML model
SBMLReader oReader = new SBMLReader();
SBMLDocument oDocument = oReader.readSBML(args[0]);
Model oModel = oDocument.getModel();
// print ID and Name
Console.WriteLine(
String.Format(
"Id: '{0}' and Name: '{1}'{2}",
oModel.getId(),
oModel.getName(), Environment.NewLine)
);
// loop through species
PrintSpecies(oModel);
// loop through reactions and write script style reaction scheme
PrintReactions(oModel);
Console.WriteLine(String.Format("{0}<Hit <Return> to continue>{0}", Environment.NewLine));
Console.ReadLine();
return;
}
/// <summary>
/// Loop through all species in the given model and print ID and initialConcentration
/// </summary>
/// <param name="oModel">the model</param>
private static void PrintSpecies(Model oModel)
{
Console.WriteLine(String.Format("Number of Species: {0}", oModel.getNumSpecies()));
for (uint i = 0; i < oModel.getNumSpecies(); i++)
{
Species s = oModel.getSpecies(i);
Console.WriteLine(String.Format("...id: {0} concentration: {1}", s.getId(), s.getInitialConcentration()));
}
}
/// <summary>
/// Loop through all reactions in the given model and print the reaction scheme.
/// </summary>
/// <param name="oModel">the model</param>
private static void PrintReactions(Model oModel)
{
Console.WriteLine(String.Format("{1}Number of Reactions: {0}", oModel.getNumReactions(), Environment.NewLine));
for (uint n = 0; n < oModel.getNumReactions(); n++)
{
PrintNthReaction(oModel, n);
}
}
/// <summary>
/// Obtain the reaction with the given index and print its reaction scheme and kinetic law.
/// </summary>
/// <param name="oModel">the model</param>
/// <param name="index">index of the reaction</param>
private static void PrintNthReaction(Model oModel, uint index)
{
Reaction r = oModel.getReaction(index);
Console.Write(String.Format("\t{0} : ", r.getId()));
// Print lefthand side of the reaction scheme
for (uint j = 0; j < r.getNumReactants(); j++)
{
SpeciesReference oRef = r.getReactant(j);
Console.Write(
String.Format("{0} {1} ", oRef.getStoichiometry(), oRef.getSpecies()) );
if (j + 1 < r.getNumReactants())
Console.Write(" + ");
else
{
if (r.getReversible())
{
Console.Write(" => ");
}
else
{
Console.Write(" -> ");
}
}
}
// Print righthand side of the reaction scheme
for (uint j = 0; j < r.getNumProducts(); j++)
{
SpeciesReference oRef = r.getProduct(j);
Console.Write(
String.Format("{0} {1} ", oRef.getStoichiometry(), oRef.getSpecies()) );
if (j + 1 < r.getNumReactants())
Console.Write(" + ");
else
Console.Write(";" + Environment.NewLine);
}
// Print the kineticLaw
Console.WriteLine(
String.Format("\t{0};{1}",
libsbml.libsbml.formulaToString(r.getKineticLaw().getMath()),
Environment.NewLine
));
}
}
}