Skip to content

Commit

Permalink
Merge pull request #367 from lgatto/spectra_methods
Browse files Browse the repository at this point in the history
Spectra methods
  • Loading branch information
lgatto authored Oct 24, 2018
2 parents 55058b6 + 4ce04c3 commit 57fb5db
Show file tree
Hide file tree
Showing 16 changed files with 806 additions and 166 deletions.
11 changes: 7 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ importFrom(ggplot2, geom_histogram, geom_text, geom_line, geom_point,
facet_grid)

importFrom(MALDIquant, binPeaks, match.closest) ## only using ::: functions
importFrom(S4Vectors, isEmpty, metadata, DataFrame, replaceROWS, SimpleList)
importMethodsFrom(S4Vectors, showAsCell, "mcols<-", mcols, split, endoapply)
importFrom(S4Vectors, isEmpty, metadata, DataFrame, replaceROWS, SimpleList,
endoapply)
importMethodsFrom(S4Vectors, showAsCell, "mcols<-", mcols, split)
importClassesFrom(S4Vectors, SimpleList)
importFrom(MASS, rlm)

Expand Down Expand Up @@ -146,7 +147,8 @@ export(MSnSet,
requiredFvarLabels,
addMSnSetMetadata,
readSRMData,
combineSpectra,
meanMzInts,
consensusSpectrum,
combineSpectraMovingWindow,
estimateMzScattering,
hasSpectra, hasChromatograms,
Expand Down Expand Up @@ -307,7 +309,8 @@ exportMethods(updateObject,
reduce,
writeMSData,
productMz,
estimateMzResolution
estimateMzResolution,
combineSpectra
)

## methods NOT exported
Expand Down
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Changes in version 2.7.10
- Methods for Spectra class <2018-10-15 Mon>
- Change default for `timeDomain` in `combineSpectra` and
`combineSpectraMovingWindow` to `FALSE` <2018-10-18 Thu>
- Add new spectra combination function `consensusSpectrum` <2018-10-24 Wed>

Changes in version 2.7.9
- Import rather than depend on BiocParallel <2018-10-15 Mon>
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Changes in version 2.7.10
- Methods for Spectra class <2018-10-15 Mon>
- Change default for `timeDomain` in `combineSpectra` and
`combineSpectraMovingWindow` to `FALSE` <2018-10-18 Thu>
- Add new spectra combination function `consensusSpectrum` <2018-10-24 Wed>

## Changes in version 2.7.9
- Import rather than depend on BiocParallel <2018-10-15 Mon>
Expand Down
3 changes: 3 additions & 0 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,6 @@ setGeneric("productMz", function(object, ...) standardGeneric("productMz"))
## centroiding related
setGeneric("estimateMzResolution", function(object, ...)
standardGeneric("estimateMzResolution"))

setGeneric("combineSpectra", function(object, ...)
standardGeneric("combineSpectra"))
23 changes: 12 additions & 11 deletions R/functions-MSnExp.R
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ estimateMzScattering <- function(x, halfWindowSize = 1L, timeDomain = FALSE) {
#' signal to noise ratio.
#'
#' Intensities (and m/z values) for signals with the same m/z value in
#' consecutive scans are aggregated using the `intensityFun` and `mzFun`.
#' consecutive scans are aggregated using the `intensityFun`.
#' m/z values of intensities from consecutive scans will never be exactly
#' identical, even if they represent signal from the same ion. The function
#' determines thus internally a similarity threshold based on differences
Expand All @@ -457,7 +457,7 @@ estimateMzScattering <- function(x, halfWindowSize = 1L, timeDomain = FALSE) {
#' threshold is estimated on the 100 spectra with the largest number of
#' m/z - intensity pairs (i.e. mass peaks).
#'
#' See [combineSpectra()] for details.
#' See [meanMzInts()] for details.
#'
#' Parameter `timeDomain`: by default, m/z-intensity pairs from consecutive
#' scans to be aggregated are defined based on the square root of the m/z
Expand Down Expand Up @@ -485,15 +485,15 @@ estimateMzScattering <- function(x, halfWindowSize = 1L, timeDomain = FALSE) {
#'
#' @param BPPARAM parallel processing settings.
#'
#' @inheritParams combineSpectra
#' @inheritParams meanMzInts
#'
#' @return `MSnExp` with the same number of spectra than `x`.
#'
#' @md
#'
#' @seealso
#'
#' [combineSpectra()] for the function combining spectra provided in
#' [meanMzInts()] for the function combining spectra provided in
#' a `list`.
#'
#' [estimateMzScattering()] for a function to estimate m/z value scattering in
Expand Down Expand Up @@ -537,18 +537,18 @@ estimateMzScattering <- function(x, halfWindowSize = 1L, timeDomain = FALSE) {
#' plot(chr_comb)
#' ## Chromatographic data is "smoother" after combining.
combineSpectraMovingWindow <- function(x, halfWindowSize = 1L,
mzFun = base::mean,
intensityFun = base::mean,
mzd = NULL,
timeDomain = TRUE,
timeDomain = FALSE,
weighted = FALSE,
BPPARAM = bpparam()){
if (!is(x, "MSnExp"))
stop("'x' has to be a 'MSnExp' or an 'OnDiskMSnExp'")
if (is(x, "OnDiskMSnExp"))
x <- as(x, "MSnExp")
## Combine spectra per file
new_sp <- bplapply(split(spectra(x), fromFile(x)), FUN = function(z, intF,
mzF, hws,
wght, hws,
mzd,
timeD) {
len_z <- length(z)
Expand All @@ -566,13 +566,14 @@ combineSpectraMovingWindow <- function(x, halfWindowSize = 1L,
res <- vector("list", len_z)
hwsp <- hws + 1L
for (i in seq_along(z)) {
res[[i]] <- combineSpectra(z[windowIndices(i, hws, len_z)],
mzFun = mzF, intensityFun = intF,
res[[i]] <- meanMzInts(z[windowIndices(i, hws, len_z)],
weighted = wght, intensityFun = intF,
main = hwsp - (i <= hws) * (hwsp - i),
mzd = mzd, timeDomain = timeD)
mzd = mzd, timeDomain = timeD,
unionPeaks = FALSE)
}
res
}, intF = intensityFun, mzF = mzFun, hws = as.integer(halfWindowSize),
}, intF = intensityFun, wght = weighted, hws = as.integer(halfWindowSize),
mzd = mzd, timeD = timeDomain, BPPARAM = BPPARAM)
new_sp <- unsplit(new_sp, fromFile(x))
names(new_sp) <- featureNames(x)
Expand Down
Loading

0 comments on commit 57fb5db

Please sign in to comment.