Closed sushilashenoy closed 7 years ago
Nevermind, I see now that this function was renamed JoinMatrices so the functionality I am looking for does not exist currently.
In case anyone else is trying to do this or if you want to add this functionality to the package, here is my quick implementation:
JoinSparseMatrices <- function(x = list()) {
if (!is.list(x) ) {
stop("Please specify a list of matrices.")
} else if ( any(sapply(x, function (y) !canCoerce(y, 'dgCMatrix'))) ) {
stop("Not all items can be coerced to dgCMatrix.")
}
# Use union of genes from each matrix
all.gene.ids <- sort(Reduce(union, lapply(x, rownames)))
# Make sure we don't have duplicate cell barcodes
all.cell.ids <- do.call(c, lapply(x, colnames))
if ( any(duplicated(all.cell.ids)) ) {
stop('Duplicate cell IDs found.')
}
# Map all matrices to the same rows (all.gene.ids)
mat.list <- lapply(x, function (mat) {
mat <- as(mat, 'dgCMatrix')
mat.sum <- summary(mat) # Get i,j,x format for sparse matrix
map.genes <- match(rownames(mat), all.gene.ids)
new.i <- map.genes[mat.sum$i]
sparseMatrix(i=new.i, j=mat.sum$j, x=mat.sum$x, dims=c(length(all.gene.ids), ncol(mat)), dimnames=list(all.genes, colnames(mat)))
})
return ( do.call(cbind, mat.list) )
}
Hi @sushilashenoy, Thanks for flagging the error in the vignette - I have fixed it. Great suggestion - I will add some more functionality to the JoinMatrices so it will take and output matrices that are sparse and dense.
Extra functionality now added to JoinMatrices.
The tutorial vignette mentions this function by name but the function does not seem to exist.
There is a function called JoinMatrices but this function doesn't do what I'm looking for - my expression matrices are sparse (class dgCMatrix) and this function wants data.frames. I'd prefer not to convert my sparse matrices to data.frames.