From ff3178eebd7e395d0bba5bbd8c3c67b9010ff47f Mon Sep 17 00:00:00 2001 From: mase5 Date: Wed, 5 Dec 2018 17:05:43 +0100 Subject: [PATCH] Fix bug when using build_loom with sparse matrices (e.g.: dgCMatrix). --- DESCRIPTION | 4 ++-- R/loom.R | 20 +++++++++++++++----- README.md | 5 ++++- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 42b6ad7..b775158 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,13 +1,13 @@ Package: SCopeLoomR Type: Package Title: Build .loom files (compatible with SCope) and extract data from .loom files. -Version: 0.3.0 +Version: 0.3.1 Author: mase5 Maintainer: mase5 Description: R package to build generic .loom files aligning with the default naming convention of the .loom format and to integrate other data types e.g.: regulons (SCENIC), clusters from Seurat, ... The package can also be used to extract data from .loom files. -Imports: hdf5r, rjson, utils, methods, base, base64enc, igraph, plyr, rlist +Imports: hdf5r, rjson, utils, methods, base, base64enc, igraph, plyr, rlist, Matrix Suggests: seurat, stringr License: Apache-2 Encoding: UTF-8 diff --git a/R/loom.R b/R/loom.R index f98d4d7..6939aa2 100644 --- a/R/loom.R +++ b/R/loom.R @@ -1162,6 +1162,7 @@ build_loom<-function(file.name , fbgn.gn.mapping.file.path = NULL , chunk.size = 1000 , display.progress = T) { + is.dgem.sparse<-F loom<-H5File$new(filename = file.name, mode = "w") tryCatch({ print("Adding global attributes...") @@ -1193,9 +1194,10 @@ build_loom<-function(file.name if(class(dgem) == "dgTMatrix") { print("Converting to dgCMatrix...") dgem<-methods::as(object = dgem, Class = "dgCMatrix") - } else if(class(dgem) == "dgTMatrix") { - print("Converting to Matrix...") - dgem<-as.matrix(x = dgem) + } + # Check if sparse matrix + if(sum(class(x = dgem) %in% c("dgCMatrix","dgTMatrix")) > 0) { + is.dgem.sparse<-T } print("Adding matrix...") add_matrix(loom = loom, dgem = dgem, chunk.size = chunk.size, display.progress = display.progress) @@ -1206,13 +1208,21 @@ build_loom<-function(file.name # Check if matrix is raw counts if(sum(dgem%%1!=0) == 0) { print("Adding default metrics nUMI...") - nUMI<-colSums(dgem) + if(is.dgem.sparse) { + nUMI<-Matrix::colSums(dgem) + } else { + nUMI<-colSums(dgem) + } add_col_attr(loom = loom, key = "nUMI", value = nUMI, as.metric = T) } else { warning("Default metric nUMI was not added because the input matrix does not seem to be the raw counts.") } print("Adding default metrics nGene...") - nGene<-colSums(dgem > 0) + if(is.dgem.sparse) { + nGene<-Matrix::colSums(dgem > 0) + } else { + nGene<-colSums(dgem > 0) + } add_col_attr(loom = loom, key = "nGene", value = nGene, as.metric = T) print("Adding default embedding...") # Add the default embedding diff --git a/README.md b/README.md index fba8901..cecbe67 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# SCopeLoomR v0.3.0 +# SCopeLoomR v0.3.1 An R package (compatible with SCope) to create generic .loom files and extend them with other data e.g.: SCENIC regulons, Seurat clusters and markers, ... The package can also be used to extract data from .loom files. @@ -36,6 +36,9 @@ You can find a tutorial on how to create .loom files and extract data from them November 8, 2018 +* Version 0.3.1 + * Fix bug when adding sparse matrices. + * Version 0.3.0 * Add feature to extract the gene expression matrix of a given cluster. * Update [tutorial](https://github.com/aertslab/SCopeLoomR/blob/master/vignettes/SCopeLoomR_tutorial.Rmd).