KChen-lab / METAFlux

Other
38 stars 6 forks source link

Error when computing averaged expression values from seurat object: counts slot not existing #11

Closed LiuCanidk closed 4 months ago

LiuCanidk commented 4 months ago

Hi, I encountered an error when computing averaged expression values from the seurat object.

sce=readRDS('sce.final.rm.rds') library(Seurat) library(METAFlux) setwd('14.METAFlux') df.sample=calculate_avg_exp(sce, 'orig.ident', n_bootstrap = 100, seed = 2024)

Error

Error in FUN(X[[i]], ...) : no slot of name "counts" for this object of class "Assay5"

  1. FUN(X[[i]], ...)
  2. lapply(1:n_bootstrap, get_ave_exp, myseurat, samples, myident) atC:\Users\Administrator\AppData\Local\Temp\RtmpWUlcOq\rstudio-scratch-233013cd6afa.R#6
  3. calculate_avg_exp(sce, "orig.ident", 1000, 2024)

I guess it's a problem with seuratV5 but found no specific information on how to solve this. My seurat object indeed has the counts layer.

sce An object of class Seurat 51795 features across 35412 samples within 1 assay Active assay: RNA (51795 features, 2000 variable features) 3 layers present: data, counts, scale.data 4 dimensional reductions calculated: pca, umap, harmony, UMAP_harmony

Any help would be greatly appreciated!

yechanYS commented 4 months ago

I've encountered similar issue. Also, I think METAFlux package is not compatible with Seurat v5, yet.

Therefore, in Issue #5, I generated new codes for calculate_avg_exp() function by editing its source codes. Also, I slightly changed these codes, from Issue #5, in order to get counts matrix from seurat v5 object.

For Seurat v5, I think it will be working if you execute below codes and use 'edit_calculate_avg_exp()' function instead of 'calculate_avg_exp()'. All parameters are same with vignette.

generate_boots <- function(celltype, n) {
  dt <- data.frame(cluster = celltype, id = 1:length(celltype))
  index <- do.call(cbind, sapply(1:n, function(x) {
    splits <- dt %>%
      group_by(cluster) %>%
      sample_n(dplyr::n(), replace = TRUE) %>%
      ungroup() %>%
      dplyr::select("id")
  }))
  return(index)
}

get_ave_exp <- function(i, myseurat, samples,myident) {
  meta.data=myseurat@meta.data[samples[,i],]
  sample <-GetAssayData(myseurat, assay = "RNA")[,samples[,i]]
  name <- colnames(sample)
  for (j in 1:length(name)) {
    name[j] <- paste0(name[j], "_", j)
  }
  colnames(sample) <- name
  rownames(meta.data) <- name
  SeuratObject<-suppressWarnings(
    CreateSeuratObject(count=sample, meta.data = meta.data))
  SeuratObject<-NormalizeData(SeuratObject,verbose = TRUE)
  ave<-GetAssayData(AverageExpression(SeuratObject,group.by = myident,return.seurat = T), assay = "RNA") %>% as.data.frame()
  return(ave)
}

edit_calculate_avg_exp <- function(myseurat,myident,n_bootstrap,seed) {
  set.seed(seed)
  samples=generate_boots(myseurat@meta.data[,myident],n_bootstrap)
  exp <- lapply(1:n_bootstrap,get_ave_exp,myseurat,samples,myident)
  exp <- do.call(cbind, exp)
  return(exp)
}

mean_exp = edit_calculate_avg_exp(myseurat = sc, myident = 'celltype', n_bootstrap = 50, seed = 1) 
LiuCanidk commented 4 months ago

Great! Thanks for your kind sharing code! @yechanYS It worked after adjusting the source code. And I notice seuratV5 recommend using AggrerateExpression to perform pseudobulk analysis, maybe it is relevant. Anyway, again thank you very much!