theMILOlab / SPATA2

A Toolbox for Spatial Gene Expression Analysis
77 stars 15 forks source link

platform compatibility - seurat #102

Open rhlgyb opened 4 months ago

rhlgyb commented 4 months ago

To the developers of SPATA2,

It's been about a year since I've last used SPATA2, and I see a lot of interesting changes!

I've reinstalled the latest version of SPATA2, and tried using asSPATA2() for converting my seurat object to a spata2 object. I've encountered an error that also comes up as I've checked, with the tutorial data and code. The picture below shows the error that comes up with the tutorial.

image

I'm using transformSeuratToSpata() temporarily to avoid this issue, but the resulting image from using this function turns out very small in a corner of the plot, unaligned with the spots and I'm unable to fix this using the image handling functions (rotations and flips work, but scaling doesn't). I'm hoping to find a breakthrough with either one of these functions to initiate my spata2 object with the image aligned.

image

Thanks so much for your help and support.

RHL

simonmfr commented 4 months ago

Hi @rhlgyb,

asSPATA2() could not find the count matrix in your Seurat object. By default, asSPATA2() is looking for the name "counts" (eg seurat_object[["Spatial"]]$counts).

Did you possibly rename the count matrix in Seurat? Custom names for the count and scaled matrices can be provided to SPATA2 via:

asSPATA2(..., count_mtr_name = "counts", scaled_mtr_name = "scale.data")
juliach2 commented 4 months ago

Dear SPATA2 team,

I am getting the same error when running

spata_obj2 <- SPATA2::asSPATA2( object = LYFR414, sample_name = "LYFR414", image_name = "tissue_hires_image", spatial_method = "Visium" )

15:11:05 Setting up new spata2 object. 15:11:05 Transferring data. Error in value[3L] : Could not find count matrix in specified seurat object. Did you choose the correct method?

The count matrix is present and does not have a custom name.

LYFR414 An object of class Seurat 17943 features across 4519 samples within 1 assay Active assay: Spatial (17943 features, 0 variable features) 1 layer present: counts 1 image present: tissue_hires_image

By LYFR414[["Spatial"]]$counts I can get the count matrix:

17943 x 4519 sparse Matrix of class "dgCMatrix" [[ suppressing 57 column names ‘AAACAACGAATAGTTC-1’, ‘AAACAAGTATCTCCCA-1’, ‘AAACAATCTACTAGCA-1’ ... ]] [[ suppressing 57 column names ‘AAACAACGAATAGTTC-1’, ‘AAACAAGTATCTCCCA-1’, ‘AAACAATCTACTAGCA-1’ ... ]]

SAMD11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...... NOC2L . 3 . 3 . . . . . . 2 . 2 1 . . . 1 . . . . 2 . 1 . 2 2 2 . . 1 . . 1 . 1 . 2 1 . 1 2 2 . . . 1 . . 1 . . 1 2 . 1 ...... KLHL17 . 1 . . . . . . . . . . . . . . 1 . . . . . . . . . 1 . . . . . . . . . . 1 . . . . . . . 1 . . 1 . . 1 . . . . . ...... PLEKHN1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...... PERM1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...... HES4 1 . . . . 1 1 . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . ...... ISG15 . 3 4 4 6 5 3 2 . 4 1 1 3 5 1 3 4 2 3 . 2 1 . 3 1 4 1 5 5 4 1 2 5 8 1 3 2 2 5 5 4 4 6 2 1 3 2 4 1 9 3 4 1 1 4 3 5 ...... AGRN . 1 . . . 1 1 . . . . . 2 . . . . . . . . 1 . . . . . . . 1 . . . . . . . 1 . 1 . . . . . . . . . 1 . . . . 2 . . ...... RNF223 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......

Is there a workaround to still convert seurat to spata2?

Thank you!

simonmfr commented 3 months ago

Hi, which versions of SPATA2 and Seurat are you using?

juliach2 commented 3 months ago

I am using Seurat Version 5.0.1 and SPATA2 Version 2.0.4.

simonmfr commented 3 months ago

Hi, can you please try to to update SPATA2 and re-run with:

SPATA2::asSPATA2( object = LYFR414, sample_name = "LYFR414", image_name = "tissue_hires_image", spatial_method = "Visium", scaled_mtr_name = NULL )

juliach2 commented 3 months ago

Hi,

thank you! This soved the problem.

However since I updated SPATA2 I can not import datasets using the initiateSpataObject_10X function, anymore. Loading saved spata objects in RDS is still working.

spata_obj <- initiateSpataObject_10X( directory_10X = "Q:/XX/SpaceRanger/LYFR414", sample_name = "LYFR414") )

08:56:37 Starting initiation. 08:56:37 No gene-set data.frame path specified. 08:56:37 10X folder found. 08:56:37 Reading in .h5 file. 08:56:37 Loading from directory: 'Q:/xx/SpaceRanger/LYFR414/outs' 08:56:40 Performing Seurat-analysis steps. 08:56:40 Calculating percentage of ribosomal and mitochondrial genes. 08:56:41 Removing stress genes and mitochondrial genes. 08:56:41 Skip running 'SCTransform()' as it's argument input is neither TRUE nor a list. 08:56:41 Running 'Seurat::NormalizeData()' with specified parameters. Normalizing layer: counts Performing log-normalization 0% 10 20 30 40 50 60 70 80 90 100% [----|----|----|----|----|----|----|----|----|----| **| 08:56:42 Running 'Seurat::FindVariableFeatures()' with specified parameters. Finding variable features for layer counts Calculating gene variances 0% 10 20 30 40 50 60 70 80 90 100% [----|----|----|----|----|----|----|----|----|----| **| Calculating feature variances of standardized and clipped values 0% 10 20 30 40 50 60 70 80 90 100% [----|----|----|----|----|----|----|----|----|----| **| 08:56:43 Running 'Seurat::ScaleData()' with default parameters. Centering and scaling data matrix |============================================================================================================| 100% 08:56:46 Running 'Seurat::RunPCA()' with specified parameters. PC_ 1 Positive: xx Negative: xx ... 08:56:58 Running 'Seurat::FindNeighbors()' with specified parameters. Computing nearest neighbor graph Computing SNN 08:56:59 Running 'Seurat::FindClusters()' with specified parameters. Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck

Number of nodes: 4519 Number of edges: 197213

Running Louvain algorithm... 0% 10 20 30 40 50 60 70 80 90 100% [----|----|----|----|----|----|----|----|----|----| **| Maximum modularity in 10 random starts: 0.8029 Number of communities: 11 Elapsed time: 0 seconds 08:56:59 Running 'Seurat::RunTSNE()' with default parameters. 08:57:08 Running 'Seurat::RunUMAP()' with specified parameters. 08:57:08 UMAP embedding parameters a = 0.9922 b = 1.112 Mehr als eine Klasse "dist" im Cache gefunden; nutze die erste, kommt aus Namensraum 'BiocGenerics' Auch definiert durch ‘spam’ 08:57:08 Read 4519 rows and found 30 numeric columns 08:57:08 Using Annoy for neighbor search, n_neighbors = 30 Mehr als eine Klasse "dist" im Cache gefunden; nutze die erste, kommt aus Namensraum 'BiocGenerics' Auch definiert durch ‘spam’ 08:57:08 Building Annoy index with metric = cosine, n_trees = 50 0% 10 20 30 40 50 60 70 80 90 100% [----|----|----|----|----|----|----|----|----|----| **| 08:57:09 Writing NN index file to temp file C:\Users\xx\AppData\Local\Temp\RtmpmAVSnC\filec049a02abd 08:57:09 Searching Annoy index using 1 thread, search_k = 3000 08:57:10 Annoy recall = 100% 08:57:10 Commencing smooth kNN distance calibration using 1 thread with target n_neighbors = 30 08:57:11 Initializing from normalized Laplacian + noise (using RSpectra) 08:57:11 Commencing optimization for 500 epochs, with 203174 positive edges Using method 'umap' 0% 10 20 30 40 50 60 70 80 90 100% [----|----|----|----|----|----|----|----|----|----| **| 08:57:20 Optimization finished 08:57:21 Initiating spata2 object. 08:57:21 Setting up new spata2 object. 08:57:21 Transferring data. 08:57:22 Done. 08:57:22 Saving. 08:57:22 No directory specified. Skip saving Seurat object. 08:57:22 Computing pixel scale factor. 08:57:22 Using center to center distance to compute pixel scale factor. 08:57:22 Computing barcode spot distances. 08:57:25 Done. Error in getActiveMatrixName(object, verbose = FALSE) : Please set an active matrix with setActivenMatrix() In addition: Warning message: Not validating Seurat objects

One can import data in seurat and transfer it to a spata2 object, but I think as this is an essential function it might be worth fixing.

Thank you!

simonmfr commented 3 months ago

Hi, thank you for testing. Have you tried setting spata_obj <- setActiveMatrix(spata_obj, mtr_name="scaled") manually?

We are about to release a major update which will fix many of the issues with loading and platform compatibility.

Best, Simon

juliach2 commented 3 months ago

Hallo,

Thank you for helping! I am looking forward to the update!

Due to the error in spata_obj <- initiateSpataObject_10X() I do not get an object spata_obj. So it is not possible to run setActiveMatrix. I tried to stop the function at an earlier point of processing the data, by setting NormalizeData=F. This has not solved the problem. The setActiveMatrix() function itself works well in another context.

Best, Julia

simonmfr commented 3 months ago

Now it should work, please update SPATA2 and confuns, and try again

juliach2 commented 3 months ago

Thank you it now works perfectly!

Leilingshan commented 2 months ago

Hi, can you please try to to update SPATA2 and re-run with:

SPATA2::asSPATA2( object = LYFR414, sample_name = "LYFR414", image_name = "tissue_hires_image", spatial_method = "Visium", scaled_mtr_name = NULL )

Hi, @simonmfr the solution didn't work for me when i got the error : "17:38:29 Setting up new spata2 object. 17:38:29 Transferring data. Error in value[3L] : Could not find count matrix in specified seurat object. Did you choose the correct method?"

And, st.seurat[['Spatial']]$counts i got the error : Error in st.seurat[["Spatial"]]$counts : $ operator not defined for this S4 class

packageVersion("Seurat") [1] ‘4.3.0.1’ packageVersion("SPATA2") [1] ‘2.0.4’

I check the source code, and found that asSPATA2 for seurat: " count_mtr <- getFromSeurat( return_value = assay[count_mtr_name], error_handling = "stop", error_ref = "count matrix" ) " "return_value = assay[count_mtr_name]," was replaced by "return_value = Seurat::GetAssayData(object = assay,slot=count_mtr_name)" maybe can fix the error.

And, " getFromSeurat( return_value = assay[scaled_mtr_name], error_handling = "stop", error_ref = "scaled matrix", error_value = NULL ) " "return_value = assay[scaled_mtr_name]," maybe be replaced by "return_value = Seurat::GetAssayData(object = assay,slot=scaled_mtr_name)"

I'm not sure that it can fix the error. Looking forward to your reply, Thanks!