-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fig3_Pre_and_postOP.R
220 lines (183 loc) · 8.64 KB
/
Fig3_Pre_and_postOP.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#Author: Amanda Frydendahl
#load packages
library(dplyr)
library(ggplot2)
library(ggpubr)
library(scales)
library(survival)
library(survminer)
# Set working directory
mywd <- "..." #Set your working dir
setwd(mywd)
# Read data from CSV file
dta <- read.csv2(file = file.path(mywd, "ctDNA_results.csv"))
#### Pre-OP ####
#### Fig. 3a - Pre-OP detection rate ####
# Filter pre-OP samples and handle zero TumorFraction values
preOP <- dta[dta$time_category == "pre-op",]
preOP <- preOP %>% mutate(
TumorFraction = as.numeric(ifelse(TumorFraction == 0, NA, TumorFraction))
)
#plot pre-OP detection rate
preOP %>%
group_by(ctDNA_status) %>%
summarize(n=n()) %>%
mutate(freq=n/sum(n)) %>%
ggplot(., aes(x = 1, y = freq, fill = factor(ctDNA_status, levels = c("ND", "D")))) +
geom_bar(stat = "identity", col = "black") +
geom_text(position = position_fill(vjust = 0.5),
aes(label = paste0("n = ",n, "\n(", round(freq,2)*100, "%)"))) +
scale_fill_manual("", values = c("#748BAA", "#952422"),
labels = c("ctDNA-negative", "ctDNA-positive")) +
labs(x = paste0("n = ", nrow(preOP)),
y = "Detection rate") +
theme_classic() +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank())
#### Fig. 3b - Pre-OP tumor fractions ####
# Plot pre-OP tumor fractions for ctDNA positive samples
ggplot(preOP[preOP$ctDNA_status == "D",],
aes(x = 1, y = TumorFraction)) +
geom_violin(width = 0.5, fill = "#952422", alpha = 0.5) +
geom_boxplot(width= 0.2, fill = "#952422", alpha = 0.5, outlier.shape = NA) +
geom_jitter(width = 0.08) +
labs(x = paste0("n = ", nrow(preOP[!is.na(preOP$TumorFraction),])),
y = "Tumor fraction") +
scale_y_log10() +
annotation_logticks(
base = 10,
sides = "l") +
theme_classic() +
theme(legend.position = "none",
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
plot.margin = margin(r = 350))
#### Fig. 3c - Total mutational burden and pre-OP ctDNA status ####
# Read data with total mutational burden and MSI status from CSV file
TMB <- read.csv2(file = file.path(mywd, "TMB_MSIstatus.csv"))
# Merge TMB data and ctDNA results
TMB_ctDNA <- merge(preOP, TMB, by = "PtID")
# Find median TMB of pre-OP ctDNA positive and negative patients
TMB_ctDNA[TMB_ctDNA$time_category == "pre-op" & TMB_ctDNA$ctDNA_status == "D",]$TMB %>% summary()
TMB_ctDNA[TMB_ctDNA$time_category == "pre-op" & TMB_ctDNA$ctDNA_status == "ND",]$TMB %>% summary()
# For synchronous tumors, use max TMB when plotting
TMB_ctDNA <- TMB_ctDNA %>%
select(PtID, time_category, ctDNA_status, TMB, MSI_cosmic) %>%
group_by(PtID) %>%
mutate(TMB = ifelse(n() > 1, max(TMB, na.rm = TRUE), TMB)) %>%
unique()
# Plot TMB, stratified by pre-OP ctDNA status and colored by MSI status
TMB_ctDNA[TMB_ctDNA$time_category == "pre-op",] %>%
ggplot(aes(x = ctDNA_status , y = TMB)) +
geom_boxplot(outlier.shape = NA) +
geom_jitter(width = 0.2, aes(col = MSI_cosmic), size = 2) +
#scale_y_continuous(limits = c(0, 50000)) +
stat_compare_means(label.x = 1.3) +
scale_color_manual("", values = c("#952422", "black")) +
scale_x_discrete(label = c(paste0("ctDNA-positive\nn= ",
nrow(TMB_ctDNA[TMB_ctDNA$time_category == "pre-op" &
TMB_ctDNA$ctDNA_status == "D",])),
paste0("ctDNA-negative\nn= ",
nrow(TMB_ctDNA[TMB_ctDNA$time_category == "pre-op" &
TMB_ctDNA$ctDNA_status == "ND",])))) +
scale_y_continuous(trans = 'log10',
limits = c(500, 500000),
breaks = 10^(1:6),
labels = comma) +
annotation_logticks(
base = 10,
sides = "l") +
labs(title = "", y = "Total mutational burden", x = "") +
theme_bw()
#### Post-OP ####
#### Fig 3d - Post-OP detection rate ####
# Filter post-OP samples and handle zero TumorFraction values
postOP <- dta[dta$time_category == "post-op",]
postOP <- postOP %>% mutate(
TumorFraction = as.numeric(ifelse(TumorFraction == 0, NA, TumorFraction))
)
# Plot post-OP detection rate
postOP %>%
group_by(ctDNA_status, Relapse) %>%
summarize(n = n(), .groups = "drop_last") %>% # Use .groups = "drop_last" to drop the last grouping variable
mutate(freq = n / sum(n)) %>%
ggplot(., aes(x = ctDNA_status, y = freq, fill = factor(Relapse, levels = c("0", "1")))) +
geom_bar(stat = "identity", col = "black") +
geom_text(position = position_fill(vjust = 0.5),
aes(label = paste0("n = ", n, "\n(", round(freq, 2) * 100, "%)"))) +
scale_fill_manual("", values = c("white", "#6D6D6D"), labels = c("No Recurrence", "Recurrence")) +
scale_x_discrete(labels = c(paste0("ctDNA-positive\nn = ", sum(postOP$ctDNA_status == "D")),
paste0("ctDNA-negative\nn = ", sum(postOP$ctDNA_status == "ND")))) +
labs(x = "", y = "Detection rate") +
theme_classic() +
theme(legend.position = "top")
#### Fig 3E - Post-OP KM plot ####
#Calculate recurrence-free survival and make ctDNA_status binary (0,1)
#For recurrence patients: time to recurrence
#For non recurrence patients: last scan
postOP <- postOP %>%
mutate(RFS_days = ifelse(Relapse == 1, time_to_recurrence_days, last_scan)) %>%
mutate(ctDNA_status = ifelse(ctDNA_status == "ND", 0, 1))
#Plot RFS as KM plot
ggsurvplot(survfit(Surv(RFS_days/(365.25/12), Relapse) ~ ctDNA_status,
data = postOP,
conf.type=c("log-log")),
pval=T,
conf.int = T,
risk.table=T, ncensor.plot = F,
title= paste0("ctDNA post-OP"),
palette=c("#748BAA", "#952422"),
xlim = c(0, 36),
legend= "top", legend.title="", legend.labs=c("ctDNA-negative","ctDNA-positive"),
ylab="Recurrence-free survival",
risk.table.height=0.22, xlab="Time since operation (months)",
break.time.by=4, font.y = c(12, "bold", "black"))
#cox regression
c <- coxph(Surv(time = RFS_days/(365.25/12), event = Relapse)~ctDNA_status, data = postOP)
summary(c)
#### Fig 3f - Post-OP false negative rate ####
# Add postOP_category and call_category columns to the postOP dataframe
postOP <- postOP %>%
mutate(postOP_category = ifelse(sample_timepoint > 14, "Post_day14", "Pre_day14")) %>%
mutate(call_category = case_when(
ctDNA_status == 1 & Relapse == 1 ~ "TruePositive",
ctDNA_status == 1 & Relapse == 0 ~ "FalsePositive",
ctDNA_status == 0 & Relapse == 1 ~ "FalseNegative",
ctDNA_status == 0 & Relapse == 0 ~ "TrueNegative",
TRUE ~ NA_character_
))
# Create a summary dataframe (dta) for further analysis
dta <- postOP %>%
filter(call_category %in% c("FalseNegative", "TruePositive")) %>%
group_by(postOP_category, call_category) %>%
summarize(n = n(), .groups = "drop_last") %>% # Drop the last grouping variable
mutate(freq = n / sum(n) * 100)
# Calculate total number of samples drawn within 14 days post-OP
total_n_Pre_day14 <- dta %>%
filter(postOP_category == "Pre_day14") %>%
summarize(total_n = sum(n)) %>%
pull(total_n) %>%
as.integer()
# Calculate total number of samples drawn later than 14 days post-OP
total_n_Post_day14 <- dta %>%
filter(postOP_category == "Post_day14") %>%
summarize(total_n = sum(n)) %>%
pull(total_n) %>%
as.integer()
# Plot the false negative and true positive rate as piecharts
ggplot(dta, aes(x = "", y = freq,
fill = factor(call_category, levels = c("TruePositive", "FalseNegative")))) +
geom_bar(stat = "identity", width = 1, color = "black") +
coord_polar("y", start = 0) +
scale_fill_manual("", values = c("#93BEDF", "#26547C"),
labels = c("True positive rate\n(sensitivity)", "False negative rate")) +
geom_text(aes(y = freq, label = paste0(round(freq, 0), " %")),
position = position_stack(vjust = 0.5)) +
facet_wrap(~factor(postOP_category, levels = c("Pre_day14", "Post_day14")),
labeller = as_labeller(c("Pre_day14" = paste0("Expected positive samples drawn\nwithin 14 days post-OP, n = ",
total_n_Pre_day14),
"Post_day14" = paste0("Expected positive samples drawn\nlater than 14 days post-OP, n = ",
total_n_Post_day14))),
dir = "v") +
theme_void() +
theme(legend.position = "bottom")