libSBML Python API  5.18.0
translateMath.py

Translates infix formulas into MathML and vice-versa.

1 #!/usr/bin/env python
2 ##
3 ## @file translateMath.py
4 ## @brief Translates infix formulas into MathML and vice-versa
5 ## @author Sarah Keating
6 ## @author Ben Bornstein
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 import sys
45 import time
46 import os
47 import os.path
48 from libsbml import *
49 
50 
51 #
52 # Translates the given infix formula into MathML.
53 #
54 # @return the MathML as a string. The caller owns the memory and is
55 # responsible for freeing it.
56 #
57 def translateInfix(formula):
58  math = parseFormula(formula)
59  return writeMathMLToString(math)
60 
61 
62 #
63 # Translates the given MathML into an infix formula. The MathML must
64 # contain no leading whitespace, but an XML header is optional.
65 #
66 # @return the infix formula as a string. The caller owns the memory and
67 # is responsible for freeing it.
68 #
69 def translateMathML(xml):
70  math = readMathMLFromString(xml)
71  return formulaToString(math)
72 
73 
74 def main(args):
75  """Usage: readSBML filename
76  """
77 
78  print("This program translates infix formulas into MathML and")
79  print("vice-versa. Enter or return on an empty line triggers")
80  print("translation. Ctrl-C quits")
81 
82  sb = ""
83  try:
84  while True:
85  print("Enter infix formula or MathML expression (Ctrl-C to quit):")
86  print("> ")
87 
88  line = sys.stdin.readline()
89  while line is not None:
90  trimmed = line.strip()
91  length = len(trimmed)
92  if length > 0:
93  sb = sb + trimmed
94  else:
95  str = sb
96  result = ""
97  if str[0] == '<':
98  result = translateMathML(str)
99  else:
100  result = translateInfix(str)
101 
102  print("Result:\n\n" + result + "\n\n")
103  sb = ""
104  break
105 
106  line = sys.stdin.readline()
107  except:
108  pass
109  return 0
110 
111 
112 if __name__ == '__main__':
113  main(sys.argv)