def test_MathMLFormatter_cn_e_notation_7(self): expected = wrapMathML( " <cn type=\"e-notation\"> -1 <sep/> -6 </cn>\n") self.N = libsbml.parseFormula("-1e-6") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_sin(self): expected = wrapMathML(" <apply>\n" + " <sin/>\n" + " <ci> x </ci>\n" + " </apply>\n") self.N = libsbml.parseFormula("sin(x)") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_cn_e_notation_4(self): expected = wrapMathML(" <cn type=\"e-notation\"> 6.0221367 <sep/> 23 </cn>\n" ) self.N = libsbml.parseFormula("6.0221367e+23") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def generate(self): def mathc2p(s): r = StringIO() t = etree.parse(StringIO(s)) t.xslt(mathmlc2p_xslt).write(r) return r.getvalue() reader = libsbml.SBMLReader() doc = reader.readSBMLFromString(self.input) model = doc.getModel() reactions = model.getListOfReactions() result = [] for i in reactions: name = i.getName() reactants = [model.getSpecies(j.getSpecies()).getName() for j in i.getListOfReactants()] reversible = i.getReversible() products = [model.getSpecies(j.getSpecies()).getName() for j in i.getListOfProducts()] modifiers = [model.getSpecies(j.getSpecies()).getName() for j in i.getListOfModifiers()] rawmath = libsbml.writeMathMLToString(i.getKineticLaw().getMath()) math = mathc2p(rawmath) part = (name, reactants, reversible, products, modifiers, math) result.append(part) return ( ('reactions', result), )
def test_MathMLFormatter_cn_rational(self): expected = wrapMathML(" <cn type=\"rational\"> 1 <sep/> 3 </cn>\n") self.N = libsbml.ASTNode() self.N.setValue(1, 3) self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_cn_units(self): expected = wrapMathMLUnits(" <cn sbml:units=\"mole\"> 1.2 </cn>\n") self.N = libsbml.parseFormula("1.2") self.N.setUnits("mole") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_constant_infinity(self): expected = wrapMathML(" <infinity/>\n") self.N = libsbml.ASTNode() self.N.setValue(util_PosInf()) self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_constant_notanumber(self): expected = wrapMathML(" <notanumber/>\n") self.N = libsbml.ASTNode(libsbml.AST_REAL) self.N.setValue(util_NaN()) self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_cn_e_notation_7(self): expected = wrapMathML(" <cn type=\"e-notation\"> -1 <sep/> -6 </cn>\n" ) self.N = libsbml.parseFormula("-1e-6") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_minus_unary_1(self): expected = wrapMathML(" <cn type=\"integer\"> -2 </cn>\n" ) self.N = libsbml.parseFormula("-2") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_lambda(self): expected = wrapMathML(" <lambda>\n" + " <bvar>\n" + " <ci> x </ci>\n" + " </bvar>\n" + " <bvar>\n" + " <ci> y </ci>\n" + " </bvar>\n" + " <apply>\n" + " <root/>\n" + " <degree>\n" + " <cn type=\"integer\"> 2 </cn>\n" + " </degree>\n" + " <apply>\n" + " <plus/>\n" + " <apply>\n" + " <power/>\n" + " <ci> x </ci>\n" + " <cn type=\"integer\"> 2 </cn>\n" + " </apply>\n" + " <apply>\n" + " <power/>\n" + " <ci> y </ci>\n" + " <cn type=\"integer\"> 2 </cn>\n" + " </apply>\n" + " </apply>\n" + " </apply>\n" + " </lambda>\n") self.N = libsbml.parseFormula("lambda(x, y, root(2, x^2 + y^2))") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_constant_notanumber(self): expected = wrapMathML(" <notanumber/>\n") self.N = libsbml.ASTNode(libsbml.AST_REAL) self.N.setValue( util_NaN() ) self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_cn_e_notation_4(self): expected = wrapMathML( " <cn type=\"e-notation\"> 6.0221367 <sep/> 23 </cn>\n") self.N = libsbml.parseFormula("6.0221367e+23") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_csymbol_time(self): expected = wrapMathML(" <csymbol encoding=\"text\" " + "definitionURL=\"http://www.sbml.org/sbml/symbols/time\"> t </csymbol>\n") self.N = libsbml.ASTNode(libsbml.AST_NAME_TIME) self.N.setName("t") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def astnode_to_mathml(astnode: libsbml.ASTNode) -> str: """Convert to MathML string representation. :param astnode: ASTNode instance :return string rendering of MathML content for the ASTNode instance """ return libsbml.writeMathMLToString(astnode) # type: ignore
def test_MathMLFormatter_constant_infinity(self): expected = wrapMathML(" <infinity/>\n") self.N = libsbml.ASTNode() self.N.setValue( util_PosInf() ) self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_cn_units(self): expected = wrapMathMLUnits(" <cn sbml:units=\"mole\"> 1.2 </cn>\n") self.N = libsbml.parseFormula("1.2") self.N.setUnits("mole") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_minus_unary_2(self): expected = wrapMathML(" <apply>\n" + " <minus/>\n" + " <ci> a </ci>\n" + " </apply>\n") self.N = libsbml.parseFormula("-a") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_piecewise(self): expected = wrapMathML(" <piecewise>\n" + " <piece>\n" + " <apply>\n" + " <minus/>\n" + " <ci> x </ci>\n" + " </apply>\n" + " <apply>\n" + " <lt/>\n" + " <ci> x </ci>\n" + " <cn type=\"integer\"> 0 </cn>\n" + " </apply>\n" + " </piece>\n" + " <piece>\n" + " <cn type=\"integer\"> 0 </cn>\n" + " <apply>\n" + " <eq/>\n" + " <ci> x </ci>\n" + " <cn type=\"integer\"> 0 </cn>\n" + " </apply>\n" + " </piece>\n" + " <piece>\n" + " <ci> x </ci>\n" + " <apply>\n" + " <gt/>\n" + " <ci> x </ci>\n" + " <cn type=\"integer\"> 0 </cn>\n" + " </apply>\n" + " </piece>\n" + " </piecewise>\n") f = "piecewise(-x, lt(x, 0), 0, eq(x, 0), x, gt(x, 0))"; self.N = libsbml.parseFormula(f) self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_cn_rational(self): expected = wrapMathML(" <cn type=\"rational\"> 1 <sep/> 3 </cn>\n" ) self.N = libsbml.ASTNode() self.N.setValue(1,3) self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_sin(self): expected = wrapMathML(" <apply>\n" + " <sin/>\n" + " <ci> x </ci>\n" + " </apply>\n") self.N = libsbml.parseFormula("sin(x)") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def formula_to_mathml(string: str) -> str: """Parse formula string. :param string: formula string :return string rendering of parsed formula in the formula string """ astnode = libsbml.parseL3Formula(str(string)) mathml = libsbml.writeMathMLToString(astnode) return str(mathml)
def test_MathMLFormatter_minus_unary_2(self): expected = wrapMathML(" <apply>\n" + " <minus/>\n" + " <ci> a </ci>\n" + " </apply>\n") self.N = libsbml.parseFormula("-a") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_plus_binary(self): expected = wrapMathML(" <apply>\n" + " <plus/>\n" + " <cn type=\"integer\"> 1 </cn>\n" + " <cn type=\"integer\"> 2 </cn>\n" + " </apply>\n") self.N = libsbml.parseFormula("1 + 2") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_plus_binary(self): expected = wrapMathML(" <apply>\n" + " <plus/>\n" + " <cn type=\"integer\"> 1 </cn>\n" + " <cn type=\"integer\"> 2 </cn>\n" + " </apply>\n") self.N = libsbml.parseFormula("1 + 2") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_csymbol_time(self): expected = wrapMathML( " <csymbol encoding=\"text\" " + "definitionURL=\"http://www.sbml.org/sbml/symbols/time\"> t </csymbol>\n" ) self.N = libsbml.ASTNode(libsbml.AST_NAME_TIME) self.N.setName("t") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_lambda_no_bvars(self): expected = wrapMathML(" <lambda>\n" + " <apply>\n" + " <plus/>\n" + " <cn type=\"integer\"> 2 </cn>\n" + " <cn type=\"integer\"> 2 </cn>\n" + " </apply>\n" + " </lambda>\n") self.N = libsbml.parseFormula("lambda(2 + 2)") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_function_1(self): expected = wrapMathML(" <apply>\n" + " <ci> foo </ci>\n" + " <cn type=\"integer\"> 1 </cn>\n" + " <cn type=\"integer\"> 2 </cn>\n" + " <cn type=\"integer\"> 3 </cn>\n" + " </apply>\n") self.N = libsbml.parseFormula("foo(1, 2, 3)") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_log(self): expected = wrapMathML(" <apply>\n" + " <log/>\n" + " <logbase>\n" + " <cn type=\"integer\"> 2 </cn>\n" + " </logbase>\n" + " <ci> N </ci>\n" + " </apply>\n") self.N = libsbml.parseFormula("log(2, N)") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_root(self): expected = wrapMathML(" <apply>\n" + " <root/>\n" + " <degree>\n" + " <cn type=\"integer\"> 3 </cn>\n" + " </degree>\n" + " <ci> x </ci>\n" + " </apply>\n") self.N = libsbml.parseFormula("root(3, x)") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_function_1(self): expected = wrapMathML(" <apply>\n" + " <ci> foo </ci>\n" + " <cn type=\"integer\"> 1 </cn>\n" + " <cn type=\"integer\"> 2 </cn>\n" + " <cn type=\"integer\"> 3 </cn>\n" + " </apply>\n") self.N = libsbml.parseFormula("foo(1, 2, 3)") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_csymbol_delay(self): expected = wrapMathML( " <apply>\n" + " <csymbol encoding=\"text\" definitionURL=\"http://www.sbml.org/sbml/" + "symbols/delay\"> my_delay </csymbol>\n" + " <ci> x </ci>\n" + " <cn> 0.1 </cn>\n" + " </apply>\n") self.N = libsbml.parseFormula("delay(x, 0.1)") self.N.setName("my_delay") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_csymbol_delay(self): expected = wrapMathML(" <apply>\n" + " <csymbol encoding=\"text\" definitionURL=\"http://www.sbml.org/sbml/" + "symbols/delay\"> my_delay </csymbol>\n" + " <ci> x </ci>\n" + " <cn> 0.1 </cn>\n" + " </apply>\n") self.N = libsbml.parseFormula("delay(x, 0.1)") self.N.setName("my_delay") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_root(self): expected = wrapMathML(" <apply>\n" + " <root/>\n" + " <degree>\n" + " <cn type=\"integer\"> 3 </cn>\n" + " </degree>\n" + " <ci> x </ci>\n" + " </apply>\n") self.N = libsbml.parseFormula("root(3, x)") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def _get_math(formula, parameters): '''Returns math element from formula and parameters.''' math_elem = '<math xmlns="http://www.w3.org/1998/Math/MathML">' param_str = math_elem + '<lambda>' for parameter in parameters: param_str += '<bvar><ci>' + parameter + '</ci></bvar>' mathml = libsbml.writeMathMLToString(libsbml.parseFormula(formula)) mathml = mathml.replace(math_elem, param_str) mathml = mathml.replace('</math>', '</lambda></math>') return libsbml.readMathMLFromString(mathml)
def test_MathMLFormatter_log(self): expected = wrapMathML(" <apply>\n" + " <log/>\n" + " <logbase>\n" + " <cn type=\"integer\"> 2 </cn>\n" + " </logbase>\n" + " <ci> N </ci>\n" + " </apply>\n") self.N = libsbml.parseFormula("log(2, N)") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_lambda_no_bvars(self): expected = wrapMathML(" <lambda>\n" + " <apply>\n" + " <plus/>\n" + " <cn type=\"integer\"> 2 </cn>\n" + " <cn type=\"integer\"> 2 </cn>\n" + " </apply>\n" + " </lambda>\n") self.N = libsbml.parseFormula("lambda(2 + 2)") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_piecewise_otherwise(self): expected = wrapMathML(" <piecewise>\n" + " <piece>\n" + " <cn type=\"integer\"> 0 </cn>\n" + " <apply>\n" + " <lt/>\n" + " <ci> x </ci>\n" + " <cn type=\"integer\"> 0 </cn>\n" + " </apply>\n" + " </piece>\n" + " <otherwise>\n" + " <ci> x </ci>\n" + " </otherwise>\n" + " </piecewise>\n") self.N = libsbml.parseFormula("piecewise(0, lt(x, 0), x)") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_piecewise_otherwise(self): expected = wrapMathML(" <piecewise>\n" + " <piece>\n" + " <cn type=\"integer\"> 0 </cn>\n" + " <apply>\n" + " <lt/>\n" + " <ci> x </ci>\n" + " <cn type=\"integer\"> 0 </cn>\n" + " </apply>\n" + " </piece>\n" + " <otherwise>\n" + " <ci> x </ci>\n" + " </otherwise>\n" + " </piecewise>\n") self.N = libsbml.parseFormula("piecewise(0, lt(x, 0), x)") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_piecewise(self): expected = wrapMathML( " <piecewise>\n" + " <piece>\n" + " <apply>\n" + " <minus/>\n" + " <ci> x </ci>\n" + " </apply>\n" + " <apply>\n" + " <lt/>\n" + " <ci> x </ci>\n" + " <cn type=\"integer\"> 0 </cn>\n" + " </apply>\n" + " </piece>\n" + " <piece>\n" + " <cn type=\"integer\"> 0 </cn>\n" + " <apply>\n" + " <eq/>\n" + " <ci> x </ci>\n" + " <cn type=\"integer\"> 0 </cn>\n" + " </apply>\n" + " </piece>\n" + " <piece>\n" + " <ci> x </ci>\n" + " <apply>\n" + " <gt/>\n" + " <ci> x </ci>\n" + " <cn type=\"integer\"> 0 </cn>\n" + " </apply>\n" + " </piece>\n" + " </piecewise>\n") f = "piecewise(-x, lt(x, 0), 0, eq(x, 0), x, gt(x, 0))" self.N = libsbml.parseFormula(f) self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_lambda(self): expected = wrapMathML(" <lambda>\n" + " <bvar>\n" + " <ci> x </ci>\n" + " </bvar>\n" + " <bvar>\n" + " <ci> y </ci>\n" + " </bvar>\n" + " <apply>\n" + " <root/>\n" + " <degree>\n" + " <cn type=\"integer\"> 2 </cn>\n" + " </degree>\n" + " <apply>\n" + " <plus/>\n" + " <apply>\n" + " <power/>\n" + " <ci> x </ci>\n" + " <cn type=\"integer\"> 2 </cn>\n" + " </apply>\n" + " <apply>\n" + " <power/>\n" + " <ci> y </ci>\n" + " <cn type=\"integer\"> 2 </cn>\n" + " </apply>\n" + " </apply>\n" + " </apply>\n" + " </lambda>\n") self.N = libsbml.parseFormula("lambda(x, y, root(2, x^2 + y^2))") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def astnode_to_mathml(astnode: libsbml.ASTNode) -> str: """Convert to MathML string representation.""" return libsbml.writeMathMLToString(astnode)
def test_MathMLFormatter_minus_unary_1(self): expected = wrapMathML(" <cn type=\"integer\"> -2 </cn>\n") self.N = libsbml.parseFormula("-2") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def astnodeToMathML(astnode): mathml = libsbml.writeMathMLToString(astnode) return mathml
def test_MathMLFormatter_constant_true(self): expected = wrapMathML(" <true/>\n") self.N = libsbml.ASTNode(libsbml.AST_CONSTANT_TRUE) self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_cn_real_2(self): expected = wrapMathML(" <cn> 1234567.8 </cn>\n") self.N = libsbml.parseFormula("1234567.8") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_cn_real_locale(self): expected = wrapMathML(" <cn> 2.72 </cn>\n") self.N = libsbml.parseFormula("2.72") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_cn_real_2(self): expected = wrapMathML(" <cn> 1234567.8 </cn>\n") self.N = libsbml.parseFormula("1234567.8") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass
def test_MathMLFormatter_constant_true(self): expected = wrapMathML(" <true/>\n") self.N = libsbml.ASTNode(libsbml.AST_CONSTANT_TRUE) self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def stringToMathML(string): """Parses formula string. """ astnode = libsbml.parseL3Formula(str(string)) mathml = libsbml.writeMathMLToString(astnode) return mathml
def astnode_to_latex(astnode: libsbml.ASTNode) -> str: """Convert ASTNode to Latex using XSLT transformation.""" cmml_str: str = libsbml.writeMathMLToString(astnode) cmml_str = cmml_str.replace('<?xml version="1.0" encoding="UTF-8"?>', "") return cmathml_to_latex(cmml_str)
def test_MathMLFormatter_cn_real_locale(self): expected = wrapMathML(" <cn> 2.72 </cn>\n") self.N = libsbml.parseFormula("2.72") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def formula_to_mathml(string: str) -> str: """Parses formula string. """ astnode = libsbml.parseL3Formula(str(string)) mathml = libsbml.writeMathMLToString(astnode) return mathml
def test_MathMLFormatter_ci(self): expected = wrapMathML(" <ci> foo </ci>\n") self.N = libsbml.parseFormula("foo") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual(True, self.equals(expected, self.S)) pass
def test_MathMLFormatter_ci(self): expected = wrapMathML(" <ci> foo </ci>\n") self.N = libsbml.parseFormula("foo") self.S = libsbml.writeMathMLToString(self.N) self.assertEqual( True, self.equals(expected,self.S) ) pass