|
142 | 142 | #' |
143 | 143 | #' @author Tyler Sagendorf |
144 | 144 | #' |
145 | | -#' @importFrom data.table data.table := chmatch |
| 145 | +#' @importFrom data.table data.table := chmatch setattr |
146 | 146 | #' @importFrom Matrix sparseMatrix |
147 | 147 | #' |
148 | 148 | #' @noRd |
|
170 | 170 | stringsAsFactors = TRUE |
171 | 171 | ) |
172 | 172 |
|
173 | | - dt[, sets := rep( |
| 173 | + dt[, sets := rep.int( |
174 | 174 | names(gene_sets), |
175 | 175 | lengths(gene_sets) |
176 | 176 | )] |
|
181 | 181 | # Strip information about direction of change. This may reduce the number of |
182 | 182 | # levels if an element is both "up" and "down": "gene;u" and "gene;d" become |
183 | 183 | # "gene". |
184 | | - levels(dt$elements) <- sub(";[ud]{1}$", "", levels(dt$elements)) |
| 184 | + setattr(dt$elements, "levels", sub(";[ud]{1}$", "", levels(dt[["elements"]]))) |
185 | 185 |
|
186 | 186 | # Do not chain with previous line, since the number of levels may change. |
187 | | - unique_elements <- levels(dt$elements) |
| 187 | + unique_elements <- levels(dt[["elements"]]) |
188 | 188 |
|
189 | 189 | # Convert to characters to use chmatch() |
190 | 190 | dt[, elements := as.character.factor(elements)] |
|
202 | 202 | dt[, i := chmatch(elements, unique_elements, nomatch = 0L)] |
203 | 203 |
|
204 | 204 | # Remove elements not in the background |
205 | | - dt <- subset(dt, subset = i != 0L) |
| 205 | + dt <- dt[i != 0L] |
206 | 206 |
|
207 | | - unique_sets <- unique(dt[["sets"]]) |
| 207 | + unique_sets <- unique.default(dt[["sets"]]) |
208 | 208 |
|
209 | 209 | # Column indices for sparse matrix |
210 | 210 | dt[, j := chmatch(sets, unique_sets)] |
|
213 | 213 | dims <- lengths(dim_names) |
214 | 214 |
|
215 | 215 | # Keep genes expected to be "up" |
216 | | - dt_u <- subset(dt, subset = direction_down == FALSE) |
| 216 | + dt_u <- dt[direction_down == FALSE] |
217 | 217 |
|
218 | 218 | # Incidence matrix where a 1 indicates that the element is in the set. If x |
219 | 219 | # is a directional database, then A will only contain elements that are |
|
231 | 231 | # In the unlikely event where an element appears multiple times in the same |
232 | 232 | # set, some values of A will be > 1. Replace all values with 1. Could also |
233 | 233 | # use the use.last.ij parameter in sparseMatrix(), but this is faster. |
234 | | - attr(A, which = "x") <- rep(1, length(attr(A, which = "x"))) |
| 234 | + attr(A, which = "x") <- rep.int(1, length(attr(A, which = "x"))) |
235 | 235 |
|
236 | 236 | A_d <- NULL # default |
237 | 237 |
|
238 | 238 | if (nrow(dt_u) < nrow(dt)) { |
239 | | - dt_d <- subset(dt, subset = direction_down == TRUE) |
| 239 | + dt_d <- dt[direction_down == TRUE] |
240 | 240 |
|
241 | 241 | # Incidence matrix where a 1 indicates that a feature is expected to be |
242 | 242 | # down in the set. |
|
250 | 250 | use.last.ij = FALSE |
251 | 251 | ) |
252 | 252 |
|
253 | | - attr(A_d, which = "x") <- rep(1, length(attr(A_d, which = "x"))) |
| 253 | + attr(A_d, which = "x") <- rep.int(1, length(attr(A_d, which = "x"))) |
254 | 254 |
|
255 | 255 | # The Hadamard product A * A.d should be a matrix of zeros |
256 | 256 | if (length(attr(A * A_d, which = "x"))) { |
|
617 | 617 | n_batches <- ceiling(nperm / batch_size) |
618 | 618 |
|
619 | 619 | batch_sizes <- c( |
620 | | - rep(batch_size, n_batches - 1L), |
| 620 | + rep.int(batch_size, n_batches - 1L), |
621 | 621 | nperm - batch_size * (n_batches - 1L) |
622 | 622 | ) |
623 | 623 |
|
624 | | - batch_id <- rep(seq_len(n_batches), batch_sizes) |
| 624 | + batch_id <- rep.int(seq_len(n_batches), batch_sizes) |
625 | 625 |
|
626 | 626 | # Seeds for permutations |
627 | 627 | set.seed(seed) |
|
684 | 684 | #' \describe{ |
685 | 685 | #' \item{"rep_idx"}{a vector with length \eqn{\geq} \code{ncol(A_perm)} that |
686 | 686 | #' maps each row of \code{A_perm} to the corresponding entry of \code{m_i}. |
687 | | -#' This is used by \code{.extractPermInfo}.} |
| 687 | +#' This is used by \code{.makeResultsTable}.} |
688 | 688 | #' |
689 | 689 | #' \item{"A_perm"}{dense incidence matrix where the number of rows is the |
690 | 690 | #' number of unique gene set sizes and the number of columns is the size of |
|
811 | 811 | } |
812 | 812 |
|
813 | 813 |
|
814 | | -#' @title Extract Information from a Permutation Enrichment Score Matrix |
815 | | -#' |
816 | | -#' @description Extract information from a matrix of permutation enrichment |
817 | | -#' scores run as a single batch. |
818 | | -#' |
819 | | -#' @param ES_ls list of enrichment scores grouped by gene set size. |
820 | | -#' @param ES_perm lis of permutation ES. The length of the list is equal to the |
821 | | -#' length of \code{ES}, while the length of each vector is at most the total |
822 | | -#' number of permutations: more likely, it is a fraction of the total number |
823 | | -#' of permutations. See the \code{batch_size} parameter of |
824 | | -#' \code{\link{fast_ssgsea}} for more details. |
825 | | -#' |
826 | | -#' @returns A \code{data.table} with 3 columns: |
827 | | -#' |
828 | | -#' \describe{ |
829 | | -#' \item{"n_same_sign_b"}{integer; the number of permutation ES in each |
830 | | -#' row of \code{ES_perm} with the same sign as the corresponding ES in |
831 | | -#' \code{ES}.} |
832 | | -#' \item{"n_as_extreme_b"}{integer; the number of permutation ES in |
833 | | -#' each row of \code{ES_perm} that were at least as extreme as the |
834 | | -#' corresponding ES in \code{ES}. At most \code{"n_same_sign_b"}.} |
835 | | -#' \item{"sum_ES_perm_b"}{integer; the sum of the absolute values of the |
836 | | -#' permutation ES that have the same sign as the corresponding ES in |
837 | | -#' \code{ES}.} |
838 | | -#' } |
839 | | -#' |
840 | | -#' @author Tyler Sagendorf |
841 | | -#' |
842 | | -#' @importFrom data.table data.table := setorderv rbindlist |
843 | | -#' |
844 | | -#' @noRd |
845 | | -.extractPermInfo <- function(ES_ls, |
846 | | - ES_perm) { |
847 | | - out <- lapply(seq_along(ES_ls), function(i) { |
848 | | - ES_i <- ES_ls[[i]] |
849 | | - |
850 | | - ES_perm_i <- ES_perm[i, , drop = TRUE] |
851 | | - |
852 | | - out_i <- .Rcpp_extractPermInfo(ES_i, ES_perm_i) # returns list |
853 | | - class(out_i) <- "data.table" |
854 | | - |
855 | | - return(out_i) |
856 | | - }) |
857 | | - |
858 | | - out <- rbindlist(out) |
859 | | - |
860 | | - return(out) |
861 | | -} |
862 | | - |
863 | | - |
864 | 814 | #' @title Generate ssGSEA Results Table for a Single Sample |
865 | 815 | #' |
866 | 816 | #' @inheritParams fast_ssgsea |
|
924 | 874 | #' |
925 | 875 | #' @author Tyler Sagendorf |
926 | 876 | #' |
927 | | -#' @importFrom data.table data.table := setorderv |
| 877 | +#' @importFrom data.table data.table := setorderv rbindlist |
928 | 878 | #' |
929 | 879 | #' @noRd |
930 | 880 | .makeResultsTable <- function(alpha = 1, |
|
948 | 898 | ES = ES_i, |
949 | 899 | # Initialize vectors of 0's. These 3 vectors will be updated using the |
950 | 900 | # results from each batch of permutations. |
951 | | - n_same_sign = rep(0L, length(ES_i)), |
952 | | - n_as_extreme = rep(0L, length(ES_i)), |
953 | | - sum_ES_perm = rep(0, length(ES_i)), |
| 901 | + n_same_sign = rep.int(0L, length(ES_i)), |
| 902 | + n_as_extreme = rep.int(0L, length(ES_i)), |
| 903 | + sum_ES_perm = rep.int(0, length(ES_i)), |
954 | 904 | row_order = seq_along(ES_i), |
955 | 905 | stringsAsFactors = FALSE |
956 | 906 | ) |
|
999 | 949 | theta_w_d_i = theta_w_d_i |
1000 | 950 | ) |
1001 | 951 |
|
1002 | | - perm_dt <- .extractPermInfo( |
| 952 | + perm_dt <- .Rcpp_extractPermInfo( |
1003 | 953 | ES_ls = ES_ls, |
1004 | 954 | ES_perm = ES_perm |
1005 | 955 | ) |
1006 | 956 |
|
| 957 | + perm_dt <- rbindlist(perm_dt) |
| 958 | + |
1007 | 959 | # Update summary vectors |
1008 | 960 | tab_i[, `:=`( |
1009 | 961 | n_same_sign = n_same_sign + perm_dt[["n_same_sign_b"]], |
|
0 commit comments