Skip to content

Commit fa7c433

Browse files
freeseekanashen
authored andcommitted
added function readMM_fast
1 parent e44cb2c commit fa7c433

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

R/preprocessing.R

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ globalVariables(
1212
package = 'Seurat',
1313
add = TRUE
1414
)
15+
#' read a MatrixMarket file using data.table::fread which is much faster than Matrix:readMM
16+
#'
17+
#' @param filename Path to MatrixMarket file
18+
#'
19+
#' @return Returns a sparse matrix in coordinate format
20+
#'
21+
#' @author Giulio Genovese, \email{[email protected]}
22+
#''
23+
readMM_fast <- function(file) {
24+
has_dt <- requireNamespace("data.table", quietly = TRUE) && requireNamespace("R.utils", quietly = TRUE)
25+
if (has_dt) {
26+
dt <- data.table::fread(file, header = TRUE, skip = 1, colClasses = c("integer", "integer", "numeric"), quote = "", data.table = FALSE)
27+
data <- new("dgTMatrix", i = dt[,1] - 1L, j = dt[,2] - 1L, x = dt[,3], Dim = as.integer(c(names(dt)[1], names(dt)[2])))
28+
} else {
29+
data <- Matrix::readMM(file)
30+
}
31+
return(data)
32+
}
33+
1534
#' Calculate the Barcode Distribution Inflection
1635
#'
1736
#' This function calculates an adaptive inflection point ("knee") of the barcode distribution
@@ -813,7 +832,7 @@ LoadCurioSeeker <- function(data.dir, assay = "Spatial") {
813832
coordinates.file <- coordinates.file[1]
814833

815834
# load counts matrix and create seurat object
816-
mtx <- readMM(mtx.file)
835+
mtx <- readMM_fast(mtx.file)
817836
mtx <- as.sparse(mtx)
818837
barcodes <- read.csv(barcodes.file, header = FALSE)
819838
genes <- read.csv(genes.file, header = FALSE)
@@ -1010,7 +1029,7 @@ Read10X <- function(
10101029
if (!file.exists(matrix.loc)) {
10111030
stop("Expression matrix file missing. Expecting ", basename(path = matrix.loc))
10121031
}
1013-
data <- readMM(file = matrix.loc)
1032+
data <- readMM_fast(file = matrix.loc)
10141033
if (has_dt) {
10151034
cell.barcodes <- as.data.frame(data.table::fread(barcode.loc, header = FALSE))
10161035
} else {
@@ -1880,7 +1899,7 @@ ReadMtx <- function(
18801899
if (unique.features) {
18811900
feature.names <- make.unique(names = feature.names)
18821901
}
1883-
data <- readMM(file = all.files[['expression matrix']])
1902+
data <- readMM_fast(file = all.files[['expression matrix']])
18841903
if (mtx.transpose) {
18851904
data <- t(x = data)
18861905
}

0 commit comments

Comments
 (0)