-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_TMB.R
More file actions
50 lines (44 loc) · 872 Bytes
/
extract_TMB.R
File metadata and controls
50 lines (44 loc) · 872 Bytes
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
38
39
40
41
42
43
44
45
46
47
48
49
50
#' #' Extract total mutation burden (TMB) of tumors in a maf file
#' @inheritParams extract_design
#'
#' @return
#' Returns a numeric vector providing the total
#' number of mutations observed in each tumor in maf, named
#' by the sample ids in maf.
#'
#' @examples
#' data("impact")
#' tmb <- extract_tmb(
#' maf = impact,
#' variant_col = "Variant",
#' sample_id_col = "patient_id"
#' )
#' tmb
#'
#' @export
extract_tmb <- function(
maf,
variant_col = "variant",
sample_id_col = "sample",
...
) {
dt <- as.data.table(
maf
)[,
c(sample_id_col, variant_col),
with = FALSE
]
setnames(
dt,
old = c(sample_id_col, variant_col),
new = c("p", "v")
)
p <- v <- nv <- NULL # so that CRAN doesn't complain
setkey(dt, p)
dt_tmb <- dt[
,
.(nv = length(unique(v))),
by = p
]
setNames(dt_tmb$nv, dt_tmb$p)
}