Just making a note of this if anyone else is encountering a similar issue.
We have been converting Scanpy AnnData objects to SCE so that we can run decontX and we were encountering the error:
ValueError: Converting pandas "Category" series to R factor is only possible when categories are strings.
After a bit of digging we found that this is because the original AnnData file we were trying to convert had the .raw.X matrix stored as a sparse float32 (and I think the sparse matrices are by definition categorical with how the data are stored under the hood).
adata.raw.X
<Compressed Sparse Row sparse matrix of dtype 'float32'
with 6558823 stored elements and shape (2343, 36397)>
Making things more challenging, you cannot directly modify adata.raw.X in scanpy to convert this to an appropriate dtype
AttributeError: property 'X' of 'Raw' object has no setter
A workaround that has got us past the issue is to create a new AnnData object with the data in the right format:
adata2 = anndata.AnnData(adata.raw.X.toarray().astype(int).astype(str))
sce = anndata2ri.py2rpy(adata2)
sce
<rpy2.robjects.methods.RS4 object at 0x00000276111B4150> [25]
R classes: ('SingleCellExperiment',)
I'm not sure if the contributors of anndata2ri want to put in dtype checks for this issue and create an if statement to perform the conversion rather than just exiting on an error (although this is potentially convoluted & memory intensive, as it would require creating the new AnnData object). Maybe/hopefully this workaround is useful to someone else either way.
Just making a note of this if anyone else is encountering a similar issue.
We have been converting Scanpy AnnData objects to SCE so that we can run decontX and we were encountering the error:
ValueError: Converting pandas "Category" series to R factor is only possible when categories are strings.After a bit of digging we found that this is because the original AnnData file we were trying to convert had the .raw.X matrix stored as a sparse float32 (and I think the sparse matrices are by definition categorical with how the data are stored under the hood).
Making things more challenging, you cannot directly modify adata.raw.X in scanpy to convert this to an appropriate dtype
AttributeError: property 'X' of 'Raw' object has no setterA workaround that has got us past the issue is to create a new AnnData object with the data in the right format:
I'm not sure if the contributors of anndata2ri want to put in dtype checks for this issue and create an if statement to perform the conversion rather than just exiting on an error (although this is potentially convoluted & memory intensive, as it would require creating the new AnnData object). Maybe/hopefully this workaround is useful to someone else either way.