BayraktarLab / cell2location

Comprehensive mapping of tissue cell architecture via integrated single cell and spatial transcriptomics (cell2location model)
https://cell2location.readthedocs.io/en/latest/
Apache License 2.0
321 stars 58 forks source link

How can I use cell2location output for downstream analysis in R Studio #369

Open 532at opened 5 months ago

532at commented 5 months ago

Please use the template below to post a question to https://discourse.scverse.org/c/ecosytem/cell2location/.

Problem

Hi, since I was working with R Studio before doing cell2location deconvolution, I was wondering if it is possible to do cell2location deconvolution and use its output for further analysis in R Studio. If yes, how to do it?

I was looking for a file I could use after running the tutorial, but I cannot see anything useful for annotating cell type proportions to Visium spots. Specifically, I looked at the three folders: cell_type_fractions_mean; factor_markers and location_factor_mean, but they seem wrong as there are 11 factors and in the tutorial data is much more cell types?

Running the tutorial, I experienced an error (below), could it be a reason behind it? As I don't see any quantile cell abundance files?

TypeError Traceback (most recent call last) Cell In[30], line 1 ----> 1 adata_ref = mod.export_posterior( 2 adata_ref, use_quantiles=True, 3
4 add_to_obsm=["q05","q50", "q95", "q0001"], 5 sample_kwargs={'batch_size': 2500, 'use_gpu': False} 6 )

TypeError: export_posterior() got an unexpected keyword argument 'add_to_obsm'

Best wishes, Adrian

vitkl commented 5 months ago

The reason behind the error is likely a package version mismatch. I recommend creating a conda environment from scratch.

You can save results from h5ad anndata object and load them into R. Here is the pd.DataFrame you are looking for:

adata_vis.obsm['q05_cell_abundance_w_sf'].to_csv('./q05_cell_abundance_w_sf.csv')
532at commented 5 months ago

Thank you, I will update you when it is done.