sbmlteam / jsbml

JSBML is a community-driven project to create a free, open-source, pure Java™ library for reading, writing, and manipulating SBML files (the Systems Biology Markup Language) and data streams. It is an alternative to the mixed Java/native code-based interface provided in libSBML.

Home Page:https://sbml.org/software/jsbml/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ParseException when reading formula

Khartris opened this issue · comments

I am writing a bidirectional converter between an SBML model and a COBRAme model and encountered a bug in the parseFormula method in the ASTNode class.
In the reactions in my test model I encounter stoichiometries that look like this:
"-4.27350427350427e-6*mu*(0.000116266666666667*mu + 4.55184e-5) - 1.54e-6"
The parseFormula method can read this string without any problems when converting to SBML, but when I convert this stoichiometry back I get a string without whitespace and some additional brackets that looks like this:
"(-4.27350427350427E-6)*mu*(0.000116266666666667*mu+4.55184E-5)-1.54E-6"
For testing purposes, I tried reading that string back in with the parseFormula method and was given the following ParseException:

Exception in thread "main" org.sbml.jsbml.text.parser.ParseException: org.sbml.jsbml.text.parser.ParseException:` Encountered " <EXPNUMBER> "-1.54E-6 "" at line 1, column 63.
Was expecting one of:
    <EOF> 
    "+" ...
    "^" ...
    "-" ...
    "*" ...
    "/" ...
    "%" ...
    <COMPARISON> ...
    <BOOLEAN_LOGIC> ...
    <EOL> ...
    
	at org.sbml.jsbml.ASTNode.parseFormula(ASTNode.java:1106)
	at sbmlme.ExampleModel.main(ExampleModel.java:63)
Caused by: org.sbml.jsbml.text.parser.ParseException: Encountered " <EXPNUMBER> "-1.54E-6 "" at line 1, column 63.
Was expecting one of:
    <EOF> 
    "+" ...
    "^" ...
    "-" ...
    "*" ...
    "/" ...
    "%" ...
    <COMPARISON> ...
    <BOOLEAN_LOGIC> ...
    <EOL> ...
    
	at org.sbml.jsbml.text.parser.FormulaParserLL3.generateParseException(FormulaParserLL3.java:1758)
	at org.sbml.jsbml.text.parser.FormulaParserLL3.jj_consume_token(FormulaParserLL3.java:1643)
	at org.sbml.jsbml.text.parser.FormulaParserLL3.expression(FormulaParserLL3.java:700)
	at org.sbml.jsbml.text.parser.FormulaParserLL3.parse(FormulaParserLL3.java:683)
	at org.sbml.jsbml.ASTNode.parseFormula(ASTNode.java:1103)
	... 1 more

After some additional testing I could identify some conditions for this error to appear:

  1. The E-notation needs to be after a parameter or a bracket in the formula.
  2. The E-notation needs to be negative, when "…)-1.54E-6" is changed to "…)+1.54E-6" no ParseException is encountered.
  3. There has to be no space between the minus and the E-notation, when "…)-1.54E-6" is changed to
    "…)- 1.54E-6" no ParseException is encountered.

Thanks for the report. It seems to be taking '-1.54E-6' as a whole as a number and then the operator is missing in the expression. It is probably happening without the E-notation. A simple fix would be to write the formula with a space but we want to be able to parse properly these kind of expressions.

It should be fixed on the latest github version and I have updated the1.4-SNAPSHOT target in maven.

Thanks