Skip to content

Commit

Permalink
fix+feat: aggregate_by_matrix now correctly handles missing data and …
Browse files Browse the repository at this point in the history
…implements 'na.rm'
  • Loading branch information
cvanderaa committed Sep 28, 2022
1 parent bf2cddd commit d5249ca
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
40 changes: 33 additions & 7 deletions R/aggregate_by_matrix.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
#' @rdname aggregate
#'
#' @param na.rm A `logical(1)` indicating whether the missing values
#' (including NaN) should be omitted from the calculations or not.
#' Defaults to `FALSE`.
#'
#' @export
colMeansMat <- function(x, MAT)
colSumsMat(x, MAT) / Matrix::colSums(MAT)
colMeansMat <- function(x, MAT, na.rm = FALSE) {
if (na.rm) {
n <- Matrix::crossprod(MAT, !is.na(x))
} else {
n <- Matrix::colSums(MAT)
}
colSumsMat(x, MAT, na.rm = na.rm) / n
}


#' @rdname aggregate
#'
#' @export
colSumsMat <- function(x, MAT)
Matrix::crossprod(MAT, x)
colSumsMat <- function(x, MAT, na.rm = FALSE) {
if (na.rm) {
x[is.na(x)] <- 0
res <- Matrix::crossprod(MAT, x)
} else {
## Locate the missing data post-aggregation
xna <- Matrix::crossprod(MAT, is.na(x))
## Avoid NAs to propagate to the whole column
x[is.na(x)] <- 0
res <- Matrix::crossprod(MAT, x)
## Replace aggregated values that should be missing
res[xna == 1] <- NA
}
res
}



##' @param MAT An adjacency matrix that defines peptide-protein
Expand All @@ -27,8 +52,9 @@ colSumsMat <- function(x, MAT)
##'
##' @export
aggregate_by_matrix <- function(x, MAT, FUN, ...) {
if (!is.matrix(x))
stop("'x' must be a matrix.")
if (!(is.matrix(x) | inherits(x, "HDF5Matrix")))
stop("'x' must be a matrix or an object that inherits from ",
"'HDF5Matrix'.")
if (!is(MAT, "Matrix") && !is(MAT, "matrix"))
stop("'MAT' must be a matrix.")
if (!identical(nrow(MAT), nrow(x)))
Expand All @@ -39,5 +65,5 @@ aggregate_by_matrix <- function(x, MAT, FUN, ...) {
if (!is.null(colnames(x)) && !identical(colnames(x), colnames(res)))
stop("The column names of 'x' have to be identical to the column names of 'res'!")
colnames(res) <- colnames(x)
res
as.matrix(res)
}
21 changes: 8 additions & 13 deletions man/aggregate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d5249ca

Please sign in to comment.