R/read_spec_data.R

#' Read Data from XML Files Exported by Spectrophotometer
#'
#' @param file The name of the file to read
#' @param ... Additional arguments (not currently used)
#'
#' @return A data frame containing all data from the reads. Column
#' \code{Absorbance.Adj} will be the absorbance data from a culture minus the
#' absorbance of that same well in the control plate (of that drug). If no
#' control plate exists for that drug, then \code{NA} is given.
#' @export
#'
#' @examples
#' \dontrun{
#' dSpec <- read_spec_data(file = "myfile.xml")
#' }
read_spec_data <- function(file, ...) {
    dSpec <- softermax::read_softmax6_xml(file = file) %>%
        tibble::as.tibble(platesAsFactors = FALSE, wellsAsFactors = FALSE) %>%
        dplyr::select(Plate, Well, Absorbance = Value)

    # Get the data from control plates (if any)
    dControl <- dSpec %>%
        dplyr::filter(substring(tolower(Plate), 1, 7) == "control") %>%
        dplyr::mutate(
            Drug = toupper(substring(Plate, 9, 11))
        ) %>%
        dplyr::select(Drug, Well, Absorbance)
    controls <- split(dControl, dControl$Drug)

    # Get the data from Group plates
    dGroups <- dSpec %>%
        dplyr::filter(substring(tolower(Plate), 1, 1) == "s")

    plate_info <- extract_plate_info(unique(dGroups$Plate))

    # Merge in Section, Group, and Drug information
    dGroups <- dGroups %>%
        dplyr::left_join(plate_info, by = "Plate") %>%
        dplyr::select(Drug, Section, Group, Well, Absorbance)

    # Combine the Group data and the Control data
    dAll <- dGroups %>%
        split(.$Drug) %>%
        purrr::map_df(function(x) {
            drug <- unique(x$Drug)

            if (drug %in% names(controls)) {
                x %>%
                    dplyr::inner_join(
                        controls[[drug]],
                        by = c("Drug", "Well"),
                        suffix = c(".group", ".control")
                    ) %>%
                    dplyr::mutate(
                        Absorbance.Adj = Absorbance.group - Absorbance.control
                    ) %>%
                    dplyr::select(Drug, Section, Group, Well, Absorbance = Absorbance.group, Absorbance.Adj)
            } else {
                dplyr::mutate(x, Absorbance.Adj = NA)
            }

        }) %>%
        #dplyr::bind_rows() %>%
        dplyr::mutate(
            Drug = as.factor(Drug),
            Section = as.factor(Section),
            Well = as.factor(Well)
        )

    list(Groups = dAll, Controls = dControl)
}
KerrLab/introbiocure documentation built on May 17, 2019, 11:36 a.m.