Skip to content

Commit

Permalink
Fix bug when using build_loom with sparse matrices (e.g.: dgCMatrix).
Browse files Browse the repository at this point in the history
  • Loading branch information
mase5 committed Dec 5, 2018
1 parent b999d1e commit ff3178e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
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
Expand Down
20 changes: 15 additions & 5 deletions R/loom.R
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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).
Expand Down

0 comments on commit ff3178e

Please sign in to comment.