zinbwave icon indicating copy to clipboard operation
zinbwave copied to clipboard

Computing observational weights in the zinbwave function using a sparse matrix

Open rfarouni opened this issue 4 years ago • 5 comments

Hi,

When I try to compute the observational weights using by supplying a fitted model object to the zinbwave function zinbwave(sce10x, fitted_model = zinb, K = 0,observationalWeights = TRUE) I get the following error

Error in t.default(x) : argument is not a matrix

Since the matrix in the SCE object is sparse, I had to convert the count matrix to dense manually

weights <- computeObservationalWeights(zinb, as.matrix(assay(sce10x)))
dimnames(weights) <- dimnames(sce10x)
assay(sce10x, "weights") <- weights

I think a one line change to the code can fix the problem.

Best, Rick

rfarouni avatar Jul 29 '20 14:07 rfarouni

I was wondering if you can give me an estimation as to how long the calculation of the zinb model should take.

In order to calculate the observationalWeights you have used the zinbFit function to create the model first and then passed it to the zinbwave function.

I'm trying to do the same with a singleCellExpreiment obejct of dim: 2000 14187 dimensions. Unfortunately it runs forever and I'm not sre whether I should stop it or let it run to the end.

Any idea, how long the model calculation should take for such an object?

thanks

yeroslaviz avatar Sep 15 '20 07:09 yeroslaviz

@rfarouni we have on our todo list to make zinbwave compatible with sparse matrices, but unfortunately I think it's not just a matter of changing one line of code. There are many internal functions that assume the matrix is dense and unless we densify the matrix right away there is no obvious quick fix. Of course I could be wrong, and I always welcome pull requests! :)

@yeroslaviz that depends on how many cores you are using and RAM you have on your machine. My bet is that your RAM memory is swapping and that's why it takes forever. It also depends on how you set K, the smaller the faster.

drisso avatar Sep 18 '20 09:09 drisso

What I'm not sure about is how is possible, that some people have this error, but others do not. I mean we all use a singleCellExperiment object. So what is the source of the error?

When I convert the Seurat object to I get a singleCellExperiment with a matrix in the assay slot

> class(assay(sce))
[1] "dgCMatrix"
attr(,"package")
[1] "Matrix"

So why do I encounter such errors?

yeroslaviz avatar Sep 18 '20 10:09 yeroslaviz

SingleCellExperiment objects can contain several type of matrix-like objects in their assay slots.

In your case, the assay contains an object of class dgCMatrix, which is not of class matrix, hence the error.

Unfortunately, at the moment, zinbwave only works with matrix inputs. A workaround is to save your object as a dense matrix, by doing assay(sce) <- as.matrix(assay(sce)). Note however that this will dramatically increase the RAM usage of your dataset, so make sure to work on a machine with enough RAM.

drisso avatar Sep 18 '20 10:09 drisso

thanks, this make sense. I'll modify the object before running the weights- and normalized values calculations.

yeroslaviz avatar Sep 18 '20 10:09 yeroslaviz