Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spaceranger 2.1 update #7141

Merged
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -282,6 +282,7 @@ export(Radius)
export(Read10X)
export(Read10X_Image)
export(Read10X_h5)
export(Read10x_probe_metadata)
export(ReadAkoya)
export(ReadMtx)
export(ReadNanostring)
Expand Down
130 changes: 105 additions & 25 deletions R/preprocessing.R
Expand Up @@ -368,14 +368,14 @@ HTODemux <- function(
#' pbmc_small <- GetResidual(object = pbmc_small, features = c('MS4A1', 'TCL1A'))
#'
GetResidual <- function(
object,
features,
assay = NULL,
umi.assay = NULL,
clip.range = NULL,
replace.value = FALSE,
na.rm = TRUE,
verbose = TRUE
object,
features,
assay = NULL,
umi.assay = NULL,
clip.range = NULL,
replace.value = FALSE,
na.rm = TRUE,
verbose = TRUE
) {
assay <- assay %||% DefaultAssay(object = object)
if (IsSCT(assay = object[[assay]])) {
Expand Down Expand Up @@ -500,39 +500,119 @@ GetResidual <- function(
#' }
#'
Load10X_Spatial <- function(
data.dir,
filename = 'filtered_feature_bc_matrix.h5',
assay = 'Spatial',
slice = 'slice1',
filter.matrix = TRUE,
to.upper = FALSE,
image = NULL,
...
data.dir,
filename = 'filtered_feature_bc_matrix.h5',
assay = 'Spatial',
slice = 'slice1',
filter.matrix = TRUE,
to.upper = FALSE,
image = NULL,
...
) {
if (length(x = data.dir) > 1) {
warning("'Load10X_Spatial' accepts only one 'data.dir'", immediate. = TRUE)
warning("'Load10X_Spatial' accepts only one 'data.dir'",
immediate. = TRUE)
data.dir <- data.dir[1]
}
data <- Read10X_h5(filename = file.path(data.dir, filename), ...)
data <- Read10X_h5(filename = file.path(data.dir, filename),
...)

if (to.upper) {
rownames(x = data) <- toupper(x = rownames(x = data))
data <- imap(data, ~{
rownames(.x) <- toupper(rownames(.x))
.x
})
}
if (is.list(data) & "Antibody Capture" %in% names(data)) {
stephenwilliams22 marked this conversation as resolved.
Show resolved Hide resolved
matrix_gex <- data$`Gene Expression`
matrix_protein <- data$`Antibody Capture`
object <- CreateSeuratObject(counts = matrix_gex, assay = assay)
object_protein <- CreateAssayObject(counts = matrix_protein)
object[["Protein"]] <- object_protein
}
else {
object <- CreateSeuratObject(counts = data, assay = assay)
}
object <- CreateSeuratObject(counts = data, assay = assay)
if (is.null(x = image)) {
image <- Read10X_Image(
image.dir = file.path(data.dir, 'spatial'),
filter.matrix = filter.matrix
)
} else {
image <- Read10X_Image(image.dir = file.path(data.dir,"spatial"),
filter.matrix = filter.matrix)
}
else {
if (!inherits(x = image, what = "VisiumV1"))
stop("Image must be an object of class 'VisiumV1'.")
}
image <- image[Cells(x = object)]
DefaultAssay(object = image) <- assay
object[[slice]] <- image

# if using the meta-data available for probes add to @misc slot
file_path <- file.path(data.dir, filename)
infile <- hdf5r::H5File$new(filename = file_path, mode = 'r')
if("matrix/features/probe_region" %in% hdf5r::list.objects(infile)) {
probe_metadata <- Read10x_probe_metadata(data.dir, filename)
Misc(object = object[['Spatial']], slot = "probe_metadata") <- probe_metadata
}
return(object)
}


#' Read10x Probe Metadata
#'
#' This function reads the probe metadata from a 10x Genomics probe barcode matrix file in HDF5 format.
#'
#' @param data.dir The directory where the file is located.
#' @param filename The name of the file containing the raw probe barcode matrix in HDF5 format. Currently the only file that contains meta-data is 'raw_probe_bc_matrix.h5'.
#'
#' @return Returns a data.frame containing the probe metadata.
#'
#' @export
Read10x_probe_metadata <- function(data.dir, filename) {

if (!requireNamespace('hdf5r', quietly = TRUE)) {
stop("Please install hdf5r to read HDF5 files")
}
file_path = paste0(data.dir,"/", filename)
if (!file.exists(file_path)) {
stop("File not found")
}
infile <- hdf5r::H5File$new(filename = file_path, mode = 'r')
if("matrix/features/probe_region" %in% hdf5r::list.objects(infile)) {
probe_name <- infile[['matrix/features/name']][]
probe_region<- infile[['matrix/features/probe_region']][]
meta_data <- data.frame(probe_name, probe_region)
return(meta_data)
}
}

#' Read10x Probe Metadata
#'
#' This function reads the probe metadata from a 10x Genomics probe barcode matrix file in HDF5 format.
#'
#' @param data.dir The directory where the file is located.
#' @param filename The name of the file containing the raw probe barcode matrix in HDF5 format. The default filename is 'raw_probe_bc_matrix.h5'.
#'
#' @return Returns a data.frame containing the probe metadata.
#'
#' @export
Read10x_probe_metadata <- function(data.dir,
filename = 'raw_probe_bc_matrix.h5') {

if (!requireNamespace('hdf5r', quietly = TRUE)) {
stop("Please install hdf5r to read HDF5 files")
}
file_path = paste0(data.dir,"/", filename)
if (!file.exists(file_path)) {
stop("File not found")
}
infile <- hdf5r::H5File$new(filename = file_path, mode = 'r')
if("matrix/features/probe_region" %in% hdf5r::list.objects(infile)) {
probe_name <- infile[['matrix/features/name']][]
probe_region<- infile[['matrix/features/probe_region']][]
meta_data <- data.frame(probe_name, probe_region)
return(meta_data)
}
}

#' Load STARmap data
#'
#' @param data.dir location of data directory that contains the counts matrix,
Expand Down
25 changes: 25 additions & 0 deletions man/Read10x_probe_metadata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/Seurat-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions man/SpatialPlot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added tests/testdata/visium/raw_probe_bc_matrix.h5
Binary file not shown.