theislab/anndata2ri

unintuitive mapping for `adata.var`

Closed this issue · 3 comments

As per https://github.com/theislab/anndata2ri/blob/master/anndata2ri/py2r.py#L63 it appears obs should map to colData and var should map to rowData, but instead it appears var maps to rowRanges@elementMetadata.

import numpy as np
import rpy2.robjects as ro
import anndata
import anndata2ri

X = np.random.poisson(0.2, (500, 100))
adata = anndata.AnnData(X)
adata.obs['libsize'] = adata.X.sum(axis=1)
adata.var['n_counts'] = adata.X.sum(axis=0)

anndata2ri.activate()
ro.globalenv['sce'] = adata

ro.r('sce@colData')
# pandas DataFrame with 'libsize' defined
ro.r('sce@rowData')
# R[write to console]: Error in (new("nonstandardGenericFunction", .Data = function (expr, envir = parent.frame(),  :
#   no slot of name "rowData" for this object of class "SingleCellExperiment"
ro.r('sce@rowRanges@elementMetadata')
# pandas DataFrame with 'n_counts' defined

And actually just adding to this -- it might be good to describe the mapping in the README. E.g. it's not clear where obsm goes and it would be nice to have a table that describes each of the pieces

AnnData SingleCellExperiment
X assays@X
layers['counts'] assays@counts
var['n_counts'] rowData$n_counts
obs['n_counts'] colData$n_counts
obsm['pca']
... ...

Well, you should probably use SCE functions instead of accessing implementation details via @. You inspired me to at least get the docs working, which was hard enough due to rpy2: #56

The docs now start with a small table like yours, check it out!