Skip to content
14 changes: 14 additions & 0 deletions scprep/run/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ def _pysce2rpy(pyobject):
return pyobject


def _rpyspmatrix2py(robject):
if utils._try_import("anndata2ri"):
robject = anndata2ri.scipy2ri.rpy2py(robject)
return robject


def _pyspmatrix2rpy(pyobject):
if utils._try_import("anndata2ri"):
pyobject = anndata2ri.scipy2ri.py2rpy(pyobject)
return pyobject


def _is_r_object(obj):
return "rpy2.robjects" in str(type(obj)) or "rpy2.rinterface" in str(type(obj))

Expand Down Expand Up @@ -75,6 +87,7 @@ def rpy2py(robject):
"""
for converter in [
_rpynull2py,
_rpyspmatrix2py,
_rpysce2py,
rpy2.robjects.pandas2ri.rpy2py,
_rpylist2py,
Expand Down Expand Up @@ -118,6 +131,7 @@ def py2rpy(pyobject):
"""
for converter in [
_pynull2rpy,
_pyspmatrix2rpy,
_pysce2rpy,
rpy2.robjects.pandas2ri.py2rpy,
rpy2.robjects.numpy2ri.py2rpy,
Expand Down
2 changes: 2 additions & 0 deletions test/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,8 @@ def test_conversion_spmatrix():
assert isinstance(x, scipy.sparse.csc_matrix)
assert x.shape == (2, 3)
assert np.all(x.toarray() == np.array([[1, 3, 5], [2, 4, 6]]))
y = scprep.run.conversion.py2rpy(x)
assert (scprep.run.conversion.rpy2py(y) != x).nnz == 0

def test_conversion_dataframe():
x = scprep.run.conversion.rpy2py(
Expand Down