seurat icon indicating copy to clipboard operation
seurat copied to clipboard

Remove empty image slots after SplitObject()

Open whitneyt1 opened this issue 10 months ago • 0 comments

I have a large object with 36 visium reactions that I have SCTransformed -> rpca integrated -> performed downstream analysis. I would now like to sub-cluster the larger clusters by subsetting->reclustering, however to avoid, sample-specific clusters, I have found the best way to do this is ->SplitObject by sample ID -> SCTransform -> reintegrate -> perform downstream analysis as suggested by atakanekiz in issue #1883. However, each split object retains all of the (empty) image slots for other samples, and so when reintegrated, I have an obscene number of images.

For example:

$AZ044.Surg2.23 Spatial data from the VisiumV1 technology for 431 samples Associated assay: Spatial Image key: az044surg223_

$AZ044.Surg3.23 Spatial data from the VisiumV1 technology for 0 samples Associated assay: Spatial Image key: az044surg323_

$AZ044.Surg4.23 Spatial data from the VisiumV1 technology for 0 samples Associated assay: Spatial Image key: az044surg423_

$AZ049.23 Spatial data from the VisiumV1 technology for 0 samples Associated assay: Spatial Image key: az04923_

$AZ050.23 Spatial data from the VisiumV1 technology for 0 samples Associated assay: Spatial Image key: az05023_

This is the function i have written to subset and reintegrate my subsetted clusters:

#Create Subsetting and Reintegration Function: subset_and_reintegrate<-function(object, subset_name, npcs, kweight) {

#Split subsetted object into list of objects object_list=SplitObject(object, split.by = 'Sample_ID') #Select Integration Features for list of objects from subsetted object

object_list=lapply(X=object_list, FUN=function(x) { x<-SCTransform(x) })

tmp_features=SelectIntegrationFeatures(object.list=object_list, nfeatures=3000)

#save features saveRDS(tmp_features, paste0('~/Whitney_Analysis/subset_outs/st07.',subset_name,'.variablefeatures.Rds'))

print("Prepping SCT Integration") object_list<-PrepSCTIntegration(object.list = object_list, anchor.features = tmp_features)

print(paste0("Running PCA. npcs=", npcs)) object_list <- lapply(X = object_list, FUN = function(x) { x <- RunPCA(x, features = tmp_features, npcs=npcs) })

tmp_anchors<-FindIntegrationAnchors(object.list=object_list, anchor.features = tmp_features, reduction="rpca", normalization.method = 'SCT', dims=1:29) saveRDS(tmp_anchors, paste0('~/Whitney_Analysis/subset_outs/st07.',subset_name,'.anchors.Rds'))

subset.integrated<-IntegrateData(anchorset=tmp_anchors, normalization.method = 'SCT', new.assay.name='subsetintegrated', k.weight=kweight) saveRDS(subset.integrated, file=paste0('~/Whitney_Analysis/subset_outs/st07.',subset_name,'.integrated.Rds')) subset.integrated<-RunPCA(subset.integrated, assay='subsetintegrated', verbose=TRUE) %>% FindNeighbors(assay='subsetintegrated', reduction="pca", dims=1:50) %>% FindClusters(resolution=0.5) %>% RunUMAP(reduction="pca", assay='subsetintegrated', dims=1:50)

#store Seurat clusters from transcriptional PCA as metadata slot #itx=PCA on integrated assay transcriptional scale.data subset.integrated<-AddMetaData(subset.integrated,metadata = subset.integrated$seurat_clusters, col.name = 'subset_cluster') saveRDS(subset.integrated, file=paste0('~/Whitney_Analysis/subset_outs/st07.', subset_name,'.integrated.Rds')) return(subset.integrated) }

Is there a way to remove empty images slots either when splitting images or after integration?

Thank you so much!

whitneyt1 avatar May 01 '24 18:05 whitneyt1