1#!/usr/bin/env python 2## 3## @file printMath.py 4## @brief Prints Rule, Reaction, and Event formulas in a given SBML Document 5## @author Ben Bornstein 6## @author Sarah Keating 7## 8## 9## <!-------------------------------------------------------------------------- 10## This sample program is distributed under a different license than the rest 11## of libSBML. This program uses the open-source MIT license, as follows: 12## 13## Copyright (c) 2013-2018 by the California Institute of Technology 14## (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK) 15## and the University of Heidelberg (Germany), with support from the National 16## Institutes of Health (USA) under grant R01GM070923. All rights reserved. 17## 18## Permission is hereby granted, free of charge, to any person obtaining a 19## copy of this software and associated documentation files (the "Software"), 20## to deal in the Software without restriction, including without limitation 21## the rights to use, copy, modify, merge, publish, distribute, sublicense, 22## and/or sell copies of the Software, and to permit persons to whom the 23## Software is furnished to do so, subject to the following conditions: 24## 25## The above copyright notice and this permission notice shall be included in 26## all copies or substantial portions of the Software. 27## 28## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 31## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 33## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 34## DEALINGS IN THE SOFTWARE. 35## 36## Neither the name of the California Institute of Technology (Caltech), nor 37## of the European Bioinformatics Institute (EMBL-EBI), nor of the University 38## of Heidelberg, nor the names of any contributors, may be used to endorse 39## or promote products derived from this software without specific prior 40## written permission. 41## ------------------------------------------------------------------------ --> 42## 43 44 45import sys 46import os.path 47from libsbml import * 48 49def printFunctionDefinition(n, fd): 50 if fd.isSetMath(): 51 print("FunctionDefinition " + str(n) + ", " + fd.getId()) 52 53 math = fd.getMath() 54 55 # Print function arguments. 56 if math.getNumChildren() > 1: 57 print("(" + (math.getLeftChild()).getName()) 58 59 for n in range (1, math.getNumChildren()): 60 print(", " + (math.getChild(n)).getName()) 61 62 print(") := ") 63 64 # Print function body. 65 if math.getNumChildren() == 0: 66 print("(no body defined)") 67 else: 68 math = math.getChild(math.getNumChildren() - 1) 69 formula = formulaToString(math) 70 print(formula + "\n") 71 72 73def printRuleMath(n, r): 74 if r.isSetMath(): 75 formula = formulaToString(r.getMath()) 76 77 if len(r.getVariable()) > 0: 78 print("Rule " + str(n) + ", formula: " 79 + r.getVariable() + " = " + formula + "\n") 80 else: 81 print("Rule " + str(n) + ", formula: " 82 + formula + " = 0" + "\n") 83 84 85def printReactionMath(n, r): 86 if r.isSetKineticLaw(): 87 kl = r.getKineticLaw() 88 if kl.isSetMath(): 89 formula = formulaToString(kl.getMath()) 90 print("Reaction " + str(n) + ", formula: " + formula + "\n") 91 92 93def printEventAssignmentMath(n, ea): 94 if ea.isSetMath(): 95 variable = ea.getVariable() 96 formula = formulaToString(ea.getMath()) 97 print(" EventAssignment " + str(n) 98 + ", trigger: " + variable + " = " + formula + "\n") 99 100 101def printEventMath(n, e): 102 if e.isSetDelay(): 103 formula = formulaToString(e.getDelay().getMath()) 104 print("Event " + str(n) + " delay: " + formula + "\n") 105 106 if e.isSetTrigger(): 107 formula = formulaToString(e.getTrigger().getMath()) 108 print("Event " + str(n) + " trigger: " + formula + "\n") 109 110 for i in range(0,e.getNumEventAssignments()): 111 printEventAssignmentMath(i + 1, e.getEventAssignment(i)) 112 113 print() 114 115 116def printMath(m): 117 for n in range(0,m.getNumFunctionDefinitions()): 118 printFunctionDefinition(n + 1, m.getFunctionDefinition(n)) 119 120 for n in range(0,m.getNumRules()): 121 printRuleMath(n + 1, m.getRule(n)) 122 123 print() 124 for n in range(0, m.getNumReactions()): 125 printReactionMath(n + 1, m.getReaction(n)) 126 127 print() 128 for n in range(0,m.getNumEvents()): 129 printEventMath(n + 1, m.getEvent(n)) 130 131 132def main (args): 133 """Usage: printMath filename 134 """ 135 if len(args) != 2: 136 print("\n" + "Usage: printMath filename" + "\n" + "\n") 137 return 1 138 139 filename = args[1] 140 document = readSBML(filename) 141 142 if document.getNumErrors() > 0: 143 print("Encountered the following SBML errors:" + "\n") 144 document.printErrors() 145 return 1 146 147 model = document.getModel() 148 149 if model is None: 150 print("No model present." + "\n") 151 return 1 152 153 printMath(model) 154 print() 155 return 0 156 157 158if __name__ == '__main__': 159 main(sys.argv) 160