Skip to content

Commit ff3178e

Browse files
author
mase5
committed
Fix bug when using build_loom with sparse matrices (e.g.: dgCMatrix).
1 parent b999d1e commit ff3178e

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Package: SCopeLoomR
22
Type: Package
33
Title: Build .loom files (compatible with SCope) and extract data from .loom files.
4-
Version: 0.3.0
4+
Version: 0.3.1
55
Author: mase5
66
Maintainer: mase5 <mdewaegeneer@gmail.com>
77
Description: R package to build generic .loom files aligning with the default naming convention of the .loom format and
88
to integrate other data types e.g.: regulons (SCENIC), clusters from Seurat, ... The package can also be used to extract
99
data from .loom files.
10-
Imports: hdf5r, rjson, utils, methods, base, base64enc, igraph, plyr, rlist
10+
Imports: hdf5r, rjson, utils, methods, base, base64enc, igraph, plyr, rlist, Matrix
1111
Suggests: seurat, stringr
1212
License: Apache-2
1313
Encoding: UTF-8

R/loom.R

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,7 @@ build_loom<-function(file.name
11621162
, fbgn.gn.mapping.file.path = NULL
11631163
, chunk.size = 1000
11641164
, display.progress = T) {
1165+
is.dgem.sparse<-F
11651166
loom<-H5File$new(filename = file.name, mode = "w")
11661167
tryCatch({
11671168
print("Adding global attributes...")
@@ -1193,9 +1194,10 @@ build_loom<-function(file.name
11931194
if(class(dgem) == "dgTMatrix") {
11941195
print("Converting to dgCMatrix...")
11951196
dgem<-methods::as(object = dgem, Class = "dgCMatrix")
1196-
} else if(class(dgem) == "dgTMatrix") {
1197-
print("Converting to Matrix...")
1198-
dgem<-as.matrix(x = dgem)
1197+
}
1198+
# Check if sparse matrix
1199+
if(sum(class(x = dgem) %in% c("dgCMatrix","dgTMatrix")) > 0) {
1200+
is.dgem.sparse<-T
11991201
}
12001202
print("Adding matrix...")
12011203
add_matrix(loom = loom, dgem = dgem, chunk.size = chunk.size, display.progress = display.progress)
@@ -1206,13 +1208,21 @@ build_loom<-function(file.name
12061208
# Check if matrix is raw counts
12071209
if(sum(dgem%%1!=0) == 0) {
12081210
print("Adding default metrics nUMI...")
1209-
nUMI<-colSums(dgem)
1211+
if(is.dgem.sparse) {
1212+
nUMI<-Matrix::colSums(dgem)
1213+
} else {
1214+
nUMI<-colSums(dgem)
1215+
}
12101216
add_col_attr(loom = loom, key = "nUMI", value = nUMI, as.metric = T)
12111217
} else {
12121218
warning("Default metric nUMI was not added because the input matrix does not seem to be the raw counts.")
12131219
}
12141220
print("Adding default metrics nGene...")
1215-
nGene<-colSums(dgem > 0)
1221+
if(is.dgem.sparse) {
1222+
nGene<-Matrix::colSums(dgem > 0)
1223+
} else {
1224+
nGene<-colSums(dgem > 0)
1225+
}
12161226
add_col_attr(loom = loom, key = "nGene", value = nGene, as.metric = T)
12171227
print("Adding default embedding...")
12181228
# Add the default embedding

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SCopeLoomR v0.3.0
1+
# SCopeLoomR v0.3.1
22
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
33
data from .loom files.
44

@@ -36,6 +36,9 @@ You can find a tutorial on how to create .loom files and extract data from them
3636

3737
November 8, 2018
3838

39+
* Version 0.3.1
40+
* Fix bug when adding sparse matrices.
41+
3942
* Version 0.3.0
4043
* Add feature to extract the gene expression matrix of a given cluster.
4144
* Update [tutorial](https://github.com/aertslab/SCopeLoomR/blob/master/vignettes/SCopeLoomR_tutorial.Rmd).

0 commit comments

Comments
 (0)