Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some more Spectra methods #378

Merged
merged 5 commits into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: MSnbase
Title: Base Functions and Classes for Mass Spectrometry and Proteomics
Version: 2.9.0
Version: 2.9.1
Description: MSnbase provides infrastructure for manipulation,
processing and visualisation of mass spectrometry and
proteomics data, ranging from raw to quantitative and
Expand Down
8 changes: 7 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# MSnbase 2.9

Changes in version 2.9.1
- Add clean,Chromatograms (issue #354) <2018-11-07 Wed>
- Add as,Spectra,list as,Spectra,MSnExp and as,MSnExp,Spectra (issue #370)
<2018-11-07 Wed>
- Add filterMsLevel,Spectra <2018-11-07 Wed>

Changes in version 2.9.0
- New version for Bioc 3.9 devel

Expand All @@ -20,7 +26,7 @@ Changes in version 2.7.12


Changes in version 2.7.11
- Change default for `timeDomain` in `combineSpectra` and
- Change default for `timeDomain` in `combineSpectra` and
`combineSpectraMovingWindow` to `FALSE` <2018-10-18 Thu>
- Add new spectra combination function `consensusSpectrum`
<2018-10-24 Wed>
Expand Down
9 changes: 8 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# MSnbase 2.9

## Changes in version 2.9.1
- Add clean,Chromatograms (issue
[#354](https://github.com/lgatto/MSnbase/issues/354)) <2018-11-07 Wed>
- Add as,Spectra,list as,Spectra,MSnExp and as,MSnExp,Spectra
(issue [#370](https://github.com/lgatto/MSnbase/issues/370)) <2018-11-07 Wed>
- Add filterMsLevel,Spectra <2018-11-07 Wed>

## Changes in version 2.9.0
- New version for Bioc 3.9 devel

Expand All @@ -20,7 +27,7 @@


## Changes in version 2.7.11
- Change default for `timeDomain` in `combineSpectra` and
- Change default for `timeDomain` in `combineSpectra` and
`combineSpectraMovingWindow` to `FALSE` <2018-10-18 Thu>
- Add new spectra combination function `consensusSpectrum`
<2018-10-24 Wed>
Expand Down
18 changes: 10 additions & 8 deletions R/DataClasses.R
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,9 @@ setClass("ProcessingStep",
#' example, a \code{Chromatogram} for a SRM transition 273 -> 153 will have
#' a \code{@precursorMz = c(273, 273)} and a
#' \code{@productMz = c(153, 153)}.
#'
#'
sgibb marked this conversation as resolved.
Show resolved Hide resolved
#' @rdname Chromatogram-class
#'
#'
#' @export
#'
#' @seealso \code{\link{Chromatograms}} for combining \code{Chromatogram} in
Expand All @@ -535,7 +535,7 @@ setClass("ProcessingStep",
#' object.
#' \code{\link{clean}} for the method to \emph{clean} a \code{Chromatogram}
#' object.
#'
#'
#' @author Johannes Rainer
#'
#' @examples
Expand Down Expand Up @@ -588,7 +588,7 @@ setClass("Chromatogram",
#' @title Container for multiple Chromatogram objects
#'
#' @aliases coerce,matrix,Chromatograms-method
#'
#'
#' @description The \code{Chromatograms} class allows to store
#' \code{\link{Chromatogram}} objects in a \code{matrix}-like
#' two-dimensional structure.
Expand All @@ -611,9 +611,9 @@ setClass("Chromatogram",
#' \code{\linkS4class{OnDiskMSnExp}} object.
#' \code{\link{readSRMData}} for the function to read chromatographic data
#' of an SRM/MRM experiment.
#'
#'
#' @author Johannes Rainer
#'
#'
#' @examples
#' ## Creating some chromatogram objects to put them into a Chromatograms object
#' ints <- abs(rnorm(25, sd = 200))
Expand Down Expand Up @@ -676,7 +676,7 @@ setClass("Chromatograms",

#' @name Spectra
#'
#' @aliases Spectra-class show,Spectra-method
#' @aliases Spectra-class show,Spectra-method coerce,Spectra,list-method coerce,Spectra,MSnExp-method
#'
#' @title List of Spectrum objects along with annotations
#'
Expand All @@ -687,6 +687,8 @@ setClass("Chromatograms",
#' the possibility to add arbitrary annotations to each individual
#' `Spectrum` object. These can be accessed/set with the [mcols()] method.
#'
#' `Spectra` objects can be created with the `Spectra` function.
#'
#' Functions to access the individual spectra's attributes are available
#' (listed below).
#'
Expand All @@ -699,7 +701,7 @@ setClass("Chromatograms",
#' @param object For all functions: a `Spectra` object.
#'
#' @param x For all functions: a `Spectra` object.
#'
#'
#' @md
#'
#' @rdname Spectra
Expand Down
9 changes: 4 additions & 5 deletions R/functions-Chromatograms.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
#'
#' @param featureData either a \code{data.frame} or \code{AnnotatedDataFrame}
#' with additional information for each row of chromatograms.
#'
#'
#' @param ... Additional parameters to be passed to the
#' \code{\link[base]{matrix}} constructor, such as \code{nrow}, \code{ncol}
#' and \code{byrow}.
#'
#'
#' @rdname Chromatograms-class
Chromatograms <- function(data, phenoData, featureData, ...) {
if (missing(data))
Expand Down Expand Up @@ -112,7 +112,7 @@ Chromatograms <- function(data, phenoData, featureData, ...) {
## to the number of data points that were actually measured.
lens <- lengths(x)
maxLens <- max(lens)

ints <- rts <- matrix(NA_real_, nrow = maxLens, ncol = length(x))
for (i in seq(along = x)) {
if (lens[i]) {
Expand Down Expand Up @@ -180,7 +180,7 @@ Chromatograms <- function(data, phenoData, featureData, ...) {
#' `Chromatogram` objects in each row.
#'
#' @author Johannes Rainer
#'
#'
#' @noRd
.bin_Chromatograms <- function(object, binSize = 0.5, breaks = numeric(),
fun = max) {
Expand All @@ -196,4 +196,3 @@ Chromatograms <- function(data, phenoData, featureData, ...) {
if (validObject(object))
object
}

39 changes: 31 additions & 8 deletions R/methods-Chromatograms.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ setMethod("show", "Chromatograms", function(object) {
}
rn <- rownames(out)
out <- rbind(rep("<Chromatogram>", ncol(out)), out)
rownames(out) <- c("", rn)
rownames(out) <- c("", rn)
print(out, quote = FALSE, right = TRUE)
}
cat("phenoData with", length(varLabels(object@phenoData)), "variables\n")
Expand All @@ -55,9 +55,9 @@ setAs("matrix", "Chromatograms", function(from) {
#' object (with the exception of extracting a single element)
#' unless \code{drop = TRUE} is specified. This is different from the
#' default subsetting behaviour of \code{matrix}-like objects.
#'
#'
#' @param x For all methods: a \code{Chromatograms} object.
#'
#'
#' @param i For \code{[}: \code{numeric}, \code{logical} or \code{character}
#' defining which row(s) to extract.
#'
Expand Down Expand Up @@ -123,7 +123,7 @@ setMethod("[", "Chromatograms",
#' @param value For \code{[<-}: the replacement object(s). Can be a \code{list}
#' of \code{\link{Chromatogram}} objects or, if length of \code{i} and
#' \code{j} are 1, a single \code{\link{Chromatogram}} object.
#'
#'
#' For \code{pData<-}: a \code{data.frame} with the number of rows matching
#' the number of columns of \code{object}.
#'
Expand Down Expand Up @@ -163,7 +163,7 @@ setReplaceMethod("[", "Chromatograms",
})

#' @rdname Chromatograms-class
#'
#'
#' @description \code{plot}: plots a \code{Chromatograms} object. For each row
#' in the object one plot is created, i.e. all \code{\link{Chromatogram}}
#' objects in the same row are added to the same plot.
Expand All @@ -182,11 +182,11 @@ setReplaceMethod("[", "Chromatograms",
#' @param type For \code{plot}: the type of plot (see
#' \code{\link[graphics]{plot}} for more details. Can be either a vector
#' of length 1 or of length equal to \code{ncol(x)}.
#'
#'
#' @inheritParams Chromatogram-class
#'
#' @examples
#'
#'
#' ## Create some random Chromatogram objects
#' ints <- abs(rnorm(123, mean = 200, sd = 32))
#' ch1 <- Chromatogram(rtime = seq_along(ints), intensity = ints, mz = 231)
Expand Down Expand Up @@ -312,7 +312,7 @@ setMethod("isEmpty", "Chromatograms", function(x) {
})

#' @rdname Chromatograms-class
#'
#'
#' @description \code{featureNames}: returns the feature names of the
#' \code{Chromatograms} object.
setMethod("featureNames", "Chromatograms", function(object)
Expand Down Expand Up @@ -463,3 +463,26 @@ setMethod("polarity", "Chromatograms", function(object) {
#'
#' @rdname Chromatograms-class
setMethod("bin", "Chromatograms", .bin_Chromatograms)

#' @description
#'
#' \code{clean}: removes 0-intensity data points. Either all of them
#' (with \code{all = TRUE}) or all except those adjacent to non-zero
#' intensities (\code{all = FALSE}; default). See \code{\link{clean}}
#' documentation for more details and examples.
#'
#' @param all for \code{clean}: \code{logical(1)} whether all 0 intensities
#' should be removed (\code{all = TRUE}), or whether 0-intensities
#' adjacent to peaks should be kept (\code{all = FALSE}; default).
#'
#' @param na.rm for \code{clean}: \code{logical(1)} whether all \code{NA}
#' intensities should be removed prior to clean 0 intensity data points.
#'
#' @rdname Chromatograms-class
setMethod("clean", "Chromatograms", function(object, all = FALSE,
na.rm = FALSE) {
[email protected] <- matrix(lapply(object, clean, all = all, na.rm = na.rm),
nrow = nrow(object), dimnames = dimnames(object))
if (validObject(object))
object
})
12 changes: 12 additions & 0 deletions R/methods-MSnExp.R
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,15 @@ setAs("MSnExp", "data.frame", function(from) {
})
as.data.frame.MSnExp <- function(x, row.names = NULL, optional=FALSE, ...)
as(x, "data.frame")

setAs("MSnExp", "Spectra", function(from) {
fdta <- fData(from)
red_cn <- c("fileIdx", "spIdx", "smoothed", "seqNum", "acquisitionNum",
"msLevel", "polarity", "originalPeaksCount", "totIonCurrent",
"retentionTime", "basePeakMZ", "basePeakIntensity",
"collisionEnergy", "ionisationEnergy", "precursorScanNum",
"precursorMZ", "precursorCharge", "precursorIntensity",
"mergedScan", "centroided", "spectrum")
fdta <- fdta[, !colnames(fdta) %in% red_cn, drop = FALSE]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why aren't all feature variables passed along during coercion? I would prefer to keep all of them by default and have a selectFeatureData function to select those to keep.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guessed because they are part of a Spectrum object?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not pass all feature variables to avoid redundancy. All these that are not passed along will be present (are set) in the individual Spectrum object - so no need to have them in addition as metadata columns.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, thank you. Is there already an accessor for these (centroided,Spectra, ...)? The implementation will be identical to the corresponding in memory MSnExp methods.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, all methods to access slot values for Spectrum objects are also implemented for Spectra objects (in methods-Spectra.R), returning always a vector of length equal to Spectra with the corresponding slot values.

Spectra(spectra(from), elementMetadata = DataFrame(fdta))
})
Loading