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

Unable to load Seurat library after activating anndata2ri #138

Closed jchen80 closed 10 months ago

jchen80 commented 10 months ago

Hi,

I'm trying to run the tutorial in Jupyter notebook. I ran into the situation where I can successfully install and load Seurat within jupyter after running %load_ext rpy2.ipython. However, if I first run anndata2ri.activate() before loading the extension, then I get the following error when I attempt to load in the Seurat library.

%%R 
library(Seurat)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/miniconda3/envs/bio/lib/python3.10/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)
lab/Ithaka/E1 scRNAseq analysis/integrate.ipynb Cell 4 line 1
----> get_ipython().run_cell_magic('R', '', 'library("Seurat")\n')

File ~/miniconda3/envs/bio/lib/python3.10/site-packages/IPython/core/interactiveshell.py:2493, in InteractiveShell.run_cell_magic(self, magic_name, line, cell)
   2491 with self.builtin_trap:
   2492     args = (magic_arg_s, cell)
-> 2493     result = fn(*args, **kwargs)
   2495 # The code below prevents the output from being displayed
   2496 # when using magics with decorator @output_can_be_silenced
   2497 # when the last Python token in the expression is a ';'.
   2498 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File ~/miniconda3/envs/bio/lib/python3.10/site-packages/rpy2/ipython/rmagic.py:935, in RMagics.R(self, line, cell, local_ns)
...
    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'

Any idea what might be the issue? Also are there alternative ways of invoking anndata2ri within the Jupyter notebook without needing to globally activate it before loading the extension?

Thanks!

flying-sheep commented 10 months ago

Also are there alternative ways of invoking anndata2ri within the Jupyter notebook without needing to globally activate it before loading the extension?

yes, see e.g. the first example from the README:

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

with localconverter(anndata2ri.converter):
    adata = r('as(some_data, "SingleCellExperiment")')

you can also use the -c parameter for the R cell magic:

%% R -c anndata2ri.converter
do_stuff <- in_r

Finally, once I get around to it, I will implement a more convenient way to do this, see https://github.com/theislab/anndata2ri/issues/109

flying-sheep commented 10 months ago

I closed the issue, since not using activate is the future, so #109 covers this