R/tidy_lab.R

Defines functions tidy_lab.nanodrop tidy_lab

Documented in tidy_lab tidy_lab.nanodrop

#' Tidy a lab object
#'
#' @param x An object to tidy
#' @param ... Arguments passed to their respective methods
#'
#' @export
tidy_lab <- function(x, ...) {
  UseMethod("tidy_lab")
}

#' Tidy a nanodrop object
#'
#' @param x a `nanodrop` object
#' @param force_tidy Logical. Should the tidying take place, even if the `is_tidy` attribute is `TRUE`?
#'
#' @return a `nanodrop` object
#'
#' @details This function:
#'
#' \itemize{
#'   \item renames columns to sensible substitutes
#'   \item converts date-times to ISO 8601-esque date-time format (YYYY-MM-DD HH:MM:SS vs YYYY-MM-DDTHH:MM:SSZ)
#' }
#'
#' It's recommended that you do any manipulations to these data after you tidy,
#' rather than before, as `tidy_nanodrop` expects the output to be fairly similar
#' to the output from `read_nanodrop`.
#'
#' A tidy `nanodrop` object will (usually) contain the following columns:
#'
#' \describe{
#'   \item{date}{The date-time of sample reading, as YYYY-MM-DD HH:MM:SS}
#'   \item{sample_name}{The name of the sample provided by the NanoDrop itself}
#'   \item{conc}{The concentration of nucleic acid in ng/uL}
#'   \item{a260_280}{The absorbance at 260nm / absorbance at 280nm. Typically a marker of protein contamination. Pure nucleic acid is typically around 2.}
#'   \item{a260_230}{The absorbance at 260nm / absorbance at 230nm. Typically a marker of guanidine salt contamination. Pure nucleic acid is typically around 2.}
#'   \item{a260}{The absorbance at 260nm, the wavelength nucleic acids absorb most strongly.}
#'   \item{a280}{The absorbance at 280nm, the wavelength proteins absorb most strongly.}
#'   \item{tube_name}{The label given the physical tube that contains sample}
#'   \item{cell_line}{The cell line from which the sample came, if applicable}
#'   \item{experimental_condition}{A brief description of the conditions these samples were subject to for the experiment}
#' }
#'
#' `tube_name`, `cell_line`, and `experimental_condition` are always unfilled,
#' because these data are not within nanodrop data. However, they are provided
#' because they are included in the masterlist data, and it's wise to fill them
#' out for your records.
#'
#' The remaining columns are typically unused.
#' @importFrom rlang .data
#' @export
#' @rdname tidy_lab
tidy_lab.nanodrop <- function(x, force_tidy = FALSE, ...) {

  if (x$is_tidy && !force_tidy) {
    message("nanodrop already looks tidy, skipping. To tidy anyway, set force_tidy = TRUE.")
    return(x)
  }

  data <- x$data |>
    dplyr::rename_with(dplyr::recode,
                       Nucleic.Acid.ng.uL. = "conc",
                       A260.A280 = "a260_280",
                       A260.A230 = "a260_230"
    ) |>
    dplyr::rename_with(snakecase::to_snake_case) |>
    dplyr::rename_with(stringr::str_replace, .cols = dplyr::everything(), "_u_l", "ul") |>
    dplyr::rename_with(stringr::str_replace, .cols = dplyr::everything(), "a_260", "a260") |>
    dplyr::rename_with(stringr::str_replace, .cols = dplyr::everything(), "a_280", "a280") |>
    dplyr::mutate(date = lubridate::mdy_hms(.data$date) |> as.character(),
                  tube_name = "",
                  cell_line = "",
                  experimental_condition = "")

  new_nanodrop(data = data, raw_data = x$data, nucleotide = x$nucleotide, is_tidy = TRUE, date = x$date)
}
KaiAragaki/ragaki documentation built on Dec. 25, 2021, 2:24 a.m.