Skip to content

Commit 11bfb29

Browse files
authored
Remove CS-CORE and improve data extraction
Removed CS-CORE functionality and updated data extraction methods for Seurat objects.
1 parent 1be0c73 commit 11bfb29

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

06_imputation/correlation_workflow.qmd

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@ library(pheatmap)
6060
library(gridExtra)
6161
library(RColorBrewer)
6262
library(viridis)
63-
64-
library(CSCORE)
6563
library(SAVER)
66-
library(Rmagic)
64+
# library(Rmagic)
6765
6866
invisible(list2env(params, environment()))
6967
source(project_file)
@@ -218,30 +216,32 @@ 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 = [email protected]
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
244+
# magic returns cells x genes; transpose to features x cells
245245
seurat_imputed[["MAGIC"]] <- CreateAssayObject(data = t(data_magic))
246246
247247
# SAVER
@@ -293,8 +293,7 @@ We have a few different ways to compute correlation scores with their associated
293293
- `SCTransform` counts -> spearman correlation matrix
294294
- `MAGIC` imputed -> spearman correlation matrix
295295
- `SAVER` imputed -> spearman correlation matrix
296-
2. `CS-CORE`
297-
- Raw RNA counts -> co-expression matrix
296+
2. (removed) `CS-CORE` (this report no longer runs CS-CORE)
298297

299298
```{r correlations}
300299
# Store output so we don't have to re-run correlation each time
@@ -321,8 +320,15 @@ if (!file.exists(filename)) {
321320
gene_2 <- genes_comb[idx, 2]
322321
323322
for (assay_ in assays) {
324-
gene_exp <- t(seurat_imputed[[assay_]]$data[c(gene_1, gene_2), ]) %>%
325-
as.data.frame()
323+
# extract assay data safely (features x cells) and subset to the two genes
324+
assay_mat <- tryCatch(as.matrix(GetAssayData(seurat_imputed[[assay_]], slot = "data")), error = function(e) NULL)
325+
if (is.null(assay_mat)) {
326+
gene_exp <- data.frame()
327+
} else {
328+
sub_mat <- assay_mat[c(gene_1, gene_2), , drop = FALSE]
329+
# transpose to cells x genes for cor.test
330+
gene_exp <- as.data.frame(t(sub_mat))
331+
}
326332
327333
if (all(gene_exp[[gene_1]] == 0) | all(gene_exp[[gene_2]] == 0)) {
328334
corr_val <- 0.0
@@ -342,15 +348,7 @@ if (!file.exists(filename)) {
342348
}
343349
}
344350
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)
351+
# CS-CORE removed: no additional co-expression estimates are appended here
354352
355353
# Save output
356354
write.csv(df_corr, filename)
@@ -367,7 +365,7 @@ Showing the patterns of correlation for each method. The x-axis and y-axis are t
367365

368366
```{r visualize-cors}
369367
#| fig-width: 7
370-
methods <- c("RNA", "SCT", "MAGIC", "SAVER", "CSCORE")
368+
methods <- c("RNA", "SCT", "MAGIC", "SAVER")
371369
372370
cor_List <- purrr::map(methods, \(method){
373371
corr <- df_corr[c("Var1", "Var2", method)]
@@ -381,9 +379,6 @@ cor_List <- purrr::map(methods, \(method){
381379
382380
breaks <- seq(-1, 1, by = 0.1)
383381
show_legend <- F
384-
if (method == "CSCORE") {
385-
show_legend <- T
386-
}
387382
p <- pheatmap(mtx,
388383
color = inferno(10),
389384
show_rownames = FALSE,
@@ -410,7 +405,7 @@ In these scatterplots, the gene-pairs that are colored red have different result
410405
```{r cor-compare}
411406
#| fig-width: 3
412407
#| fig-height: 8
413-
methods <- c("MAGIC", "SAVER", "CSCORE")
408+
methods <- c("MAGIC", "SAVER")
414409
methods_comb <- data.frame(t(combn(methods, 2)))
415410
plot_list <- list()
416411
@@ -460,3 +455,4 @@ List and version of tools used for the QC report generation.
460455
```{r}
461456
sessionInfo()
462457
```
458+

0 commit comments

Comments
 (0)