samuel-marsh / scCustomize

R package with collection of functions created and/or curated to aid in the visualization and analysis of single-cell data using R.
https://samuel-marsh.github.io/scCustomize/
GNU General Public License v3.0
183 stars 19 forks source link

conversion to anndata #188

Open Flu09 opened 3 weeks ago

Flu09 commented 3 weeks ago

Not sure what is the issue here

as.anndata(x = obj2, file_path = "/tmp", file_name = "obj_anndata.h5ad") • Checking Seurat object validity & Extracting Data Warning: No layers found matching search pattern provided Error in as.anndata(): ! main_layer must be one of counts Run rlang::last_trace() to see where the error occurred. as.anndata(x = obj2, file_name = "obj_anndata.h5ad") Error in as.anndata.Seurat(x = obj2, file_name = "obj_anndata.h5ad") : argument "file_path" is missing, with no default obj2 An object of class Seurat 40090 features across 3600 samples within 1 assay Active assay: RNA (40090 features, 0 variable features) 1 layer present: counts

marlmatos commented 1 week ago

also having the same issue

samuel-marsh commented 1 week ago

Hi @Flu09 @matosmr

Apologies for the delay responding! Could you both try running the function using the following code and Seurat data package:

pbmc <- pbmc3k.SeuratData::pbmc3k.final
pbmc <- UpdateSeuratObject

Then run the conversion with identical code that is failing with your own objects (just swap out the object) in code.

Will help to figure out whether it is object or object format issue vs. function issue.

Thanks! Sam

joshuak94 commented 1 day ago

Hello Sam! Just wanted to say I'm having the same issue.

With the pbmc dataset it seems to work. The dataset where it doesn't work, however is the MOCA dataset (https://oncoscape.v3.sttrcancer.org/atlas.gs.washington.edu.mouse.rna/downloads)

Here is my code:

library(Seurat)
library(scCustomize)

data = readRDS("~/gene_count_cleaned.RDS")
metadata = read.csv("/~cell_annotate.csv")

metadata_filtered = metadata[which(metadata$sample %in% colnames(data)), ]

data_seurat = CreateSeuratObject(data, meta.data = metadata_filtered)

as.anndata(x = data_seurat, file_path = "/project/MDL_JoshKim_Data/vista_sc_240706/moca/", file_name = "gene_count_cleaned.h5ad")

The error message:

• Checking Seurat object validity & Extracting Data
Warning message:
“No layers found matching search pattern provided”

Error in `as.anndata()`:
! `main_layer` must be one of counts
Traceback:

1. as.anndata(x = data_seurat, file_path = "[~/](http://localhost:8890/lab/tree/notebooks/moca/)", 
 .     file_name = "gene_count_cleaned.h5ad")
2. as.anndata.Seurat(x = data_seurat, file_path = "[~/](http://localhost:8890/lab/tree/notebooks/moca/)", 
 .     file_name = "gene_count_cleaned.h5ad")
3. cli_abort(message = "{.code main_layer} must be one of {.field {main_approved_slots}}")
4. rlang::abort(message, ..., call = call, use_cli_format = TRUE, 
 .     .frame = .frame)
5. signal_abort(cnd, .file)

Seurat version 5.1.0

When executing the following:

Layers(data_seurat, search = c("counts", "data"))

I get 'counts' as an answer.

So I see here:

https://github.com/samuel-marsh/scCustomize/blob/fc7a282af3bef6e1cb816fac3b9229f536159238/R/Object_Conversion.R#L1053

That main_layer must be in the layers. But main_layer is 'data' and I only have 'counts'. And I guess this makes sense, because I am downloading/using raw count values, and not normalized data. But it would be nice to make work with raw count data as well.

samuel-marsh commented 11 hours ago

Hi All (@Flu09 @marlmatos @joshuak94)

Sorry again for delays. Could you all please try updating to dev release prep version:

devtools::install_github(repo = "samuel-marsh/scCustomize", ref = "release/2.2.0")

If that doesn’t solve the issue please let me know!

Best, Sam