rformassspectrometry / MetaboCoreUtils

Core utilities for metabolomics.
https://rformassspectrometry.github.io/MetaboCoreUtils/index.html
7 stars 6 forks source link

refactor: countElements returns NA if an element is invalid instead of dropping it #65

Closed sgibb closed 11 months ago

sgibb commented 11 months ago

This PR propose a solution to #64 .

I had to touch .sum_elements (change order of tests), calculateMass (test for NULL) and adductFormula (drop invalid formulas silently) to keep the current behaviour (and pass all unit tests).

countElements("C1Fo")

# $C1Fo
# [1] NA
# 
# Warning message:
# In (function (xx, rr)  : The given formula 'C1Fo' contains invalid symbols

Unfortunately we lose the position information (e.g. pos 3 contains "Fo").

BTW: I removed unnecessary [ and ] in the regexp.

sgibb commented 11 months ago

BTW: with this PR #63 is easy to solve: containsElements could return NA / FALSE for invalid formulas (we just have to call suppressWarnings(countElements(y)) and return NA/FALSE if countElements yield NA)