This tutorial is for developers who want to read or write the SBGN-ML file format using [[LibSBGN]].
To use this tutorial, you'll need to obtain LibSBGN. There are three ways to do so:
- Obtain release from
https://github.com/sbgn/libsbgn/releases - Compile LibSGBN from source using Ant
https://github.com/sbgn/libsbgn/blob/master/README.md - Compile LibSBGN from source using Eclipse
[[Developer_Tutorial_Eclipse]]
Here follows a snippet of Java code that shows how to read an SBGN-ML file. This uses the file "adh.sbgn" as input file, which you can download here: adh.sbgn
// our sbgn-ml file goes in "f"
File f = new File ("../test-files/adh.sbgn");
// Now read from "f" and put the result in "sbgn"
Sbgn sbgn = SbgnUtil.readFromFile(f);
// map is a container for the glyphs and arcs
Map map = sbgn.getMap();
// we can get a list of glyphs (nodes) in this map with getGlyph()
for (Glyph g : map.getGlyph())
{
// print the sbgn class of this glyph
System.out.print (" Glyph with class " + g.getId());
`
// if there is a label, print it as well
if (g.getLabel() != null)
System.out.println (", and label " + g.getLabel().getText());
else
System.out.println (", without label");
}
// we can get a list of arcs (edges) in this map with getArc()
for (Arc a : map.getArc())
{
// print the class of this arc
System.out.println (" Arc with class " + a.getClazz());
}
See ReadExample.java
This will produce the following output:
Begin of Map
Glyph with class glyph1, and label Ethanol
Glyph with class glyph_ethanal, and label Ethanal
Glyph with class glyph_adh1, and label ADH1
Glyph with class glyph_h, and label H+
Glyph with class glyph_nad, and label NAD+
Glyph with class glyph_nadh, and label NADH
Glyph with class pn1, without label
Arc with class consumption
Arc with class production
Arc with class catalysis
Arc with class production
Arc with class production
Arc with class consumption
Example code for writing an SBGN file:
File f = new File ("test-output.sbgn");
Sbgn sbgn = new Sbgn();
Map map = new Map();
sbgn.setMap(map);
// create a glyph with an id and class "macromolecule"
Glyph g1 = new Glyph();
g1.setId("glyph1");
g1.setClazz("macromolecule");
// add the glyph to the map
map.getGlyph().add (g1);
// define the bounding box of the glyph
Bbox bbox1 = new Bbox();
bbox1.setX(125);
bbox1.setY(60);
bbox1.setW(100);
bbox1.setH(40);
g1.setBbox(bbox1);
// define a label for this glyph
Label label1 = new Label();
label1.setText("P53");
// now write everything to disk
SbgnUtil.writeToFile(sbgn, f);
This will produce an SBGN-ML document called "test-output.sbgn", containing a single Glyph. In XML it look like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sbgn ns="http://sbgn.org/libsbgn/pd/0.1">
<map>
<glyph class="macromolecule" id="glyph1">
<bbox w="100.0" h="40.0" x="125.0" y="60.0"/>
</glyph>
</map>
</sbgn>
Validating a file is very simple, see the snippet below. To run this, you need to download a copy of SBGN.xsd
Note that this validation is not comprehensive: it checks only for certain kinds of errors, but a passed validation does not guarantee that your diagram is fully SBGN compliant.
File xsd = new File ("SBGN.xsd");
File f = new File ("adh.sbgn");
boolean isValid = SbgnUtil.isValid(f, xsd);
if (isValid)
System.out.println ("Validation succeeded");
else
System.out.println ("Validation failed");
If validation didn't succeed, the method isValid() will return false and an error message will be printed to the standard error stream. See also: ValidationExample.java
Information for using SBGN with python is available from
https://github.com/matthiaskoenig/libsbgn-python
A tutorial for C++ developers can be found at [[Developer_Tutorial_C++]].
For information about how to extend LibSBGN for arbitrary data, please see [[SBGN-ML_Extensions]].
If you have further questions after reading this, you can always contact the mailing-list at sbgn-libsbgn@lists.sourceforge.net