Skip to content

Commit

Permalink
refactor: countElements returns NA if an element is invalid instead o…
Browse files Browse the repository at this point in the history
…f dropping it
  • Loading branch information
sgibb committed Aug 4, 2023
1 parent e95886f commit 1edc232
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
5 changes: 4 additions & 1 deletion R/adducts.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ adductFormula <- function(formulas, adduct = "[M+H]+", standardize = TRUE) {
ionMatrix <- lapply(formulas, function(formula, adduct) {
formulaAdduct <- apply(adduct, 1, function(x) {
current_f <- formula

if (is.null(current_f)) return(NULL)

multiplicity <- round(as.numeric(x["mass_multi"]) *
as.numeric(x["charge"]))
if (multiplicity != 1) {
Expand Down Expand Up @@ -264,7 +267,7 @@ adductFormula <- function(formulas, adduct = "[M+H]+", standardize = TRUE) {
return(formulaAdduct)
}, adduct = adduct)
ionMatrix <- do.call(rbind, ionMatrix)
rownames(ionMatrix) <- formulas
rownames(ionMatrix) <- formulas[as.logical(lengths(formulas))]
colnames(ionMatrix) <- rownames(adduct)
return(ionMatrix)
}
Expand Down
50 changes: 28 additions & 22 deletions R/chemFormula.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ countElements <- function(x) {
"(?<Element>",
paste0("[0-9]*",
c(
"[A][cglmrstu]|",
"[B][aehikr]?|",
"[C][adeflmnorsu]?|",
"[D][bsy]|",
"[E][rsu]|",
"[F][elmr]?|",
"[G][ade]|",
"[H][efgos]?|",
"[I][nr]?|",
"[K][r]?|",
"[L][airuv]|",
"[M][cdgnot]|",
"[N][abdehiop]?|",
"[O][gs]?|",
"[P][abdmortu]?|",
"[R][abefghnu]|",
"[S][bcegimnr]?|",
"[T][abcehilms]|",
"[U]|[V]|[W]|[X][e]|[Y][b]?|[Z][nr]"
"A[cglmrstu]|",
"B[aehikr]?|",
"C[adeflmnorsu]?|",
"D[bsy]|",
"E[rsu]|",
"F[elmr]?|",
"G[ade]|",
"H[efgos]?|",
"I[nr]?|",
"K[r]?|",
"L[airuv]|",
"M[cdgnot]|",
"N[abdehiop]?|",
"O[gs]?|",
"P[abdmortu]?|",
"R[abefghnu]|",
"S[bcegimnr]?|",
"T[abcehilms]|",
"U|V|W|Xe|Yb?|Z[nr]"
),
collapse = ""
),
Expand All @@ -58,6 +58,10 @@ countElements <- function(x) {

if (is.na(xx))
return(NA_integer_)
if (sum(attr(rr, "match.length")) != nchar(gsub("\\[|\\]", "", xx))) {
warning("The given formula '", xx, "' contains invalid symbols.")
return(NA_integer_)
}

start <- attr(rr, "capture.start")
end <- start + attr(rr, "capture.length") - 1L
Expand Down Expand Up @@ -175,10 +179,10 @@ standardizeFormula <- function(x) {
#' @examples
#' .sum_elements(c(H = 6, C = 3, O = 6, C = 3, H = 6))
.sum_elements <- function(x) {
if (!is.character(names(x)))
stop("element names missing")
if (anyNA(x))
return(NA_integer_)
if (!is.character(names(x)))
stop("element names missing")
unlist(lapply(split(x, names(x)), sum))
}

Expand Down Expand Up @@ -336,7 +340,9 @@ calculateMass <- function(x) {
stop("x must be either a character or a list with element counts.")
vapply(x, function(z) {
isotopes <- names(z)
if (!length(z) || !all(isotopes %in% names(.ISOTOPES))) {
if (!length(z) ||
is.null(isotopes) ||
!all(isotopes %in% names(.ISOTOPES))) {
message("not for all isotopes a mass is found")
return(NA_real_)
}
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test_chemFormula.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ test_that("countElements", {
), names = c("C6H12O6", NA, "H2O")
)
)
expect_warning(countElements("Foo"), "invalid")
expect_identical(
suppressWarnings(countElements(c("C6H12O6", "Foo", "H2O"))),
list(
"C6H12O6" = c(C = 6L, H = 12L, O = 6L),
Foo = NA_integer_,
"H2O" = c(H = 2L, O = 1L)
)
)

## heavy isotopes
expect_identical(
Expand Down

0 comments on commit 1edc232

Please sign in to comment.