theislab / anndata2ri

Convert between AnnData and SingleCellExperiment
https://icb-anndata2ri.readthedocs-hosted.com/
GNU General Public License v3.0
124 stars 16 forks source link

Missing layers after conversion from SCE #136

Closed shaln closed 11 months ago

shaln commented 11 months ago

I am trying to convert an SCE object to Anndata, and noticed that the 'logcounts' assay in the SCE object failed to transfer over to the layers of the Anndata. I'm not sure why this is the case, as using another package for the conversion (sceasy) did not result in this problem. I have attached below the code I used.

import scanpy as sc
import numpy as np
import anndata2ri
import anndata as ad
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.colors as mcolors

# Activate the anndata2ri conversion between SingleCellExperiment and AnnData
anndata2ri.activate()

# Loading the rpy2 extension enables cell magic to be used
# This runs R code in jupyter notebook cells
%load_ext rpy2.ipython

sc.settings.verbosity = 3
sc.logging.print_versions()

%%R -o control_sce
suppressPackageStartupMessages(library(SingleCellExperiment))

# Load SingleCellExperiment object that was previously created when running SlingShot
control_sce <- readRDS("RDS Files/control.int.sce.RDS")
control_sce
class: SingleCellExperiment 
dim: 2002 10194 
metadata(0):
assays(1): logcounts
rownames(2002): Ccl21a Fabp4 ... TdTomato-UTR Pecam1
rowData names(0):
colnames(10194): Cont1_AAACCTGCAAGTTAAG-1 Cont1_AAACCTGGTATGAAAC-1 ...
  ContB_TTTGTCATCGACAGCC-1 ContB_TTTGTCATCTTCGGTC-1
colData names(29): sum detected ... subclusters.sample ident
reducedDimNames(2): PCA UMAP
mainExpName: integrated
altExpNames(0):

control_sce

AnnData object with n_obs × n_vars = 10194 × 2002
    obs: 'sum', 'detected', 'percent_top_50', 'percent_top_100', 'percent_top_200', 'percent_top_500', 'subsets_Mito_sum', 'subsets_Mito_detected', 'subsets_Mito_percent', 'total', 'discard', 'orig.ident', 'condition', 'experiment', 'RNA_snn_res.0.1', 'seurat_clusters', 'cell.type', 'nCount_RNA', 'nFeature_RNA', 'integrated_snn_res.0.1', 'celltype', 'celltype.orig', 'celltype.cond', 'integrated_snn_res.0.2', 'integrated_snn_res.0.3', 'subclusters', 'subclusters.cond', 'subclusters.sample', 'ident'
    obsm: 'X_pca', 'X_umap'

As you can see above, the 'logcounts' assay is present in the SCE object, but there aren't any 'layers' in the AnnData object. Any idea what might have gone wrong here and how to fix this? In case this is helpful to know, the SCE object was previously a Seurat object that I had converted using the as.SingleCellExperiment function in R.

I've also tried using the sceasy package to convert the Seurat object to AnnData in R instead. This way the logcounts assay were successfully imported into the AnnData layers. I imported this AnnData directly into Jupyter for trajectory analysis using PAGA but obtained slightly different plots even though it's the exact same data - will open a separate issue for this on the PAGA page and comment below to link the issues.

flying-sheep commented 11 months ago

The first assay gets stored in .X:

https://github.com/theislab/anndata2ri/blob/093ef078a1e08f6b7b029be3dad273ab59e9d275/src/anndata2ri/_r2py.py#L102-L107

Once we do https://github.com/scverse/anndata/issues/244, this should no longer be a problem

I hope this answered your question. If you have suggestions on how to better document this or improve anndata2ri’s behavior, please comment, and we can reopen this issue.