Search code examples
python-3.xbioinformaticsrpy2

Create named list from matrix using rpy2


I have a 2D numpy array which I converted to R matrix and now I need to convert it further to named list:

rpy2.robjects.numpy2ri.activate()
nr,nc = counts.shape
r_mtx = robjects.r.matrix(counts, nrow=nr, ncol=nc)

So, I got the matrix r_mtx, but I am not sure how to make a named list out of it similar to how we do it in R:

 named_list <- list(counts=mtx)

I need it to feed into SingleCellExperiment object to do dataset normalization:

https://bioconductor.org/packages/devel/bioc/vignettes/scran/inst/doc/scran.html

I tried using rpy2.rlike.container both TaggedList and OrdDict but can't figure out how to apply them to my case.


Solution

  • Ultimately I solved it (avoiding convertion of numpy array to r matrix), straight making the named list from the numpy array:

    named_list = robjects.r.list(counts=counts)
    

    Where counts is a 2D numpy array