This is a test version of Biostars. For the public version, visit https://www.biostars.org.
RangedSummarizedExperiment to Sparse Matrix

Looking to make a sparse matrix from a RangedSummarizedExperiment object with with regions as rows and cells as columns. The corresponding cell and region names must be provided as list using the parameters cell_names and region_names, respectively. Not quite sure how to do this, any help much appreciated! Thanks!

dgcmatrix sparse rangedsummarizedexperiment matrix

1 answer

The typical way of converting matrix-like objects to a dgCMatrix is as(x, "CsparseMatrix") (formerly as(x, "dgCMatrix")).

Get the assay from the rangedSE with assay(rse, assayName) and then run the above conversion.

So the column names seem to come out as the individual cells (this is converted from an ArchR object) however Im wondering how to change the rownames to the ranges from the rangedSE. Want to use this matrix as an input to pycisTopic which requires one of the two following:

pycisTopic

> assay(test)
109254 x 526 sparse Matrix of class "dgCMatrix"
  [[ suppressing 78 column names 'Klf4_KO#TAATTCCTCACAAGCT-1', 'Klf4_KO#GGGTCTGGTCAACTGT-1', 'Klf4_KO#CCCGTTATCTATTTCG-1' ... ]]
  [[ suppressing 78 column names 'Klf4_KO#TAATTCCTCACAAGCT-1', 'Klf4_KO#GGGTCTGGTCAACTGT-1', 'Klf4_KO#CCCGTTATCTATTTCG-1' ... ]]

[1,] 2 . . . . . . . . . 2 2 . . 2 2 . 2 . . . . . . . . . . . . . . . . . . . . . 2 . . . . . . . . . . . . . . . 2 . . . . . . . . . . . . . . . . . . . . . . ......
[2,] . . . . 2 . 2 . . . 2 . . . . . . 1 . . . . . . . . . . . . . . 2 . . . . . . . 1 . . . . . . . . . . 2 . . . . . 4 . . 2 2 . . . 4 . . 2 . . 2 . 2 . . . . ......
[3,] 4 4 4 2 4 4 2 4 4 . . 4 2 . 4 4 . 4 2 1 4 . 2 . 4 2 4 4 . 4 . . 4 4 4 4 2 . 4 4 . 3 2 4 4 . 4 4 . . 1 4 4 2 . . 2 2 4 2 . 4 . . . . 4 . 4 . 4 4 4 . 2 3 . . ......
[4,] . . . . . . . 2 . . . . . . . . . . . . . . . . . . . . 2 . . . . . . . . . . . . . . . . . . . . . 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
[5,] . . . . . . . 4 . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . 2 . . 2 . . . . . . . 1 . 4 2 . . . . . . . . . . 2 . . . . . . . . . . . . . ......
[6,] . 2 4 . . . 3 . . . . . . . . . . 4 4 4 . . . 3 . 2 . . . . 2 . 2 . 2 . . . . . . . . . 2 . . . . . 2 2 4 4 . . . 3 3 . . 2 . 2 . 1 4 . 2 . . . 1 1 . . 2 . ......

Thanks again for your help!

If you have the ranges then just paste them to a string, something like "chr:start-end" and put that as rownames.

library(SummarizedExperiment)

nrows <- 200; ncols <- 6
counts <- matrix(runif(nrows * ncols, 1, 1e4), nrows)
rowRanges <- GRanges(rep(c("chr1", "chr2"), c(50, 150)),
                     IRanges(floor(runif(200, 1e5, 1e6)), width=100),
                     strand=sample(c("+", "-"), 200, TRUE),
                     feature_id=sprintf("ID%03d", 1:200))
colData <- DataFrame(Treatment=rep(c("ChIP", "Input"), 3),
                     row.names=LETTERS[1:6])
rse <- SummarizedExperiment(assays=SimpleList(counts=counts),
                            rowRanges=rowRanges, colData=colData)

chr <- as.character(seqnames(rowRanges(rse)))
starts <- start(rse)
ends <- end(rse)
new_rownames <- paste(chr, paste(starts, ends, sep="-"), sep=":")

rownames(rse) <- new_rownames

Log in to answer this question.