Running for loop for resolution of Seurat got error: Error: Not an S4 object.
1
0
Entering edit mode
13 months ago
synat.keam ▴ 100

Dear All fellows,

I'm using SCTtransform to normalize data and try to run various resolutions to determine which one is good for clustering. However, once it comes to for loop sections, it shows me the clustering plot of resolution 0, but the rest was terminated with error saying that Error: Not an S4 object. Not sure anyone could have a look at my script and give some suggestions. I read the error description, but it was beyond what I could understand.

#Set the bar!
Filtered.cells <- subset(Arthritis.merge, subset = nFeature_RNA >500 
              & nFeature_RNA <5500 & nCount_RNA>800 & nCount_RNA <39000 & percent.mt < 10)

#SCTransform
seuraobj<- SCTransform(Filtered.cells, vars.to.regress = "percent.mt", verbose = FALSE)

#Determine # of PCs
seuraobj<- RunPCA(seuraobj, seed.use = 12345, verbose = FALSE)

#Integration
library(harmony)
set.seed(12345)
seuraobj<- RunHarmony(seuraobj ,group.by.vars = "orig.ident")
seuraobj<- FindNeighbors(seuraobj, reduction = "harmony", dims = 1:30, verbose = FALSE)

#Testing various resolution 
# Create a directory to save the plots
dir.create("resolution_plots", showWarnings = FALSE)

#Iterate through the resolutions and create plots
for(res in c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1)) {
 seuraobj<- FindClusters(seuraobj, random.seed = 12345, verbose = FALSE, resolution = res)
 seuraobj<- RunUMAP(seuraobj, dims = 1:30, reduction = "harmony", verbose = FALSE)

  #plot umaps 
 seuraobj<- DimPlot(seuraobj, reduction= "umap",label = TRUE, label.size = 3, plot.title=paste0('res', res)) +
  theme_minimal() +
  NoLegend()

  #Display plots 
  print( seuraobj)

  #Save plots 
  ggsave(filename = paste0("resolution_plots/plot_resolution_", res, ".png"), plot = seuraobj)

Error: Not an S4 object.
R single seurat cell • 1.4k views
ADD COMMENT
3
Entering edit mode
13 months ago

You can provide multiple resolutions to the resolution argument and it will cluster using all of them. Each resolution will get stored as a separate column in your metadata.

seuraobj<- FindClusters(
  seuraobj, random.seed=12345, verbose=FALSE,
  resolution=seq(0, 1, by=0.1)
)

It's pretty easy to then plot all of the resolutions by supplying them to the group.by argument of DimPlot. Note depending on the workflow RNA_snn_res may be something else. Just check your metadata column names to see what they are.

p <- DimPlot(
  seuratobj, reduction="umap", ncol=2,
  group.by=glue::glue("RNA_snn_res.{seq(0, 1, by=0.1)}")
)

print(p)

ggsave(
  filename="resolution_plots.png", plot=p,
  height=16, width=10,
  device=ragg::agg_png, dpi=300
)
ADD COMMENT
0
Entering edit mode

Thank you brother for your help. Since you are an expert, I would like to ask another question. Hope you do not mind me.
I am trying to test different values of min.dist and trying to test your code, but I got error again. I did not have a sequence of value this time and some number are random. Therefore, I replaced sth like this

group.by=glue::glue("RNA_snn_res.{c(0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.05, 0.01, 0.005, 0.001)})
# min.dist
for(dist in c(0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.05, 0.01, 0.005, 0.001)){
    Arthritis <- RunUMAP(object = Arthritis ,
                          reduction = "harmony",
                          dims = 1:30,
                          min.dist = dist)

    Arthritis <- FindNeighbors(object = Arthritis, reduction="harmony", dims = 1:30)
    snn.obj <- FindClusters(object = Arthritis, resolution = 0.2)

    p <- DimPlot(snn.obj, reduction="umap", ncol=2,
  group.by=glue::glue("RNA_snn_res.{c(0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.05, 0.01, 0.005, 0.001)})

    ggsave(filename="resolution_plots.png", plot=p, height=16, width=10, device=ragg::agg_png, dpi=300)
}

I got error at group.by. Not sure if you could kindly check it for me.

Thanks in advance

ADD REPLY

Login before adding your answer.

Traffic: 1743 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6