Skip to content

Commit acaf1d1

Browse files
authored
Merge pull request #35 from mojaveazure/develop
SeuratObject v4.0.4
2 parents dc2f740 + 9cac32d commit acaf1d1

12 files changed

+197
-16
lines changed

DESCRIPTION

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: SeuratObject
22
Type: Package
33
Title: Data Structures for Single Cell Data
4-
Version: 4.0.3
5-
Date: 2021-11-10
4+
Version: 4.0.4
5+
Date: 2021-11-23
66
Authors@R: c(
77
person(given = 'Rahul', family = 'Satija', email = '[email protected]', role = 'aut', comment = c(ORCID = '0000-0001-9448-8833')),
88
person(given = 'Andrew', family = 'Butler', email = '[email protected]', role = 'aut', comment = c(ORCID = '0000-0003-3608-0463')),
@@ -22,14 +22,14 @@ Description: Defines S4 classes for single-cell genomic data and associated
2222
Macosko E, Basu A, Satija R, et al (2015) <doi:10.1016/j.cell.2015.05.002>,
2323
and Stuart T, Butler A, et al (2019) <doi:10.1016/j.cell.2019.05.031> for
2424
more details.
25-
URL: https://satijalab.org/seurat,
25+
URL: https://mojaveazure.github.io/seurat-object/,
2626
https://github.com/mojaveazure/seurat-object
2727
BugReports:
2828
https://github.com/mojaveazure/seurat-object/issues
2929
License: MIT + file LICENSE
3030
Encoding: UTF-8
3131
LazyData: true
32-
RoxygenNote: 7.1.1
32+
RoxygenNote: 7.1.2
3333
Depends:
3434
R (>= 4.0.0)
3535
Imports:

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ S3method(Cells,DimReduc)
4242
S3method(Cells,Neighbor)
4343
S3method(Cells,SpatialImage)
4444
S3method(Cells,default)
45+
S3method(CheckMatrix,dMatrix)
46+
S3method(CheckMatrix,default)
47+
S3method(CheckMatrix,lMatrix)
4548
S3method(Command,Seurat)
4649
S3method(CreateSeuratObject,Assay)
4750
S3method(CreateSeuratObject,default)
@@ -159,6 +162,7 @@ export(Cells)
159162
export(CellsByIdentities)
160163
export(CheckDots)
161164
export(CheckGC)
165+
export(CheckMatrix)
162166
export(Command)
163167
export(CreateAssayObject)
164168
export(CreateDimReducObject)

NEWS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# SeuratObject 4.0.4
2+
## Changed
3+
- `CreateSeuratObject.Assay` sets Assay key when not present (#29)
4+
- Ignore warnings when creating an `Assay` from a data frame (#32)
5+
6+
## Added
7+
- New `CheckMatrix` generic for validating expression matrices
8+
19
# SeuratObject 4.0.3
210

311
## Changed

R/assay.R

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Assay <- setClass(
6767
#' new object with a lower cutoff.
6868
#' @param min.features Include cells where at least this many features are
6969
#' detected.
70+
#' @param check.matrix Check counts matrix for NA, NaN, Inf, and non-integer values
7071
#' @param ... Arguments passed to \code{\link{as.sparse}}
7172
#'
7273
#' @return A \code{\link{Assay}} object
@@ -93,6 +94,7 @@ CreateAssayObject <- function(
9394
data,
9495
min.cells = 0,
9596
min.features = 0,
97+
check.matrix = FALSE,
9698
...
9799
) {
98100
if (missing(x = counts) && missing(x = data)) {
@@ -127,7 +129,14 @@ CreateAssayObject <- function(
127129
stop("No feature names (rownames) names present in the input matrix")
128130
}
129131
if (!inherits(x = counts, what = 'dgCMatrix')) {
130-
counts <- as.sparse(x = counts, ...)
132+
if (inherits(x = counts, what = "data.frame")) {
133+
counts <- as.sparse(x = counts, ...)
134+
} else {
135+
counts <- as.sparse(x = counts)
136+
}
137+
}
138+
if (isTRUE(x = check.matrix)) {
139+
CheckMatrix(object = counts)
131140
}
132141
# Filter based on min.features
133142
if (min.features > 0) {

R/seurat.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,10 @@ CreateSeuratObject.Assay <- function(
10561056
meta.data <- new.meta.data
10571057
}
10581058
}
1059+
# Check assay key
1060+
if (!length(x = Key(object = counts)) || !nchar(x = Key(object = counts))) {
1061+
Key(object = counts) <- UpdateKey(key = tolower(x = assay))
1062+
}
10591063
assay.list <- list(counts)
10601064
names(x = assay.list) <- assay
10611065
# Set idents

R/utils.R

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,33 @@ as.sparse <- function(x, ...) {
2525
UseMethod(generic = 'as.sparse', object = x)
2626
}
2727

28+
#' Check Matrix Validity
29+
#'
30+
#' @param object A matrix
31+
#' @param checks Type of checks to perform, choose one or more from:
32+
#' \itemize{
33+
#' \item \dQuote{\code{infinite}}: Emit a warning if any value is infinite
34+
#' \item \dQuote{\code{logical}}: Emit a warning if any value is a logical
35+
#' \item \dQuote{\code{integer}}: Emit a warning if any value is \emph{not}
36+
#' an integer
37+
#' \item \dQuote{\code{na}}: Emit a warning if any value is an \code{NA}
38+
#' or \code{NaN}
39+
#' }
40+
#' @param ... Arguments passed to other methods
41+
#'
42+
#' @return Emits warnings for each test and invisibly returns \code{NULL}
43+
#'
44+
#' @name CheckMatrix
45+
#' @rdname CheckMatrix
46+
#'
47+
#' @keywords internal
48+
#'
49+
#' @export
50+
#'
51+
CheckMatrix <- function(object, checks, ...) {
52+
UseMethod(generic = 'CheckMatrix', object = object)
53+
}
54+
2855
#' S4/List Conversion
2956
#'
3057
#' Convert S4 objects to lists and vice versa. Useful for declassing an S4
@@ -436,6 +463,58 @@ as.sparse.Matrix <- function(x, ...) {
436463
#'
437464
as.sparse.matrix <- as.sparse.Matrix
438465

466+
#' @rdname CheckMatrix
467+
#' @method CheckMatrix default
468+
#' @export
469+
#'
470+
CheckMatrix.default <- function(object, checks, ...) {
471+
return(invisible(x = NULL))
472+
}
473+
474+
#' @rdname CheckMatrix
475+
#' @method CheckMatrix dMatrix
476+
#' @export
477+
#'
478+
CheckMatrix.dMatrix <- function(
479+
object,
480+
checks = c('infinite', 'logical', 'integer', 'na'),
481+
...
482+
) {
483+
checks <- match.arg(arg = checks, several.ok = TRUE)
484+
x <- slot(object = object, name = 'x')
485+
for (i in checks) {
486+
switch(
487+
EXPR = i,
488+
'infinite' = if (any(is.infinite(x = x))) {
489+
warning("Input matrix contains infinite values")
490+
},
491+
'logical' = if (any(is.logical(x = x))) {
492+
warning("Input matrix contains logical values")
493+
},
494+
'integer' = if (!all(round(x = x) == x, na.rm = TRUE)) {
495+
warning("Input matrix contains non-integer values")
496+
},
497+
'na' = if (anyNA(x = x)) {
498+
warning("Input matrix contains NA/NaN values")
499+
},
500+
)
501+
}
502+
return(invisible(x = NULL))
503+
}
504+
505+
#' @rdname CheckMatrix
506+
#' @method CheckMatrix lMatrix
507+
#' @export
508+
#'
509+
CheckMatrix.lMatrix <- function(
510+
object,
511+
checks = c('infinite', 'logical', 'integer', 'na'),
512+
...
513+
) {
514+
warning("Input matrix contains logical values")
515+
return(invisible(x = NULL))
516+
}
517+
439518
#' @importFrom methods slotNames
440519
#'
441520
#' @rdname s4list

cran-comments.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SeuratObject v4.0.3
1+
# SeuratObject v4.0.4
22

33
## Test environments
44
* local ubuntu 20.04 install, R 4.1.1

man/CheckMatrix.Rd

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/CreateAssayObject.Rd

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/SeuratObject-package.Rd

Lines changed: 2 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/RcppExports.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
using namespace Rcpp;
88

9+
#ifdef RCPP_USE_GLOBAL_ROSTREAM
10+
Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
11+
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
12+
#endif
13+
914
// GraphToNeighborHelper
1015
List GraphToNeighborHelper(Eigen::SparseMatrix<double> mat);
1116
RcppExport SEXP _SeuratObject_GraphToNeighborHelper(SEXP matSEXP) {

tests/testthat/test_matrix.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Tests for matrix checks
2+
3+
data("pbmc_small")
4+
5+
pbmc_small <- suppressWarnings(suppressMessages(UpdateSeuratObject(pbmc_small)))
6+
7+
counts <- GetAssayData(pbmc_small, assay = "RNA", slot = "counts")
8+
9+
test_that("CheckMatrix works for valid matrix", {
10+
expect_null(CheckMatrix(counts))
11+
})
12+
13+
test_that("CheckMatrix warns for non-integer counts", {
14+
counts[1,1] <- 1/2
15+
expect_warning(CheckMatrix(counts))
16+
})
17+
18+
test_that("CheckMatrix warns for NA values", {
19+
counts[1,1] <- NA
20+
expect_warning(CheckMatrix(counts))
21+
})
22+
23+
test_that("CheckMatrix warns for NaN values", {
24+
counts[1,1] <- NaN
25+
expect_warning(CheckMatrix(counts))
26+
})
27+
28+
test_that("CheckMatrix warns for logical values", {
29+
counts <- as(object = counts, Class = "lsparseMatrix")
30+
expect_warning(CheckMatrix(counts))
31+
})

0 commit comments

Comments
 (0)