bnprks / BPCells

Scaling Single Cell Analysis to Millions of Cells
https://bnprks.github.io/BPCells
Other
166 stars 17 forks source link

For `RowBindMatrices`, `[<-` method of both indices error #100

Closed Yunuuuu closed 4 months ago

Yunuuuu commented 4 months ago
mock_matrix <- function(ngenes, ncells) {
    cell.means <- 2^stats::runif(ngenes, 2, 10)
    cell.disp <- 100 / cell.means + 0.5
    cell.data <- matrix(stats::rnbinom(ngenes * ncells,
        mu = cell.means,
        size = 1 / cell.disp
    ), ncol = ncells)
    rownames(cell.data) <- sprintf("Gene_%s", formatC(seq_len(ngenes),
        width = 4, flag = 0
    ))
    colnames(cell.data) <- sprintf("Cell_%s", formatC(seq_len(ncells),
        width = 3, flag = 0
    ))
    cell.data
}
mat1 <- mock_matrix(30, 20)
mat2 <- mock_matrix(30, 20)
path <- normalizePath(c(tempfile(), tempfile()), mustWork = FALSE)
mat <- rbind(mat1, mat2)
mat1 <- BPCells::write_matrix_dir(as(mat1, "dgCMatrix"), dir = path[1L])
#> Warning: Matrix compression performs poorly with non-integers.
#> • Consider calling convert_matrix_type if a compressed integer matrix is intended.
#> This message is displayed once every 8 hours.
mat2 <- BPCells::write_matrix_dir(as(mat2, "dgCMatrix"), dir = path[2L])
obj <- rbind(mat1, mat2)
# if we omit this line, it'll work fine
obj <- BPCells::convert_matrix_type(obj, "uint32_t")
values <- matrix(sample(mat, length(mat)), nrow = nrow(mat))
# this'll be okay
# obj[1:10, ] <- values[1:10, ]
# this'll be okay
# obj[, 1:10] <- values[, 1:10]
obj[1:10, 1:10] <- values[1:10, 1:10]
#> Warning: Converting input matrix type to match destination
#> • input type: double
#> • destination type: uint32_t
as.matrix(obj)
#> Warning: Converting to a dense matrix may use excessive memory
#> This message is displayed once every 8 hours.
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'as.matrix': invalid class "dgTMatrix" object: length of Dimnames[[2]] (10) is not equal to Dim[2] (20)

Created on 2024-07-04 with reprex v2.1.0 ~
~
~
~
~
~
~
~
~
~

bnprks commented 4 months ago

Thanks again for the issue reporting @Yunuuuu, very helpful! I think this should be fixed in main now with pull #101.