Skip to content

Commit 3b2d792

Browse files
committed
Merge branch 'imputation_qmd' of https://github.com/bcbio/singlecell-reports into imputation_qmd
2 parents efd9e30 + f09356a commit 3b2d792

1 file changed

Lines changed: 35 additions & 34 deletions

File tree

06_imputation/correlation_workflow.qmd

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ library(pheatmap)
6060
library(gridExtra)
6161
library(RColorBrewer)
6262
library(viridis)
63-
64-
library(CSCORE)
6563
library(SAVER)
6664
library(Rmagic)
6765
@@ -108,7 +106,7 @@ min_perc <- 0.2
108106

109107
This report intends to both calculate correlations of gene of interests and also compares how imputation changes those values.
110108

111-
Throughout this report, we assess two main ways of calculating gene correlations scores (**`Spearman` and `CS-CORE`**).
109+
Throughout this report, we assess Spearman correlation estimates.
112110

113111
We further assessed two ways of imputating gene expression values: MAGIC and SAVER.
114112

@@ -218,36 +216,45 @@ We compare three alternative methods of estimating expression levels to log norm
218216
# Store output so we don't have to re-run imputation each time
219217
filename <- glue("{path_outs}/imputed.RDS")
220218
if (!file.exists(filename)) {
221-
# Get raw counts
222-
raw_rna <- LayerData(seurat, assay = "RNA", layer = "counts")
219+
# Get raw counts (genes x cells matrix)
220+
raw_rna <- as.matrix(GetAssayData(seurat[["RNA"]], slot = "counts"))
223221
224222
# SCT
225223
# Re-run SCT on subset data
226224
seurat <- SCTransform(seurat, return.only.var.genes = FALSE, min_cells = 1)
227225
228226
# Creating new seurat object for genes of interest only
229-
data_raw <- FetchData(seurat, assay = "RNA", layer = "counts", vars = corr_genes)
230-
data_rna <- FetchData(seurat, assay = "RNA", layer = "data", vars = corr_genes)
231-
data_sct <- FetchData(seurat, assay = "SCT", layer = "data", vars = corr_genes)
227+
# Use GetAssayData to reliably extract counts/data (features x cells)
228+
data_raw <- as.matrix(GetAssayData(seurat[["RNA"]], slot = "counts")[corr_genes, , drop = FALSE])
229+
data_rna <- as.matrix(GetAssayData(seurat[["RNA"]], slot = "data")[corr_genes, , drop = FALSE])
230+
data_sct <- as.matrix(GetAssayData(seurat[["SCT"]], slot = "data")[corr_genes, , drop = FALSE])
232231
233232
seurat_imputed <- CreateSeuratObject(
234-
counts = t(data_raw),
235-
data = t(data_rna),
233+
counts = data_raw,
234+
data = data_rna,
236235
meta.data = seurat@meta.data
237236
)
238-
seurat_imputed[["SCT"]] <- CreateAssayObject(data = t(data_sct))
237+
seurat_imputed[["SCT"]] <- CreateAssayObject(data = data_sct)
239238
seurat_imputed[["RAW"]] <- CreateAssayObject(counts = raw_rna)
240239
241240
# Delete the original seurat object to save memory
242241
rm(seurat)
243242
244243
data_magic <- magic(t(raw_rna), genes = corr_genes)$result
245-
seurat_imputed[["MAGIC"]] <- CreateAssayObject(data = t(data_magic))
244+
# magic may return a data.frame/tibble or matrix (cells x genes). Force to matrix and transpose to features x cells
245+
if (!is.matrix(data_magic)) data_magic <- as.matrix(data_magic)
246+
data_magic_t <- t(data_magic)
247+
# ensure dimnames align: rows = genes, cols = cells
248+
if (is.null(rownames(data_magic_t))) rownames(data_magic_t) <- corr_genes
249+
if (is.null(colnames(data_magic_t))) colnames(data_magic_t) <- colnames(raw_rna)
250+
seurat_imputed[["MAGIC"]] <- CreateAssayObject(data = data_magic_t)
246251
247252
# SAVER
248253
# Generate SAVER predictions for those genes
249254
genes.ind <- which(rownames(raw_rna) %in% corr_genes)
250-
data_saver <- saver(raw_rna,
255+
saverD <- raw_rna
256+
attr(saverD, "class") <- "matrix"
257+
data_saver <- saver(saverD,
251258
pred.genes = genes.ind,
252259
pred.genes.only = TRUE,
253260
estimates.only = TRUE,
@@ -293,8 +300,7 @@ We have a few different ways to compute correlation scores with their associated
293300
- `SCTransform` counts -> spearman correlation matrix
294301
- `MAGIC` imputed -> spearman correlation matrix
295302
- `SAVER` imputed -> spearman correlation matrix
296-
2. `CS-CORE`
297-
- Raw RNA counts -> co-expression matrix
303+
2. (removed) `CS-CORE` (this report no longer runs CS-CORE)
298304

299305
```{r correlations}
300306
# Store output so we don't have to re-run correlation each time
@@ -321,8 +327,15 @@ if (!file.exists(filename)) {
321327
gene_2 <- genes_comb[idx, 2]
322328
323329
for (assay_ in assays) {
324-
gene_exp <- t(seurat_imputed[[assay_]]$data[c(gene_1, gene_2), ]) %>%
325-
as.data.frame()
330+
# extract assay data safely (features x cells) and subset to the two genes
331+
assay_mat <- tryCatch(as.matrix(GetAssayData(seurat_imputed[[assay_]], slot = "data")), error = function(e) NULL)
332+
if (is.null(assay_mat)) {
333+
gene_exp <- data.frame()
334+
} else {
335+
sub_mat <- assay_mat[c(gene_1, gene_2), , drop = FALSE]
336+
# transpose to cells x genes for cor.test
337+
gene_exp <- as.data.frame(t(sub_mat))
338+
}
326339
327340
if (all(gene_exp[[gene_1]] == 0) | all(gene_exp[[gene_2]] == 0)) {
328341
corr_val <- 0.0
@@ -342,15 +355,7 @@ if (!file.exists(filename)) {
342355
}
343356
}
344357
345-
# Run CS-CORE
346-
DefaultAssay(seurat_imputed) <- "RAW"
347-
CSCORE_result <- CSCORE(seurat_imputed, genes = corr_genes)
348-
349-
# Store CS-CORE results
350-
tmp <- reshape2::melt(as.matrix(CSCORE_result$est)) %>% rename(CSCORE = value)
351-
df_corr <- left_join(df_corr, tmp)
352-
tmp <- reshape2::melt(as.matrix(CSCORE_result$p_value)) %>% rename(CSCORE = value)
353-
df_p_val <- left_join(df_p_val, tmp)
358+
# CS-CORE removed: no additional co-expression estimates are appended here
354359
355360
# Save output
356361
write.csv(df_corr, filename)
@@ -367,7 +372,7 @@ Showing the patterns of correlation for each method. The x-axis and y-axis are t
367372

368373
```{r visualize-cors}
369374
#| fig-width: 7
370-
methods <- c("RNA", "SCT", "MAGIC", "SAVER", "CSCORE")
375+
methods <- c("RNA", "SCT", "MAGIC", "SAVER")
371376
372377
cor_List <- purrr::map(methods, \(method){
373378
corr <- df_corr[c("Var1", "Var2", method)]
@@ -381,9 +386,6 @@ cor_List <- purrr::map(methods, \(method){
381386
382387
breaks <- seq(-1, 1, by = 0.1)
383388
show_legend <- F
384-
if (method == "CSCORE") {
385-
show_legend <- T
386-
}
387389
p <- pheatmap(mtx,
388390
color = inferno(10),
389391
show_rownames = FALSE,
@@ -403,14 +405,14 @@ plot(cor_comb)
403405

404406
# Compare correlation estimates across methods
405407

406-
Comparing the correlation scores for each gene pair for MAGIC, SAVER, and CS-CORE.
408+
Comparing the correlation scores for each gene pair for MAGIC and SAVER.
407409

408410
In these scatterplots, the gene-pairs that are colored red have different results for significance.
409411

410412
```{r cor-compare}
411413
#| fig-width: 3
412414
#| fig-height: 8
413-
methods <- c("MAGIC", "SAVER", "CSCORE")
415+
methods <- c("MAGIC", "SAVER")
414416
methods_comb <- data.frame(t(combn(methods, 2)))
415417
plot_list <- list()
416418
@@ -447,8 +449,6 @@ grid.arrange(grobs = plot_list, nrow = 3)
447449

448450
# Method description
449451

450-
[`CS-CORE`](https://github.com/ChangSuBiostats/CS-CORE) is a R package for cell-type-specific co-expression inference from single cell RNA-sequencing data. It provides an implementation for the statistical method CS-CORE proposed in this [paper](https://www.nature.com/articles/s41467-023-40503-7).
451-
452452
[`MAGIC`](https://github.com/KrishnaswamyLab/MAGIC) is an algorithm for denoising high-dimensional data most commonly applied to single-cell RNA sequencing data. MAGIC learns the manifold data, using the resultant graph to smooth the features and restore the structure of the data.
453453

454454
[`SAVER`](https://github.com/mohuangx/SAVER) implements a regularized regression prediction and empirical Bayes method to recover the true gene expression profile in noisy and sparse single-cell RNA-seq data.
@@ -460,3 +460,4 @@ List and version of tools used for the QC report generation.
460460
```{r}
461461
sessionInfo()
462462
```
463+

0 commit comments

Comments
 (0)