DoubletFinder icon indicating copy to clipboard operation
DoubletFinder copied to clipboard

Script for doublet finder with seurat 5

Open synatkeamsk opened this issue 1 year ago • 9 comments

Dear Chris and team,

Sorry for asking you and your team again. Hope you do not mind. You said Doublet Finder should work with Seurat 5 now. I am using Seurat 5.0.1. I used the following scripts!

#read data of data !
data<- Read10X(data.dir = "patient_1")

#create seurat object
data<- CreateSeuratObject(counts = data, project = "164S", min.cells = 3, min.features = 200)

#SCTransform !
data<- SCTransform(data, method = "glmGamPoi", vars.to.regress = "percent.mt", verbose = FALSE)
data <- RunPCA(data, seed.use = 12345,verbose = FALSE)

#PC plot
ElbowPlot(data, ndims = 50,reduction = "pca") +
  ggtitle("Number of principle component")  + 
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))

#Run umap 
data <- RunUMAP(data, seed.use = 12345, dims = 1:45)
DimPlot(data, reduction = "umap", label = TRUE, label.size = 4) +
  theme_void() +
    NoLegend() 

library(DoubletFinder)

### ## pK Identification
doublet.data<- paramSweep_v3(data, PCs = 1:45, sct = TRUE)

 **Error in paramSweep_v3(doublet.data, PCs = 1:45, sct = TRUE) : 
  no slot of name "counts" for this object of class "Assay5"**

doublet.stats_data<- summarizeSweep(data, GT = FALSE)
bcmvn_data <- find.pK(doublet.stats_data)

Since chris has tested and has shown to work with seurat V5. I am wondering whether you could have a look at my script and suggested why it generated error. Hope you could help!

Kind Regards,

Synat,

synatkeamsk avatar Dec 02 '23 23:12 synatkeamsk

Apologise. I did not update the DoubletFinder package. After removing old package and install new one. All worked now. Thanks you, Chris and team for your wonderful work!

Kind Regards,

synat

synatkeamsk avatar Dec 03 '23 00:12 synatkeamsk

I'm getting the same error. I removed the old packages and updated but I still have the same issue.

olneykimberly avatar Jan 03 '24 19:01 olneykimberly

I think the function have changed. From your UMAP object, you can try the following script

## pK Identification
doublet.A184S2<- paramSweep(Normalized.184S2, PCs = 1:40, sct = TRUE)
doublet.stats_A184S2 <- summarizeSweep(doublet.A184S2, GT = FALSE)
bcmvn_A184S2 <- find.pK(doublet.stats_A184S2)

#save bcmv_A164S object
saveRDS(bcmvn_A184S2, file = "bcmvn_A184S2.RDS")

#ggplot 
ggplot(bcmvn_A184S2, aes(pK, BCmetric, group = 1)) +
  geom_point() +
  geom_line() +
  theme_minimal()

pK <- bcmvn_A184S2 %>% # select the pK that corresponds to max bcmvn to optimize doublet detection
  filter(BCmetric == max(BCmetric)) %>%
  select(pK) 
pK <- as.numeric(as.character(pK[[1]]))

## Homotypic Doublet Proportion Estimate 
annotations <- [email protected]$seurat_clusters
homotypic.prop <- modelHomotypic(annotations)           
nExp_poi <- round(0.025*nrow([email protected]))  ## Assuming 2.5 percent doublet
nExp_poi.adj <- round(nExp_poi*(1-homotypic.prop))

# run doubletFinder 
Normalized.184S2 <- doubletFinder(Normalized.184S2, 
                                     PCs = 1:40, 
                                     pN = 0.25, 
                                     pK = pK, 
                                     nExp = nExp_poi.adj,
                                     reuse.pANN = FALSE, sct = TRUE)

# visualize doublets
view([email protected])
DimPlot(Normalized.184S2, reduction = 'umap', group.by = "DF.classifications_0.25_0.14_89") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))

Please use SCT= TRUE if you use SCTransform or SCT= FALSE if you use standard seurat pipeline

synatkeamsk avatar Jan 03 '24 20:01 synatkeamsk

@synatkeamsk Thank you!!! I followed it as you have described and it works now as expected. Thank you so much for the help and fast reply.

olneykimberly avatar Jan 03 '24 21:01 olneykimberly

Hi, I am facing the same issue, could you let me know which version did you use? Thank you so much!

Yijia-Jiang avatar Jan 11 '24 18:01 Yijia-Jiang

@Yijia-Jiang,

Here is my package version packageVersion("Seurat") [1] ‘5.0.1’

packageVersion("DoubletFinder") [1] ‘2.0.4’

Hope you could fix it. @olneykimberly may be able to comment further.

Thanks,

synatkeamsk avatar Jan 11 '24 18:01 synatkeamsk

@synatkeamsk Thank you very much for such a quick response! I am using the exact same version, but got an error when running paramSweep function, and the error shows "Error in .subscript.2ary(x, , j, drop = TRUE)". I wonder if you have ever encountered that before?

Yijia-Jiang avatar Jan 11 '24 18:01 Yijia-Jiang

I have not encountered this error before. I have just re-run my script again. I did not see any error! These are the sort scripts I run for one sample.

#read data of 164S !
A164S<- Read10X(data.dir = "164S")

#create seurat object
A164S<- CreateSeuratObject(counts = A164S, project = "164S", min.cells = 3, min.features = 200)
A164S

#SCTransform !
Normalized.164s<- SCTransform(A164S, method = "glmGamPoi", verbose = FALSE)
Normalized.164s <- RunPCA(Normalized.164s, seed.use = 12345,verbose = FALSE)

ElbowPlot(Normalized.164s, ndims = 50,reduction = "pca") +
  ggtitle("Number of principle component")  + 
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))

#Run umap 
Normalized.164s <- RunUMAP(Normalized.164s, seed.use = 12345, dims = 1:40)
DimPlot(Normalized.164s, reduction = "umap", label = TRUE, label.size = 4) +
  theme_void() +
    NoLegend() 

library(DoubletFinder)

## pK Identification
doublet.A164S<- paramSweep(Normalized.164s, PCs = 1:40, sct = TRUE)

I did not see any error until this step, but I understand your data may be different from mine.

synatkeamsk avatar Jan 11 '24 19:01 synatkeamsk

I am using the same versions of Seurat. For doubletFinder, I copied the git repo and sourced the functions. Like so: source("/tools/DoubletFinder/R/parallel_paramSweep.R")

olneykimberly avatar Jan 11 '24 21:01 olneykimberly