Skip to content

Commit

Permalink
fix: fix entropy unit tests and add general method
Browse files Browse the repository at this point in the history
  • Loading branch information
jorainer committed Jan 30, 2024
1 parent 8eaa18c commit cd2545d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ exportMethods(containsNeutralLoss)
exportMethods(dataOrigin)
exportMethods(dataStorage)
exportMethods(dropNaSpectraVariables)
exportMethods(entropy)
exportMethods(export)
exportMethods(filterAcquisitionNum)
exportMethods(filterDataOrigin)
Expand Down Expand Up @@ -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)
Expand Down
33 changes: 22 additions & 11 deletions R/Spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
})
12 changes: 12 additions & 0 deletions man/Spectra.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/hidden_aliases.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions tests/testthat/test_Spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})

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)))
})

0 comments on commit cd2545d

Please sign in to comment.