|
52 | 52 | batch_size = 1000L, |
53 | 53 | adjust_globally = FALSE, |
54 | 54 | min_size = 2L, |
| 55 | + max_size = Inf, |
55 | 56 | sort = TRUE, |
56 | 57 | seed = NULL, |
57 | 58 | n_genes) { |
|
103 | 104 | stop("`min_size` must be >= 2 and < nrow(X).") |
104 | 105 | } |
105 | 106 |
|
| 107 | + if ( |
| 108 | + !(is.infinite(max_size) || is.vector(max_size, mode = "numeric")) || |
| 109 | + length(max_size) != 1L || |
| 110 | + is.na(max_size) || |
| 111 | + max_size < min_size || |
| 112 | + (!is.infinite(max_size) && max_size %% 1 != 0) |
| 113 | + ) { |
| 114 | + stop("`max_size` must be Inf or an integer >= min_size.") |
| 115 | + } |
| 116 | + |
106 | 117 | if ( |
107 | 118 | !is.vector(adjust_globally, mode = "logical") || |
108 | 119 | length(adjust_globally) != 1L || |
|
319 | 330 | #' Indicates which genes are expected to be down-regulated in each sample. |
320 | 331 | #' @param min_size integer; minimum gene set size required for testing. Default |
321 | 332 | #' is 2. |
| 333 | +#' @param max_size integer or \code{Inf}; size of the largest gene set that will |
| 334 | +#' be tested. |
322 | 335 | #' |
323 | 336 | #' @returns A named list with the following components: |
324 | 337 | #' |
|
350 | 363 | Z_prime, |
351 | 364 | A, |
352 | 365 | A_d = NULL, |
353 | | - min_size = 2L) { |
| 366 | + min_size = 2L, |
| 367 | + max_size = Inf) { |
354 | 368 | # Matrix with samples as rows and genes as columns. Elements are the number |
355 | 369 | # of genes in each set with nonmissing values in the sample. |
356 | 370 | M <- .Cpp_matmult_sparse(Z_prime, A) # Z'A |
357 | 371 | M[M < min_size] <- 0L |
358 | 372 |
|
359 | | - # Identify sets with too few or too many genes in at least one sample |
360 | | - extreme_sets_M <- apply(M, 2L, function(m_j) { |
361 | | - any(m_j == 0L | m_j == n) |
362 | | - }) |
| 373 | + # It is extremely unlikely that a gene set would consist of all genes with |
| 374 | + # nonmissing values, but the set size limit will be 1 less than the smallest |
| 375 | + # number of nonmissing values. |
| 376 | + max_size <- max(min_size, min(max_size, min(n) - 1L)) |
363 | 377 |
|
364 | 378 | if (!is.null(A_d)) { |
365 | 379 | M_d <- .Cpp_matmult_sparse(Z_prime, A_d) |
366 | 380 | M_d[M_d < min_size] <- 0L |
367 | 381 |
|
368 | | - extreme_sets_M_d <- apply(M_d, 2L, function(m_d_j) { |
369 | | - any(m_d_j == 0L | m_d_j == n) |
370 | | - }) |
| 382 | + small_sets_M <- apply(M == 0L, 2L, any) |
| 383 | + small_sets_M_d <- apply(M_d == 0L, 2L, any) |
| 384 | + |
| 385 | + # Combined set size exceeds max_size |
| 386 | + large_sets <- apply((M + M_d) > max_size, 2L, any) |
371 | 387 |
|
372 | | - extreme_sets <- which(extreme_sets_M & extreme_sets_M_d) |
| 388 | + # Too small in both up and down portions or too large overall |
| 389 | + extreme_sets <- which((small_sets_M & small_sets_M_d) | large_sets) |
373 | 390 | } else { |
374 | 391 | M_d <- W_d <- NULL |
375 | 392 |
|
376 | | - extreme_sets <- which(extreme_sets_M) |
| 393 | + extreme_sets <- which( |
| 394 | + apply(M == 0 | M > max_size, 2L, any) |
| 395 | + ) |
377 | 396 | } |
378 | 397 |
|
379 | 398 | if (length(extreme_sets)) { |
380 | 399 | # If any sets are extreme, check if they are all extreme. |
381 | 400 | if (length(extreme_sets) == ncol(A)) { |
382 | 401 | stop( |
383 | | - "All sets in `gene_sets` contain fewer than `min_size` genes ", |
384 | | - "with nonmissing values or consist of all genes with nonmissing ", |
385 | | - "values in at least one sample." |
| 402 | + "All sets in `gene_sets` contain fewer than `min_size` genes or more ", |
| 403 | + "than `max_size` genes with nonmissing values." |
386 | 404 | ) |
387 | 405 | } |
388 | 406 |
|
|
0 commit comments