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

Can't find SCE #150

Closed eblchen closed 6 months ago

eblchen commented 8 months ago

This may be something obvious I'm missing, but I can't seem to get the local convertor working: image It can't seem to find the SCE data loaded into Python.

I had been using the method outlines in the Usage from Jupyter, but this also seems to be having issues:

%%R -o cell_line_sce
library(scater)
cell_line_sce <- readRDS("ut_sce_cell_lines.rds")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/.conda/envs/anndata/lib/python3.11/site-packages/rpy2/rinterface_lib/conversion.py:179, in _get_cdata(obj)
    178 try:
--> 179     cdata = obj.__sexp__._cdata
    180 except AttributeError:

AttributeError: 'numpy.str_' object has no attribute '__sexp__'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
Cell In[16], line 1
----> 1 get_ipython().run_cell_magic('R', '-o cell_line_sce', '#library(scater)\n\ncell_line_sce <- readRDS("ut_sce_cell_lines.rds")\n')

File ~/.conda/envs/anndata/lib/python3.11/site-packages/IPython/core/interactiveshell.py:2517, in InteractiveShell.run_cell_magic(self, magic_name, line, cell)
   2515 with self.builtin_trap:
   2516     args = (magic_arg_s, cell)
-> 2517     result = fn(*args, **kwargs)
   2519 # The code below prevents the output from being displayed
   2520 # when using magics with decorator @output_can_be_silenced
   2521 # when the last Python token in the expression is a ';'.
   2522 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File ~/.conda/envs/anndata/lib/python3.11/site-packages/rpy2/ipython/rmagic.py:935, in RMagics.R(self, line, cell, local_ns)
    927                 if self.cache_display_data:
    928                     stack.enter_context(
    929                         rpy2.rinterface_lib
    930                         .callbacks
   (...)
    933                                        'consolewrite_print',
    934                                        self.write_console_regular))
--> 935                 cell_display(result, args)
    936                 text_output += self.flush()
    938 except RInterpreterError as e:
    939     # TODO: Maybe we should make this red or something?

File ~/.conda/envs/anndata/lib/python3.11/site-packages/rpy2/ipython/rmagic.py:134, in CELL_DISPLAY_DEFAULT(res, args)
    133 def CELL_DISPLAY_DEFAULT(res, args):
--> 134     return ro.r.show(res)

File ~/.conda/envs/anndata/lib/python3.11/site-packages/rpy2/robjects/functions.py:208, in SignatureTranslatedFunction.__call__(self, *args, **kwargs)
    206         v = kwargs.pop(k)
    207         kwargs[r_k] = v
--> 208 return (super(SignatureTranslatedFunction, self)
    209         .__call__(*args, **kwargs))

File ~/.conda/envs/anndata/lib/python3.11/site-packages/rpy2/robjects/functions.py:131, in Function.__call__(self, *args, **kwargs)
    129     else:
    130         new_kwargs[k] = cv.py2rpy(v)
--> 131 res = super(Function, self).__call__(*new_args, **new_kwargs)
    132 res = cv.rpy2py(res)
    133 return res

File ~/.conda/envs/anndata/lib/python3.11/site-packages/rpy2/rinterface_lib/conversion.py:45, in _cdata_res_to_rinterface.<locals>._(*args, **kwargs)
     44 def _(*args, **kwargs):
---> 45     cdata = function(*args, **kwargs)
     46     # TODO: test cdata is of the expected CType
     47     return _cdata_to_rinterface(cdata)

File ~/.conda/envs/anndata/lib/python3.11/site-packages/rpy2/rinterface.py:807, in SexpClosure.__call__(self, *args, **kwargs)
    804 error_occured = _rinterface.ffi.new('int *', 0)
    805 with memorymanagement.rmemory() as rmemory:
    806     call_r = rmemory.protect(
--> 807         _rinterface.build_rcall(self.__sexp__._cdata, args,
    808                                 kwargs.items()))
    809     call_context = evaluation_context.get()
    810     res = rmemory.protect(
    811         openrlib.rlib.R_tryEval(
    812             call_r,
    813             call_context.__sexp__._cdata,
    814             error_occured)
    815     )

File ~/.conda/envs/anndata/lib/python3.11/site-packages/rpy2/rinterface_lib/_rinterface_capi.py:317, in build_rcall(rfunction, args, kwargs)
    315 item = rlib.CDR(rcall)
    316 for val in args:
--> 317     cdata = rmemory.protect(conversion._get_cdata(val))
    318     rlib.SETCAR(item, cdata)
    319     item = rlib.CDR(item)

File ~/.conda/envs/anndata/lib/python3.11/site-packages/rpy2/rinterface_lib/conversion.py:181, in _get_cdata(obj)
    179         cdata = obj.__sexp__._cdata
    180     except AttributeError:
--> 181         raise ValueError('Not an rpy2 R object and unable '
    182                          'to map it to one: %s' % repr(obj))
    183 else:
    184     cdata = cls(obj)

ValueError: Not an rpy2 R object and unable to map it to one: 'value'

Also I have tried with different SCE objects, including one that I had been able to convert to AnnData in the past, and I still get the same errors.

flying-sheep commented 6 months ago

Hi, sorry for missing this.

Please check out the rpy2 tutorial especially the part about the “global environment”: https://rpy2.github.io/doc/v3.5.x/html/introduction.html#the-r-instance

cell_line_sce is a Python variable that R doesn’t know about. You need to either run cell_line_sce <- readRDS(...) in R or use robjects.globalenv["cell_line_sce"] = ... in Python to move the object over.