Skip to content

Commit

Permalink
inflate the now tested gather_spectra_stats function
Browse files Browse the repository at this point in the history
this fixes #45
  • Loading branch information
cpauvert committed May 3, 2024
1 parent e5dc66f commit 0017bd1
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 8 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export("%>%")
export(check_spectra)
export(delineate_with_identification)
export(delineate_with_similarity)
export(gather_spectra_stats)
export(get_spectra_names)
export(import_biotyper_spectra)
export(import_spede_clusters)
Expand Down
54 changes: 54 additions & 0 deletions R/gather_spectra_stats.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# WARNING - Generated by {fusen} from dev/flat_utils.Rmd: do not edit by hand

#' Aggregate spectra quality-check statistics
#'
#'
#' @param check_vectors A list of logical vectors from [check_spectra]
#'
#' @return A tibble of one row with the following 5 columns of integers:
#' * `n_spectra`: total number of raw spectra.
#' * `n_valid_spectra`: total number of spectra passing all quality checks
#' * `is_empty`, `is_outlier_length` and `is_not_regular`: total of spectra flagged with these irregularities.
#'
#' @seealso [check_spectra]
#' @export
#' @examples
#' # Get an example directory of six Bruker MALDI Biotyper spectra
#' directory_biotyper_spectra <- system.file(
#' "toy-species-spectra",
#' package = "maldipickr"
#' )
#' # Import the six spectra
#' spectra_list <- import_biotyper_spectra(directory_biotyper_spectra)
#' # Display the list of checks, with FALSE where no anomaly is detected
#' checks <- check_spectra(spectra_list)
#' # Aggregate the statistics of quality-checked spectra
#' gather_spectra_stats(checks)
gather_spectra_stats <- function(check_vectors) {
if (typeof(check_vectors) != "list" ||
is.null(names(check_vectors))) {
stop(
"check_vectors is not a named list. See maldipickr::check_spectra() help page for a correct format."
)
}
equal_length <- unique(lengths(check_vectors))
if (length(equal_length) != 1 ||
any(names(check_vectors) != c("is_empty", "is_outlier_length", "is_not_regular"))
) {
stop(
"Unexpected format for checks_vectors. Are you sure this is the output of maldipickr::check_spectra()?"
)
}

# check_vectors from maldipickr::check_spectra
# src: https://stackoverflow.com/a/51140480/21085566
aggregated_checks <- Reduce(`|`, check_vectors)
check_stats <- vapply(check_vectors, sum, FUN.VALUE = integer(1)) %>%
tibble::as_tibble_row()
tibble::tibble(
"n_spectra" = length(aggregated_checks),
"n_valid_spectra" = .data$n_spectra - sum(aggregated_checks)
) %>%
dplyr::bind_cols(check_stats) %>%
return()
}
2 changes: 2 additions & 0 deletions dev/config_fusen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ keep:
path: keep
state: active
R:
- R/gather_spectra_stats.R
- R/remove_spectra_logical.R
- R/remove_spectra.R
- R/maldipickr-package.R
- R/utils-pipe.R
tests:
- tests/testthat/test-gather_spectra_stats.R
- tests/testthat/test-remove_spectra_logical.R
- tests/testthat/test-remove_spectra.R
vignettes: []
Expand Down
31 changes: 23 additions & 8 deletions dev/flat_utils.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -640,17 +640,22 @@ test_that("remove_spectra fails with wrong input on raw spectra", {
#'
#' @seealso [check_spectra]
#' @export
gather_spectra_stats <- function(check_vectors){
equal_length<-unique(lengths(check_vectors))
if(typeof(check_vectors) != "list" |
length(equal_length) != 1 |
names(check_vectors) != c("is_empty", "is_outlier_length", "is_not_regular")
){
gather_spectra_stats <- function(check_vectors) {
if (typeof(check_vectors) != "list" ||
is.null(names(check_vectors))) {
stop(
"Unexpected format for checks_vectors. Are you sure this is the output of maldicpickr::check_spectra()?"
"check_vectors is not a named list. See maldipickr::check_spectra() help page for a correct format."
)
}
equal_length <- unique(lengths(check_vectors))
if (length(equal_length) != 1 ||
any(names(check_vectors) != c("is_empty", "is_outlier_length", "is_not_regular"))
) {
stop(
"Unexpected format for checks_vectors. Are you sure this is the output of maldipickr::check_spectra()?"
)
}
# check_vectors from maldipickr::check_spectra
# src: https://stackoverflow.com/a/51140480/21085566
aggregated_checks <- Reduce(`|`, check_vectors)
Expand Down Expand Up @@ -691,6 +696,16 @@ test_that("gather_spectra_stats works", {
"tbl", "data.frame"
), row.names = c(NA, -1L)))
})
test_that("gather_spectra_stats fails",{
expect_error(
gather_spectra_stats("spectra"),
"check_vectors is not a named list"
)
expect_error(
gather_spectra_stats(list("foo" = c(TRUE,TRUE),"bar" = c(TRUE))),
"Unexpected format for checks_vectors. Are you sure this is the output of maldipickr::check_spectra()?"
)
})
```


Expand Down
38 changes: 38 additions & 0 deletions man/gather_spectra_stats.Rd

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

23 changes: 23 additions & 0 deletions tests/testthat/test-gather_spectra_stats.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# WARNING - Generated by {fusen} from dev/flat_utils.Rmd: do not edit by hand

test_that("gather_spectra_stats works", {
expect_equal(check_spectra(
c(MALDIquant::createMassSpectrum(1:3, 1:3), MALDIquant::createMassSpectrum(11:13, 11:13))
) |> gather_spectra_stats(), structure(list(
n_spectra = 2L, n_valid_spectra = 2L, is_empty = 0L,
is_outlier_length = 0L, is_not_regular = 0L
), class = c(
"tbl_df",
"tbl", "data.frame"
), row.names = c(NA, -1L)))
})
test_that("gather_spectra_stats fails",{
expect_error(
gather_spectra_stats("spectra"),
"check_vectors is not a named list"
)
expect_error(
gather_spectra_stats(list("foo" = c(TRUE,TRUE),"bar" = c(TRUE))),
"Unexpected format for checks_vectors. Are you sure this is the output of maldipickr::check_spectra()?"
)
})

0 comments on commit 0017bd1

Please sign in to comment.