/
TMB.R
62 lines (53 loc) · 1.87 KB
/
TMB.R
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
51
52
53
54
55
56
57
58
59
60
61
62
rm(list=ls())
setwd("./rawdata/TMB/")
load("../tpm.Rdata")
library(TCGAbiolinks)
library(dplyr)
library(stringr)
projects <- getGDCprojects()$project_id
projects <- projects[grepl('^TCGA', projects, perl=TRUE)]
projects <- projects[order(projects)]
TMB=list()
for (project in projects){
query <- GDCquery(
project = project,
data.category = "Simple Nucleotide Variation",
access = "open",
data.type = "Masked Somatic Mutation",
workflow.type = "Aliquot Ensemble Somatic Variant Merging and Masking")
GDCdownload(query)
mafFilePath2 = dir(path = paste0("GDCdata/",project),
pattern = "masked.maf.gz$",full.names = T,recursive=T)
dat = lapply(mafFilePath2, data.table::fread, skip = "Hugo_Symbol")
dat = data.table::rbindlist(l = dat, use.names = TRUE, fill = TRUE)
## TMB calculation
# code reference https://zhuanlan.zhihu.com/p/394609586
get_TMB <- function(file) {
use_cols <- c("Hugo_Symbol", "Variant_Classification", "Tumor_Sample_Barcode",
"HGVSc", "t_depth", "t_alt_count")
# read file
df <- select(file, use_cols)
data <- df %>%
# calculate VAF
mutate(vaf = t_alt_count / t_depth) %>%
group_by(Tumor_Sample_Barcode) %>%
summarise(mut_num = n(), TMB = mut_num / 30, MaxVAF = max(vaf))
return(data)
}
TMB[[project]]=get_TMB(dat)
}
save(TMB,file="TMB_orig.Rdata")
TMB=as.data.frame(do.call(rbind,TMB))%>%
dplyr::filter(!duplicated(Tumor_Sample_Barcode))%>%
tibble::remove_rownames(.)%>%
tibble::column_to_rownames('Tumor_Sample_Barcode')%>%
dplyr::mutate(ID=stringr::str_sub(rownames(.),1,16))%>%
dplyr::filter(!duplicated(ID))%>%
dplyr::select(ID,TMB)%>%
dplyr::filter(ID %in% rownames(subset(tpm,Group=="Tumor")))%>%
tibble::remove_rownames(.)%>%
tibble::column_to_rownames('ID')%>%
round(2)
save(TMB,file="../TMB.Rdata")
setwd("../../")
rm(list=ls())