1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
================================================================================
How to use package extensions in user's code.
================================================================================
This documentation describes the summary of how to manipulate an SBML
document with package extensions in user's code.
------------------------------------------------------------
Table of contents:
1. Required header files
2. Read/Write an SBML document with package extensions
3. Get SBML elements of package extensions
4. Creates an SBML document with package extensions
5. Compile a program using package extensions
------------------------------------------------------------
--------------------------------------------------------------------------------
1. Required header files
--------------------------------------------------------------------------------
To use a package extension in your program, header files of the following
classes need to be included:
(1) SBMLExtension derived class of the package
(e.g. GroupsExtension class is the derived class of the groups
package.)
(2) one or more SBasePlugin derived classes of the package
(e.g. GroupsModelPlugin class is the derived class of the groups
package which is plugged in the Model class of the SBML core.)
(3) one or more SBase derived classes of the package
(e.g. Group, ListOfGroups, Member, and ListOfMembers are the SBase
derived classes of the groups package.)
Each pacakge extension provides a single header file which serves to include
most of the above header files in a similar fashion to the SBMLTypes.h file
in libSBML core.
For example, GroupsExtensionTypes.h is provided by the groups package.
So, basically, the following two inclusions are sufficient for a program using
the groups package extension.
#include "sbml/SBMLTypes.h"
#include "sbml/groups/GroupsExtensionTypes.h"
Similarly, the following two inclusions are sufficient for a program using
the layout package extension.
#include "sbml/SBMLTypes.h"
#include "sbml/layout/LayoutExtensionTypes.h"
--------------------------------------------------------------------------------
2. Read/Write an SBML document with package extensions
--------------------------------------------------------------------------------
An SBML document with package extensions can be read from a file or an
in-memory character string by the following existing two global functions :
1. SBMLDocument* readSBML(const char* filename)
2. SBMLDocument* readSBMLFromString(const char* xml)
If the SBML document contains an unknown package extension (e.g. a libSBML
extension for the package is not linked), there are two possible
outcomes. If the 'required' attribute of the package in the SBMLDocument
object is 'false' then the elements/attributes of the unknown package are
preserved by libSBML but it makes no attempt to interpret them. If the
'required' attribute is true, reading the document fails and an error is
logged.
To write an SBML document with package extensions to a named file or a
character string, the following existing two global functions can be used:
1. int writeSBML(const SBMLDocument* d, const char* filename)
2. char* writeSBMLFromString(const SBMLDocument *d)
Note that if the given SBMLDocument object contains information about an
unknown package extension that has been preserved by the libSBML read
functions, then the elements/attributes of the unknown package are also
written to the file or string as-is.
--------------------------------------------------------------------------------
3. Get SBML elements of package extensions
--------------------------------------------------------------------------------
In libSBML-5, elements/attributes of each package extension need to be
accessed via each SBasePlugin derived object which is plugged in an SBML
element to be extended.
For example, two SBasePlugin derived classes, SBMLDocumentPlugin and
GroupsModelPlugin, are used by the groups extension.
SBMLDocumentPlugin, which is plugged in the sbml element, is a common plugin
class for pacakge extensions which use only 'required' attribute in the
SBMLDocument class, and thus the class is basically not used in user's
code (**).
On the other hand, GroupsModelPlugin, which is plugged into the model element,
provides public functions for manipulating the top level element of the
extension (ListOfGroups) such as getGroup(), addGroup(), createGroup(),
removeGroup(), getNumGroups() and etc.
---------------------------------------------------------------------------
(**) 'required' attribute of each package extension in <sbml> element
can be accessed via functions in SBMLDocument class:
getPkgRequired(string), setPkgRequired(string,bool), and
isSetPkgRequired(string).
Thus, SBMLDocumentPlugin class is not directly used for manipulating
the attribute in user's code.
---------------------------------------------------------------------------
Each plugin object is internally automatically created and plugged in the
extended SBML element when the SBML element is created by its constructor with
information of the package extension (the information is an xmlns attribute of
the package extension which is contained in an SBMLNamespaces object).
The following example code shows how to get a group and a member object of the
groups extension from an input SBMLDocument object:
========================================================================
#include <iostream>
#include "sbml/SBMLTypes.h"
#include "sbml/groups/GroupsExtensionTypes.h"
using namespace std;
int main(int argc, char* argv[])
{
SBMLDocument *document = readSBML("groups_example1.xml")
Model *model = document->getModel();
//
// Get a GroupsModelPlugin object plugged in the model object.
//
// The type of the returned value of SBase::getPlugin() function is
// SBasePlugin*, and thus the value needs to be cast for the
// corresponding derived class.
//
GroupsModelPlugin* mplugin;
mplugin = static_cast<GroupsModelPlugin*>(model->getPlugin("groups"));
//
// get a Group object via GroupsModelPlugin object.
//
Group* group = mplugin->getGroup(0);
cout << "Group id: " << group->getId() << endl;
cout << "Group SBOTerm: " << group->getSBOTerm() << endl;
//
// get a Member object via the Group object.
//
Member* member = group->getMember(0);
std::cout << "Member symbol: " << member->getSymbol() << std::endl;
delete document;
}
(error check code is omitted in the example code.)
===========================================================================
--------------------------------------------------------------------------------
4. Create an SBML document with package extensions
--------------------------------------------------------------------------------
To create an SBML document with package extensions, an SBMLNamespaces object
with the xmlns attributes of the package extensions needs to be passed to
the constructor of the SBMLDocument class, by which plugin objects of the
package extensions are automatically internally created and plugged into the
corresponding SBML elements as above mentioned.
For example, an SBMLDocument object with group and layout packages can be
created as follows:
===========================================================================
//
// Creates an SBMLNamespaces with Layout Level 3 Version 1 Package Version1
// and Groups Level 3 Version 1 Package Version 1
//
SBMLNamespaces sbmlns(3,1);
sbmlns.addPkgNamespace("layout",1);
sbmlns.addPkgNamespace("groups",1);
//
// Creates an SBML Level 3 Version 1 document with layout and group Version 1.
//
SBMLDocument document(&sbmlns);
===========================================================================
The above code for creating an SBMLNamespaces object can be replaced with one
of the following other styles.
===========================================================================
(1) Create an SBMLNamespace object with the given SBML level, version,
one of package names, package version, and then adds a namespace
of another package to the object.
SBMLNamespaces sbmlns(3,1,"layout",1);
sbmlns.addPkgNamespace("groups",1);
OR
SBMLNamespaces sbmlns(3,1,"groups",1);
sbmlns.addPkgNamespace("layout",1);
(2) Create a GroupsPkgNamespaces (*) object with the given SBML level,
version, and package version and then adds a namespace of another
package to the object.
GroupsPkgNamespaces sbmlns(3,1,1);
sbmlns.addPkgNamespace("layout",1);
----------------------------------------------------------------------
(*) GroupPkgNamespaces is an SBMLNamespace derived class for the groups
package. The class is basically used when creating an SBase derived
object defined in the groups package (e.g. Group, Member) by the
constructor.
----------------------------------------------------------------------
(3) Create a LayoutPkgNamespaces (**) object with the given SBML level,
version, and package version and then adds a namespace of another package
to the object.
LayoutPkgNamespaces sbmlns(3,1,1);
sbmlns.addPkgNamespace("groups",1);
----------------------------------------------------------------------
(**) LayoutPkgNamespaces is an SBMLNamespace derived class for the
layout package. The class is basically used for creating an SBase
derived object defined in the layout package.
----------------------------------------------------------------------
===========================================================================
The following example code shows how to create an SBMLDocument with elements of
the groups extension:
========================================================================
#include <iostream>
#include "sbml/SBMLTypes.h"
#include "sbml/groups/GroupsExtensionTypes.h"
using namespace std;
int main(int argc, char* argv[])
{
// SBMLNamespaces of SBML Level 3 Version 1 with Groups Version 1
SBMLNamespaces sbmlns(3,1,"groups",1);
// create the L3V1 document with groups package
SBMLDocument document(&sbmlns);
// create the Model
Model *model = document.createModel();
// create the Compartment
Compartment* compartment = model->createCompartment();
compartment->setId("cytosol");
compartment->setConstant(true);
// create the Species
Species* species = model->createSpecies();
species->setId("ATPc");
species->setCompartment("cytosol");
species->setInitialConcentration(1);
species->setHasOnlySubstanceUnits(false);
species->setBoundaryCondition(false);
species->setConstant(false);
//
// Get a GroupsModelPlugin object plugged in the model object.
//
// The type of the returned value of SBase::getPlugin() function is
// SBasePlugin*, and thus the value needs to be casted for the
// corresponding derived class.
//
GroupsModelPlugin* mplugin;
mplugin = static_cast<GroupsModelPlugin*>(model->getPlugin("groups"));
//
// Creates a Group object via GroupsModelPlugin object.
//
Group* group = mplugin->createGroup();
group->setId("ATP");
group->setSBMLTerm("SBO:0000252");
Member* member = group->createMember();
member->setSymbol("ATPc");
writeSBML(&document, "groups_example.xml");
}
(error check code is omitted in the example code.)
===========================================================================
If you want to read an SBML file without a package extension and want to
add package extensions to the SBML document later, then SBase::enablePackage()
function needs to be invoked via the SBMLDocument object. The function has
arguments (1) the URI of the package, (2) the prefix of the package, and
(3) "true".
For example, groups package can be enabled by the function as follows:
===========================================================================
SBMLDocument *document = readSBML("sbmlcore-only.xml");
//
// enable (add) the groups package (L3V1 groups V1)
//
string pkgURI = GroupsExtension::XmlnsL3V1V1;
document->enablePackage(pkgURI,"groups",true)
===========================================================================
Similarly, if you want to remove package extensions from the SBML document,
then SBase::enablePackage() function needs to be invoked with "false" value
via the SBMLDocument object as follows:
===========================================================================
SBMLDocument *document = readSBML("sbml-with-groups.xml");
//
// disable (remove) the groups package (L3V1 groups V1)
//
string pkgURI = GroupsExtension::XmlnsL3V1V1;
document->enablePackage(pkgURI,"groups",false)
===========================================================================
--------------------------------------------------------------------------------
5. Compile a program using package extensions
--------------------------------------------------------------------------------
A program which uses package extensions must be linked with not only a shared
library of the SBML core package but also those of the package extensions.
For example, if the shared library files of the SBML core package, the layout
package, and the groups package are provided as separate files (e.g. libsbml.so
(core), libsbml-layout.so (layout), and libsbml-groups.so (groups) on Linux),
then a program (e.g. example.cpp) which manipulates those packages are compiled
with linker flags (-lsbml -lsbml-layout -lsbml-groups) as follows:
g++ -o example example.cpp -lsbml -lsbml-layout -lsbml-groups
--------------------------------------------------------------------------------
(NOTE1) To build the shared library files of layout and/or groups extensions,
the libSBML-5 needs to be built with "--enable-layout" and/or
"--enable-groups" configure options.
--------------------------------------------------------------------------------
|