diff --git a/NAMESPACE b/NAMESPACE index 4984dc2be..f2a3568cd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -282,6 +282,7 @@ export(Radius) export(Read10X) export(Read10X_Image) export(Read10X_h5) +export(Read10x_probe_metadata) export(ReadAkoya) export(ReadMtx) export(ReadNanostring) diff --git a/R/preprocessing.R b/R/preprocessing.R index 45baa4254..e819d73f8 100644 --- a/R/preprocessing.R +++ b/R/preprocessing.R @@ -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]])) { @@ -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)) { + 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, diff --git a/man/Read10x_probe_metadata.Rd b/man/Read10x_probe_metadata.Rd new file mode 100644 index 000000000..d3dd53e61 --- /dev/null +++ b/man/Read10x_probe_metadata.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/preprocessing.R +\name{Read10x_probe_metadata} +\alias{Read10x_probe_metadata} +\title{Read10x Probe Metadata} +\usage{ +Read10x_probe_metadata(data.dir, filename = "raw_probe_bc_matrix.h5") + +Read10x_probe_metadata(data.dir, filename = "raw_probe_bc_matrix.h5") +} +\arguments{ +\item{data.dir}{The directory where the file is located.} + +\item{filename}{The name of the file containing the raw probe barcode matrix in HDF5 format. The default filename is 'raw_probe_bc_matrix.h5'.} +} +\value{ +Returns a data.frame containing the probe metadata. + +Returns a data.frame containing the probe metadata. +} +\description{ +This function reads the probe metadata from a 10x Genomics probe barcode matrix file in HDF5 format. + +This function reads the probe metadata from a 10x Genomics probe barcode matrix file in HDF5 format. +} diff --git a/man/Seurat-package.Rd b/man/Seurat-package.Rd index 351af75c9..69a18bf33 100644 --- a/man/Seurat-package.Rd +++ b/man/Seurat-package.Rd @@ -53,7 +53,9 @@ Other contributors: \item Jeff Farrell \email{jfarrell@g.harvard.edu} [contributor] \item Christoph Hafemeister \email{chafemeister@nygenome.org} (\href{https://orcid.org/0000-0001-6365-8254}{ORCID}) [contributor] \item Yuhan Hao \email{yhao@nygenome.org} (\href{https://orcid.org/0000-0002-1810-0822}{ORCID}) [contributor] + \item Austin Hartman \email{ahartman@nygenome.org} (\href{https://orcid.org/0000-0001-7278-1852}{ORCID}) [contributor] \item Jaison Jain \email{jjain@nygenome.org} (\href{https://orcid.org/0000-0002-9478-5018}{ORCID}) [contributor] + \item Madeline Kowalski \email{mkowalski@nygenome.org} (\href{https://orcid.org/0000-0002-5655-7620}{ORCID}) [contributor] \item Efthymia Papalexi \email{epapalexi@nygenome.org} (\href{https://orcid.org/0000-0001-5898-694X}{ORCID}) [contributor] \item Patrick Roelli \email{proelli@nygenome.org} [contributor] \item Rahul Satija \email{rsatija@nygenome.org} (\href{https://orcid.org/0000-0001-9448-8833}{ORCID}) [contributor] diff --git a/man/SpatialPlot.Rd b/man/SpatialPlot.Rd index 133501e2a..5bd9f8162 100644 --- a/man/SpatialPlot.Rd +++ b/man/SpatialPlot.Rd @@ -15,6 +15,7 @@ SpatialPlot( image.alpha = 1, crop = TRUE, slot = "data", + keep.scale = "feature", min.cutoff = NA, max.cutoff = NA, cells.highlight = NULL, @@ -67,6 +68,7 @@ SpatialFeaturePlot( images = NULL, crop = TRUE, slot = "data", + keep.scale = "feature", min.cutoff = NA, max.cutoff = NA, ncol = NULL, @@ -103,6 +105,13 @@ entire background image.} \item{slot}{If plotting a feature, which data slot to pull from (counts, data, or scale.data)} +\item{keep.scale}{How to handle the color scale across multiple plots. Options are: +\itemize{ + \item{"feature" (default; by row/feature scaling):}{ The plots for each individual feature are scaled to the maximum expression of the feature across the conditions provided to 'split.by'.} + \item{"all" (universal scaling):}{ The plots for all features and conditions are scaled to the maximum expression value for the feature with the highest overall expression.} + \item{NULL (no scaling):}{ Each individual plot is scaled to the maximum expression value of the feature in the condition provided to 'split.by'. Be aware setting NULL will result in color scales that are not comparable between plots.} +}} + \item{min.cutoff, max.cutoff}{Vector of minimum and maximum cutoff values for each feature, may specify quantile in the form of 'q##' where '##' is the quantile (eg, 'q1', 'q10')} diff --git a/tests/testdata/visium/raw_probe_bc_matrix.h5 b/tests/testdata/visium/raw_probe_bc_matrix.h5 new file mode 100644 index 000000000..7b424ed6e Binary files /dev/null and b/tests/testdata/visium/raw_probe_bc_matrix.h5 differ