Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion massql/msql.ebnf
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ factor: floating
| "formula(" moleculeformula ")"
| "aminoaciddelta(" aminoacids ")"
| peptidefunction
| "(" numericalexpression ")"
| "(" numericalexpression ")" -> factor_parens
peptidefunction: "peptide(" peptide "," "charge=" peptidecharge "," "ion=" peptideion ")"
multiply: "*"
divide: "/"
Expand Down
3 changes: 3 additions & 0 deletions massql/msql_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ def divide(self, s):
def factor(self, s):
return s[0]

def factor_parens(self, items):
return f"({items[0]})"

def term(self, items):
if len(items) == 1:
return items[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"qualifierintensitymatch": {
"comparator": "equal",
"name": "qualifierintensitymatch",
"value": "Y*0.0608+2e-06*X"
"value": "Y*(0.0608+(2e-06*X))"
},
"qualifierintensitytolpercent": {
"comparator": "equal",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"conditiontype": "where",
"type": "ms2productcondition",
"value": [
"2.0*X-55.9349375"
"2.0*(X-55.9349375)"
]
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"conditiontype": "where",
"type": "ms2productcondition",
"value": [
"2.0*X-55.9349375"
"2.0*(X-55.9349375)"
]
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"conditiontype": "where",
"type": "ms2productcondition",
"value": [
"2.0*X-55.9349375"
"2.0*(X-55.9349375)"
]
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"qualifierintensitymatch": {
"comparator": "equal",
"name": "qualifierintensitymatch",
"value": "Y*0.0608+2e-06*X"
"value": "Y*(0.0608+(2e-06*X))"
},
"qualifierintensitytolpercent": {
"comparator": "equal",
Expand Down
19 changes: 19 additions & 0 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,25 @@ def test_variable_formula_parse2():
parsed_output = msql_parser.parse_msql(query)
print(json.dumps(parsed_output, indent=4))

def test_formula_distribution():
"""Verify that formula() is correctly distributed through multiplication in X-expressions.

Previously, 2*(X - formula(Fe)) was parsed as 2*X - 55.93 instead of 2*(X - 55.93),
which is a ~56 Da error when evaluated.
"""
query = "QUERY scaninfo(MS2DATA) WHERE MS2PROD=X AND MS2PROD=2.0*(X - formula(Fe))"
parsed_output = msql_parser.parse_msql(query)

value = parsed_output["conditions"][1]["value"][0]
assert value == "2.0*(X-55.9349375)", f"Expected parenthesized form, got: {value}"

# Verify the expression evaluates correctly
from py_expression_eval import Parser
p = Parser()
result = p.parse(value).evaluate({"X": 100.0})
expected = 2.0 * (100.0 - 55.9349375)
assert abs(result - expected) < 1e-6, f"Expected {expected}, got {result}"

def test_xrange_parse():
query = "QUERY scaninfo(MS2DATA) WHERE MS2PROD=X AND MS2PROD=2.0*(X - formula(Fe)) AND X=range(min=5, max=100)"

Expand Down
Loading