This repository has been archived by the owner on Apr 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2022-01-14_100k_tmb_by_site.R
195 lines (156 loc) · 6.14 KB
/
2022-01-14_100k_tmb_by_site.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Description: Create plots by center and oncotree code for tumor-mutation burden
# distributions for 100k GENIE samples.
# Author: Haley Hunter-Zinck
# Date: 2022-01-14
# pre-setup ---------------------------
library(optparse)
waitifnot <- function(cond, msg) {
if (!cond) {
for (str in msg) {
message(str)
}
message("Press control-C to exit and try again.")
while(T) {}
}
}
# user input ----------------------------
option_list <- list(
make_option(c("-i", "--synid_file_input"), type = "character",
help="Synapse ID of input file"),
make_option(c("-o", "--synid_folder_output"), type = "character",
help="Synapse ID of output folder"),
make_option(c("-v", "--verbose"), action="store_true", default = FALSE,
help="Output script messages to the user.")
)
opt <- parse_args(OptionParser(option_list=option_list))
waitifnot(!is.null(opt$synid_file_input) && !is.null(opt$synid_folder_output),
msg = "Rscript template.R -h")
synid_file_input <- opt$synid_file_input
synid_folder_output <- opt$synid_folder_output
verbose <- opt$verbose
# setup ----------------------------
tic = as.double(Sys.time())
library(glue)
library(dplyr)
library(ggplot2)
library(synapser)
synLogin()
# files
synid_table_sample <- "syn7517674"
outplot <- "tmb.pdf"
# constants
BINS <- c("Low (<2)", "Mid (2-16)", "High (>16)")
# functions ----------------------------
#' Download and load data stored in csv or other delimited format on Synapse
#' into an R data frame.
#'
#' @param synapse_id Synapse ID
#' @version Version of the Synapse entity to download. NA will load current
#' version
#' @param set Delimiter for file
#' @param na.strings Vector of strings to be read in as NA values
#' @param header TRUE if the file contains a header row; FALSE otherwise.
#' @param check_names TRUE if column names should be modified for compatibility
#' with R upon reading; FALSE otherwise.
#' @return data frame
get_synapse_entity_data_in_csv <- function(synapse_id,
version = NA,
sep = ",",
na.strings = c("NA"),
header = T,
check_names = F) {
if (is.na(version)) {
entity <- synGet(synapse_id)
} else {
entity <- synGet(synapse_id, version = version)
}
data <- read.csv(entity$path, stringsAsFactors = F,
na.strings = na.strings, sep = sep, check.names = check_names,
header = header)
return(data)
}
#' Store a file on Synapse with options to define provenance.
#'
#' @param path Path to the file on the local machine.
#' @param parent_id Synapse ID of the folder or project to which to load the file.
#' @param file_name Name of the Synapse entity once loaded
#' @param prov_name Provenance short description title
#' @param prov_desc Provenance long description
#' @param prov_used Vector of Synapse IDs of data used to create the current
#' file to be loaded.
#' @param prov_exec String representing URL to script used to create the file.
#' @return Synapse ID of entity representing file
save_to_synapse <- function(path,
parent_id,
file_name = NA,
prov_name = NA,
prov_desc = NA,
prov_used = NA,
prov_exec = NA) {
if (is.na(file_name)) {
file_name = path
}
file <- File(path = path, parentId = parent_id, name = file_name)
if (!is.na(prov_name) || !is.na(prov_desc) || !is.na(prov_used) || !is.na(prov_exec)) {
act <- Activity(name = prov_name,
description = prov_desc,
used = prov_used,
executed = prov_exec)
file <- synStore(file, activity = act)
} else {
file <- synStore(file)
}
return(file$properties$id)
}
# read ----------------------------
data <- get_synapse_entity_data_in_csv(synid_file_input, sep = "\t")
query <- glue("SELECT SAMPLE_ID, ONCOTREE_CODE FROM {synid_table_sample}")
sam <- as.data.frame(synTableQuery(query, includeRowIdAndRowVersion = F))
# main ----------------------------
# join TMB and sample info
mut <- data %>%
mutate(center = unlist(lapply(strsplit(SAMPLE_ID, split = "-"), function(x) {return(x[[2]])}))) %>%
left_join(sam, by = "SAMPLE_ID")
# TMB bin by center
df_center <- mut %>%
group_by(tmb_bin, center) %>%
count()
# TMB bin by code
common_code <- as.character(unlist(mut %>%
group_by(ONCOTREE_CODE) %>%
count() %>%
filter(n > 1000 & !is.na(ONCOTREE_CODE)) %>%
select(ONCOTREE_CODE)))
df_cancer <- mut %>%
filter(is.element(ONCOTREE_CODE, common_code)) %>%
group_by(tmb_bin, ONCOTREE_CODE) %>%
count()
# plots -----------------------------
pdf(outplot)
# plot TMB bin by center
ggplot(df_center, aes(fill = factor(tmb_bin, levels = BINS), y = n, x = factor(center, levels = rev(sort(unique(center)))))) +
geom_bar(position = "fill", stat = "identity") +
labs(fill = "TMB bin") +
ylab("Fraction of samples") +
xlab("Center") +
coord_flip()
# plot TMB bin by cancer
ggplot(df_cancer, aes(fill = factor(tmb_bin, levels = BINS), y = n, x = factor(ONCOTREE_CODE, levels = rev(sort(unique(ONCOTREE_CODE)))))) +
geom_bar(position = "fill", stat = "identity") +
labs(fill = "TMB bin") +
ylab("Fraction of samples") +
xlab("OncoTree code") +
coord_flip()
graphics.off()
# write -------------------------------
synid_file_output <- save_to_synapse(path = outplot,
parent_id = synid_folder_output,
prov_name = "tmb plots",
prov_desc = "plot tmb for genie release 9.1-public by center and oncotree code",
prov_used = synid_file_input,
prov_exec = "https://github.com/hhunterzinck/genie_requests/blob/main/2022-01-14_100k_tmb_by_site.R")
file.remove(outplot)
# close out ----------------------------
print(glue("Plots loaded to {synid_file_output} as '{outplot}'."))
toc = as.double(Sys.time())
print(glue("Runtime: {round(toc - tic)} s"))