Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dgRMatrix to dgCMatrix Conversion #55

Closed
const-ae opened this issue Jun 21, 2021 · 3 comments
Closed

dgRMatrix to dgCMatrix Conversion #55

const-ae opened this issue Jun 21, 2021 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@const-ae
Copy link

Hi Luke,

when I tried to load the https://data.caltech.edu/tindfiles/serve/f0d567c5-cea6-4a60-923e-e9fb4a4019e8/ dataset (from Svensson 2019 Nature Biotech Supplement), I get the following error:

>   se <- zellkonverter::readH5AD("../data/klein_2015.h5ad")
Error in as(mat, "dgCMatrix") : 
  no method or default for coercing "dgRMatrix" to "dgCMatrix"

The error occurs here, and I think it is related to the discussion in #34 and the fix in 732789e

Unfortunately, it turns out that you cannot directly coerce a dgRMatrix to a dgCMatrix, but for some weird reason you have to coerce it to CsparseMatrix:

library(Matrix)
library(methods)
mat <- matrix(0, nrow = 10, ncol = 3)
mat[c(1, 14, 17)] <- 1:3
mat
#>       [,1] [,2] [,3]
#>  [1,]    1    0    0
#>  [2,]    0    0    0
#>  [3,]    0    0    0
#>  [4,]    0    2    0
#>  [5,]    0    0    0
#>  [6,]    0    0    0
#>  [7,]    0    3    0
#>  [8,]    0    0    0
#>  [9,]    0    0    0
#> [10,]    0    0    0
row_sp_mat <- as(mat, "dgRMatrix")
as(row_sp_mat, "dgCMatrix")
#> Error in as(row_sp_mat, "dgCMatrix"): no method or default for coercing "dgRMatrix" to "dgCMatrix"
as(row_sp_mat, "CsparseMatrix")
#> 10 x 3 sparse Matrix of class "dgCMatrix"
#>            
#>  [1,] 1 . .
#>  [2,] . . .
#>  [3,] . . .
#>  [4,] . 2 .
#>  [5,] . . .
#>  [6,] . . .
#>  [7,] . 3 .
#>  [8,] . . .
#>  [9,] . . .
#> [10,] . . .

Created on 2021-06-21 by the reprex package (v2.0.0)

Currently, I can work around the issue by defining

setAs("dgRMatrix", to = "dgCMatrix", function(from){
  as(as(from, "CsparseMatrix"), "dgCMatrix")
})

but that is, of course, inconvenient for the average user.

Best,
Constantin

@lazappi lazappi added the bug Something isn't working label Jun 22, 2021
@lazappi lazappi self-assigned this Jun 22, 2021
@lazappi
Copy link
Member

lazappi commented Jun 22, 2021

Hi @const-ae

Thanks for the issue. This has been reported before but I haven't quite got around to fixing it. Will make it a priority now, should be pretty quick.

@const-ae
Copy link
Author

Great. Thanks for the quick fix :)

@lazappi
Copy link
Member

lazappi commented Jun 22, 2021

Should be fixed on release and devel now. Give Bioconductor a couple of days or install from GitHub if you want it right away.

laurenhsu1 added a commit to laurenhsu1/CellChat that referenced this issue May 22, 2024
I got an error when trying to feed the createCellChat function a SingleCellExperiment containing dgRMatrix format in logcounts. The error occurs in trying to coerce a dgRMatrix to dgCMatrix, but it works to coerce to "CsparseMatrix", which ultimately results in a dgCMatrix. 

Supposedly this issue has been resolved in zellkonverter a couple years ago, (theislab/zellkonverter#55), but I just ran into this today with data imported with zellkonverter which was a dgRMatrix, and my packages are up to date. I then found a couple other issues with discussion of this particular conversion problem, so I would suggest making this small update so that it will seamlessly work with dgRMatrix input too! 
theislab/zellkonverter#34
satijalab/seurat#6438
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants