satijalab / seurat

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

Creating Spatial Object #2790

Closed MarcElosua closed 4 years ago

MarcElosua commented 4 years ago

Good morning and thank you very much for all the work you do!!!

I'm trying to build a Seurat object from Spatial data and I'm bumping into some issues. The main setback is that the data I'm trying to use has not been processed by Space Ranger and therefore I do not have the files in the right format to simply use Seurat::Load10X_Spatial. I've tried manually transforming the files to the right format as shown below: #### Convert count matrix to h5 format #### M0 <- writeTENxMatrix(x = sparse_matrix, filepath = "path/to/filtered_feature_bc_matrix.h5", group="m0") but I get the following error:

Error in x$exists(name) : HDF5-API Errors: error #000: ../../../src/H5L.c in H5Lexists(): line 881: unable to get link info class: HDF5 major: Symbol table minor: Object not found error #001: ../../../src/H5L.c in H5L__exists(): line 2962: path doesn't exist

    class: HDF5
    major: Symbol table
    minor: Object already exists

error #002: ../../../src/H5Gtraverse.c in H5G_traverse(): line 869: internal path traversal failed

    class: HDF5
    major: Symbol table
    minor: Object not found

error #003: ../../../src/H5Gtraverse.c in H5G_traverse_real(): line 755: component not found

    class: HDF5
    major: Symbol table
    minor: Object not found

I then tried to build my own Seurat spatial object using the Spaniel package: GSM3405534_se <- Spaniel::createSeurat(counts = sparse_matrix, barcodeFile = "path/to/data/spatial/tissue_positions_list_spatial_object.tsv", projectName = "PDAC", sectionNumber = "1")

But there is no option to add an image to the images slot.

Is it possible to add an image to a preexisiting Seurat spatial object?

Thank you very much for your time and attention, Marc

JZL commented 3 years ago

Hi,

This is an old issue but for anyone else who runs into this and doesn't want to go through Spatial, I also had a hard time finding documentation for spatial seurat construction, although I probably missed it somewhere. (@ Seurat maintainers, I think it could be helpful to add a section in the spatial vignette pointing towards the canonical way of constructing it. Both for 10X/Slideseq (which do have the nice constructors) and also for generic spatial data for other modalities like FISH)

After reading through some of the constructor files, I think the easiest route to go through for generic XY spatial data is the ReadSlideSeq constructor which takes in a .csv file. But if you already have the coordinates loaded into a variable (say a list of X, Y coordinates and BARCODES (same order as in your CreateSeuratObject constructor just to be safe) then this preliminarily worked for me:

slide.seq = CreateSeuratObject(counts = COUNTS_MTX, assay="Spatial")

coord.df = data.frame(x=X, y=Y, stringsAsFactors=FALSE) # (stringsAsFactors only if also have a separate barcodes column)
rownames(coord.df) = BARCODES

slide.seq@images$image =  new(
    Class = 'SlideSeq',
    assay = "Spatial",
    key = "image_",
    coordinates = coord.df
  )
willmonty14 commented 3 years ago

So I know that it's been a while since anyone commented on this, but I'm trying to do the same thing that you did with an image, but I don't know how to actually get the image file itself into the object. Whenever I try to use SpatialFeaturePlot, for example, my image comes up on a white background. How do I actually put the image into the Seurat object?

ice4prince commented 3 years ago

Hi,

This is an old issue but for anyone else who runs into this and doesn't want to go through Spatial, I also had a hard time finding documentation for spatial seurat construction, although I probably missed it somewhere. (@ Seurat maintainers, I think it could be helpful to add a section in the spatial vignette pointing towards the canonical way of constructing it. Both for 10X/Slideseq (which do have the nice constructors) and also for generic spatial data for other modalities like FISH)

After reading through some of the constructor files, I think the easiest route to go through for generic XY spatial data is the ReadSlideSeq constructor which takes in a .csv file. But if you already have the coordinates loaded into a variable (say a list of X, Y coordinates and BARCODES (same order as in your CreateSeuratObject constructor just to be safe) then this preliminarily worked for me:

slide.seq = CreateSeuratObject(counts = COUNTS_MTX, assay="Spatial")

coord.df = data.frame(x=X, y=Y, stringsAsFactors=FALSE) # (stringsAsFactors only if also have a separate barcodes column)
rownames(coord.df) = BARCODES

slide.seq@images$image =  new(
    Class = 'SlideSeq',
    assay = "Spatial",
    key = "image_",
    coordinates = coord.df
  )

It looks like the scale.factors have to be defined in the "image":

Error in validObject(.Object) : invalid class “VisiumV1” object: invalid object for slot "scale.factors" in class "VisiumV1": got class "character", should be or extend class "scalefactors"

My code is as below: data.BC@images$image = new(Class = "VisiumV1",assay = "spatial", key = "image_",coordinates = Spot_metadata)

HosseinMousavi commented 2 years ago

Hi,

This is an old issue but for anyone else who runs into this and doesn't want to go through Spatial, I also had a hard time finding documentation for spatial seurat construction, although I probably missed it somewhere. (@ Seurat maintainers, I think it could be helpful to add a section in the spatial vignette pointing towards the canonical way of constructing it. Both for 10X/Slideseq (which do have the nice constructors) and also for generic spatial data for other modalities like FISH)

After reading through some of the constructor files, I think the easiest route to go through for generic XY spatial data is the ReadSlideSeq constructor which takes in a .csv file. But if you already have the coordinates loaded into a variable (say a list of X, Y coordinates and BARCODES (same order as in your CreateSeuratObject constructor just to be safe) then this preliminarily worked for me:

slide.seq = CreateSeuratObject(counts = COUNTS_MTX, assay="Spatial")

coord.df = data.frame(x=X, y=Y, stringsAsFactors=FALSE) # (stringsAsFactors only if also have a separate barcodes column)
rownames(coord.df) = BARCODES

slide.seq@images$image =  new(
    Class = 'SlideSeq',
    assay = "Spatial",
    key = "image_",
    coordinates = coord.df
  )

Hi, Thank you for providing this information. I need to add the expression to the Spatial Seurat object. Your suggestion would be deeply appreciated!

Best, Hossein

Prakrithi-P commented 1 year ago

Hi, I couldn't find the solution to an error that had already been raised. sample_combinedmatPHNBn@images$image = new(Class = 'VisiumV1', assay = "Spatial", key = "HNB", coordinates = c)

while trying to run the above command, i get the following: Error in validObject(.Object) : invalid class “VisiumV1” object: invalid object for slot "scale.factors" in class "VisiumV1": got class "S4", should be or extend class "scalefactors"

liaoyjruby commented 10 months ago

Hi, I couldn't find the solution to an error that had already been raised. sample_combinedmatPHNBn@images$image = new(Class = 'VisiumV1', assay = "Spatial", key = "HNB", coordinates = c)

while trying to run the above command, i get the following: Error in validObject(.Object) : invalid class “VisiumV1” object: invalid object for slot "scale.factors" in class "VisiumV1": got class "S4", should be or extend class "scalefactors"

It should work if you add one more line when creating the image object to define the scale.factors, which is a "scalefactors" object (see ?Seurat::scalefactors() for creating the list). The code ends up looking like:

  # Ex. scalefactors
  sfs <- scalefactors(spot = 138.656, fiducial = 223.9828, hires = 0.1139861, lowres = 0.03419583)
  sobj@images$slice1 =  new(
    Class = 'VisiumV1',
    assay = "Spatial",
    key = "slice1_",
    coordinates = coords,
    scale.factors = sfs
  )