theislab / anndata2ri

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

TypeError when following sc-tutorial #58

Open mshirazi opened 3 years ago

mshirazi commented 3 years ago

Hi, I've been running through the sc-tutorial using the most recent versions of Scanpy and AnnData (anndata 0.7.4 & scanpy 1.6.0 & anndata2ri 1.0.5). I ran into an issue when trying to run the Slingshot code (similar to this issue on the sc-tutorial Github: https://github.com/theislab/single-cell-tutorial/issues/49). It's also similar to issue #50 but the error is different, and I never activate numpy2ri. It's not a problem with the location of the graph data in adata either, since I get the same error with a test script, and even get the same error before I start the subsetting. When I run

%%R -i adata_ent
print('test')

I get the error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-45-f526c490bacf> in <module>
----> 1 get_ipython().run_cell_magic('R', '-i adata_ent', "print('test')\n")

~/opt/anaconda3/envs/sc-tutorial/lib/python3.8/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2379             with self.builtin_trap:
   2380                 args = (magic_arg_s, cell)
-> 2381                 result = fn(*args, **kwargs)
   2382             return result
   2383 

<decorator-gen-121> in R(self, line, cell, local_ns)

~/opt/anaconda3/envs/sc-tutorial/lib/python3.8/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

~/opt/anaconda3/envs/sc-tutorial/lib/python3.8/site-packages/rpy2/ipython/rmagic.py in R(self, line, cell, local_ns)
    735                         raise NameError("name '%s' is not defined" % input)
    736                 with localconverter(converter) as cv:
--> 737                     ro.r.assign(input, val)
    738 
    739         if args.display:

~/opt/anaconda3/envs/sc-tutorial/lib/python3.8/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs)
    195                 v = kwargs.pop(k)
    196                 kwargs[r_k] = v
--> 197         return (super(SignatureTranslatedFunction, self)
    198                 .__call__(*args, **kwargs))
    199 

~/opt/anaconda3/envs/sc-tutorial/lib/python3.8/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs)
    124                 new_kwargs[k] = conversion.py2rpy(v)
    125         res = super(Function, self).__call__(*new_args, **new_kwargs)
--> 126         res = conversion.rpy2py(res)
    127         return res
    128 

~/opt/anaconda3/envs/sc-tutorial/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/anaconda3/envs/sc-tutorial/lib/python3.8/site-packages/anndata2ri/r2py.py in rpy2py_s4(obj)
     25         return rpy2py_data_frame(obj)
     26     elif "SingleCellExperiment" in r_classes:
---> 27         return rpy2py_single_cell_experiment(obj)
     28     elif supported_r_matrix_classes() & r_classes:
     29         return rmat_to_spmat(obj)

~/opt/anaconda3/envs/sc-tutorial/lib/python3.8/site-packages/anndata2ri/r2py.py in rpy2py_single_cell_experiment(obj)
     82 
     83     # TODO: Once the AnnData bug is fixed, remove the “or None”
---> 84     return AnnData(exprs, obs, var, uns, obsm or None, layers=layers)

~/opt/anaconda3/envs/sc-tutorial/lib/python3.8/site-packages/anndata/_core/anndata.py in __init__(self, X, obs, var, uns, obsm, varm, layers, raw, dtype, shape, filename, filemode, asview, obsp, varp, oidx, vidx)
    305             self._init_as_view(X, oidx, vidx)
    306         else:
--> 307             self._init_as_actual(
    308                 X=X,
    309                 obs=obs,

~/opt/anaconda3/envs/sc-tutorial/lib/python3.8/site-packages/anndata/_core/anndata.py in _init_as_actual(self, X, obs, var, uns, obsm, varm, varp, obsp, raw, layers, dtype, shape, filename, filemode)
    514 
    515         # Backwards compat for connectivities matrices in uns["neighbors"]
--> 516         _move_adj_mtx({"uns": self._uns, "obsp": self._obsp})
    517 
    518         self._check_dimensions()

~/opt/anaconda3/envs/sc-tutorial/lib/python3.8/site-packages/anndata/compat/__init__.py in _move_adj_mtx(d)
    153         if (
    154             (k in n)
--> 155             and isinstance(n[k], (spmatrix, np.ndarray))
    156             and len(n[k].shape) == 2
    157         ):

~/opt/anaconda3/envs/sc-tutorial/lib/python3.8/site-packages/rpy2/robjects/vectors.py in __getitem__(self, i)
    263 
    264     def __getitem__(self, i):
--> 265         res = super().__getitem__(i)
    266 
    267         if isinstance(res, Sexp):

~/opt/anaconda3/envs/sc-tutorial/lib/python3.8/site-packages/rpy2/rinterface_lib/sexp.py in __getitem__(self, i)
    628             )
    629         else:
--> 630             raise TypeError(
    631                 'Indices must be integers or slices, not %s' % type(i))
    632         return res

TypeError: Indices must be integers or slices, not <class 'str'>

I tried running the example code

import scanpy.datasets as scd 
adata_paul = scd.paul15()
%%R -i adata_paul
print('test')

which worked without any warning messages, so I don't think it's a problem with importing and activating the anndata2ri package. I tried backtracking to see when the error started appearing, since when I try pulling adata into R right after I create it, it doesn't throw any errors. This exact error message appears after trying to use adata in R in the following part of the tutorial:

sc.pp.highly_variable_genes(adata, flavor='cell_ranger', n_top_genes=4000)
print('\n','Number of highly variable genes: {:d}'.format(np.sum(adata.var['highly_variable'])))

but not right after running combat

# ComBat batch correction
sc.pp.combat(adata, key='sample')

However, I do get the following warning message when testing using R with adata right after the ComBat batch correction:

/Users/Maddie/opt/anaconda3/envs/sc-tutorial/lib/python3.8/site-packages/anndata2ri/py2r.py:38: NotConvertedWarning: Conversion 'py2rpy' not defined for objects of type '<class 'NoneType'>'
  warn(str(e), NotConvertedWarning)

It runs error free right before the ComBat batch correction.

Let me know if there's any other information that would help, and thanks in advance!

flying-sheep commented 3 years ago

Hmm, @ivirshup any idea why that might happen?

Looks like self._uns['neighbors'] is a R vector for some reason.

ivirshup commented 3 years ago

I'm not sure I get what needs to happen for this bug to come up. When did self._uns['neighbors'] become an R vector? Is there a mini-reproducer for this?

mshirazi commented 3 years ago

I've managed to reproduce the error with the paul dataset: after importing and activating all the packages in python and R, just running

import scanpy.datasets as scd
adata_paul = scd.paul15()
%%R -i adata_paul
print('test')
sc.pp.highly_variable_genes(adata_paul, flavor='cell_ranger', n_top_genes=4000)
print('\n','Number of highly variable genes: {:d}'.format(np.sum(adata_paul.var['highly_variable'])))
# Calculate the visualizations
sc.pp.pca(adata_paul, n_comps=50, use_highly_variable=True, svd_solver='arpack')
sc.pp.neighbors(adata_paul)

sc.tl.tsne(adata_paul, n_jobs=12) #Note n_jobs works for MulticoreTSNE, but not regular implementation)
sc.tl.umap(adata_paul)
sc.tl.diffmap(adata_paul)
sc.tl.draw_graph(adata_paul)
%%R -i adata_paul
print('test')

First one works, second one throws an error. Also tried running this after:

adata_paul.uns['neighbors']['connectivities'] = adata_paul.obsp['connectivities']
adata_paul.uns['neighbors']['distances'] = adata_paul.obsp['distances']
del adata_paul.obsp
%%R -i adata_paul
print('test')

And it throws the same error. Not sure if the problem in these mini-examples is all of the steps I cut out, or if that's the proper way to get rid of a field, but it seemed to work when I checked what was in adata_paul.

MugenQin commented 1 year ago

Hi, I had the same problem as you reported above. I have no idea how to change my Anndata. I wanna know whether you solve this error and how you pass it?