Skip to content

Commit 8091b86

Browse files
committed
Run devtools::document() and make read10xVisiumWrapper internally depend on spe_add_info()
1 parent 3512bc4 commit 8091b86

File tree

4 files changed

+76
-77
lines changed

4 files changed

+76
-77
lines changed

R/read10xVisiumWrapper.R

Lines changed: 15 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#' @param gtf_cols A `character()` specifying which columns to keep from the GTF
2020
#' file. `"gene_name"` and `"gene_id"` have to be included in `gtf_cols`.
2121
#' @param verbose A `logical(1)` specifying whether to show progress updates.
22+
#' @param add_analysis A `logical(1)` specifying whether to read and add
23+
#' SpaceRanger analysis results.
2224
#'
2325
#' @return A [SpatialExperiment][SpatialExperiment-class] object with the
2426
#' clustering and dimension reduction (projection) results from SpaceRanger by
@@ -53,34 +55,11 @@ read10xVisiumWrapper <- function(samples = "",
5355
reference_gtf = NULL,
5456
chrM = "chrM",
5557
gtf_cols = c("source", "type", "gene_id", "gene_version", "gene_name", "gene_type"),
56-
verbose = TRUE) {
57-
stopifnot(all(c("gene_name", "gene_id") %in% gtf_cols))
58-
59-
if (missing(reference_gtf)) {
60-
summary_file <- file.path(samples[1], "web_summary.html")
61-
web <- readLines(summary_file)
62-
63-
# For spaceranger versions before 3.0
64-
reference_path <- gsub('.*"', "", regmatches(web, regexpr('\\["Reference Path", *"[/|A-z|0-9|-]+', web)))
65-
66-
# For recent spaceranger versions (3.0.0+?)
67-
if (length(reference_path) == 0) {
68-
reference_path <- sub(
69-
".*--transcriptome=(\\S*).*",
70-
"\\1",
71-
web[grep("--transcriptome=", web)]
72-
)
73-
}
74-
reference_gtf <- list.files(
75-
file.path(reference_path, "genes"), "^genes\\.gtf(\\.gz)?$",
76-
full.names = TRUE
77-
)
78-
}
79-
reference_gtf <- reference_gtf[file.exists(reference_gtf)]
80-
if (length(reference_gtf) > 1) {
81-
stop("More than one 'reference_gtf' was provided or detected. Manually specify the path to just one 'reference_gtf'. If different GTF files were used, then different genes will have been quantified and thus cannot be merged naively into a single SpatialExperiment object. If that's the case, we recommend you build separate SPE objects based on the different 'reference_gtf' files used.", call. = FALSE)
82-
} else if (length(reference_gtf) == 0) {
83-
stop("No 'reference_gtf' files were detected. Please check that the files are available.", call. = FALSE)
58+
verbose = TRUE,
59+
add_analysis = TRUE
60+
) {
61+
if (!all(c("gene_name", "gene_id") %in% gtf_cols)) {
62+
stop("'gtf_cols' must include 'gene_name' and 'gene_id'", call. = FALSE)
8463
}
8564

8665
if (verbose) message(Sys.time(), " SpatialExperiment::read10xVisium: reading basic data from SpaceRanger")
@@ -93,56 +72,17 @@ read10xVisiumWrapper <- function(samples = "",
9372
load = load
9473
)
9574

96-
if (verbose) message(Sys.time(), " read10xVisiumAnalysis: reading analysis output from SpaceRanger")
97-
visium_analysis <- read10xVisiumAnalysis(
75+
spe <- spe_add_info(
76+
spe = spe,
9877
samples = samples,
99-
sample_id = sample_id
78+
sample_id = sample_id,
79+
reference_gtf = reference_gtf,
80+
chrM = chrM,
81+
gtf_cols = gtf_cols,
82+
add_analysis = add_analysis,
83+
verbose = verbose
10084
)
10185

102-
if (verbose) message(Sys.time(), " add10xVisiumAnalysis: adding analysis output from SpaceRanger")
103-
spe <- add10xVisiumAnalysis(spe, visium_analysis)
104-
105-
## Read in the gene information from the annotation GTF file
106-
if (verbose) message(Sys.time(), " rtracklayer::import: reading the reference GTF file")
107-
gtf <- rtracklayer::import(reference_gtf)
108-
gtf <- gtf[gtf$type == "gene"]
109-
names(gtf) <- gtf$gene_id
110-
111-
## Match the genes
112-
if (verbose) message(Sys.time(), " adding gene information to the SPE object")
113-
match_genes <- match(rownames(spe), gtf$gene_id)
114-
115-
if (all(is.na(match_genes))) {
116-
## Protect against scenario where one set has GENCODE IDs and the other one has ENSEMBL IDs.
117-
warning("Gene IDs did not match. This typically happens when you are not using the same GTF file as the one that was used by SpaceRanger. For example, one file uses GENCODE IDs and the other one ENSEMBL IDs. read10xVisiumWrapper() will try to convert them to ENSEMBL IDs.", call. = FALSE)
118-
match_genes <- match(gsub("\\..*", "", rownames(spe)), gsub("\\..*", "", gtf$gene_id))
119-
}
120-
121-
if (any(is.na(match_genes))) {
122-
warning("Dropping ", sum(is.na(match_genes)), " out of ", length(match_genes), " genes for which we don't have information on the reference GTF file. This typically happens when you are not using the same GTF file as the one that was used by SpaceRanger.", call. = FALSE)
123-
## Drop the few genes for which we don't have information
124-
spe <- spe[!is.na(match_genes), ]
125-
match_genes <- match_genes[!is.na(match_genes)]
126-
}
127-
128-
## Keep only some columns from the gtf
129-
mcols(gtf) <- mcols(gtf)[, gtf_cols[gtf_cols %in% colnames(mcols(gtf))]]
130-
131-
## Add the gene info to our SPE object
132-
rowRanges(spe) <- gtf[match_genes]
133-
134-
## Add information used by spatialLIBD
135-
if (verbose) message(Sys.time(), " adding information used by spatialLIBD")
136-
spe <- add_key(spe)
137-
spe$sum_umi <- colSums(counts(spe))
138-
spe$sum_gene <- colSums(counts(spe) > 0)
139-
rowData(spe)$gene_search <- paste0(rowData(spe)$gene_name, "; ", rowData(spe)$gene_id)
140-
is_mito <- which(seqnames(spe) == chrM)
141-
spe$expr_chrM <- colSums(counts(spe)[is_mito, , drop = FALSE])
142-
spe$expr_chrM_ratio <- spe$expr_chrM / spe$sum_umi
143-
## Add a variable for saving the manual annotations
144-
spe$ManualAnnotation <- "NA"
145-
14686
## Done!
14787
return(spe)
14888
}

R/spe_add_info.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#' @param spe A [SpatialExperiment-class][SpatialExperiment::SpatialExperiment-class]
99
#' object, such as one created by initially running `VisiumIO::TENxVisium()` or
1010
#' `VisiumIO::TENxVisiumHD()`.
11+
#'
12+
#' @return A [SpatialExperiment-class][SpatialExperiment::SpatialExperiment-class]
13+
#' object with gene info, SpaceRanger analysis results, and other information
14+
#' attached.
1115
spe_add_info <- function(
1216
spe,
1317
samples,

man/read10xVisiumWrapper.Rd

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/spe_add_info.Rd

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)