libSBML C++ API  libSBML 5.18.0 C++ API
spec_example3.cpp

Example #3 from the SBML Level 3 Hierarchical Model Composition specification.

/**
* @file spec_example3.cpp
* @brief SBML hierarchical composition example
* @author Lucian Smith
*
* <!--------------------------------------------------------------------------
* This sample program is distributed under a different license than the rest
* of libSBML. This program uses the open-source MIT license, as follows:
*
* Copyright (c) 2013-2018 by the California Institute of Technology
* (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
* and the University of Heidelberg (Germany), with support from the National
* Institutes of Health (USA) under grant R01GM070923. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Neither the name of the California Institute of Technology (Caltech), nor
* of the European Bioinformatics Institute (EMBL-EBI), nor of the University
* of Heidelberg, nor the names of any contributors, may be used to endorse
* or promote products derived from this software without specific prior
* written permission.
* ------------------------------------------------------------------------ -->
*/
#include <iostream>
#include <sstream>
#include <sbml/SBMLTypes.h>
#ifdef WIN32
#include <conio.h>
#endif
using namespace std;
int main(int argc,char** argv)
{
int retval = 0;
SBMLNamespaces sbmlns(3,1,"comp",1);
// create the document
SBMLDocument *document = new SBMLDocument(&sbmlns);
//Define the external model definition
= static_cast<CompSBMLDocumentPlugin*>(document->getPlugin("comp"));
compdoc->setRequired(true);
extmod->setId("ExtMod1");
extmod->setSource("enzyme_model.xml");
extmod->setModelRef("enzyme");
//Define the 'simple' model
mod1->setId("simple");
comp->setSpatialDimensions((unsigned int)3);
comp->setConstant(true);
comp->setId("comp");
comp->setSize(1L);
// We have to construct it this way because we get the comp
// plugin from it later.
Species spec(&sbmlns);
spec.setCompartment("comp");
spec.setConstant(false);
spec.setBoundaryCondition(false);
spec.setId("S");
mod1->addSpecies(&spec);
spec.setId("D");
mod1->addSpecies(&spec);
Reaction rxn(&sbmlns);
rxn.setReversible(true);
rxn.setFast(false);
rxn.setId("J0");
SpeciesReference sr(&sbmlns);
sr.setConstant(true);
sr.setSpecies("S");
rxn.addReactant(&sr);
sr.setSpecies("D");
rxn.addProduct(&sr);
mod1->addReaction(&rxn);
CompModelPlugin* mod1plug
= static_cast<CompModelPlugin*>(mod1->getPlugin("comp"));
Port port;
port.setId("S_port");
port.setIdRef("S");
mod1plug->addPort(&port);
Port* port2 = mod1plug->createPort();
port2->setId("D_port");
port2->setIdRef("D");
port.setId("comp_port");
port.setIdRef("comp");
mod1plug->addPort(&port);
port.setId("J0_port");
port.setIdRef("J0");
mod1plug->addPort(&port);
// create the Model
Model* model=document->createModel();
model->setId("complexified");
// Set the submodels
CompModelPlugin* mplugin
= static_cast<CompModelPlugin*>(model->getPlugin("comp"));
Submodel* submod1 = mplugin->createSubmodel();
submod1->setId("A");
submod1->setModelRef("ExtMod1");
Submodel* submod2 = mplugin->createSubmodel();
submod2->setId("B");
submod2->setModelRef("simple");
Deletion* del = submod2->createDeletion();
del->setPortRef("J0_port");
// Synchronize the compartments
Compartment* mcomp=model->createCompartment();
mcomp->setSpatialDimensions((unsigned int)3);
mcomp->setConstant(true);
mcomp->setId("comp");
mcomp->setSize(1L);
CompSBasePlugin* compartplug
= static_cast<CompSBasePlugin*>(mcomp->getPlugin("comp"));
re.setIdRef("comp");
re.setSubmodelRef("A");
compartplug->addReplacedElement(&re);
re.setSubmodelRef("B");
re.unsetIdRef();
re.setPortRef("comp_port");
compartplug->addReplacedElement(&re);
//Synchronize the species
spec.setId("S");
CompSBasePlugin* specplug
= static_cast<CompSBasePlugin*>(spec.getPlugin("comp"));
sre->setSubmodelRef("A");
sre->setIdRef("S");
ReplacedBy* srb = specplug->createReplacedBy();
srb->setSubmodelRef("B");
srb->setPortRef("S_port");
model->addSpecies(&spec);
spec.setId("D");
sre->setIdRef("D");
srb->setPortRef("D_port");
model->addSpecies(&spec);
writeSBMLToFile(document,"eg-ports.xml");
writeSBMLToFile(document,"spec_example3.xml");
delete document;
document = readSBMLFromFile("spec_example3.xml");
if (document == NULL)
{
cout << "Error reading back in file." << endl;
retval = -1;
}
else
{
document->checkConsistency();
if (document->getErrorLog()->getNumFailsWithSeverity(2) > 0
|| document->getErrorLog()->getNumFailsWithSeverity(3) > 0)
{
stringstream errorstream;
document->printErrors(errorstream);
cout << "Errors encoutered when round-tripping SBML file: \n"
<< errorstream.str() << endl;
retval = -1;
}
writeSBMLToFile(document, "spec_example3_rt.xml");
delete document;
}
#ifdef WIN32
if (retval != 0)
{
cout << "(Press any key to exit.)" << endl;
_getch();
}
#endif
return retval;
}
SpeciesReference
A reference to an SBML species in a reaction.
Definition: SpeciesReference.h:275
SBMLExtensionRegistry.h
The registry class for tracking package extensions.
Compartment::setConstant
int setConstant(bool value)
Sets the value of the "constant" attribute of this Compartment object.
Definition: Compartment.cpp:681
Reaction
An SBML reaction between species in an SBML model.
Definition: Reaction.h:218
ExternalModelDefinition::setModelRef
virtual int setModelRef(const std::string &id)
Sets the value of the "modelRef" attribute of this ExternalModelDefinition.
Definition: ExternalModelDefinition.cpp:222
Species::setInitialConcentration
int setInitialConcentration(double value)
Sets the "initialConcentration" attribute of this Species and marks the field as set.
Definition: Species.cpp:699
Port
An interface to be used in composition.
Definition: Port.h:85
ExternalModelDefinition::setSource
virtual int setSource(const std::string &source)
Sets the value of the "source" attribute of this ExternalModelDefinition.
Definition: ExternalModelDefinition.cpp:328
SBMLTypes.h
Include all SBML types in a single header file.
Reaction::setReversible
int setReversible(bool value)
Sets the value of the "reversible" attribute of this Reaction.
Definition: Reaction.cpp:607
Reaction::addProduct
int addProduct(const SpeciesReference *sr)
Adds a given SpeciesReference object as a product in this Reaction.
Definition: Reaction.cpp:847
ModelDefinition
A model used in model composition.
Definition: ModelDefinition.h:65
compExtensionRegistry
static SBMLExtensionRegister< CompExtension > compExtensionRegistry
Definition: CompExtension.cpp:104
Species::unsetInitialConcentration
int unsetInitialConcentration()
Unsets the "initialConcentration" attribute value of this Species object.
Definition: Species.cpp:961
SBMLDocument::createModel
Model * createModel(const std::string sid="")
Creates a new Model inside this SBMLDocument, and returns a pointer to it.
Definition: SBMLDocument.cpp:643
Reaction::setFast
int setFast(bool value)
Sets the value of the "fast" attribute of this Reaction.
Definition: Reaction.cpp:620
CompModelPlugin.h
Definition of CompModelPlugin, the plugin class of comp package for the Model element.
CompSBMLDocumentPlugin::setRequired
virtual int setRequired(bool value)
Sets the bool value of "required" attribute of corresponding package in SBMLDocument element.
Definition: CompSBMLDocumentPlugin.cpp:603
Compartment::setId
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this Compartment object.
Definition: Compartment.cpp:474
writeSBMLToFile
int writeSBMLToFile(const SBMLDocument_t *d, const char *filename)
Writes the given SBML document d to the file filename.
Reaction::addReactant
int addReactant(const SpeciesReference *sr)
Adds a given SpeciesReference object as a reactant in this Reaction.
Definition: Reaction.cpp:792
ExternalModelDefinition
A reference to an externally-defined model.
Definition: ExternalModelDefinition.h:122
Species::setConstant
int setConstant(bool value)
Sets the "constant" attribute of this Species object.
Definition: Species.cpp:824
Replacing::setSubmodelRef
virtual int setSubmodelRef(const std::string &id)
Sets the value of the "submodelRef" attribute of this SBaseRef.
Definition: Replacing.cpp:95
Compartment
An SBML compartment, where species are located.
Definition: Compartment.h:484
Deletion
Deletion of an object from a submodel.
Definition: Deletion.h:104
CompSBMLDocumentPlugin::createExternalModelDefinition
ExternalModelDefinition * createExternalModelDefinition()
Creates a ExternalModelDefinition object, adds it to the end of the ExternalModelDefinition objects l...
Definition: CompSBMLDocumentPlugin.cpp:539
CompSBMLDocumentPlugin
Extension of SBMLDocument.
Definition: CompSBMLDocumentPlugin.h:88
CompSBasePlugin::createReplacedElement
ReplacedElement * createReplacedElement()
Creates a ReplacedElement object, adds it to the end of the ReplacedElement objects list and returns ...
Definition: CompSBasePlugin.cpp:370
SBMLExtensionRegister
Template class for extension package registration
Definition: SBMLExtensionRegister.h:68
CompSBasePlugin
Extension of SBase.
Definition: CompSBasePlugin.h:79
SimpleSpeciesReference::setSpecies
int setSpecies(const std::string &sid)
Sets the "species" attribute of this SimpleSpeciesReference.
Definition: SimpleSpeciesReference.cpp:192
Model
An SBML model.
Definition: Model.h:479
Species::setId
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this Species.
Definition: Species.cpp:586
CompModelPlugin::addPort
int addPort(const Port *port)
Adds a copy of the given Port object to the list of ports.
Definition: CompModelPlugin.cpp:411
SBaseRef::setPortRef
virtual int setPortRef(const std::string &id)
Sets the value of the "portRef" attribute of this SBaseRef.
Definition: SBaseRef.cpp:242
readSBMLFromFile
SBMLDocument_t * readSBMLFromFile(const char *filename)
SBaseRef::setIdRef
virtual int setIdRef(const std::string &id)
Sets the value of the "idRef" attribute of this SBaseRef.
Definition: SBaseRef.cpp:304
Port::setId
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this Port.
Definition: Port.cpp:98
SBMLDocument::checkConsistency
unsigned int checkConsistency()
Performs consistency checking and validation on this SBML document.
Definition: SBMLDocument.cpp:713
CompSBMLDocumentPlugin.h
Definition of CompSBMLDocumentPlugin, the plugin class of comp package for the Model element.
CompSBasePlugin::addReplacedElement
int addReplacedElement(const ReplacedElement *replacedElement)
Adds a copy of the given ReplacedElement object to the list of ReplacedElements.
Definition: CompSBasePlugin.cpp:320
Submodel::setModelRef
virtual int setModelRef(const std::string &modelRef)
Sets the value of the "modelRef" attribute of this Submodel.
Definition: Submodel.cpp:249
SBMLDocument::printErrors
void printErrors(std::ostream &stream=std::cerr) const
Prints all the errors or warnings encountered trying to parse, check, or translate this SBML document...
Definition: SBMLDocument.cpp:1207
CompModelPlugin::createPort
Port * createPort()
Creates a Port object, adds it to the end of the port objects list and returns a pointer to the newly...
Definition: CompModelPlugin.cpp:452
Submodel::setId
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this Submodel.
Definition: Submodel.cpp:173
CompExtension.h
Definition of CompExtension, the core module of comp package.
CompSBMLDocumentPlugin::createModelDefinition
ModelDefinition * createModelDefinition()
Creates a ModelDefinition object, adds it to the end of the ModelDefinition objects list and returns ...
Definition: CompSBMLDocumentPlugin.cpp:398
SBMLErrorLog::getNumFailsWithSeverity
unsigned int getNumFailsWithSeverity(unsigned int severity)
Returns the number of errors that have been logged with the given severity code.
Definition: SBMLErrorLog.cpp:337
CompSBasePlugin.h
Definition of CompSBasePlugin, the plugin class of comp package for the Model element.
Reaction::setId
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this Reaction.
Definition: Reaction.cpp:516
ReplacedBy
Indicates an object replaced by another.
Definition: ReplacedBy.h:65
SpeciesReference::setStoichiometry
int setStoichiometry(double value)
Sets the value of the "stoichiometry" attribute of this SpeciesReference.
Definition: SpeciesReference.cpp:307
LIBSBML_CAT_UNITS_CONSISTENCY
Definition: SBMLError.h:970
CompSBasePlugin::createReplacedBy
ReplacedBy * createReplacedBy()
Creates a new, empty ReplacedBy, adds it to this CompSBasePlugin and returns the created ReplacedBy.
Definition: CompSBasePlugin.cpp:442
SBMLExtensionRegister.h
Template class for registering extension packages.
Submodel::createDeletion
Deletion * createDeletion()
Creates a Deletion object, adds it to the end of the deletion objects list and returns a pointer to t...
Definition: Submodel.cpp:495
Model::addSpecies
int addSpecies(const Species *s)
Adds a copy of the given Species object to this Model.
Definition: Model.cpp:1274
SpeciesReference::setConstant
int setConstant(bool flag)
Sets the "constant" attribute of this SpeciesReference to the given boolean flag.
Definition: SpeciesReference.cpp:395
Model::addReaction
int addReaction(const Reaction *r)
Adds a copy of the given Reaction object to this Model.
Definition: Model.cpp:1394
SBMLNamespaces
Set of SBML Level + Version + namespace triples.
Definition: SBMLNamespaces.h:139
CompModelPlugin::createSubmodel
Submodel * createSubmodel()
Creates a Submodel object, adds it to the end of the submodel objects list and returns a pointer to t...
Definition: CompModelPlugin.cpp:311
SBMLDocument::getErrorLog
SBMLErrorLog * getErrorLog()
Returns the list of errors or warnings logged during parsing, consistency checking,...
Definition: SBMLDocument.cpp:1364
Species::setCompartment
int setCompartment(const std::string &sid)
Sets the "compartment" attribute of this Species object.
Definition: Species.cpp:665
ExternalModelDefinition::setId
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this ExternalModelDefinition.
Definition: ExternalModelDefinition.cpp:150
Model::setId
virtual int setId(const std::string &sid)
Sets the value of the "id" attribute of this Model.
Definition: Model.cpp:717
Model::createCompartment
Compartment * createCompartment()
Creates a new Compartment inside this Model and returns it.
Definition: Model.cpp:1563
CompModelPlugin
Extension of Model.
Definition: CompModelPlugin.h:81
SBMLDocument::setConsistencyChecks
void setConsistencyChecks(SBMLErrorCategory_t category, bool apply)
Controls the consistency checks that are performed when SBMLDocument::checkConsistency() is called.
Definition: SBMLDocument.cpp:691
Species
An SBML species – a pool of entities.
Definition: Species.h:423
Submodel
A model instance inside another model.
Definition: Submodel.h:150
Compartment::setSpatialDimensions
int setSpatialDimensions(unsigned int value)
Sets the "spatialDimensions" attribute of this Compartment object.
Definition: Compartment.cpp:556
LIBSBML_CPP_NAMESPACE_USE
#define LIBSBML_CPP_NAMESPACE_USE
Definition: libsbml-namespace.h:67
SBMLDocument
Overall SBML container object.
Definition: SBMLDocument.h:342
Compartment::setSize
int setSize(double value)
Sets the "size" attribute (or "volume" in SBML Level&#160;1) of this Compartment object.
Definition: Compartment.cpp:605
Species::setBoundaryCondition
int setBoundaryCondition(bool value)
Sets the "boundaryCondition" attribute of this Species object.
Definition: Species.cpp:791
SBase::getPlugin
SBasePlugin * getPlugin(const std::string &package)
Returns a plug-in object (extension interface) for an SBML Level&#160;3 package extension with the given p...
Definition: SBase.cpp:3460
Species::setHasOnlySubstanceUnits
int setHasOnlySubstanceUnits(bool value)
Sets the "hasOnlySubstanceUnits" attribute of this Species object.
Definition: Species.cpp:771
ReplacedElement
Indicates an object replaces another.
Definition: ReplacedElement.h:137