Skip to content

Commit

Permalink
Merge pull request #309 from mar-garcia/main
Browse files Browse the repository at this point in the history
add entropy functions
  • Loading branch information
jorainer authored Jan 30, 2024
2 parents 9732eb2 + 01eaca1 commit 8eaa18c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: Spectra
Title: Spectra Infrastructure for Mass Spectrometry Data
Version: 1.13.3
Version: 1.13.4
Description: The Spectra package defines an efficient infrastructure
for storing and handling mass spectrometry spectra and functionality to
subset, process, visualize and compare spectra data. It provides different
Expand Down Expand Up @@ -32,6 +32,10 @@ Authors@R: c(person(given = "RforMassSpectrometry Package Maintainer",
comment = c(ORCID = "0000-0003-0541-7369")),
person(given = "Nir", family = "Shahaf",
email = "[email protected]",
role = "ctb"),
person(given = "Mar", family = "Garcia-Aloy",
email = "[email protected]",
comment = c(ORCID = "0000-0002-1330-6610"),
role = "ctb"))
Depends:
R (>= 4.0.0),
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Spectra 1.13

## Changes in 1.13.4

- Add `entropy` and `nentropy` functions to allow to calculate the (normalized)
entropy for each spectrum.

## Changes in 1.13.3

- Fix issue in `setBackend` that might cause chunk-wise processing to be not
Expand Down
3 changes: 3 additions & 0 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ setGeneric("containsNeutralLoss", function(object, ...)
setGeneric("dropNaSpectraVariables", function(object, ...)
standardGeneric("dropNaSpectraVariables"))
#' @rdname hidden_aliases
setGeneric("entropy", function(object, ...)
standardGeneric("entropy"))
#' @rdname hidden_aliases
setGeneric("export", function(object, ...)
standardGeneric("export"))
setGeneric("filterFourierTransformArtefacts", function(object, ...)
Expand Down
17 changes: 17 additions & 0 deletions R/Spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,9 @@ 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).
#'
#' - `estimatePrecursorIntensity`: defines the precursor intensities for MS2
#' spectra using the intensity of the matching MS1 peak from the
Expand Down Expand Up @@ -2639,3 +2642,17 @@ setMethod("combinePeaks", "Spectra", function(object, tolerance = 0, ppm = 20,
ppm, " and tolerance = ", tolerance, ".")
object
})


#' @rdname Spectra
#'
#' @importFrom MsCoreUtils entropy nentropy
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
)
})

18 changes: 18 additions & 0 deletions tests/testthat/test_Spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -1783,3 +1783,21 @@ test_that("processingChunkSize works", {
processingChunkSize(tmp) <- 10
expect_equal(processingChunkSize(tmp), 10)
})

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

0 comments on commit 8eaa18c

Please sign in to comment.