seurat
seurat copied to clipboard
subsetting object with multiple spatial fields of view fails with error
Hi, I ran into a bug that prevents subsetting the Seurat object with imaging-based spatial data that contains multiple different fields of view in images
slot. Using subset on such object returns:
Error in .nextMethod(x = x, i = i) : NAs not permitted in row index
see example code below:
# Two replicate sections with different cells, same genes
data1
An object of class Seurat
302 features across 162939 samples within 1 assay
Active assay: RNA (302 features, 0 variable features)
1 spatial field of view present: fov_data1
data2
An object of class Seurat
302 features across 154414 samples within 1 assay
Active assay: RNA (302 features, 0 variable features)
1 spatial field of view present: fov_data2
# Merge two sections
data_merged = merge(x = data1, y = data2)
An object of class Seurat
302 features across 317353 samples within 1 assay
Active assay: RNA (302 features, 0 variable features)
2 spatial fields of view present: fov_data1 fov_data2
# Create classification column
data_merged$Groups = c(rep(c("Group1", "Group2"), ncol(data_merged)/2), "Group1")
# Try to subset the data
data_merged_subset = subset(data_merged, Groups == "Group1") # this fails with error
Error in .nextMethod(x = x, i = i) : NAs not permitted in row index
# When fields of view are removed, subset works again
data_merged@images$fov_data1 = NULL
data_merged@images$fov_data2 = NULL
data_merged_subset = subset(data_merged, Groups == "Group1")
data_merged_subset
An object of class Seurat
302 features across 158677 samples within 1 assay
Active assay: RNA (302 features, 0 variable features)
Same error occurs if Crop()
is used to produce new FOV of one dataset and subsequently subset()
is used on the whole object.
I've had the similar issue as well. I followed the spatial transcriptome vignette and created my object, but when I tried to subset the object based on the sum of counts for each cell, the same issue occurred: Error in .nextMethod(x = x, i = i) : NAs not permitted in row index.
The following is the script that I used:
vizgen.obj <- LoadVizgen(data.dir = getwd(), fov = "MERFISH_TEST1")
vizgen.obj
An object of class Seurat 135 features across 328851 samples within 1 assay Active assay: Vizgen (135 features, 0 variable features) 1 spatial field of view present: MERFISH_TEST1
vizgen.obj<- subset(vizgen.obj, subset = nCount_Vizgen > 0) Error in .nextMethod(x = x, i = i) : NAs not permitted in row index
Same issue here, following!
I think I have a workaround for my case.
When I use LoadVizgen() I get an error in the subsetting of the loaded data:
seur <- LoadVizgen(data.dir =sample_dir, fov=sample_name)
Warning: 50956 cells missing polygon information
seur=subset(seur,subset = nCount_Vizgen >0)
Error in .nextMethod(x = x, i = i) : NAs not permitted in row index
So I removed the cells for which there was no polygon information before adding the spatial info to the Seurat object:
data <- ReadVizgen(data.dir = paste0(sample_dir), filter = "^Blank-", type = c("centroids", "segmentations"), z = 3L)
data$centroids=data$centroids[data$centroids$cell %in%data$segmentations$cell,]
data$transcripts=data$transcripts[,colnames(data$transcripts) %in%data$segmentations$cell]
segs <- CreateSegmentation(data$segmentations)
cents <- CreateCentroids(data$centroids)
segmentations.data <- list(centroids = cents, segmentation = segs)
coords <- CreateFOV(coords = segmentations.data, type = c("segmentation", "centroids"), molecules = data$microns, assay = "Vizgen")
seurObj <- CreateSeuratObject(counts = data$transcripts, assay = "Vizgen")
coords <- subset(x = coords, cells = intersect(x = Cells(x = coords[["segmentation"]]), y = Cells(x = seurObj)))
seurObj[[sample_name]] <- coords
Now subsetting and normalizing of the seurat object works
Hi, apologies for the slow reply - the bug in the FOV
subset method should now be fixed on the feat/imaging
branch.