R/read_cellranger.R

Defines functions read_cellranger

Documented in read_cellranger

#' Read in counts data processed with Cell Ranger
#'
#' @param quant_dir Path to directory where output files are located.
#'
#' @return SingleCellExperiment of gene x cell counts matrix
#' @export
#'
#' @examples
#' \dontrun{
#'
#' # Import data from cellranger output directory quant_dir
#' read_cellranger(quant_dir)
#' }
#'
read_cellranger <- function(quant_dir) {
  cellranger_file <- file.path(quant_dir, "outs", "filtered_feature_bc_matrix.h5")
  if (!file.exists(cellranger_file)) {
    stop("Missing filtered_feature_bc_matrix.h5 file from cellranger output")
  }

  sce <- DropletUtils::read10xCounts(cellranger_file,
    sample.names = basename(quant_dir),
    col.names = TRUE
  )

  # for consistency with other quantifiers:
  # change the column names just the barcode value, which is the first part of the barcode name
  # drop colData
  colnames(sce) <- str_extract(colnames(sce), "^([ACGT]+)")
  SummarizedExperiment::colData(sce) <- NULL
  return(sce)
}
AlexsLemonade/scpcaTools documentation built on July 12, 2024, 8:34 a.m.