Subcluster all the clusters of a Seurat object

148 Views Asked by At

I have a Seurat object made from integrating 4 different objects, the results is a Seurat object with 70 clusters (0 to 69) I wanted to subset each single cluster and recluster it to achieve higher resolution. here is the code I wrote, however, I keep getting an error. would you know what this could be ? thank you

here there is the code

clusters_to_subset <- 0:69

for (cluster_to_subset in clusters_to_subset) {
  #Step 1: Identify cells from the specific cluster
  cells_to_subset <- WhichCells(full_seurat_object, ident = clusters_to_subset)
  
  #Step 2: Create a new Seurat object with subsetted cells
  sub_seurat_object <- subset(full_seurat_object, cells = cells_to_subset)
  
  #Step 3: Recluster the subsetted Seurat object with increased resolution
  sub_seurat_object <- FindNeighbors(sub_seurat_object, dims = 1:30)  # You can adjust 'dims' based on your data
  sub_seurat_object <- FindClusters(sub_seurat_object, resolution = 1.5)  # Adjust 'resolution' as needed
  
  #Plot or save the new clustering results if desired
  plot(sub_seurat_object, label = TRUE)
  
  #Save the ggplot2 in .pdf format
  ggsave(paste0("/Users/avolaa/Documents/Single_Cell_Paper/Species_Integration_Seurat/All_species_20230102/Seurat_Subset_Plots_20230102/Subset_clusters_", cluster_to_subset, ".pdf"))
  
  #Save the ggplot2 in .png format
  ggsave(paste0("/Users/avolaa/Documents/Single_Cell_Paper/Species_Integration_Seurat/All_species_20230102/Seurat_Subset_Plots_20230102/Subset_clusters_", cluster_to_subset, ".png"))
                
  #Save the sub Seurat object or do any further processing as needed
  saveRDS(sub_seurat_object, file = paste0("subset_cluster_", cluster_to_subset, ".rds"))
}

this is the error

Error in as.double(y) : cannot coerce type 'S4' to vector of type 'double'

thank you!

0

There are 0 best solutions below