HaojiaWu / CellScopes.jl

A Julia package for single cell and spatial data analysis
MIT License
26 stars 2 forks source link

Issue of Conversion of Seurat object to CellScopes object #5

Open Emily-Stranger opened 7 months ago

Emily-Stranger commented 7 months ago

Hello CellScopes team,

I've encountered an issue when I tried to convert an Seurat Object to CellScopes object.

Here is the traceback:

Screenshot 2024-01-26 at 15 42 03

But I'm sure that the name of assay in the rds file I used is "RNA", and it's also an Seurat Object.

Screenshot 2024-01-26 at 15 36 05

I'm new to Julia and couldn't figure out which steps I missed or how to solve this error. I'm running Julia v1.9.3 on Macbook. Hope you can help.

Best, Emily

HaojiaWu commented 7 months ago

Thank you for your interest in CellScopes. To use the from_seurat function, there are two prerequisites:

  1. Your Seurat object needs to be fully processed. This includes completing cell clustering and running UMAP in Seurat. From my initial review of your Seurat object, it appears incomplete, as the FindVariableFeatures step is not finished. Please complete the clustering and UMAP in Seurat, save the object as an RDS file, and then retry the function.
  2. The Seurat package must be installed in your current R environment. Given that you are using Seurat in R, this requirement should already be met.

CellScopes was developed on a Linux system (Ubuntu server 22.04) and its functionality on MacBook is untested. I will do a test later. If the issue is from the Julia version, we offer two alternative methods to run CellScopes:

a. Julia 1.9 Version of CellScopes: Since CellScopes was originally built on Julia V1.7.3 and there are dependency issues with Julia 1.9, we have created a version for Julia 1.9. You can install it using the following Julia commands:

using Pkg
Pkg.add(url="https://github.com/bicycle1885/Leiden.jl")
Pkg.add(url="https://github.com/HaojiaWu/CellScopes.jl#julia_v1_9")

b. CellScopes can also be run on Docker. Here are the commands to pull and run the CellScopes Docker image:

docker pull thehumphreyslab/cellscopes
docker run -it --rm thehumphreyslab/cellscopes

Hope this helps.

ishahakm commented 5 months ago

Hi,

I was getting a similar error when trying to convert a SeuratObject to scRNAObject in CellScope. I figured out the issue was that my SeuratObject was made using Seurat v5 and thus the RNA assay was a v5 assay. I converted to the v5 assay to a v3 assay using this line of code in R: data[["RNA"]] <- as(object = data[["RNA"]], Class = "Assay")

This got passed the BoundsError and I got a partial scRNAObject in CellScopes with "Available data: - Raw count - Normalized count". But, now get a Match.MatchFailure error with the following traceback:

Stacktrace: [1] matchtype(field::DataFrame) @ CellScopes ~/.julia/packages/CellScopes/iJG9x/src/properties.jl:80 [2] show(io::IOContext{IOBuffer}, sc_obj::CellScopes.scRNAObject) @ CellScopes ~/.julia/packages/CellScopes/iJG9x/src/properties.jl:9 [3] show @ ./multimedia.jl:47 [inlined] [4] limitstringmime(mime::MIME{Symbol("text/plain")}, x::CellScopes.scRNAObject, forcetext::Bool) @ IJulia ~/.julia/packages/IJulia/Vo51o/src/inline.jl:43 [5] limitstringmime @ ~/.julia/packages/IJulia/Vo51o/src/inline.jl:38 [inlined] [6] display_mimestring @ ~/.julia/packages/IJulia/Vo51o/src/display.jl:71 [inlined] [7] display_dict(x::CellScopes.scRNAObject) @ IJulia ~/.julia/packages/IJulia/Vo51o/src/display.jl:102 [8] #invokelatest#2 @ ./essentials.jl:716 [inlined] [9] invokelatest @ ./essentials.jl:714 [inlined] [10] execute_request(socket::ZMQ.Socket, msg::IJulia.Msg) @ IJulia ~/.julia/packages/IJulia/Vo51o/src/execute_request.jl:112 [11] #invokelatest#2 @ ./essentials.jl:716 [inlined] [12] invokelatest @ ./essentials.jl:714 [inlined] [13] eventloop(socket::ZMQ.Socket) @ IJulia ~/.julia/packages/IJulia/Vo51o/src/eventloop.jl:8 [14] (::IJulia.var"#15#18")() @ IJulia ./task.jl:429

This appears to be an issue with assigning the metadata from the SeuratObject to the scRNAObject. I think it also has to do with how the SeuratObject structure changed from v4 to v5. But I also get the same Match.MatchFailure error when trying to load my raw scRNAseq data directly into CellScopes, without trying to convert from the SeuratObject.

HaojiaWu commented 5 months ago

I fixed it. Now CellScopes supports Seurat V5. You may need to reinstall the package.

using Pkg;
Pkg.rm("CellScopes")
Pkg.add(url="https://github.com/bicycle1885/Leiden.jl") 
Pkg.add(url="https://github.com/HaojiaWu/CellScopes.jl")
import CellScopes as cs
seurat_sc = cs.from_seurat("pbmc3k.rds"; 
        data_type="scRNA", 
        assay="RNA", version="v5")
YuxiZhang-0113 commented 1 month ago

I encountered a problem similar to those described when converting Visium data. Below are the details:

I'm using Seurat v4 to manage my data, and I saved my Seurat object as follows: saveRDS(dt, file = "seurat_Visium.rds") print(dt)

An object of class Seurat 51707 features across 3007 samples within 2 assays Active assay: SCT (18169 features, 3000 variable features) 1 other assay present: Spatial 3 dimensional reductions calculated: pca, umap, tsne 1 image present: slice1

However, when I try to convert this using the CellScopes package in Julia with the following code: import CellScopes as cs seurat_visium = cs.from_seurat("seurat_Visium.rds"; data_type="spatial", tech="visium", assay="SCT") I receive the following error:

ERROR: BoundsError Stacktrace: [1] getindex @ ~/.julia/packages/RCall/dDAVd/src/methods.jl:314 [inlined] [2] getindex(s::Ptr{RCall.S4Sxp}, sym::String) @ RCall ~/.julia/packages/RCall/dDAVd/src/methods.jl:317 [3] from_seurat(seurat_file::String; data_type="spatial", tech="visium", anno="String", assay="SCT", version="String") @ CellScopes ~/.julia/packages/CellScopes/pM38e/src/spatial/sp_utils.jl:654 [4] top-level scope @ REPL[3]:1

I have also attempted to convert a Seurat 5 Visium object and encountered similar errors. Any insights or guidance on resolving this would be greatly appreciated.

HaojiaWu commented 1 month ago

I see. Your object is from Visium, not regular scRNA. I haven't tested Visium since upgrading this function to accommodate Seurat V5. I will take a look and let you know once I have it to work for visium (and perhaps xenium as well).

HaojiaWu commented 1 month ago

@YuxiZhang-0113 @Emily-Stranger It should work for the rds file prepared by Seurat V5 now. For scRNA-seq:

seurat_sc = cs.from_seurat("rna_test_sct.rds"; 
        data_type="scRNA", 
        assay="SCT")

## or if you prepared the object from the "RNA" assay
seurat_sc = cs.from_seurat("rna_test.rds"; 
        data_type="scRNA", 
        assay="RNA")

For Visium:

visium = cs.from_seurat("visium_test.rds"; data_type="spatial", tech="visium", assay="Spatial")

For Xenium:

seurat_xenium=cs.from_seurat("xenium_test.rds"; 
    data_type="spatial",
    tech="xenium", 
    assay="Xenium")
YuxiZhang-0113 commented 1 month ago

Thank you very much for your detailed response.

Based on your previous suggestions, I generated a CSV file with meta information processed by Seurat and Scanpy and inserted it into the CellScopes object. This allows me to read Seurat information in CellScopes. However, the ability to directly convert is also significant because many authors do not provide complete datasets, for example, they may provide only location information without any images. I often use Seurat to read the data. If conversion is possible, it would enable me to utilize the powerful functionalities of CellScopes.

This leads me to another question: does CellScopes have a subset function similar to Seurat?

For example, in Seurat, I often use: ce1 <- dt@meta.data %>% as.data.frame() %>% dplyr::filter(score >= 1) %>% rownames() dt <- subset(x=dt, cells=ce1)

How can I subset the spots or barcodes I want in CellScopes?

Additionally, I think the add_xenium_img and add_visium_img functions are fantastic. I noticed that there is a DAPIASham.tif file in the Cartana data. This is my first time working with Cartana data, and there doesn't seem to be a function to load the DAPI image into a Cartana object. Or could I use Seurat to read it as a Xenium object and then convert it to CellScopes, subsequently using the add_xenium_img function? I am not sure if this is feasible.

Thank you very much for your assistance. Your efforts are greatly appreciated and make a significant difference to the community.

Best regards,