Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

containElements returns TRUE for invalid elements #66

Closed
Closed
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 DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: MetaboCoreUtils
Title: Core Utils for Metabolomics Data
Version: 1.9.2
Version: 1.9.3
Description: MetaboCoreUtils defines metabolomics-related core functionality
provided as low-level functions to allow a data structure-independent usage
across various R packages. This includes functions to calculate between ion
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# MetaboCoreUtils 1.9

## MetaboCoreUtils 1.9.3

- `containsElements` returns `NA` for `NA` and `FALSE` for invalid elements;
see issue
[#63](https://github.com/rformassspectrometry/MetaboCoreUtils/issues/63).

## MetaboCoreUtils 1.9.2

- `countElements` returns `NA` for invalid elements instead of silently
Expand Down
7 changes: 6 additions & 1 deletion R/chemFormula.R
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,16 @@ standardizeFormula <- function(x) {
#' containsElements("C6H12O6", "H2O")
#' containsElements("C6H12O6", "NH3")
containsElements <- function(x, y) {
mapply(
r <- mapply(
FUN = function(xx, yy)all(.sum_elements(c(xx, -yy)) >= 0),
xx = countElements(x), yy = countElements(y),
SIMPLIFY = TRUE, USE.NAMES = FALSE
)
## return FALSE for invalid elements
r[is.na(r)] <- FALSE
## return NA for NA as input
r[is.na(x) | is.na(y)] <- NA
r
}

#' @title subtract two chemical formula
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test_chemFormula.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ test_that("containsElements", {
expect_true(containsElements("C6H12O6", "H2O"))
expect_false(containsElements("C6H12O6", "NH3"))
expect_identical(containsElements("C6H12O6", NA), NA)
expect_false(suppressWarnings(containsElements("C6H12O6", "Z")))
expect_false(suppressWarnings(containsElements("C6H12O6", "")))
expect_identical(
containsElements("C6H12O6", c("H2O", "NH3")),
c(TRUE, FALSE)
Expand Down
Loading