libSBML Python API  5.11.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-2014 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 #Translates the given infix formula into MathML.
52 #
53 #@return the MathML as a string. The caller owns the memory and is
54 #responsible for freeing it.
55 #
56 def translateInfix(formula):
57  math = parseFormula(formula);
58  return writeMathMLToString(math);
59 
60 #
61 # Translates the given MathML into an infix formula. The MathML must
62 # contain no leading whitespace, but an XML header is optional.
63 #
64 # @return the infix formula as a string. The caller owns the memory and
65 # is responsible for freeing it.
66 #
67 def translateMathML(xml):
68  math = readMathMLFromString(xml);
69  return formulaToString(math);
70 
71 def main (args):
72  """Usage: readSBML filename
73  """
74 
75 
76  print("This program translates infix formulas into MathML and");
77  print("vice-versa. Enter or return on an empty line triggers");
78  print("translation. Ctrl-C quits");
79 
80  sb = ""
81  try:
82  while True:
83  print("Enter infix formula or MathML expression (Ctrl-C to quit):");
84  print "> ",
85 
86  line = sys.stdin.readline()
87  while line != None:
88  trimmed = line.strip();
89  length = len(trimmed);
90  if (length > 0):
91  sb = sb + trimmed;
92  else:
93  str = sb;
94  result = ""
95  if (str[0] == '<'):
96  result = translateMathML(str)
97  else:
98  result = translateInfix(str)
99 
100  print("Result:\n\n" + result + "\n\n");
101  sb = "";
102  break;
103 
104  line = sys.stdin.readline()
105  except:
106  return 0;
107  return 0;
108 
109 if __name__ == '__main__':
110  main(sys.argv)