1#
2# @file    printMath.R
3# @brief   Prints Rule, Reaction, and Event formulas in a given SBML Document
4# @author  Frank Bergmann
5#
6# <!--------------------------------------------------------------------------
7# This sample program is distributed under a different license than the rest
8# of libSBML.  This program uses the open-source MIT license, as follows:
9#
10# Copyright (c) 2013-2018 by the California Institute of Technology
11# (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
12# and the University of Heidelberg (Germany), with support from the National
13# Institutes of Health (USA) under grant R01GM070923.  All rights reserved.
14#
15# Permission is hereby granted, free of charge, to any person obtaining a
16# copy of this software and associated documentation files (the "Software"),
17# to deal in the Software without restriction, including without limitation
18# the rights to use, copy, modify, merge, publish, distribute, sublicense,
19# and/or sell copies of the Software, and to permit persons to whom the
20# Software is furnished to do so, subject to the following conditions:
21#
22# The above copyright notice and this permission notice shall be included in
23# all copies or substantial portions of the Software.
24#
25# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31# DEALINGS IN THE SOFTWARE.
32#
33# Neither the name of the California Institute of Technology (Caltech), nor
34# of the European Bioinformatics Institute (EMBL-EBI), nor of the University
35# of Heidelberg, nor the names of any contributors, may be used to endorse
36# or promote products derived from this software without specific prior
37# written permission.
38# ------------------------------------------------------------------------ -->
39#
40# Usage: R --slave -f printMath.R --args <full path to input file>
41#
42library(libSBML)
43
44args <- commandArgs(trailingOnly = TRUE)
45
46if (length(args) != 1)
47{
48  stop("Usage: printMath input-filename\n");
49}
50
51
52printFunctionDefinition <- function(n, fd) {
53  if ( FunctionDefinition_isSetMath(fd) )
54  {
55    cat("FunctionDefinition ",n,", ",FunctionDefinition_getId(fd),"(");
56
57    math = FunctionDefinition_getMath(fd);
58
59    # Print function arguments.
60    if (ASTNode_getNumChildren(math) > 1) {
61      cat(ASTNode_getName( ASTNode_getLeftChild(math) ));
62
63      for (n in seq_len(ASTNode_getNumChildren(math) - 1)) {
64        cat(", ", ASTNode_getName( ASTNode_getChild(math, n) ));
65      }
66    }
67
68    cat(") := ");
69
70    # Print function body.
71    if (ASTNode_getNumChildren(math) == 0) {
72      cat("(no body defined)");
73    } else {
74      math    = ASTNode_getChild(math, ASTNode_getNumChildren(math) - 1);
75      formula = formulaToString(math);
76      cat(formula,"\n");
77    }
78  }
79}
80
81
82printRuleMath <- function(n, r) {
83  if ( Rule_isSetMath(r) ) {
84    formula = formulaToString( Rule_getMath(r) );
85    cat("Rule ",n,", formula: ",formula,"\n");
86  }
87}
88
89
90printReactionMath <- function(n, r)
91{
92  if (Reaction_isSetKineticLaw(r)) {
93    kl = Reaction_getKineticLaw(r);
94
95    if ( KineticLaw_isSetMath(kl) ) {
96      formula = formulaToString( KineticLaw_getMath(kl) );
97      cat("Reaction ",n,", formula: ",formula,"\n");
98    }
99  }
100}
101
102
103printEventAssignmentMath <- function(n, ea) {
104  if ( EventAssignment_isSetMath(ea) ) {
105    variable = EventAssignment_getVariable(ea);
106    formula  = formulaToString( EventAssignment_getMath(ea) );
107
108    cat("  EventAssignment ",n,", trigger: ",variable," = ",formula,"\n");
109
110  }
111}
112
113
114printEventMath <- function(n, e) {
115  if ( Event_isSetDelay(e) ) {
116    delay = Event_getDelay(e);
117    formula = formulaToString( Delay_getMath(delay) );
118    cat("Event ",n," delay: ",formula,"\n");
119  }
120
121  if ( Event_isSetTrigger(e) ) {
122    trigger = Event_getTrigger(e);
123
124    formula = formulaToString( Trigger_getMath(trigger) );
125    cat("Event ",n," trigger: ",formula,"\n");
126  }
127
128  for (i in seq_len(Event_getNumEventAssignments(e))) {
129    printEventAssignmentMath(i, Event_getEventAssignment(e, i-1));
130  }
131
132  cat("\n");
133}
134
135
136printMath <- function(m) {
137
138  for (n in seq_len(Model_getNumFunctionDefinitions(m))){
139    printFunctionDefinition(n, Model_getFunctionDefinition(m, n-1));
140  }
141
142  for (n in seq_len(Model_getNumRules(m))){
143    printRuleMath(n , Model_getRule(m, n-1));
144  }
145
146  cat("\n");
147
148  for (n in seq_len(Model_getNumReactions(m))){
149    printReactionMath(n, Model_getReaction(m, n-1));
150  }
151
152  cat("\n");
153
154  for (n in seq_len(Model_getNumEvents(m))){
155    printEventMath(n , Model_getEvent(m, n-1));
156  }
157}
158
159
160
161d = readSBML(args[1]);
162m = SBMLDocument_getModel(d);
163
164SBMLDocument_printErrors(d);
165
166printMath(m);
167cat("\n");
168
169q(status=0);
170
171
172