satijalab / seurat

R toolkit for single cell genomics
http://www.satijalab.org/seurat
Other
2.26k stars 908 forks source link

Load10X_Spatial {Seurat} -- Error #7530

Closed cathalgking closed 1 year ago

cathalgking commented 1 year ago

When trying to read in data directly from the 10x Space Ranger outs folder I am getting an Error which seems to be related to the Visium Image.

Error in Load10X_Spatial(data.dir = data_dir) : 
  Image must be an object of class 'VisiumV1'.

The code I run which results in the Error is:

data_dir <- "/PATH/outs/"
A1_seu <- Load10X_Spatial(data.dir = data_dir)

The files are un-touched and contain the required png images. The list of files in the outs folder look like: Screen Shot 2023-07-05 at 10 33 19 am

And in the 'spatial' folder there is the following files: Screen Shot 2023-07-05 at 10 37 19 am

I also tried to just read in the Image first with Read10X_Image() which seems to work but when I try to feed that into Load10X_Spatial I get a similar but different Error:

Error in Read10X_h5(filename = file.path(data.dir, filename), ...) : 
  unused argument (image = new("VisiumV1", image = c(0.996078431372549, ...

Screen Shot 2023-07-05 at 10 42 49 am

Read in image: Screen Shot 2023-07-05 at 10 38 49 am

LiangMingxuan00 commented 1 year ago

I also have this problem. And I edit the original Load10x_sample code and it works.

my_Load10X_Spatial <- function( data.dir, filename = 'filtered_feature_bc_matrix.h5', assay = 'Spatial', slice = 'slice1', filter.matrix = TRUE, to.upper = FALSE, image = NULL, ... ) { data <- Read10X_h5(filename = file.path(data.dir, filename)) if (to.upper) { rownames(x = data) <- toupper(x = rownames(x = data)) } object <- CreateSeuratObject(counts = data, assay = assay) image <- Read10X_Image( image.dir = file.path(data.dir, 'spatial'), filter.matrix = filter.matrix ) image <- image[Cells(x = object)] DefaultAssay(object = image) <- assay object[[slice]] <- image return(object) }

cathalgking commented 1 year ago

This works, thanks! Do you know why this issue arose? Is it the new Seurat version? I dont remember this happening before.

cathalgking commented 1 year ago

@LiangMingxuan00 Is there a way to use your above function to read in the hires image instead of the lores one? It seems to default to the lowres image.

When I specify "hires" in the function call it actually imports the low res image. I know this because I read both into 2 objects and plot them side by side. Also, when I remove the lowres image from the spaceranger folder and specify hires in the function call I get an error stating that it is looking for the lowres image png.

A1_grey_hires <- my_Load10X_Spatial(data.dir = sample, image = "hires")

Screen Shot 2023-07-06 at 1 12 04 pm

Both are actually lowres even though the one on the right should be hires

LiangMingxuan00 commented 1 year ago

Hi,

I guess it is the SeuratV5 problem. If you want to read hires image, I think you may should rewrite the Read10X_Image function again because the default input parameter is the lower resolution image

Read10X_Image <- function(image.dir, image.name = "tissue_lowres_image.png", filter.matrix = TRUE, ...) { image <- readPNG(source = file.path(image.dir, image.name)) scale.factors <- fromJSON(txt = file.path(image.dir, 'scalefactors_json.json')) tissue.positions.path <- Sys.glob(paths = file.path(image.dir, 'tissue_positions')) tissue.positions <- read.csv( file = tissue.positions.path, col.names = c('barcodes', 'tissue', 'row', 'col', 'imagerow', 'imagecol'), header = ifelse( test = basename(tissue.positions.path) == "tissue_positions.csv", yes = TRUE, no = FALSE ), as.is = TRUE, row.names = 1 ) if (filter.matrix) { tissue.positions <- tissue.positions[which(x = tissue.positions$tissue == 1), , drop = FALSE] } unnormalized.radius <- scale.factors$fiducial_diameter_fullres scale.factors$tissue_lowres_scalef spot.radius <- unnormalized.radius / max(dim(x = image)) return(new( Class = 'VisiumV1', image = image, scale.factors = scalefactors( spot = scale.factors$tissue_hires_scalef, fiducial = scale.factors$fiducial_diameter_fullres, hires = scale.factors$tissue_hires_scalef, scale.factors$tissue_lowres_scalef ), coordinates = tissue.positions, spot.radius = spot.radius )) }