Skip to content

Commit

Permalink
Merge pull request #52 from aifimmunology/adt-matrix
Browse files Browse the repository at this point in the history
Adds automatic ADT data detection for both 10x and AIFI injected ADT matrices to `read_h5_seurat()` and `read_h5_sce()`.

To avoid confusion with previous versions that don't do this automatically, version is updated from 1.2.0 to 1.3.0.
  • Loading branch information
hypercompetent authored Jun 28, 2023
2 parents b554125 + d23329c commit 4b11c66
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 13 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: H5weaver
Type: Package
Title: Read, Rearrange, and Write HDF5 files
Version: 1.2.0
Version: 1.3.0
Author: Lucas Graybuck <[email protected]>
Maintainer: Lucas Graybuck <[email protected]>
Description: This package provides functions for reading, writing, combining, and rearranging HDF5 files with conventions from 10x Genomics.
Expand All @@ -21,5 +21,7 @@ Suggests:
testthat (>= 2.1.0),
Seurat (>= 3.1.0),
SingleCellExperiment (>= 1.8.0),
SummarizedExperiment (>= 1.16.1)
SummarizedExperiment (>= 1.16.1),
assertthat,
R.utils
RoxygenNote: 7.1.1
59 changes: 48 additions & 11 deletions R/read_rna_h5.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ read_h5_feature_meta <- function(h5_file,
#'
#' @return a Seurat Class object
#' @export
#' @examples
#' h5_fpath <- system.file('testdata/cite_sample1.h5', package = "H5weaver")
#' so <- read_h5_seurat(h5_fpath)
#' # so
read_h5_seurat <- function(h5_file,
target = "matrix",
feature_names = "name",
Expand All @@ -228,32 +232,48 @@ read_h5_seurat <- function(h5_file,
rownames(cell_meta) <- cell_meta$barcodes

rownames(cell_meta) <- cell_meta$barcodes

feat_meta <- read_h5_feature_meta(h5_file,
target = target)
rownames(feat_meta) <- make.unique(feat_meta[[feature_names]])

cite <- FALSE
cite_10x <- FALSE
cite_injected <- FALSE

# Check for CITE-seq data
# Check for cellranger CITE-seq data
if("feature_type" %in% names(feat_meta)) {
if("Antibody Capture" %in% feat_meta$feature_type) {
cite <- TRUE
cite_10x <- TRUE
}
}

if(cite) {
if(cite_10x) {
cite_feat <- feat_meta[feat_meta$feature_type == "Antibody Capture",]
feat_meta <- feat_meta[feat_meta$feature_type != "Antibody Capture",]

cite_mat <- mat[cite_feat$id,]
mat <- mat[feat_meta$id,]
}

# Check for injected CITE-seq data
if("ADT" %in% h5ls(h5_file)$name) {
cite_injected <- TRUE
}

if(cite_injected) {
cite_mat <- read_h5_dgCMatrix(h5_file, "ADT", feature_names = "id")
colnames(cite_mat) <- cell_meta$barcodes
cite_feat <- read_h5_feature_meta(h5_file, target = "ADT")
rownames(cite_feat) <- make.unique(cite_feat[["id"]])
}

so <- Seurat::CreateSeuratObject(counts = mat,
meta.data = cell_meta,
...)
if(cite) {
so[["RNA"]] <- Seurat::AddMetaData( so[["RNA"]], feat_meta)

if(cite_10x|cite_injected) {
so[["ADT"]] <- Seurat::CreateAssayObject(counts = cite_mat)
so[["ADT"]] <- Seurat::AddMetaData( so[["ADT"]], cite_feat)
}

so
Expand All @@ -272,6 +292,10 @@ read_h5_seurat <- function(h5_file,
#'
#' @return a SingleCellExperiment Class object
#' @export
#' @examples
#' h5_fpath <- system.file('testdata/cite_sample1.h5', package = "H5weaver")
#' test_sce <- read_h5_sce(h5_fpath)
#' # test_sce
read_h5_sce <- function(h5_file,
target = "matrix",
feature_names = "name",
Expand All @@ -294,22 +318,34 @@ read_h5_sce <- function(h5_file,
feat_meta <- read_h5_feature_meta(h5_file,
target = target)

cite <- FALSE
cite_10x <- FALSE
cite_injected <- FALSE

# Check for CITE-seq data
if("feature_type" %in% names(feat_meta)) {
if("Antibody Capture" %in% feat_meta$feature_type) {
cite <- TRUE
cite_10x <- TRUE
}
}

if(cite) {
if(cite_10x) {
cite_feat <- feat_meta[feat_meta$feature_type == "Antibody Capture",]
feat_meta <- feat_meta[feat_meta$feature_type != "Antibody Capture",]

cite_mat <- mat[cite_feat$id,]
mat <- mat[feat_meta$id,]
}

# Check for injected CITE-seq data
if("ADT" %in% h5ls(h5_file)$name) {
cite_injected <- TRUE
}

if(cite_injected) {
cite_mat <- read_h5_dgCMatrix(h5_file, 'ADT', feature_names = 'id')
cite_feat <- read_h5_feature_meta(h5_file, 'ADT')
colnames(cite_mat) <- cell_meta$barcodes
}

sce <- SingleCellExperiment::SingleCellExperiment(
assays = list(counts = mat),
Expand All @@ -318,9 +354,10 @@ read_h5_sce <- function(h5_file,
...
)

if(cite) {
if(cite_10x|cite_injected) {
cite_se <- SummarizedExperiment::SummarizedExperiment(
assays = list(counts = cite_mat)
assays = list(counts = cite_mat),
rowData = cite_feat
)
SingleCellExperiment::altExp(sce, "ADT") <- cite_se
}
Expand Down
Binary file added inst/testdata/cite_sample1.h5
Binary file not shown.
32 changes: 32 additions & 0 deletions inst/testdata/generate_test_data_cite.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
library(rhdf5)
library(H5weaver)
library(hise)

chrM_genes <- fread(system.file("reference/GRCh38_10x_chrM_gene_metadata.csv.gz",
package = "H5weaver"))
h5_fid <- '1be65739-445a-41fe-89a4-52fbbba2000d'
hise::cacheFiles(list(h5_fid))
sample_cite_h5 <- list.files("cache/1be65739-445a-41fe-89a4-52fbbba2000d", full.names = T)

## Well 1 .h5 file
sample_list <- h5dump(sample_cite_h5)
sample_list <- h5_list_convert_to_dgCMatrix(sample_list)

set.seed(3)
keep_genes <- c(sample(sample_list$matrix$features$name, 1000), chrM_genes$name)
sample_keep <- match(keep_genes, sample_list$matrix$features$name)

sample_list$matrix_dgCMatrix <- sample_list$matrix_dgCMatrix[sample_keep,]

sample_list$matrix$features$name <- sample_list$matrix$features$name[sample_keep]
sample_list$matrix$features$genome <- sample_list$matrix$features$genome[sample_keep]
sample_list$matrix$features$feature_type <- sample_list$matrix$features$feature_type[sample_keep]
sample_list$matrix$features$interval <- sample_list$matrix$features$interval[sample_keep]

sample_list <- h5_list_convert_from_dgCMatrix(sample_list)

write_h5_list(sample_list,
"cite_sample1.h5",
overwrite = TRUE)
h5closeAll()

0 comments on commit 4b11c66

Please sign in to comment.