theislab / anndata2ri

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

localconverter error #61

Open le-ander opened 3 years ago

le-ander commented 3 years ago

Hi Phil!

I just tried to convert my jupyter anndata2ri usage to a normal python script (i.e. using localconverter rather than ipython cell magics). I am, however, hitting an unexpected error.

So this works fine:

import anndata2ri
anndata2ri.activate()
%load_ext rpy2.ipython
%%R -o sce
library(Seurat)
load("tissue.rdata")
new_obj = CreateSeuratObject(counts = tissue@raw.data)
new_obj@meta.data = tissue@meta.data
sce <- as.SingleCellExperiment(new_obj)

sce in my python namespace is a now a neat AnnData object, as expected. However trying to do the same with locaconverter gives me an error:

import anndata2ri
from rpy2.robjects import r
from rpy2.robjects.conversion import localconverter

with localconverter(anndata2ri.converter):
    adata = r('''
library(Seurat)
load("tissue.rdata")
new_obj = CreateSeuratObject(counts = tissue@raw.data)
new_obj@meta.data = tissue@meta.data
as.SingleCellExperiment(new_obj)
''')
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-2-fc7a6d222ec5> in <module>
      4 
      5 with localconverter(anndata2ri.converter):
----> 6     adata = r('''
      7 library(Seurat)
      8 load("/storage/groups/ml01/datasets/projects/20200101_Various_SfairaDataRepository_leander.dony/raw/d10_1038_s41586_019_1631_3/tissue.rdata")

/opt/python/lib/python3.8/site-packages/rpy2/robjects/__init__.py in __call__(self, string)
    436     def __call__(self, string):
    437         p = rinterface.parse(string)
--> 438         res = self.eval(p)
    439         return conversion.rpy2py(res)
    440 

/opt/python/lib/python3.8/site-packages/rpy2/robjects/__init__.py in __getattribute__(self, attr)
    411 
    412         try:
--> 413             return self.__getitem__(attr)
    414         except LookupError:
    415             raise AttributeError(orig_ae)

/opt/python/lib/python3.8/site-packages/rpy2/robjects/__init__.py in __getitem__(self, item)
    417     def __getitem__(self, item):
    418         res = _globalenv.find(item)
--> 419         res = conversion.rpy2py(res)
    420         if hasattr(res, '__rname__'):
    421             res.__rname__ = item

/opt/python/lib/python3.8/functools.py in wrapper(*args, **kw)
    873                             '1 positional argument')
    874 
--> 875         return dispatch(args[0].__class__)(*args, **kw)
    876 
    877     funcname = getattr(func, '__name__', 'singledispatch function')

/opt/python/lib/python3.8/site-packages/rpy2/robjects/conversion.py in _rpy2py(obj)
    198     non-rpy2) objects.
    199     """
--> 200     raise NotImplementedError(
    201         "Conversion 'rpy2py' not defined for objects of type '%s'" %
    202         str(type(obj))

NotImplementedError: Conversion 'rpy2py' not defined for objects of type '<class 'rpy2.rinterface.SexpClosure'>'
le-ander commented 3 years ago

This is anndata2ri 1.0.6 and rpy2 3.4.2