Skip to content

Commit

Permalink
tests: increase unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jorainer committed Dec 18, 2024
1 parent 05c3086 commit fe6208d
Show file tree
Hide file tree
Showing 25 changed files with 448 additions and 182 deletions.
8 changes: 4 additions & 4 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ setGeneric("matchedData", function(object, ...)
#' from `target` based on settings specified with `param` and returns the result
#' from this as a [MatchedSpectra] object.
#'
#' @param query [Spectra] object with the (experimental) spectra.
#' @param query [Spectra::Spectra] object with the (experimental) spectra.
#'
#' @param target spectral data to compare against. Can be another [Spectra].
#' @param target MS data to compare against. Can be another [Spectra::Spectra].
#'
#' @param param parameter object containing the settings for the matching (e.g.
#' eventual prefiltering settings, cut-off value for similarity above which
Expand All @@ -44,8 +44,8 @@ setGeneric("matchedData", function(object, ...)
#'
#' @author Johannes Rainer
#'
#' @seealso [CompareSpectraParam()] for the comparison between [Spectra]
#' objects.
#' @seealso [CompareSpectraParam()] for the comparison between
#' [Spectra::Spectra] objects.
#'
#' @export
setGeneric("matchSpectra", function(query, target, param, ...)
Expand Down
6 changes: 4 additions & 2 deletions R/CompAnnotationSource.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
#' Objects extending `CompAnnotationSource` available in this package are:
#'
#' - [CompDbSource()]: annotation source referencing an annotation source in the
#' `[CompDb()]` format ( from the `CompoundDb` Bioconductor package).
#' `[CompoundDb::CompDb()]` format ( from the `CompoundDb` Bioconductor
#' package).
#'
#' Classes extending `CompAnnotationSource` need to implement the `matchSpectra`
#' method with parameters `query`, `target` and `param` where `query` is
Expand All @@ -32,7 +33,8 @@
#' This would also enable parallel processing of annotations as no e.g. database
#' connection would have to be shared across processes.
#'
#' @param query for `matchSpectra`: [Spectra] object with the query spectra.
#' @param query for `matchSpectra`: [Spectra::Spectra] object with the query
#' spectra.
#'
#' @param target for `matchSpectra`: object extending [CompAnnotationSource]
#' (such as [CompDbSource]) with the target (reference) spectra to compare
Expand Down
17 changes: 11 additions & 6 deletions R/CompDbSource.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
#'
#' @description
#'
#' `CompDbSource` objects represent references to [CompDb] database-backed
#' annotation resources. Instances are expected to be created with the dedicated
#' construction functions such as `MassBankSource` or the generic
#' `CompDbSource`. The annotation data is not stored within the object but will
#' be accessed/loaded within the object's `matchSpectra` method.
#' `CompDbSource` objects represent references to [CompoundDb::CompDb]
#' database-backed annotation resources. Instances are expected to be
#' created with the dedicated construction functions such as
#' `MassBankSource` or the generic `CompDbSource`. The annotation data is
#' not stored within the object but will be accessed/loaded within the
#' object's `matchSpectra` method.
#'
#' New `CompDbSource` objects can be created using the functions:
#'
Expand Down Expand Up @@ -149,7 +150,7 @@ setMethod(
#'
#' @rdname CompDbSource
MassBankSource <- function(release = "2021.03", ...) {
if (!requireNamespace("AnnotationHub", quietly = TRUE))
if (!.is_available_annotation_hub())
stop("'MassBankSource' requires the 'AnnotationHub' package.\n",
"Please install it with 'BiocManager::install(",
"\"AnnotationHub\")' and try again.")
Expand All @@ -163,3 +164,7 @@ MassBankSource <- function(release = "2021.03", ...) {
fn <- unname(AnnotationHub::cache(res))
CompDbSource(fn)
}

.is_available_annotation_hub <- function(x) {
requireNamespace("AnnotationHub", quietly = TRUE)
}
26 changes: 14 additions & 12 deletions R/Matched.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
#' structures such as `SummarizedExperiments` or `QFeatures`. Combinations of
#' all these different data types are also supported. Matches are represented
#' between elements of one-dimensional objects, or rows for two-dimensional
#' objects (including `SummarizedExperiment` or `QFeatures`). For [QFeatures()]
#' objects matches to only one of the *assays* within the object is supported.
#' objects (including `SummarizedExperiment` or `QFeatures`). For
#' [QFeatures::QFeatures()] objects matches to only one of the *assays*
#' within the object is supported.
#'
#' @section Creation and general handling:
#'
Expand Down Expand Up @@ -142,8 +143,9 @@
#' extracted with `x$query` (`x$target`). More precisely, when
#' *query* (*target*) is a `SummarizedExperiment` the columns from
#' `rowData(query)` (rowData(`target`)) are extracted; when *query* (*target*)
#' is a [QFeatures()] the columns from `rowData` of the assay specified in the
#' `queryAssay` (`targetAssay`) slot are extracted. The matching scores
#' is a [QFeatures::QFeatures()] the columns from `rowData` of the assay
#' specified in the `queryAssay` (`targetAssay`) slot are extracted.
#' The matching scores
#' are available as *variable* `"score"`. Similar to a left join between the
#' query and target elements, this function returns a value for each query
#' element, with eventual duplicated values for query elements matching more
Expand Down Expand Up @@ -315,7 +317,7 @@
#'
#' @return See individual method description above for details.
#'
#' @seealso [MatchedSpectra()] for matched [Spectra()] objects.
#' @seealso [MatchedSpectra()] for matched [Spectra::Spectra()] objects.
#'
#' @exportClass Matched
#'
Expand Down Expand Up @@ -1064,17 +1066,17 @@ pruneTarget <- function(object) {
call. = FALSE)
if (length(dim(query)) == 2) {
if (length(queryColname) == 0)
stop("\"", queryColname,
"\" must be set when 'query' is 2-dimensional", call. = FALSE)
stop("'queryColname' must be provided when ",
"'query' is 2-dimensional", call. = FALSE)
if (!queryColname %in% colnames(query))
stop("\"", queryColname, "\" is not a column of 'query'",
call. = FALSE)
}
targetColname <- sub("target_", "", targetColname)
if (length(dim(target)) == 2) {
if (length(targetColname) == 0)
stop("\"", targetColname,
"\" must be set when 'target' is 2-dimensional", call. = FALSE)
stop("'targetColname' must be provided when ",
"'target' is 2-dimensional", call. = FALSE)
if (!targetColname %in% colnames(target))
stop("\"", targetColname, "\" is not a column of 'target'",
call. = FALSE)
Expand Down Expand Up @@ -1357,7 +1359,7 @@ setMethod(
targetColname = character(),
score = rep(NA_real_, length(queryValue)),
isIndex = FALSE) {
if (!is.data.frame(score))
if (is.numeric(score))
score <- data.frame(score = score)
if (!is.data.frame(score))
stop("'score' needs to be either a 'data.frame' or a numeric",
Expand All @@ -1374,7 +1376,7 @@ setMethod(
stop("Provided indices in 'queryValue' are out-of-bounds.",
call. = FALSE)
if (any(!targetValue %in% seq_len(.nelements(target))))
stop("Provided indices in 'queryValue' are out-of-bounds.",
stop("Provided indices in 'targetValue' are out-of-bounds.",
call. = FALSE)
query_idx <- queryValue
target_idx <- targetValue
Expand Down Expand Up @@ -1432,7 +1434,7 @@ setMethod("addMatches", "Matched",
#'
#' @export
setMethod("endoapply", "ANY", function(X, FUN, ...) {
endoapply(X, FUN, ...)
S4Vectors::endoapply(X, FUN, ...)
})

#' @rdname Matched
Expand Down
17 changes: 9 additions & 8 deletions R/MatchedSpectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
#'
#' - `addProcessing`: add a processing step to both the *query* and *target*
#' `Spectra` in `object`. Additional parameters for `FUN` can be passed *via*
#' `...`. See `addProcessing` documentation in [Spectra()] for more
#' `...`. See `addProcessing` documentation in [Spectra::Spectra()] for more
#' information.
#'
#' - `plotSpectraMirror`: creates a mirror plot between the query and each
Expand All @@ -102,18 +102,19 @@
#' through `...`.
#'
#' - `setBackend`: allows to change the *backend* of both the query and target
#' [Spectra()] object. The function will return a `MatchedSpectra` object with
#' the query and target `Spectra` changed to the specified `backend`, which
#' can be any backend extending [MsBackend].
#' [Spectra::Spectra()] object. The function will return a `MatchedSpectra`
#' object with the query and target `Spectra` changed to the specified
#' `backend`, which can be any backend extending [Spectra::MsBackend].
#'
#' @param backend for `setBackend`: instance of an object extending [MsBackend].
#' See help for `setBackend` in [Spectra()] for more details.
#' @param backend for `setBackend`: instance of an object extending
#' [Spectra::MsBackend]. See help for [Spectra::setBackend()]
#' for more details.
#'
#' @param columns for `spectraData`: `character` vector with spectra variable
#' names that should be extracted.
#'
#' @param FUN for `addProcessing`: function to be applied to the peak matrix
#' of each spectrum in `object`. See [Spectra()] for more details.
#' of each spectrum in `object`. See [Spectra::Spectra()] for more details.
#'
#' @param main for `plotSpectraMirror`: an optional title for each plot.
#'
Expand All @@ -131,7 +132,7 @@
#'
#' @param spectraVariables for `addProcessing`: `character` with additional
#' spectra variables that should be passed along to the function defined
#' with `FUN`. See [Spectra()] for details.
#' with `FUN`. See [Spectra::Spectra()] for details.
#'
#' @param target `Spectra` with the spectra against which `query` has been
#' matched.
Expand Down
Loading

0 comments on commit fe6208d

Please sign in to comment.