R/check_spectra.R

Defines functions check_spectra

Documented in check_spectra

# WARNING - Generated by {fusen} from dev/import-data.Rmd: do not edit by hand

#' Evaluate the spectra regularities
#'
#' Assess whether all the spectra in the list are not empty, of the same length and correspond to profile data.
#'
#' @param spectra_list A list of [MALDIquant::MassSpectrum] objects
#' @param tolerance A numeric indicating the accepted tolerance to the spectra length.
#'  The default value is the machine numerical precision and is close to 1.5e-8.
#'
#' @return A list of logical vectors of length `spectra_list` indicating if the spectra are empty (`is_empty`), of an odd length (`is_outlier_length`) or not a profile spectra (`is_not_regular`).
#'
#' @seealso [process_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
#' check_spectra(spectra_list)
#' # The overall sanity can be checked with Reduce
#' Reduce(any, check_spectra(spectra_list)) # Should be FALSE
check_spectra <- function(spectra_list, tolerance = sqrt(.Machine$double.eps)) {
  # Checking spectra are not empty
  empty_spectra <- vapply(spectra_list, MALDIquant::isEmpty, FUN.VALUE = logical(1))
  # Checking spectra are the same length
  ## Getting the most common length
  common_length <- lengths(spectra_list) %>%
    # gives length as character/names and occurrence as value
    table() %>%
    which.max() %>%
    # extract length
    names() %>%
    strtoi()
  length_spectra <- vapply(lengths(spectra_list),
    function(x) {
      isTRUE(
        all.equal.numeric(
          target = common_length, x,
          tolerance = tolerance, scale = 1
        )
      )
    },
    FUN.VALUE = logical(1)
  )
  # Checking spectra are profile data
  regular_spectra <- vapply(spectra_list,
    MALDIquant::isRegular,
    FUN.VALUE = logical(1)
  )
  # Summarise the checks
  checking_list <- list(
    is_empty = empty_spectra,
    is_outlier_length = !length_spectra,
    is_not_regular = !regular_spectra
  )
  if (any(Reduce(any, checking_list))) {
    message(
      "Some spectra are incorrect (empty, outlier length or irregular).\n",
      "They can be removed using `remove_spectra()`"
    )
  }
  return(checking_list)
}

Try the maldipickr package in your browser

Any scripts or data that you put into this service are public.

maldipickr documentation built on Sept. 13, 2024, 1:12 a.m.