diff --git a/DESCRIPTION b/DESCRIPTION index bb6897bc..b838d992 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -72,7 +72,7 @@ VignetteBuilder: knitr BugReports: https://github.com/RforMassSpectrometry/Spectra/issues URL: https://github.com/RforMassSpectrometry/Spectra biocViews: Infrastructure, Proteomics, MassSpectrometry, Metabolomics -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Roxygen: list(markdown=TRUE) Collate: 'hidden_aliases.R' diff --git a/NAMESPACE b/NAMESPACE index 386f4c31..de65e327 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -78,6 +78,7 @@ exportMethods(containsNeutralLoss) exportMethods(dataOrigin) exportMethods(dataStorage) exportMethods(dropNaSpectraVariables) +exportMethods(entropy) exportMethods(export) exportMethods(filterAcquisitionNum) exportMethods(filterDataOrigin) @@ -153,12 +154,14 @@ importFrom(MsCoreUtils,coefMA) importFrom(MsCoreUtils,coefSG) importFrom(MsCoreUtils,coefWMA) importFrom(MsCoreUtils,common) +importFrom(MsCoreUtils,entropy) importFrom(MsCoreUtils,group) importFrom(MsCoreUtils,i2index) importFrom(MsCoreUtils,join) importFrom(MsCoreUtils,join_gnps) importFrom(MsCoreUtils,localMaxima) importFrom(MsCoreUtils,ndotproduct) +importFrom(MsCoreUtils,nentropy) importFrom(MsCoreUtils,noise) importFrom(MsCoreUtils,ppm) importFrom(MsCoreUtils,rbindFill) diff --git a/R/Spectra.R b/R/Spectra.R index 1f19765e..c6f7429b 100644 --- a/R/Spectra.R +++ b/R/Spectra.R @@ -659,9 +659,10 @@ NULL #' documentation of [isotopologues()] for more information. The approach #' and code to define the parameters for isotope prediction is described #' [here](https://github.com/EuracBiomedicalResearch/isotopologues). -#' -#' - `entropy`: calculates the entropy of each spectra based on the metrics suggested by -#' Li et al. (https://doi.org/10.1038/s41592-021-01331-z). +#' +#' - `entropy`: calculates the entropy of each spectra based on the metrics +#' suggested by Li et al. (https://doi.org/10.1038/s41592-021-01331-z). +#' See also [nentropy()] in the *MsCoreUtils* package for details. #' #' - `estimatePrecursorIntensity`: defines the precursor intensities for MS2 #' spectra using the intensity of the matching MS1 peak from the @@ -928,6 +929,9 @@ NULL #' @param neutralLoss for `containsNeutralLoss`: `numeric(1)` defining the value #' which should be subtracted from the spectrum's precursor m/z. #' +#' @param normalized for `entropy`: `logical(1)` whether the normalized entropy +#' should be calculated (default). See also [nentropy()] for details. +#' #' @param object For `Spectra`: either a `DataFrame` or `missing`. See section #' on creation of `Spectra` objects for details. For all other methods a #' `Spectra` object. @@ -2647,12 +2651,19 @@ setMethod("combinePeaks", "Spectra", function(object, tolerance = 0, ppm = 20, #' @rdname Spectra #' #' @importFrom MsCoreUtils entropy nentropy +#' +#' @export setMethod("entropy", "Spectra", function(object, normalized = TRUE) { - if (normalized) entropy_fun <- nentropy - else entropy_fun <- entropy - unlist(.peaksapply( - object, FUN = function(pks, ...) entropy_fun(pks[, "intensity"])), - use.names = FALSE - ) -}) - + if (length(object)) { + if (normalized) entropy_fun <- nentropy + else entropy_fun <- entropy + unlist(.peaksapply( + object, FUN = function(pks, ...) entropy_fun(pks[, "intensity"])), + use.names = FALSE + ) + } else numeric() +}) +#' @rdname Spectra +setMethod("entropy", "ANY", function(object, ...) { + MsCoreUtils::entropy(object) +}) \ No newline at end of file diff --git a/man/Spectra.Rd b/man/Spectra.Rd index 2152d640..aa309503 100644 --- a/man/Spectra.Rd +++ b/man/Spectra.Rd @@ -104,6 +104,8 @@ \alias{coreSpectraVariables} \alias{backendBpparam,Spectra-method} \alias{combinePeaks,Spectra-method} +\alias{entropy,Spectra-method} +\alias{entropy,ANY-method} \title{The Spectra class to manage and access MS data} \usage{ applyProcessing( @@ -480,6 +482,10 @@ coreSpectraVariables() msLevel. = uniqueMsLevels(object), ... ) + +\S4method{entropy}{Spectra}(object, normalized = TRUE) + +\S4method{entropy}{ANY}(object, ...) } \arguments{ \item{object}{For \code{Spectra}: either a \code{DataFrame} or \code{missing}. See section @@ -756,6 +762,9 @@ is ignored if \code{weighted = TRUE} (the default).} \item{weighted}{For \code{combinePeaks}: \code{logical(1)} whether m/z values of peaks within each peak group should be aggregated into a single m/z value using an intensity-weighted mean. Defaults to \code{weighted = TRUE}.} + +\item{normalized}{for \code{entropy}: \code{logical(1)} whether the normalized entropy +should be calculated (default). See also \code{\link[=nentropy]{nentropy()}} for details.} } \value{ See individual method description for the return value. @@ -1335,6 +1344,9 @@ might not be detected. See parameter \code{substDefinition} in the documentation of \code{\link[=isotopologues]{isotopologues()}} for more information. The approach and code to define the parameters for isotope prediction is described \href{https://github.com/EuracBiomedicalResearch/isotopologues}{here}. +\item \code{entropy}: calculates the entropy of each spectra based on the metrics +suggested by Li et al. (https://doi.org/10.1038/s41592-021-01331-z). +See also \code{\link[=nentropy]{nentropy()}} in the \emph{MsCoreUtils} package for details. \item \code{estimatePrecursorIntensity}: defines the precursor intensities for MS2 spectra using the intensity of the matching MS1 peak from the closest MS1 spectrum (i.e. the last MS1 spectrum measured before the diff --git a/man/hidden_aliases.Rd b/man/hidden_aliases.Rd index 127c07e1..b6f91d4e 100644 --- a/man/hidden_aliases.Rd +++ b/man/hidden_aliases.Rd @@ -13,6 +13,7 @@ \alias{containsMz} \alias{containsNeutralLoss} \alias{dropNaSpectraVariables} +\alias{entropy} \alias{export} \alias{filterMzRange} \alias{filterMzValues} @@ -191,6 +192,8 @@ containsNeutralLoss(object, ...) dropNaSpectraVariables(object, ...) +entropy(object, ...) + export(object, ...) filterMzRange(object, ...) diff --git a/tests/testthat/test_Spectra.R b/tests/testthat/test_Spectra.R index 84f45304..93df376a 100644 --- a/tests/testthat/test_Spectra.R +++ b/tests/testthat/test_Spectra.R @@ -1788,16 +1788,15 @@ test_that("entropy,Spectra works", { sps <- Spectra() res <- entropy(sps) expect_identical(res, numeric()) - + df <- DataFrame(msLevel = c(1L, 2L), centroided = TRUE) - df$intensity <- list(c(5, 9, 3), c(9, 8, 2)) df$mz <- list(1:3, 1:3) + df$intensity <- list(c(5, 9, 3), c(9, 8, 2)) sps <- Spectra(df) - - res <- entropy(sps) - expect_identical(res, c(17, 19)) - - sps <- replaceIntensitiesBelow(sps, threshold = 4) - res <- entropy(sps) - expect_identical(res, c(14, 17)) -}) \ No newline at end of file + + res <- entropy(sps, normalized = TRUE) + expect_identical(res, vapply(df$intensity, MsCoreUtils::nentropy, numeric(1))) + + res <- entropy(sps, normalized = FALSE) + expect_identical(res, vapply(df$intensity, MsCoreUtils::entropy, numeric(1))) +})