R/IO_Luminex_rcsv.R

#' Read Bead-level data generated by Luminex Xponent
#'
#' \code{read_Xponent_rcsv} reads all the files in a \emph{"_rcsv"} directory generated by Luminex Xponent.
#'   To learn how to generate this directory read \href{https://www.luminexcorp.com/blog/2012/06/19/its-all-about-the-stats/}{here}.
#'
#' @param path Either the path to the directory of the plate or a zipfile. See details for specifications.
#'
#' @return A data frame with all the csv's merged and and extract column \code{Location}
#'   indicatin the well of the measurement
#'
#' @details The directory name must follow the Xponent's naming conventions, ie \code{<plate_name>_rcsv}
#'   or if it's a zip file it should be \code{<plate_name>_rcsv.zip}. The zip file should export the
#'   original directory when unziped (ie you should zip the folder not the files).
#'   The files inside the directory must also be named as \code{<plate_name>_P1_<well_location>.csv}.
#'
#' @export
#'
#' @examples
read_Xponent_rcsv <- function(path) {
    if (!dir.exists(path)) {
        unzip(path, exdir = tempdir())
        path <- file.path(tempdir(), sub(".zip", "", basename(path)))
    }
    plate_name <- sub("_rcsv", "", basename(path))
    wells <- sub(paste0(plate_name, "_P1_"), "", dir(path))
    wells <- sub(".csv", "", wells)
    read_well <- function(w) {
        filename <- file.path(path, paste0(plate_name, "_P1_", w, ".csv"))
        df <- read.csv(filename, stringsAsFactors = FALSE, skip = 1)
        df$Location <- w
        df[c("Location", setdiff(colnames(df), "Location"))]
    }
    dplyr::bind_rows(lapply(wells, read_well))
}
biolabntua/moach documentation built on May 12, 2019, 9:24 p.m.