diff --git a/NAMESPACE b/NAMESPACE index ff3da2a..4ba8931 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,6 +8,7 @@ export(Matched) export(MatchedSpectra) export(MatchedSummarizedExperiment) export(Mz2MassParam) +export(Mz2MassRtParam) export(MzParam) export(MzRtParam) export(ValueParam) diff --git a/NEWS.md b/NEWS.md index 03453da..7bece1f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,7 +2,7 @@ ## Changes in 0.99.8 -- Add `matchMz, Mz2MassParam` . (issue +- Add `matchMz, Mz2MassParam` and `matchMz, Mz2MassRtParam`. (issue [#56](https://github.com/rformassspectrometry/MetaboAnnotation/issues/56)). ## Changes in 0.99.7 diff --git a/R/matchMz.R b/R/matchMz.R index 78b0d6a..343bb64 100644 --- a/R/matchMz.R +++ b/R/matchMz.R @@ -138,6 +138,34 @@ Mz2MassParam <- function(queryAdducts = c("[M+H]+"), targetAdducts = targetAdducts, tolerance = tolerance, ppm = ppm) } + +#' @noRd +setClass("Mz2MassRtParam", + slots = c( + toleranceRt = "numeric"), + contains = "Mz2MassParam", + prototype = prototype( + toleranceRt = 0), + validity = function(object) { + msg <- NULL + if (length(object@toleranceRt) != 1 || object@toleranceRt < 0) + msg <- c("'toleranceRt' has to be a positive number of length 1") + msg + }) + +#' @rdname matchMz +#' +#' @importFrom methods new +#' +#' @export +Mz2MassRtParam <- function(queryAdducts = c("[M+H]+"), + targetAdducts = c("[M+H]+"), + tolerance = 0, ppm = 5, toleranceRt = 0) { + new("Mz2MassRtParam", queryAdducts = queryAdducts, + targetAdducts = targetAdducts, tolerance = tolerance, ppm = ppm, + toleranceRt = toleranceRt) +} + #' @title m/z matching #' #' @name matchMz @@ -220,6 +248,19 @@ Mz2MassParam <- function(queryAdducts = c("[M+H]+"), #' define the maximal acceptable (constant or mass relative) difference #' between masses derived from `query` and `target`. #' +#' - `Mz2MassRtParam`: first convert to masses the m/z values of `query` and +#' `target` based respectively on `Mz2MassRtParam` parameters `queryAdducts` +#' (defaults to `queryAdducts = "[M+H]+"`) and `targetAdducts` (defaults to +#' `targetAdducts = "[M-H]-"`) and then match the obtained masses and +#' corresponding retention times (to each mass value corresponds the same +#' retention time associated to the m/z from which the mass value was +#' obtained). `query` must be a `data.frameOrSimilar` with a column containing +#' m/z values (whose name can be specified with parameter `mzColname`, +#' default being `mzColname = "mz"`). The same holds for `target`. +#' Parameters `tolerance` and `ppm` have the same meaning as in +#' `Mz2MassParam`. Parameter `toleranceRt` allows to specify the maximal +#' acceptable difference between retention time values. +#' #' - `ValueParam`: matches elements from from `query` (if `numeric`) or else #' from one of its columns with those from `target` (if `numeric`) or else #' from one of its columns. The name(s) of the colum(s) used for the matching @@ -773,6 +814,55 @@ setMethod("matchMz", res }) +#' @rdname matchMz +setMethod("matchMz", + signature = c(query = "data.frameOrSimilar", + target = "data.frameOrSimilar", + param = "Mz2MassRtParam"), + function(query, target, param, mzColname = c("mz", "mz"), + rtColname = c("rt", "rt")) { + if(length(mzColname) == 1) + mzColname <- rep(mzColname, 2) + if(length(rtColname) == 1) + rtColname <- rep(rtColname, 2) + if (!mzColname[1] %in% colnames(query)) + stop("Missing column \"", mzColname[1], "\" in query") + if (!mzColname[2] %in% colnames(target)) + stop("Missing column \"", mzColname[2], "\" in target") + if (!rtColname[1] %in% colnames(query)) + stop("Missing column \"", rtColname[1], "\" in query") + if (!rtColname[2] %in% colnames(target)) + stop("Missing column \"", rtColname[2], "\" in target") + query_mass <- .mz_to_mass_df(query[, mzColname[1]], + param@queryAdducts) + query_mass$rt <- rep(query[, rtColname[1]], + .nelements(param@queryAdducts)) + target_mass<- .mz_to_mass_df(target[, mzColname[2]], + param@targetAdducts) + target_mass$rt <- rep(target[, rtColname[2]], + .nelements(param@targetAdducts)) + queryl <- nrow(query_mass) + matches <- vector("list", queryl) + for (i in seq_len(queryl)) { + matches[[i]] <- + .getMatchesMzRt(query_mass$index[i], + query_mass$mass[i], + query_mass$rt[i], + target = target_mass, + tolerance = param@tolerance, + ppm = param@ppm, + toleranceRt = param@toleranceRt) + if (nrow(matches[[i]])) + matches[[i]]$query_adduct <- query_mass$adduct[i] + else matches[[i]]$query_adduct <- character() + } + matches <- do.call(rbind, matches) + colnames(matches)[3] <- "target_adduct" + Matched(query = query, target = target, + matches = matches[, c(1, 2, 7, 3, 4, 5, 6)], + metadata = list(param = param)) + }) + #' @rdname matchMz setMethod("matchMz", signature = c(query = "SummarizedExperiment", @@ -830,7 +920,7 @@ setMethod("matchMz", ppm, toleranceRt){ diffs_rt <- queryRt - target$rt cls_rt <- which(abs(diffs_rt) <= toleranceRt) - diffs <- queryMz - target$mz[cls_rt] + diffs <- queryMz - target[cls_rt, 2] absdiffs <- abs(diffs) cls <- which(absdiffs <= (tolerance + ppm(queryMz, ppm))) if ("adduct" %in% colnames(target)){ @@ -840,7 +930,7 @@ setMethod("matchMz", adduct = target$adduct[cls_rt[cls]], score = diffs[cls], ppm_error = absdiffs[cls] / - target[cls_rt[cls], "mz"] * 10^6, + target[cls_rt[cls], 2] * 10^6, score_rt = diffs_rt[cls_rt[cls]]) else data.frame(query_idx = integer(), target_idx = integer(), @@ -854,7 +944,7 @@ setMethod("matchMz", target_idx = target$index[cls_rt[cls]], score = diffs[cls], ppm_error = absdiffs[cls] / - target[cls_rt[cls], "mz"] * 10^6, + target[cls_rt[cls], 2] * 10^6, score_rt = diffs_rt[cls_rt[cls]]) else data.frame(query_idx = integer(), target_idx = integer(), diff --git a/man/matchMz.Rd b/man/matchMz.Rd index a8c6909..26fc3d3 100644 --- a/man/matchMz.Rd +++ b/man/matchMz.Rd @@ -7,6 +7,7 @@ \alias{Mass2MzRtParam} \alias{MzRtParam} \alias{Mz2MassParam} +\alias{Mz2MassRtParam} \alias{matchMz} \alias{matchMz,numeric,numeric,ValueParam-method} \alias{matchMz,numeric,data.frameOrSimilar,ValueParam-method} @@ -25,6 +26,7 @@ \alias{matchMz,numeric,data.frameOrSimilar,Mz2MassParam-method} \alias{matchMz,data.frameOrSimilar,numeric,Mz2MassParam-method} \alias{matchMz,data.frameOrSimilar,data.frameOrSimilar,Mz2MassParam-method} +\alias{matchMz,data.frameOrSimilar,data.frameOrSimilar,Mz2MassRtParam-method} \alias{matchMz,SummarizedExperiment,ANY,Param-method} \title{m/z matching} \usage{ @@ -45,6 +47,14 @@ Mz2MassParam( ppm = 5 ) +Mz2MassRtParam( + queryAdducts = c("[M+H]+"), + targetAdducts = c("[M+H]+"), + tolerance = 0, + ppm = 5, + toleranceRt = 0 +) + matchMz(query, target, param, ...) \S4method{matchMz}{numeric,numeric,ValueParam}(query, target, param) @@ -94,6 +104,14 @@ matchMz(query, target, param, ...) \S4method{matchMz}{data.frameOrSimilar,data.frameOrSimilar,Mz2MassParam}(query, target, param, mzColname = c("mz", "mz")) +\S4method{matchMz}{data.frameOrSimilar,data.frameOrSimilar,Mz2MassRtParam}( + query, + target, + param, + mzColname = c("mz", "mz"), + rtColname = c("rt", "rt") +) + \S4method{matchMz}{SummarizedExperiment,ANY,Param}(query, target, param, mzColname = "mz", rtColname = c("rt", "rt")) } \arguments{ diff --git a/tests/testthat/test_matchMz.R b/tests/testthat/test_matchMz.R index 2311d46..887488e 100644 --- a/tests/testthat/test_matchMz.R +++ b/tests/testthat/test_matchMz.R @@ -1,69 +1,83 @@ test_that("ValueParam works", { res <- ValueParam() expect_true(is(res, "ValueParam")) - + expect_error(ValueParam(tolerance = 1:3), "positive number") expect_error(ValueParam(ppm = -4), "positive number") }) test_that("Mass2MzParam works", { - res <- Mass2MzParam() - expect_true(is(res, "Mass2MzParam")) + res <- Mass2MzParam() + expect_true(is(res, "Mass2MzParam")) - expect_error(Mass2MzParam(tolerance = 1:3), "positive number") - expect_error(Mass2MzParam(ppm = -4), "positive number") - expect_error(Mass2MzParam(adducts = c("[M+H]+", "adduct2")), "Unknown") + expect_error(Mass2MzParam(tolerance = 1:3), "positive number") + expect_error(Mass2MzParam(ppm = -4), "positive number") + expect_error(Mass2MzParam(adducts = c("[M+H]+", "adduct2")), "Unknown") - adds <- data.frame(mass_add = c(1, 2, 3), mass_multi = c(1, 2, 0.5)) - rownames(adds) <- c("a", "b", "c") - res <- Mass2MzParam(adducts = adds) - expect_true(is(res, "Mass2MzParam")) + adds <- data.frame(mass_add = c(1, 2, 3), mass_multi = c(1, 2, 0.5)) + rownames(adds) <- c("a", "b", "c") + res <- Mass2MzParam(adducts = adds) + expect_true(is(res, "Mass2MzParam")) }) test_that("Mass2MzRtParam works", { - res <- Mass2MzRtParam() - expect_true(is(res, "Mass2MzRtParam")) + res <- Mass2MzRtParam() + expect_true(is(res, "Mass2MzRtParam")) - expect_error(Mass2MzRtParam(tolerance = 1:3), "positive number") - expect_error(Mass2MzRtParam(toleranceRt = -1), "positive number") - expect_error(Mass2MzRtParam(ppm = -4), "positive number") - expect_error(Mass2MzRtParam(adducts = c("[M+H]+", "adduct2")), "Unknown") + expect_error(Mass2MzRtParam(tolerance = 1:3), "positive number") + expect_error(Mass2MzRtParam(toleranceRt = -1), "positive number") + expect_error(Mass2MzRtParam(ppm = -4), "positive number") + expect_error(Mass2MzRtParam(adducts = c("[M+H]+", "adduct2")), "Unknown") }) test_that("MzParam works", { - res <- MzParam() - expect_true(is(res, "MzParam")) + res <- MzParam() + expect_true(is(res, "MzParam")) - expect_error(MzParam(tolerance = 1:3), "positive number") - expect_error(MzParam(ppm = -4), "positive number") + expect_error(MzParam(tolerance = 1:3), "positive number") + expect_error(MzParam(ppm = -4), "positive number") }) test_that("MzRtParam works", { - res <- MzRtParam() - expect_true(is(res, "MzRtParam")) + res <- MzRtParam() + expect_true(is(res, "MzRtParam")) - expect_error(MzRtParam(tolerance = 1:3), "positive number") - expect_error(MzRtParam(toleranceRt = -1), "positive number") - expect_error(MzRtParam(ppm = -4), "positive number") + expect_error(MzRtParam(tolerance = 1:3), "positive number") + expect_error(MzRtParam(toleranceRt = -1), "positive number") + expect_error(MzRtParam(ppm = -4), "positive number") }) test_that("Mz2MassParam works", { res <- Mz2MassParam() expect_true(is(res, "Mz2MassParam")) - + expect_error(Mz2MassParam(tolerance = 1:3), "positive number") expect_error(Mz2MassParam(ppm = -4), "positive number") - + adds <- data.frame(mass_add = c(1, 2, 3), mass_multi = c(1, 2, 0.5)) rownames(adds) <- c("a", "b", "c") res <- Mz2MassParam(queryAdducts = adds) expect_true(is(res, "Mz2MassParam")) }) +test_that("Mz2MassRtParam works", { + res <- Mz2MassRtParam() + expect_true(is(res, "Mz2MassRtParam")) + + expect_error(Mz2MassRtParam(tolerance = 1:3), "positive number") + expect_error(Mz2MassRtParam(ppm = -4), "positive number") + expect_error(Mz2MassRtParam(toleranceRt = -1), "positive number") + + adds <- data.frame(mass_add = c(1, 2, 3), mass_multi = c(1, 2, 0.5)) + rownames(adds) <- c("a", "b", "c") + res <- Mz2MassRtParam(queryAdducts = adds) + expect_true(is(res, "Mz2MassRtParam")) +}) + test_that("matchMz, ValueParam works", { qry <- data.frame(value = c(150, 170, 179)) trgt <- data.frame(value = seq(110, 200, 10)) - + par <- ValueParam(tolerance = 0) expect_error(matchMz(qry, trgt, par), "`valueColname` has to be provided.") expect_error(matchMz(qry, trgt, par, @@ -77,14 +91,14 @@ test_that("matchMz, ValueParam works", { expect_equal(res@matches$target_idx, c(5, 7)) expect_equal(res@matches$score, c(0, 0)) expect_equal(res@matches$ppm_error, c(0, 0)) - + ## no matches res <- matchMz(qry + 0.1, trgt, par, valueColname = "value") expect_true(is(res, "Matched")) expect_equal(query(res), qry + 0.1) expect_equal(target(res), trgt) expect_true(nrow(res@matches) == 0) - + # positive tolerance par <- ValueParam(tolerance = 10) res <- matchMz(qry, trgt, par, valueColname = "value") @@ -99,109 +113,105 @@ test_that("matchMz, ValueParam works", { test_that("matchMz,Mass2MzParam works", { - cmpds <- data.frame( - name = c("Tryptophan", "Leucine", "Isoleucine"), - formula = c("C11H12N2O2", "C6H13NO2", "C6H13NO2"), - exactmass = c(204.089878, 131.094629, 131.094629) - ) - - adducts <- c("[M+H]+", "[M+Na]+") - - x <- data.frame( - mz = c(mass2mz(204.089878, "[M+H]+"), - mass2mz(131.094629, "[M+H]+"), - mass2mz(204.089878, "[M+Na]+") + 1e-6) - ) - - par <- Mass2MzParam(adducts = adducts, tolerance = 0, ppm = 20) - res <- matchMz(x, cmpds, par) - expect_equal(query(res), x) - expect_equal(target(res), cmpds) - expect_equal(res@matches$query_idx, c(1, 2, 2, 3)) - expect_equal(res@matches$target_idx, c(1, 2, 3, 1)) - expect_equal(res@matches$score, c(0, 0, 0, 1e-6)) - expect_equal(res@matches$ppm_error, - c(0, 0, 0, 1 / mass2mz(204.089878, "[M+Na]+"))) - - par <- Mass2MzParam(adducts = adducts, tolerance = 0, ppm = 0) - res <- matchMz(x, cmpds, par) - expect_equal(query(res), x) - expect_equal(target(res), cmpds) - expect_equal(res@matches$query_idx, c(1, 2, 2)) - expect_equal(res@matches$target_idx, c(1, 2, 3)) - expect_equal(res@matches$score, c(0, 0, 0)) - expect_equal(res@matches$ppm_error, c(0, 0, 0)) - - ## no matches - adducts <- c("[M+Li]+", "[M+K]+") - par <- Mass2MzParam(adducts = adducts, tolerance = 0, ppm = 20) - res <- matchMz(x, cmpds, par) - expect_true(is(res, "Matched")) - expect_equal(query(res), x) - expect_equal(target(res), cmpds) - expect_true(nrow(res@matches) == 0) - - ## with a custom adduct definition - adducts <- data.frame(mass_add = c(1, 3), mass_multi = c(1, 0.5)) - rownames(adducts) <- c("a", "b") - x <- data.frame( - mz = c(mass2mz(204.089878, adducts["a", ]), - mass2mz(131.094629, adducts["a", ]), - mass2mz(204.089878, adducts["b", ]) - 2e-6) - ) - - par <- Mass2MzParam(adducts = adducts, tolerance = 0, ppm = 20) - res <- matchMz(x, cmpds, par) - expect_equal(query(res), x) - expect_equal(target(res), cmpds) - expect_equal(res@matches$query_idx, c(1, 2, 2, 3)) - expect_equal(res@matches$target_idx, c(1, 2, 3, 1)) - expect_equal(res@matches$score, c(0, 0, 0, -2e-6)) - expect_equal(res@matches$ppm_error, - c(0, 0, 0, 2 / mass2mz(204.089878, adducts["b", ]))) - - par <- Mass2MzParam(adducts = adducts, tolerance = 0, ppm = 0) - res <- matchMz(x, cmpds, par) - expect_equal(query(res), x) - expect_equal(target(res), cmpds) - expect_equal(res@matches$query_idx, c(1, 2, 2)) - expect_equal(res@matches$target_idx, c(1, 2, 3)) - expect_equal(res@matches$score, c(0, 0, 0)) - expect_equal(res@matches$ppm_error, c(0, 0, 0)) - - expect_error(matchMz(x, cmpds, par, massColname = "other"), "other") - - ## numeric, data.frame - res <- matchMz(x$mz, cmpds, par) - expect_equal(query(res), x$mz) - expect_equal(target(res), cmpds) - expect_equal(res@matches$query_idx, c(1, 2, 2)) - expect_equal(res@matches$target_idx, c(1, 2, 3)) - expect_equal(res@matches$score, c(0, 0, 0)) - expect_equal(res@matches$ppm_error, c(0, 0, 0)) - - - expect_error(matchMz(x$mz, cmpds, par, massColname = "other"), "other") - - ## data.frame, numeric - res <- matchMz(x, cmpds$exactmass, par) - expect_equal(query(res), x) - expect_equal(target(res), cmpds$exactmass) - expect_equal(res@matches$query_idx, c(1, 2, 2)) - expect_equal(res@matches$target_idx, c(1, 2, 3)) - expect_equal(res@matches$score, c(0, 0, 0)) - expect_equal(res@matches$ppm_error, c(0, 0, 0)) - - - expect_error(matchMz(x, cmpds$exactmass, par, mzColname = "other"), "other") - - adducts <- data.frame(mass_add = c(2, 4), mass_multi = c(2, 1)) - par <- Mass2MzParam(adducts = adducts, tolerance = 0, ppm = 20) - res <- matchMz(x, cmpds, par) - expect_true(is(res, "Matched")) - expect_equal(query(res), x) - expect_equal(target(res), cmpds) - expect_true(nrow(res@matches) == 0) + cmpds <- data.frame( + name = c("Tryptophan", "Leucine", "Isoleucine"), + formula = c("C11H12N2O2", "C6H13NO2", "C6H13NO2"), + exactmass = c(204.089878, 131.094629, 131.094629) + ) + adducts <- c("[M+H]+", "[M+Na]+") + x <- data.frame( + mz = c(mass2mz(204.089878, "[M+H]+"), + mass2mz(131.094629, "[M+H]+"), + mass2mz(204.089878, "[M+Na]+") + 1e-6) + ) + + par <- Mass2MzParam(adducts = adducts, tolerance = 0, ppm = 20) + res <- matchMz(x, cmpds, par) + expect_equal(query(res), x) + expect_equal(target(res), cmpds) + expect_equal(res@matches$query_idx, c(1, 2, 2, 3)) + expect_equal(res@matches$target_idx, c(1, 2, 3, 1)) + expect_equal(res@matches$score, c(0, 0, 0, 1e-6)) + expect_equal(res@matches$ppm_error, + c(0, 0, 0, 1 / mass2mz(204.089878, "[M+Na]+"))) + + par <- Mass2MzParam(adducts = adducts, tolerance = 0, ppm = 0) + res <- matchMz(x, cmpds, par) + expect_equal(query(res), x) + expect_equal(target(res), cmpds) + expect_equal(res@matches$query_idx, c(1, 2, 2)) + expect_equal(res@matches$target_idx, c(1, 2, 3)) + expect_equal(res@matches$score, c(0, 0, 0)) + expect_equal(res@matches$ppm_error, c(0, 0, 0)) + + ## no matches + adducts <- c("[M+Li]+", "[M+K]+") + par <- Mass2MzParam(adducts = adducts, tolerance = 0, ppm = 20) + res <- matchMz(x, cmpds, par) + expect_true(is(res, "Matched")) + expect_equal(query(res), x) + expect_equal(target(res), cmpds) + expect_true(nrow(res@matches) == 0) + + ## with a custom adduct definition + adducts <- data.frame(mass_add = c(1, 3), mass_multi = c(1, 0.5)) + rownames(adducts) <- c("a", "b") + x <- data.frame( + mz = c(mass2mz(204.089878, adducts["a", ]), + mass2mz(131.094629, adducts["a", ]), + mass2mz(204.089878, adducts["b", ]) - 2e-6) + ) + + par <- Mass2MzParam(adducts = adducts, tolerance = 0, ppm = 20) + res <- matchMz(x, cmpds, par) + expect_equal(query(res), x) + expect_equal(target(res), cmpds) + expect_equal(res@matches$query_idx, c(1, 2, 2, 3)) + expect_equal(res@matches$target_idx, c(1, 2, 3, 1)) + expect_equal(res@matches$score, c(0, 0, 0, -2e-6)) + expect_equal(res@matches$ppm_error, + c(0, 0, 0, 2 / mass2mz(204.089878, adducts["b", ]))) + + par <- Mass2MzParam(adducts = adducts, tolerance = 0, ppm = 0) + res <- matchMz(x, cmpds, par) + expect_equal(query(res), x) + expect_equal(target(res), cmpds) + expect_equal(res@matches$query_idx, c(1, 2, 2)) + expect_equal(res@matches$target_idx, c(1, 2, 3)) + expect_equal(res@matches$score, c(0, 0, 0)) + expect_equal(res@matches$ppm_error, c(0, 0, 0)) + + expect_error(matchMz(x, cmpds, par, massColname = "other"), "other") + + ## numeric, data.frame + res <- matchMz(x$mz, cmpds, par) + expect_equal(query(res), x$mz) + expect_equal(target(res), cmpds) + expect_equal(res@matches$query_idx, c(1, 2, 2)) + expect_equal(res@matches$target_idx, c(1, 2, 3)) + expect_equal(res@matches$score, c(0, 0, 0)) + expect_equal(res@matches$ppm_error, c(0, 0, 0)) + + expect_error(matchMz(x$mz, cmpds, par, massColname = "other"), "other") + + ## data.frame, numeric + res <- matchMz(x, cmpds$exactmass, par) + expect_equal(query(res), x) + expect_equal(target(res), cmpds$exactmass) + expect_equal(res@matches$query_idx, c(1, 2, 2)) + expect_equal(res@matches$target_idx, c(1, 2, 3)) + expect_equal(res@matches$score, c(0, 0, 0)) + expect_equal(res@matches$ppm_error, c(0, 0, 0)) + + expect_error(matchMz(x, cmpds$exactmass, par, mzColname = "other"), "other") + + adducts <- data.frame(mass_add = c(2, 4), mass_multi = c(2, 1)) + par <- Mass2MzParam(adducts = adducts, tolerance = 0, ppm = 20) + res <- matchMz(x, cmpds, par) + expect_true(is(res, "Matched")) + expect_equal(query(res), x) + expect_equal(target(res), cmpds) + expect_true(nrow(res@matches) == 0) }) test_that(".getMatches works", { @@ -226,104 +236,104 @@ test_that(".getMatches works", { test_that("matchMz,Mass2MzRtParam works", { - cmpds <- data.frame( - name = c("Tryptophan", "Leucine", "Isoleucine"), - formula = c("C11H12N2O2", "C6H13NO2", "C6H13NO2"), - exactmass = c(204.089878, 131.094629, 131.094629), - rt = c(150, 140, 140) - ) - - adducts <- c("[M+H]+", "[M+Na]+") - - x <- data.frame( - mz = c(mass2mz(204.089878, "[M+H]+"), - mass2mz(131.094629, "[M+H]+"), - mass2mz(204.089878, "[M+Na]+") + 1e-6), - rt = c(150, 140, 150.1) - ) - - par <- Mass2MzRtParam(adducts = adducts, tolerance = 0, ppm = 20, - toleranceRt = 0) - res <- matchMz(x, cmpds, par) - expect_equal(query(res), x) - expect_equal(target(res), cmpds) - expect_equal(res@matches$query_idx, c(1, 2, 2)) - expect_equal(res@matches$target_idx, c(1, 2, 3)) - expect_equal(res@matches$score, c(0, 0, 0)) - expect_equal(res@matches$ppm_error, c(0, 0, 0)) - expect_equal(res@matches$score_rt, c(0, 0, 0)) - - par <- Mass2MzRtParam(adducts = adducts, tolerance = 0, ppm = 20, - toleranceRt = 0.2) - res <- matchMz(x, cmpds, par) - expect_equal(query(res), x) - expect_equal(target(res), cmpds) - expect_equal(res@matches$query_idx, c(1, 2, 2, 3)) - expect_equal(res@matches$target_idx, c(1, 2, 3, 1)) - expect_equal(res@matches$score, c(0, 0, 0, 1e-6)) - expect_equal(res@matches$ppm_error, - c(0, 0, 0, 1 / mass2mz(204.089878, "[M+Na]+"))) - expect_equal(res@matches$score_rt, c(0, 0, 0, 0.1)) - - par <- Mass2MzRtParam(adducts = adducts, tolerance = 0, ppm = 0, - toleranceRt = 0.2) - res <- matchMz(x, cmpds, par) - expect_equal(query(res), x) - expect_equal(target(res), cmpds) - expect_equal(res@matches$query_idx, c(1, 2, 2)) - expect_equal(res@matches$target_idx, c(1, 2, 3)) - expect_equal(res@matches$score, c(0, 0, 0)) - expect_equal(res@matches$ppm_error, c(0, 0, 0)) - expect_equal(res@matches$score_rt, c(0, 0, 0)) - - - ## no matches - adducts <- c("[M+Li]+", "[M+K]+") - par <- Mass2MzRtParam(adducts = adducts, tolerance = 0, ppm = 20, - toleranceRt = 0.2) - res <- matchMz(x, cmpds, par) - expect_true(is(res, "Matched")) - expect_equal(query(res), x) - expect_equal(target(res), cmpds) - expect_true(nrow(res@matches) == 0) + cmpds <- data.frame( + name = c("Tryptophan", "Leucine", "Isoleucine"), + formula = c("C11H12N2O2", "C6H13NO2", "C6H13NO2"), + exactmass = c(204.089878, 131.094629, 131.094629), + rt = c(150, 140, 140) + ) + adducts <- c("[M+H]+", "[M+Na]+") + x <- data.frame( + mz = c(mass2mz(204.089878, "[M+H]+"), + mass2mz(131.094629, "[M+H]+"), + mass2mz(204.089878, "[M+Na]+") + 1e-6), + rt = c(150, 140, 150.1) + ) + + par <- Mass2MzRtParam(adducts = adducts, tolerance = 0, ppm = 20, + toleranceRt = 0) + res <- matchMz(x, cmpds, par) + expect_equal(query(res), x) + expect_equal(target(res), cmpds) + expect_equal(res@matches$query_idx, c(1, 2, 2)) + expect_equal(res@matches$target_idx, c(1, 2, 3)) + expect_equal(res@matches$score, c(0, 0, 0)) + expect_equal(res@matches$ppm_error, c(0, 0, 0)) + expect_equal(res@matches$score_rt, c(0, 0, 0)) + + par <- Mass2MzRtParam(adducts = adducts, tolerance = 0, ppm = 20, + toleranceRt = 0.2) + res <- matchMz(x, cmpds, par) + expect_equal(query(res), x) + expect_equal(target(res), cmpds) + expect_equal(res@matches$query_idx, c(1, 2, 2, 3)) + expect_equal(res@matches$target_idx, c(1, 2, 3, 1)) + expect_equal(res@matches$score, c(0, 0, 0, 1e-6)) + expect_equal(res@matches$ppm_error, + c(0, 0, 0, 1 / mass2mz(204.089878, "[M+Na]+"))) + expect_equal(res@matches$score_rt, c(0, 0, 0, 0.1)) + + par <- Mass2MzRtParam(adducts = adducts, tolerance = 0, ppm = 0, + toleranceRt = 0.2) + res <- matchMz(x, cmpds, par) + expect_equal(query(res), x) + expect_equal(target(res), cmpds) + expect_equal(res@matches$query_idx, c(1, 2, 2)) + expect_equal(res@matches$target_idx, c(1, 2, 3)) + expect_equal(res@matches$score, c(0, 0, 0)) + expect_equal(res@matches$ppm_error, c(0, 0, 0)) + expect_equal(res@matches$score_rt, c(0, 0, 0)) + + ## no matches + adducts <- c("[M+Li]+", "[M+K]+") + par <- Mass2MzRtParam(adducts = adducts, tolerance = 0, ppm = 20, + toleranceRt = 0.2) + res <- matchMz(x, cmpds, par) + expect_true(is(res, "Matched")) + expect_equal(query(res), x) + expect_equal(target(res), cmpds) + expect_true(nrow(res@matches) == 0) }) test_that(".getMatchesMzRt works", { - trgt <- data.frame(index = rep(1:7, 2), - adduct = c(rep("A", 7), rep("B", 7)), - mz = c(11:17, 12:18), - rt = rep(21:27, 2)) - trgt <- trgt[order(trgt$mz), ] - - res <- .getMatchesMzRt(queryIndex = 3, queryMz = 13, queryRt = 23, - target = trgt, tolerance = 0, ppm = 0, toleranceRt = 0) - - expect_true(is.data.frame(res)) - expect_equal(res$query_idx, c(3)) - expect_equal(res$target_idx, c(3)) - - res <- .getMatchesMzRt(queryIndex = 3, queryMz = 13, queryRt = 24, - target = trgt, tolerance = 0, ppm = 0, toleranceRt = 0) - expect_true(is.data.frame(res)) - expect_true(nrow(res) == 0) - - res <- .getMatchesMzRt(queryIndex = 3, queryMz = 13, queryRt = 24, - target = trgt, tolerance = 0, ppm = 0, toleranceRt = 1) - expect_true(is.data.frame(res)) - expect_equal(res$query_idx, c(3)) - expect_equal(res$target_idx, c(3)) - - res <- .getMatchesMzRt(queryIndex = 3, queryMz = 13, queryRt = 23, - target = trgt, tolerance = 1, ppm = 0, toleranceRt = 1) - expect_true(is.data.frame(res)) - expect_equal(res$query_idx, c(3, 3, 3, 3, 3)) - expect_equal(res$target_idx, c(2, 3, 2, 4, 3)) - expect_equal(res$adduct, c("A", "A", "B", "A", "B")) - expect_equal(res$score, c(1, 0, 0, -1, -1)) - expect_equal(res$ppm_error, c(1, 0, 0, 1, 1) / c(12, 13, 13, 14, 14) * 10^6) - expect_equal(res$score_rt, c(1, 0, 1, -1, 0)) -}) + trgt <- data.frame(index = rep(1:7, 2), + mz = c(11:17, 12:18), + adduct = c(rep("A", 7), rep("B", 7)), + rt = rep(21:27, 2)) + trgt <- trgt[order(trgt$mz), ] + res <- .getMatchesMzRt(queryIndex = 3, queryMz = 13, queryRt = 23, + target = trgt, tolerance = 0, ppm = 0, + toleranceRt = 0) + + expect_true(is.data.frame(res)) + expect_equal(res$query_idx, c(3)) + expect_equal(res$target_idx, c(3)) + + res <- .getMatchesMzRt(queryIndex = 3, queryMz = 13, queryRt = 24, + target = trgt, tolerance = 0, ppm = 0, + toleranceRt = 0) + expect_true(is.data.frame(res)) + expect_true(nrow(res) == 0) + + res <- .getMatchesMzRt(queryIndex = 3, queryMz = 13, queryRt = 24, + target = trgt, tolerance = 0, ppm = 0, + toleranceRt = 1) + expect_true(is.data.frame(res)) + expect_equal(res$query_idx, c(3)) + expect_equal(res$target_idx, c(3)) + + res <- .getMatchesMzRt(queryIndex = 3, queryMz = 13, queryRt = 23, + target = trgt, tolerance = 1, ppm = 0, + toleranceRt = 1) + expect_true(is.data.frame(res)) + expect_equal(res$query_idx, c(3, 3, 3, 3, 3)) + expect_equal(res$target_idx, c(2, 3, 2, 4, 3)) + expect_equal(res$adduct, c("A", "A", "B", "A", "B")) + expect_equal(res$score, c(1, 0, 0, -1, -1)) + expect_equal(res$ppm_error, c(1, 0, 0, 1, 1) / c(12, 13, 13, 14, 14) * 10^6) + expect_equal(res$score_rt, c(1, 0, 1, -1, 0)) +}) test_that(".mass_to_mz_df works", { mass <- c(4, 2, 5, 10) @@ -343,173 +353,237 @@ test_that(".mass_to_mz_df works", { }) test_that("matchMz, MzParam works", { - qry <- data.frame(mz = c(150, 170, 179)) - trgt <- data.frame(mz = seq(110, 200, 10)) - # qry <- data.frame(mz = c(150, 170, 179)) - # trgt <- seq(110, 200, 10) - # qry <- c(150, 170, 179) - # trgt <- data.frame(mz = seq(110, 200, 10)) - # qry <- c(150, 170, 179) - # trgt <- seq(110, 200, 10) - - par <- MzParam(tolerance = 0) - res <- matchMz(qry, trgt, par) - expect_equal(query(res), qry) - expect_equal(target(res), trgt) - expect_equal(res@matches$query_idx, c(1, 2)) - expect_equal(res@matches$target_idx, c(5, 7)) - expect_equal(res@matches$score, c(0, 0)) - expect_equal(res@matches$ppm_error, c(0, 0)) - - ## no matches - res <- matchMz(qry + 0.1, trgt, par) - expect_true(is(res, "Matched")) - expect_equal(query(res), qry + 0.1) - expect_equal(target(res), trgt) - expect_true(nrow(res@matches) == 0) - - # positive tolerance - par <- MzParam(tolerance = 10) - res <- matchMz(qry, trgt, par) - expect_equal(query(res), qry) - expect_equal(target(res), trgt) - expect_equal(res@matches$query_idx, c(1, 1, 1, 2, 2, 2, 3, 3)) - expect_equal(res@matches$target_idx, c(4, 5, 6, 6, 7, 8, 7, 8)) - expect_equal(res@matches$score, c(10, 0, -10, 10, 0, -10, 9, -1)) - expect_equal(res@matches$ppm_error, c(10, 0, 10, 10, 0, 10, 9, 1) / - c(140, 150, 160, 160, 170, 180, 170, 180) * 10^6) + qry <- data.frame(mz = c(150, 170, 179)) + trgt <- data.frame(mz = seq(110, 200, 10)) + # qry <- data.frame(mz = c(150, 170, 179)) + # trgt <- seq(110, 200, 10) + # qry <- c(150, 170, 179) + # trgt <- data.frame(mz = seq(110, 200, 10)) + # qry <- c(150, 170, 179) + # trgt <- seq(110, 200, 10) + + par <- MzParam(tolerance = 0) + res <- matchMz(qry, trgt, par) + expect_equal(query(res), qry) + expect_equal(target(res), trgt) + expect_equal(res@matches$query_idx, c(1, 2)) + expect_equal(res@matches$target_idx, c(5, 7)) + expect_equal(res@matches$score, c(0, 0)) + expect_equal(res@matches$ppm_error, c(0, 0)) + + ## no matches + res <- matchMz(qry + 0.1, trgt, par) + expect_true(is(res, "Matched")) + expect_equal(query(res), qry + 0.1) + expect_equal(target(res), trgt) + expect_true(nrow(res@matches) == 0) + + # positive tolerance + par <- MzParam(tolerance = 10) + res <- matchMz(qry, trgt, par) + expect_equal(query(res), qry) + expect_equal(target(res), trgt) + expect_equal(res@matches$query_idx, c(1, 1, 1, 2, 2, 2, 3, 3)) + expect_equal(res@matches$target_idx, c(4, 5, 6, 6, 7, 8, 7, 8)) + expect_equal(res@matches$score, c(10, 0, -10, 10, 0, -10, 9, -1)) + expect_equal(res@matches$ppm_error, c(10, 0, 10, 10, 0, 10, 9, 1) / + c(140, 150, 160, 160, 170, 180, 170, 180) * 10^6) }) test_that("matchMz, MzRtParam works", { - qry <- data.frame(mz = c(13, 14.1, 17, 18), rt = c(23, 24, 26.8, 23)) - trgt <- data.frame(mz = 11:17, rt = 21:27) - - par <- MzRtParam(tolerance = 0, ppm = 0, toleranceRt = 0) - res <- matchMz(qry, trgt, par) - expect_equal(query(res), qry) - expect_equal(target(res), trgt) - expect_equal(res@matches$query_idx, c(1)) - expect_equal(res@matches$target_idx, c(3)) - expect_equal(res@matches$score, c(0)) - expect_equal(res@matches$ppm_error, c(0)) - expect_equal(res@matches$score_rt, c(0)) - - par <- MzRtParam(tolerance = 0.1, ppm = 0, toleranceRt = 0) - res <- matchMz(qry, trgt, par) - expect_equal(res@matches$query_idx, c(1, 2)) - expect_equal(res@matches$target_idx, c(3, 4)) - expect_equal(res@matches$score, c(0, 0.1)) - expect_equal(res@matches$ppm_error, c(0, 0.1 / 14 * 10^6)) - expect_equal(res@matches$score_rt, c(0, 0)) - - par <- MzRtParam(tolerance = 0.1, ppm = 0, toleranceRt = 0.2) - res <- matchMz(qry, trgt, par) - expect_equal(res@matches$query_idx, c(1, 2, 3)) - expect_equal(res@matches$target_idx, c(3, 4, 7)) - expect_equal(res@matches$score, c(0, 0.1, 0)) - expect_equal(res@matches$ppm_error, c(0, 0.1 / 14 * 10^6, 0)) - expect_equal(res@matches$score_rt, c(0, 0, -0.2)) - - ## no matches - res <- matchMz(qry + 0.5, trgt, par) - expect_true(is(res, "Matched")) - expect_equal(query(res), qry + 0.5) - expect_equal(target(res), trgt) - expect_true(nrow(res@matches) == 0) - - #### query `SummarizedExperiment` - library(SummarizedExperiment) - qry <- SummarizedExperiment( - assays = data.frame(matrix(NA, 4, 2)), - colData = data.frame(cD1 = c(NA, NA), cD2 = c(NA, NA)), - rowData = data.frame(mz = c(13, 14.1, 17, 18), rt = c(23, 24, 26.8, 23))) - trgt <- data.frame(mz = 11:17, rt = 21:27) - - par <- MzRtParam(tolerance = 0, ppm = 0, toleranceRt = 0) - res <- matchMz(qry, trgt, par) - expect_equal(query(res), qry) - expect_equal(target(res), trgt) - expect_equal(res@matches$query_idx, c(1)) - expect_equal(res@matches$target_idx, c(3)) - expect_equal(res@matches$score, c(0)) - expect_equal(res@matches$ppm_error, c(0)) - expect_equal(res@matches$score_rt, c(0)) - - par <- MzRtParam(tolerance = 0.1, ppm = 0, toleranceRt = 0) - res <- matchMz(qry, trgt, par) - expect_equal(res@matches$query_idx, c(1, 2)) - expect_equal(res@matches$target_idx, c(3, 4)) - expect_equal(res@matches$score, c(0, 0.1)) - expect_equal(res@matches$ppm_error, c(0, 0.1 / 14 * 10^6)) - expect_equal(res@matches$score_rt, c(0, 0)) - - par <- MzRtParam(tolerance = 0.1, ppm = 0, toleranceRt = 0.2) - res <- matchMz(qry, trgt, par) - expect_equal(res@matches$query_idx, c(1, 2, 3)) - expect_equal(res@matches$target_idx, c(3, 4, 7)) - expect_equal(res@matches$score, c(0, 0.1, 0)) - expect_equal(res@matches$ppm_error, c(0, 0.1 / 14 * 10^6, 0)) - expect_equal(res@matches$score_rt, c(0, 0, -0.2)) - - ## no matches - res <- matchMz(qry , trgt + 0.5, par) - expect_true(is(res, "Matched")) - expect_equal(query(res), qry) - expect_equal(target(res), trgt + 0.5) - expect_true(nrow(res@matches) == 0) + qry <- data.frame(mz = c(13, 14.1, 17, 18), rt = c(23, 24, 26.8, 23)) + trgt <- data.frame(mz = 11:17, rt = 21:27) + + par <- MzRtParam(tolerance = 0, ppm = 0, toleranceRt = 0) + res <- matchMz(qry, trgt, par) + expect_equal(query(res), qry) + expect_equal(target(res), trgt) + expect_equal(res@matches$query_idx, c(1)) + expect_equal(res@matches$target_idx, c(3)) + expect_equal(res@matches$score, c(0)) + expect_equal(res@matches$ppm_error, c(0)) + expect_equal(res@matches$score_rt, c(0)) + + par <- MzRtParam(tolerance = 0.1, ppm = 0, toleranceRt = 0) + res <- matchMz(qry, trgt, par) + expect_equal(res@matches$query_idx, c(1, 2)) + expect_equal(res@matches$target_idx, c(3, 4)) + expect_equal(res@matches$score, c(0, 0.1)) + expect_equal(res@matches$ppm_error, c(0, 0.1 / 14 * 10^6)) + expect_equal(res@matches$score_rt, c(0, 0)) + + par <- MzRtParam(tolerance = 0.1, ppm = 0, toleranceRt = 0.2) + res <- matchMz(qry, trgt, par) + expect_equal(res@matches$query_idx, c(1, 2, 3)) + expect_equal(res@matches$target_idx, c(3, 4, 7)) + expect_equal(res@matches$score, c(0, 0.1, 0)) + expect_equal(res@matches$ppm_error, c(0, 0.1 / 14 * 10^6, 0)) + expect_equal(res@matches$score_rt, c(0, 0, -0.2)) + + ## no matches + res <- matchMz(qry + 0.5, trgt, par) + expect_true(is(res, "Matched")) + expect_equal(query(res), qry + 0.5) + expect_equal(target(res), trgt) + expect_true(nrow(res@matches) == 0) + + #### query `SummarizedExperiment` + library(SummarizedExperiment) + qry <- SummarizedExperiment( + assays = data.frame(matrix(NA, 4, 2)), + colData = data.frame(cD1 = c(NA, NA), cD2 = c(NA, NA)), + rowData = data.frame(mz = c(13, 14.1, 17, 18), + rt = c(23, 24, 26.8, 23))) + trgt <- data.frame(mz = 11:17, rt = 21:27) + + par <- MzRtParam(tolerance = 0, ppm = 0, toleranceRt = 0) + res <- matchMz(qry, trgt, par) + expect_equal(query(res), qry) + expect_equal(target(res), trgt) + expect_equal(res@matches$query_idx, c(1)) + expect_equal(res@matches$target_idx, c(3)) + expect_equal(res@matches$score, c(0)) + expect_equal(res@matches$ppm_error, c(0)) + expect_equal(res@matches$score_rt, c(0)) + + par <- MzRtParam(tolerance = 0.1, ppm = 0, toleranceRt = 0) + res <- matchMz(qry, trgt, par) + expect_equal(res@matches$query_idx, c(1, 2)) + expect_equal(res@matches$target_idx, c(3, 4)) + expect_equal(res@matches$score, c(0, 0.1)) + expect_equal(res@matches$ppm_error, c(0, 0.1 / 14 * 10^6)) + expect_equal(res@matches$score_rt, c(0, 0)) + + par <- MzRtParam(tolerance = 0.1, ppm = 0, toleranceRt = 0.2) + res <- matchMz(qry, trgt, par) + expect_equal(res@matches$query_idx, c(1, 2, 3)) + expect_equal(res@matches$target_idx, c(3, 4, 7)) + expect_equal(res@matches$score, c(0, 0.1, 0)) + expect_equal(res@matches$ppm_error, c(0, 0.1 / 14 * 10^6, 0)) + expect_equal(res@matches$score_rt, c(0, 0, -0.2)) + + ## no matches + res <- matchMz(qry , trgt + 0.5, par) + expect_true(is(res, "Matched")) + expect_equal(query(res), qry) + expect_equal(target(res), trgt + 0.5) + expect_true(nrow(res@matches) == 0) }) test_that("matchMz, Mz2MassParam works", { - - m <- c(200, 300) - qry <- c(100, as.numeric(mass2mz(m, c("[M+H]+", "[M+K]+"))) + c(0, 0, 0, 5)) - trgt <- c(mass2mz(m, "[M-H]-"), 400, 500) - par <- Mz2MassParam(queryAdducts = c("[M+H]+", "[M+K]+"), + m <- c(200, 300) + qry <- c(100, as.numeric(mass2mz(m, c("[M+H]+", "[M+K]+"))) + c(0, 0, 0, 5)) + trgt <- c(mass2mz(m, "[M-H]-"), 400, 500) + + par <- Mz2MassParam(queryAdducts = c("[M+H]+", "[M+K]+"), + targetAdducts = "[M-H]-") + res <- matchMz(qry, trgt, par) + expect_equal(query(res), qry) + expect_equal(target(res), trgt) + expect_equal(res@matches$query_idx, c(2, 3, 4)) + expect_equal(res@matches$target_idx, c(1, 2, 1)) + expect_equal(res@matches$query_adduct, c("[M+H]+", "[M+H]+", "[M+K]+")) + expect_equal(res@matches$target_adduct, rep("[M-H]-", 3)) + expect_equal(res@matches$score, c(0, 0, 0)) + expect_equal(res@matches$ppm_error, c(0, 0, 0)) + + ## no matches + res <- matchMz(qry + 0.1, trgt, par) + expect_true(is(res, "Matched")) + expect_equal(query(res), qry + 0.1) + expect_equal(target(res), trgt) + expect_true(nrow(res@matches) == 0) + + # positive tolerance + par <- Mz2MassParam(queryAdducts = c("[M+H]+", "[M+K]+"), + targetAdducts = "[M-H]-", + tolerance = 10) + res <- matchMz(qry, trgt, par) + expect_equal(query(res), qry) + expect_equal(target(res), trgt) + expect_equal(res@matches$query_idx, c(2, 3, 4, 5)) + expect_equal(res@matches$target_idx, c(1, 2, 1, 2)) + expect_equal(res@matches$target_adduct, rep("[M-H]-", 4)) + expect_equal(res@matches$query_adduct, c(rep("[M+H]+", 2), + rep("[M+K]+", 2))) + expect_equal(res@matches$score, c(0, 0, 0, 5)) + expect_equal(res@matches$ppm_error, c(0, 0, 0, 5 / 300 * 10^6)) + + # `query`and `target` data.frames + qry_df <- data.frame(mzq = qry, other = 1) + trgt_df <- data.frame(other = "b", mzt = trgt) + res <- matchMz(qry_df, trgt_df, par, mzColname = c("mzq", "mzt")) + expect_equal(query(res), qry_df) + expect_equal(target(res), trgt_df) + expect_equal(res@matches$query_idx, c(2, 3, 4, 5)) + expect_equal(res@matches$target_adduct, rep("[M-H]-", 4)) + expect_equal(res@matches$query_adduct, c(rep("[M+H]+", 2), + rep("[M+K]+", 2))) + expect_equal(res@matches$score, c(0, 0, 0, 5)) + expect_equal(res@matches$ppm_error, c(0, 0, 0, 5 / 300 * 10^6)) +}) + +test_that("matchMz, Mz2MassRtParam works", { + + m <- c(200, 300) + qry <- c(100, as.numeric(mass2mz(m, c("[M+H]+", "[M+K]+"))) + c(0, 0, 0, 5)) + trgt <- c(mass2mz(m, "[M-H]-"), 400, 500) + qry_df <- data.frame(mz = qry, rt = c(50, 100, 151, 100, 150)) + trgt_df <- data.frame(mz = trgt, rt = c(100, 150, 200, 250)) + + par <- Mz2MassRtParam(queryAdducts = c("[M+H]+", "[M+K]+"), targetAdducts = "[M-H]-") - res <- matchMz(qry, trgt, par) - expect_equal(query(res), qry) - expect_equal(target(res), trgt) - expect_equal(res@matches$query_idx, c(2, 3, 4)) - expect_equal(res@matches$target_idx, c(1, 2, 1)) - expect_equal(res@matches$query_adduct, c("[M+H]+", "[M+H]+", "[M+K]+")) - expect_equal(res@matches$target_adduct, rep("[M-H]-", 3)) - expect_equal(res@matches$score, c(0, 0, 0)) - expect_equal(res@matches$ppm_error, c(0, 0, 0)) - - ## no matches - res <- matchMz(qry + 0.1, trgt, par) - expect_true(is(res, "Matched")) - expect_equal(query(res), qry + 0.1) - expect_equal(target(res), trgt) - expect_true(nrow(res@matches) == 0) - - # positive tolerance - par <- Mz2MassParam(queryAdducts = c("[M+H]+", "[M+K]+"), + res <- matchMz(qry_df, trgt_df, par) + expect_equal(query(res), qry_df) + expect_equal(target(res), trgt_df) + expect_equal(res@matches$query_idx, c(2, 4)) + expect_equal(res@matches$target_idx, c(1, 1)) + expect_equal(res@matches$query_adduct, c("[M+H]+", "[M+K]+")) + expect_equal(res@matches$target_adduct, rep("[M-H]-", 2)) + expect_equal(res@matches$score, c(0, 0)) + expect_equal(res@matches$ppm_error, c(0, 0)) + expect_equal(res@matches$score_rt, c(0, 0)) + + ## positive toleranceRt + par <- Mz2MassRtParam(queryAdducts = c("[M+H]+", "[M+K]+"), targetAdducts = "[M-H]-", - tolerance = 10) - res <- matchMz(qry, trgt, par) - expect_equal(query(res), qry) - expect_equal(target(res), trgt) - expect_equal(res@matches$query_idx, c(2, 3, 4, 5)) - expect_equal(res@matches$target_idx, c(1, 2, 1, 2)) - expect_equal(res@matches$target_adduct, rep("[M-H]-", 4)) - expect_equal(res@matches$query_adduct, c(rep("[M+H]+", 2), rep("[M+K]+", 2))) - expect_equal(res@matches$score, c(0, 0, 0, 5)) - expect_equal(res@matches$ppm_error, c(0, 0, 0, 5 / 300 * 10^6)) - - # `query`and `target` data.frames - qry_df <- data.frame(mzq = qry, other = 1) - trgt_df <- data.frame(other = "b", mzt = trgt) - res <- matchMz(qry_df, trgt_df, par, mzColname = c("mzq", "mzt")) - expect_equal(query(res), qry_df) - expect_equal(target(res), trgt_df) - expect_equal(res@matches$query_idx, c(2, 3, 4, 5)) - expect_equal(res@matches$target_adduct, rep("[M-H]-", 4)) - expect_equal(res@matches$query_adduct, c(rep("[M+H]+", 2), rep("[M+K]+", 2))) - expect_equal(res@matches$score, c(0, 0, 0, 5)) - expect_equal(res@matches$ppm_error, c(0, 0, 0, 5 / 300 * 10^6)) + toleranceRt = 2) + res <- matchMz(qry_df, trgt_df, par) + expect_equal(query(res), qry_df) + expect_equal(target(res), trgt_df) + expect_equal(res@matches$query_idx, c(2, 3, 4)) + expect_equal(res@matches$target_idx, c(1, 2, 1)) + expect_equal(res@matches$query_adduct, c("[M+H]+", "[M+H]+", "[M+K]+")) + expect_equal(res@matches$target_adduct, rep("[M-H]-", 3)) + expect_equal(res@matches$score, c(0, 0, 0)) + expect_equal(res@matches$ppm_error, c(0, 0, 0)) + expect_equal(res@matches$score_rt, c(0, 1, 0)) + + ## positive tolerance and toleranceRt + par <- Mz2MassRtParam(queryAdducts = c("[M+H]+", "[M+K]+"), + targetAdducts = "[M-H]-", tolerance = 10, + toleranceRt = 2) + res <- matchMz(qry_df, trgt_df, par) + expect_equal(query(res), qry_df) + expect_equal(target(res), trgt_df) + expect_equal(res@matches$query_idx, c(2, 3, 4, 5)) + expect_equal(res@matches$target_idx, c(1, 2, 1, 2)) + expect_equal(res@matches$query_adduct, c(rep("[M+H]+", 2), + rep("[M+K]+", 2))) + expect_equal(res@matches$target_adduct, rep("[M-H]-", 4)) + expect_equal(res@matches$score, c(0, 0, 0, 5)) + expect_equal(res@matches$ppm_error, c(0, 0, 0, 5 / 300 * 10^6)) + expect_equal(res@matches$score_rt, c(0, 1, 0, 0)) + + ## no matches + trgt_df$rt <- trgt_df$rt + 5 + res <- matchMz(qry_df, trgt_df, par) + expect_true(is(res, "Matched")) + expect_equal(query(res), qry_df) + expect_equal(target(res), trgt_df) + expect_true(nrow(res@matches) == 0) }) test_that(".valid_adduct works", { @@ -519,8 +593,6 @@ test_that(".valid_adduct works", { expect_null(.valid_adduct(c("[M+H]+"))) expect_equal(.valid_adduct(data.frame(a = 1, b = 2)), paste("Columns \"mass_add\" and \"mass_multi\" must be", - "present when `adducts` is a data.frame")) + "present when `adducts` is a data.frame")) expect_null(.valid_adduct(data.frame(mass_add = 1, mass_multi = 2))) - - })