libSBML C++ API  5.20.2
printMath.cpp

Prints the rule, reaction, and event formulas in a given SBML document.

/**
* @file printMath.cpp
* @brief Prints Rule, Reaction, and Event formulas in a given SBML Document
* @author Ben Bornstein
* @author Sarah Keating
*
* <!--------------------------------------------------------------------------
* 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 <sbml/SBMLTypes.h>
using namespace std;
LIBSBML_CPP_NAMESPACE_USE
void
printFunctionDefinition (unsigned int n, const FunctionDefinition* fd)
{
const ASTNode* math;
char* formula;
if ( fd->isSetMath() )
{
cout << "FunctionDefinition " << n << ", " << fd->getId();
math = fd->getMath();
/* Print function arguments. */
if (math->getNumChildren() > 1)
{
cout << "(" << ( math->getLeftChild() )->getName();
for (n = 1; n < math->getNumChildren() - 1; ++n)
{
cout <<", " << ( math->getChild(n) )->getName();
}
}
cout <<") := ";
/* Print function body. */
if (math->getNumChildren() == 0)
{
cout << "(no body defined)";
}
else
{
math = math->getChild(math->getNumChildren() - 1);
formula = SBML_formulaToString(math);
cout << formula << endl;
free(formula);
}
}
}
void
printRuleMath (unsigned int n, const Rule* r)
{
char* formula;
if ( r->isSetMath() )
{
formula = SBML_formulaToString( r->getMath() );
if (r->getVariable().length() > 0)
{
cout << "Rule " << n << ", formula: "
<< r->getVariable() << " = " << formula << endl;
}
else
{
cout << "Rule " << n << ", formula: "
<< formula << " = 0" << endl;
}
free(formula);
}
}
void
printReactionMath (unsigned int n, const Reaction* r)
{
char* formula;
const KineticLaw* kl;
if (r->isSetKineticLaw())
{
kl = r->getKineticLaw();
if ( kl->isSetMath() )
{
formula = SBML_formulaToString( kl->getMath() );
cout << "Reaction " << n << ", formula: " << formula << endl;
free(formula);
}
}
}
void
printEventAssignmentMath (unsigned int n, const EventAssignment* ea)
{
std::string variable;
char* formula;
if ( ea->isSetMath() )
{
variable = ea->getVariable();
formula = SBML_formulaToString( ea->getMath() );
cout <<" EventAssignment " << n
<< ", trigger: " << variable << " = " << formula << endl;
free(formula);
}
}
void
printEventMath (unsigned int n, const Event* e)
{
char* formula;
unsigned int i;
if ( e->isSetDelay() )
{
formula = SBML_formulaToString( e->getDelay()->getMath() );
cout << "Event " << n << " delay: " << formula << endl;
free(formula);
}
if ( e->isSetTrigger() )
{
formula = SBML_formulaToString( e->getTrigger()->getMath() );
cout << "Event " << n << " trigger: " << formula << endl;
free(formula);
}
for (i = 0; i < e->getNumEventAssignments(); ++i)
{
printEventAssignmentMath(i + 1, e->getEventAssignment(i));
}
cout << endl;
}
void
printMath (const Model* m)
{
unsigned int n;
for (n = 0; n < m->getNumFunctionDefinitions(); ++n)
{
printFunctionDefinition(n + 1, m->getFunctionDefinition(n));
}
for (n = 0; n < m->getNumRules(); ++n)
{
printRuleMath(n + 1, m->getRule(n));
}
cout << endl;
for (n = 0; n < m->getNumReactions(); ++n)
{
printReactionMath(n + 1, m->getReaction(n));
}
cout << endl;
for (n = 0; n < m->getNumEvents(); ++n)
{
printEventMath(n + 1, m->getEvent(n));
}
}
int
main (int argc, char* argv[])
{
if (argc != 2)
{
cout << endl << "Usage: printMath filename" << endl << endl;
return 1;
}
const char* filename = argv[1];
SBMLDocument* document = readSBML(filename);
if (document->getNumErrors() > 0)
{
cerr << "Encountered the following SBML errors:" << endl;
document->printErrors(cerr);
return 1;
}
Model* model = document->getModel();
if (model == 0)
{
cout << "No model present." << endl;
return 1;
}
printMath(model);
cout << endl;
delete document;
return 0;
}
char * SBML_formulaToString(const ASTNode_t *tree)
Converts an AST to a string representation of a formula using a syntax basically derived from SBML Le...
Definition: FormulaFormatter.cpp:56
SBMLDocument_t * readSBML(const char *filename)
Reads an SBML document from the given file.
Include all SBML types in a single header file.
Definition: ASTNode.h:221
ASTNode * getLeftChild() const
Returns the left child of this node.
Definition: ASTNode.cpp:1031
unsigned int getNumChildren() const
Returns the number of children of this node.
Definition: ASTNode.cpp:1060
ASTNode * getChild(unsigned int n) const
Returns the child at index n of this node.
Definition: ASTNode.cpp:1019
virtual const ASTNode * getMath() const
Get the mathematical formula for the delay and return it as an AST.
Definition: Delay.cpp:168
Definition: EventAssignment.h:296
const std::string & getVariable() const
Get the value of this EventAssignment's "variable" attribute.
Definition: EventAssignment.cpp:166
virtual const ASTNode * getMath() const
Get the mathematical expression in this EventAssignment's "math" subelement.
Definition: EventAssignment.cpp:176
bool isSetMath() const
Predicate for testing whether the "math" subelement of this EventAssignment is set.
Definition: EventAssignment.cpp:198
Definition: Event.h:355
const EventAssignment * getEventAssignment(unsigned int n) const
Return a specific EventAssignment object of this Event.
Definition: Event.cpp:1055
bool isSetDelay() const
Predicate for testing whether the delay for this Event is set.
Definition: Event.cpp:465
bool isSetTrigger() const
Predicate for testing whether the trigger for this Event is set.
Definition: Event.cpp:455
unsigned int getNumEventAssignments() const
Returns the number of EventAssignment objects attached to this Event.
Definition: Event.cpp:1097
const Trigger * getTrigger() const
Get the event trigger portion of this Event.
Definition: Event.cpp:346
const Delay * getDelay() const
Get the assignment delay portion of this Event, if there is one.
Definition: Event.cpp:366
Definition: FunctionDefinition.h:150
virtual const std::string & getId() const
Returns the value of the "id" attribute of this FunctionDefinition.
Definition: FunctionDefinition.cpp:162
virtual const ASTNode * getMath() const
Get the mathematical formula of this FunctionDefinition.
Definition: FunctionDefinition.cpp:182
bool isSetMath() const
Predicate returning true if this FunctionDefinition's "math" subelement contains a value.
Definition: FunctionDefinition.cpp:216
Definition: KineticLaw.h:203
virtual const ASTNode * getMath() const
Returns the mathematical formula for this KineticLaw object and return it as as an AST.
Definition: KineticLaw.cpp:263
bool isSetMath() const
Predicate returning true if this Kinetic's "math" subelement is set.
Definition: KineticLaw.cpp:310
Definition: Model.h:485
unsigned int getNumFunctionDefinitions() const
Get the number of FunctionDefinition objects in this Model.
Definition: Model.cpp:3073
const Rule * getRule(unsigned int n) const
Get the nth Rule object in this Model.
Definition: Model.cpp:2714
unsigned int getNumRules() const
Get the number of Rule objects in this Model.
Definition: Model.cpp:3172
const Reaction * getReaction(unsigned int n) const
Get the nth Reaction object in this Model.
Definition: Model.cpp:2925
unsigned int getNumReactions() const
Get the number of Reaction objects in this Model.
Definition: Model.cpp:3192
const Event * getEvent(unsigned int n) const
Get the nth Event object in this Model.
Definition: Model.cpp:3031
const FunctionDefinition * getFunctionDefinition(unsigned int n) const
Get the nth FunctionDefinitions object in this Model.
Definition: Model.cpp:2354
unsigned int getNumEvents() const
Get the number of Event objects in this Model.
Definition: Model.cpp:3202
Definition: Reaction.h:224
const KineticLaw * getKineticLaw() const
Returns the KineticLaw object contained in this Reaction.
Definition: Reaction.cpp:396
bool isSetKineticLaw() const
Predicate returning true if this Reaction contains a kinetic law object.
Definition: Reaction.cpp:470
Definition: Rule.h:163
virtual const ASTNode * getMath() const
Get the mathematical formula of this Rule as an ASTNode tree.
Definition: Rule.cpp:204
const std::string & getVariable() const
Get the value of the "variable" attribute of this Rule object.
Definition: Rule.cpp:219
bool isSetMath() const
Predicate returning true if this Rule's mathematical expression is set.
Definition: Rule.cpp:258
Definition: SBMLDocument.h:349
unsigned int getNumErrors() const
Returns the number of errors or warnings encountered during parsing, consistency checking,...
Definition: SBMLDocument.cpp:1163
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:1186
const Model * getModel() const
Returns the Model object stored in this SBMLDocument.
Definition: SBMLDocument.cpp:350
virtual const ASTNode * getMath() const
Get the mathematical formula for the trigger and return it as an AST.
Definition: Trigger.cpp:182