# How can I translate infix to mathml using the python interface to libsbml?

Assume I have an infix expression such as k1*s1, how do I use libsbml to translate this expression to mathml using the python interface?

• Welcome to the site. Can you include examples if what you have already tried? Jul 18, 2019 at 15:52
• See answer below. It works, I tried it. Jul 18, 2019 at 17:14

It’s quite straightforward to convert infix to mathml using libsbml. Make sure you have libsbml installed in python by typing

pip install python-libsbml


at the windows/Mac/Linux command line or

!pip install python-libsbml


if you are running a Jupyter notebook. Some tools such as Tellurium already have libsbml preinstalled.

At the python command line type

import libsbml


to import the package. To enter the infix expression type the following:

p = libsbml.parseFormula ('k1*s1')


To generate the mathml type

print (libsbml.writeMathMLToString (p))

• One should use the newer libsbml.parseL3Formula which provides the full set of SBML Level 3 formula parsing. I recommend using the extended libsbml.parseL3FormulaWithModel(formula, model) which uses a model instance to resolve the actual symbols in the formula. Thereby it is clear if all the symbols exist in the model. If the functions return None make sure to check the getLastParseL3Error to find out what went wrong. Jul 29, 2019 at 6:47