File: pool_mut_matrix.R

package info (click to toggle)
r-bioc-mutationalpatterns 3.16.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,360 kB
  • sloc: sh: 8; makefile: 2
file content (37 lines) | stat: -rw-r--r-- 1,206 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#' Pool multiple samples from a mutation matrix together
#'
#' The mutation counts of columns (samples) are added up according to the grouping variable.
#'
#' @param mut_matrix Mutation count matrix (dimensions: x mutation types
#' X n samples)
#' @param grouping Grouping variable
#'
#' @return Mutation count matrix (dimensions: x mutation types
#' X n groups)
#' @export
#' @importFrom magrittr %>%
#'
#' @examples
#' ## See the 'mut_matrix()' example for how we obtained the mutation matrix:
#' mut_mat <- readRDS(system.file("states/mut_mat_data.rds",
#'   package = "MutationalPatterns"
#' ))
#' grouping <- c(rep("colon", 3), rep("intestine", 3), rep("liver", 3))
#' pool_mut_mat(mut_mat, grouping)
pool_mut_mat <- function(mut_matrix, grouping) {
  # These variables use non standard evaluation.
  # To avoid R CMD check complaints we initialize them to NULL.
  . <- NULL

  grouping <- factor(grouping)
  mut_mat_group <- mut_matrix %>%
    t(.) %>%
    tibble::as_tibble() %>%
    dplyr::mutate(factor = grouping) %>%
    dplyr::group_by(factor) %>%
    dplyr::summarise_all(sum) %>%
    dplyr::select(-factor) %>%
    t(.)
  colnames(mut_mat_group) <- levels(grouping)
  return(mut_mat_group)
}