Skip to content

Commit

Permalink
Merge branch 'gather-spectra-stats' into main
Browse files Browse the repository at this point in the history
include the fix to #45 for the new release
  • Loading branch information
cpauvert committed Sep 12, 2024
2 parents 86f8e07 + d0a17e3 commit d040200
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 0 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()
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ reference:
- title: "Miscellaneous"
contents:
- is_well_on_edge
- gather_spectra_stats
- get_spectra_names

news:
Expand Down
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
82 changes: 82 additions & 0 deletions dev/flat_utils.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,89 @@ test_that("remove_spectra fails with wrong input on raw spectra", {
})
```

# gather_spectra_stats

```{r function-gather_spectra_stats}
#' 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
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()
}
```

```{r example-gather_spectra_stats}
# 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)
```

```{r tests-gather_spectra_stats}
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()?"
)
})
```


```{r development-inflate, eval=FALSE}
# Run but keep eval=FALSE to avoid infinite loop
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 d040200

Please sign in to comment.