-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatistical_analyses_intra_host_diversity_Costanzi_et_al.R
531 lines (345 loc) · 18.1 KB
/
Statistical_analyses_intra_host_diversity_Costanzi_et_al.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
rm(list=ls())
par(mfrow=c(1,1))
setwd("")
source("HighstatLibV6.R")
# detach("package:ape", unload = TRUE)
library("ggplot2")
library("ggpubr")
library("ggeffects")
library("purrr")
library("tidyr")
library("stringr")
library("reshape2")
library("insect")
library("seqinr")
library("adegenet")
library("tools")
library("latexpdf")
library("viridis")
library("MASS")
library("gridExtra")
library("multcomp")
library("lattice")
library("car")
library("Matrix")
library("glmmTMB")
library("DHARMa")
library("compare")
library("dplyr")
library("bbmle")
library("geepack")
library("lme4")
library("lmerTest")
library("aods3")
library("performance")
library("rstatix")
##### Data setup
# Data from MNV_calling_long.R
pi_n_mnv_per_sample <- read.csv("./pi_n_mnv_per_sample_06_2023.csv")
pi_n_mnv_per_sample_v2 <- read.csv("./pi_n_mnv_per_sample_V2_06_2023.csv")
freq_per_sample <-read.csv("./freq_per_sample.csv")
pi_n_mnv_per_sample$sample_id2<-as.factor(pi_n_mnv_per_sample$sample_id2)
pi_n_mnv_per_sample_v2$sample_id2<-as.factor(pi_n_mnv_per_sample_v2$sample_id2)
variants_called<- read.csv("./variants_called_merged_06_2023.csv")
# Data from script: Merge_count_coverage_diagnosis.R
df_total <- read.csv("./Merged_cov_diag_total_ME_Redit_10_2023.csv")
# Merge datasets
df_total2<-merge(x=df_total, y=pi_n_mnv_per_sample, by.x="sample_id2", by.y="sample_id2", all.x=TRUE)
df_total2.5<-merge(x=df_total2, y=pi_n_mnv_per_sample_v2, by.x="sample_id2", by.y="sample_id2", all.x=TRUE)
df_total3<-merge(x=df_total2.5, y=freq_per_sample, by.x="sample_id2", by.y="sample_id2", all.x=TRUE)
variants_called_merged<-merge(x=variants_called, y=df_total, by.x="sample_id2", by.y="sample_id2", all.x=TRUE)
## Remove samples that have been found with coinfection
df_total3 <- filter(df_total3, sample_id2 != "sample1-HPV16" & sample_id2 != "sample2-HPV16" & sample_id2 != "sample3-HPV16" & sample_id2 != "sample4-HPV16")
df_total3$time_diff_fromA2<-as.integer(df_total3$time_diff_fromA2)
df_total3_order <- df_total3[order(df_total3$Personnummer,df_total3$time_diff_fromA2),]
################################# Data summary ###############################
summary_diag <- df_total3_order %>%
group_by(Grouping_2023) %>%
summarize(
merge2 = n(),
Personnummer = n_distinct(Personnummer)
)
summary_total <- df_total3_order %>%
summarize(
Grouping_2023 = "Total",
merge2 = n(),
Personnummer = n_distinct(Personnummer)
)
summary_dataset <- bind_rows(summary_diag, summary_total)
summary_dataset
### Total summary mean coverage and percent genome covered 100x
summary(df_total3_order[c("mean_coverage", "genome_covered_100x")])
#####################################################################
############################### Model number iSNVs ##################
# fit the model
glmm_mnv <- glmmTMB(n.x ~ Grouping_2023 * time_diff_fromA2 + (1 | Personnummer), family = nbinom2, data = df_total3_order)
# print the summary
summary(glmm_mnv)
# check the confidence interval
confint(glmm_mnv)
## Investigate residuals with DHARMA
# simulationOutput <- simulateResiduals(fittedModel = glmm_mnv, plot=T)
simulationOutput <- simulateResiduals(fittedModel = glmm_mnv, n=1000, plot=T)
testResiduals(simulationOutput)
# Test overdispersion
testDispersion(simulationOutput)
# Test zeroinflation
testZeroInflation(simulationOutput)
# Plot the predicted relationship
mydf_mnv <- ggpredict(glmm_mnv, terms = c("time_diff_fromA2", "Grouping_2023"), ci.lvl = 0.95)
mydf_mnv$group<-factor(mydf_mnv$group, levels =c("High_grade", "Low_grade", "Normal"))
mydf_mnv_order<- mydf_mnv %>% arrange(x, group)
mydf_mnv_subset<- mydf_mnv_order %>% filter((group=="High_grade") & x <= 700 | (group=="Low_grade") & x <= 700 | (group=="Normal"))
ggplot(mydf_mnv_subset, aes(x, predicted)) +
scale_color_manual(values=c("#440154", "#21918c", "#fde725"), guide = "none")+
scale_fill_manual(values=c( "#440154", "#21918c", "#fde725"), name = "Diagnostic category", labels = c("High grade", "Low grade", "Normal" ))+
theme_bw() +
theme(axis.text=element_text(size=18),
axis.title=element_text(size=16,face="bold"),
legend.title = element_text(size=18),
legend.text = element_text(size=16))+
theme(panel.grid.major = element_blank())+
geom_line(aes(color = group), size=1.5)+
geom_ribbon(data=mydf_mnv_subset,aes(ymin = conf.low, ymax = conf.high, fill = group), alpha = 0.08, color="grey", size=0.1) +
labs(y = "Number of iSNV", x = "Time difference (days)")+
geom_point(data=df_total3_order, aes(time_diff_fromA2, n.x, fill = Grouping_2023), alpha = 0.8, size=4.2,pch=21)+
guides(fill = guide_legend(override.aes = list(size=10)))
########## Generate summary iSNV #############
df_summary_iSNV <- df_total3_order %>%
group_by(Grouping_2023) %>%
summarise(
Mean_iSNV= mean(n.x),
Min_iSNV = min(n.x),
Max_iSNV = max(n.x))
#######################################################################
###################### Model mean pi ##################################
lmer_pi<- lmer((mean_pi.y) ~ Grouping_2023 * time_diff_fromA2 + (1 | Personnummer), data = df_total3_order)
# print the summary
summary(lmer_pi)
# check the confidence interval
confint(lmer_pi)
## Investigate residuals with DHARMA
# simulationOutput <- simulateResiduals(fittedModel = glmm_mnv, plot=T)
simulationOutput <- simulateResiduals(fittedModel = lmer_pi, n=1000, plot=T)
testResiduals(simulationOutput)
# Test overdispersion
testDispersion(simulationOutput)
# Test zeroinflation
testZeroInflation(simulationOutput)
# Plot the predicted relationship
mydf_pi <- ggpredict(lmer_pi, terms = c("time_diff_fromA2", "Grouping_2023"), ci.lvl = 0.95)
mydf_pi$group<-factor(mydf_pi$group, levels =c("High_grade", "Low_grade", "Normal"))
mydf_pi_order<- mydf_pi %>% arrange(x, group)
mydf_pi_subset<- mydf_pi_order %>% filter((group=="High_grade") & x <= 400 | (group=="Low_grade") & x <= 600 | (group=="Normal"))
ggplot(mydf_pi_subset, aes(x, predicted)) +
scale_color_manual(values=c("#440154", "#21918c", "#fde725"), guide = "none")+
scale_fill_manual(values=c("#440154", "#21918c", "#fde725"), name = "Diagnostic category", labels = c("High grade", "Low grade", "Normal"))+
theme_bw() +
theme(axis.text=element_text(size=18),
axis.title=element_text(size=16,face="bold"),
legend.title = element_text(size=18),
legend.text = element_text(size=16))+
theme(panel.grid.major = element_blank())+
geom_line(aes(color = group), size=1.5)+
scale_y_continuous(limits = c(0.0005,0.004))+
geom_ribbon(data=mydf_pi_subset,aes(ymin = conf.low, ymax = conf.high, fill = group), alpha = 0.08, color="grey", size=0.1) +
labs(y = "Nucleotide diversity (\u03C0)", x = "Time difference (days)")+
geom_point(data=df_total3_order, aes(time_diff_fromA2, mean_pi.y, fill = Grouping_2023), alpha = 0.8, size=4.2,pch=21)+
guides(fill = guide_legend(override.aes = list(size=10)))
########## Generate summary data quality table #############
df_summary_pi <- df_total3_order %>%
group_by(Grouping_2023) %>%
summarise(
Mean_pi= mean(mean_pi.y),
Min_pi = min(mean_pi.y),
Max_pi = max(mean_pi.y),
Var_pi = var(mean_pi.y))
df_summary_pi
###################################################################################
###################### Model mutation signature ##################################
##################### Mutation signature Graph
signature_all2 <- variants_called_merged %>%
group_by(sample_id2, Grouping_2023, six_mut, tri_context) %>%
summarise(count = n())
##################### Mutation signature models
## Preparing the dataset
signature_ct_TCA<-subset(signature_all2, six_mut=="C>T" & tri_context=="T*A")
signature_ct_TCA <- signature_ct_TCA[,-2]
signature_ct_TCA_TCT<-subset(signature_all2, six_mut=="C>T" & tri_context=="T*A" | six_mut=="C>T" & tri_context=="T*T")
signature_ct_TCA_TCT2 <- signature_ct_TCA_TCT%>%
group_by(sample_id2, six_mut) %>%
summarise(count_TCA_TCT = sum(count))
df_total3_signature<-merge(x=df_total3_order, y=signature_ct_TCA , by.x="sample_id2", by.y="sample_id2", all.x=TRUE)
df_total3_signature<-merge(x=df_total3_signature, y=signature_ct_TCA_TCT2 , by.x="sample_id2", by.y="sample_id2", all.x=TRUE)
df_total3_signature$count[is.na(df_total3_signature$count)] <- 0
df_total3_signature$count_TCA_TCT[is.na(df_total3_signature$count_TCA_TCT)] <- 0
df_total3_order_signature<- df_total3_signature[order(df_total3_signature$Personnummer,df_total3_signature$time_diff_fromA2),]
sum(df_total3_order_signature$count_TCA_TCT == 0)
# Corrected for zeroinflation
glmmtmb_model_zi<- glmmTMB(count_TCA_TCT ~ Grouping_2023 * time_diff_fromA2 + (1 | Personnummer), family = poisson, ziformula=~1, data = df_total3_order_signature)
# print the summary
summary(glmmtmb_model_zi)
# check the confidence interval
confint(glmmtmb_model_zi)
## Investigate residuals with DHARMA
# simulationOutput <- simulateResiduals(fittedModel = glmm_mnv, plot=T)
simulationOutput <- simulateResiduals(fittedModel = glmmtmb_model_zi, n=1000, plot=T)
testResiduals(simulationOutput)
# Test overdispersion
testDispersion(simulationOutput)
# Test zeroinflation
testZeroInflation(simulationOutput)
## Prepare plot
mydf_mutsig <- ggpredict(glmmtmb_model_zi, terms = c("time_diff_fromA2", "Grouping_2023"), ci.lvl = 0.95)
mydf_mutsig$group<-factor(mydf_mutsig$group, levels =c("High_grade", "Low_grade", "Normal"))
mydf_mutsig_order<- mydf_mutsig %>% arrange(x, group)
mydf_mutsig_subset<- mydf_mutsig_order %>% filter((group=="High_grade") & x <= 700 | (group=="Low_grade") & x <= 700 | (group=="Normal"))
ggplot(mydf_mutsig_subset, aes(x, predicted)) +
scale_color_manual(values=c("#440154", "#21918c", "#fde725"), guide = "none")+
scale_fill_manual(values=c("#440154", "#21918c", "#fde725"), name = "Diagnostic category", labels = c("High grade", "Low grade", "Normal"))+
theme_bw() +
theme(axis.text=element_text(size=18),
axis.title=element_text(size=16,face="bold"),
legend.title = element_text(size=18),
legend.text = element_text(size=16))+
theme(panel.grid.major = element_blank())+
geom_line(aes(color = group), size=1.5)+
geom_ribbon(data=mydf_mutsig_subset,aes(ymin = conf.low, ymax = conf.high, fill = group), alpha = 0.08, color="grey", size=0.1) +
labs(y = "Number of C>T mutations \nin T*A and T*T context", x = "Time difference (days)")+
geom_point(data=df_total3_order_signature, aes(time_diff_fromA2, count_TCA_TCT, fill = Grouping_2023), alpha = 0.8, size=4.2,pch=21)+
guides(fill = guide_legend(override.aes = list(size=10)))
########## Generate summary data quality table #############
df_summary_APOBEC <- df_total3_order_signature %>%
group_by(Grouping_2023) %>%
summarise(
Mean_n_mut= mean(count_TCA_TCT),
Min_n_mut = min(count_TCA_TCT),
Max_n_mut = max(count_TCA_TCT))
########################################################
########## Models without accountig for time ###########
########################################################
################ Model number iSNVs ###############
# fit the model
n.x <- df_total3_order$n.x
Grouping_2023 <- as.factor(df_total3_order$Grouping_2023)
Personnummer <- as.factor(df_total3_order$Personnummer)
glmm_mnv_no_time <- glmmTMB(n.x ~ Grouping_2023 + (1 | Personnummer), family = nbinom2 )
# print the summary
summary(glmm_mnv_no_time)
# check the confidence interval
confint(glmm_mnv_no_time)
# Check residuals
simulationOutput <- simulateResiduals(fittedModel = glmm_mnv_no_time, n=1000, plot=T)
testResiduals(simulationOutput)
# Test overdispersion
testDispersion(simulationOutput)
# Test zeroinflation
testZeroInflation(simulationOutput)
###################### Model mean pi ####################
lmer_pi_no_time<- lmer(mean_pi.y ~ Grouping_2023+ (1 | Personnummer), data = df_total3_order)
# print the summary
summary(lmer_pi_no_time)
# check the confidence interval
confint(lmer_pi_no_time)
## Investigate residuals with DHARMA
# simulationOutput <- simulateResiduals(fittedModel = glmm_mnv, plot=T)
simulationOutput <- simulateResiduals(fittedModel = lmer_pi_no_time, n=1000, plot=T)
testResiduals(simulationOutput)
# Test overdispersion
testDispersion(simulationOutput)
# Test zeroinflation
testZeroInflation(simulationOutput)
############### Model mutation signature ##############
glmmtmb_model_zi_no_time<- glmmTMB(count_TCA_TCT ~ Grouping_2023 + (1 | Personnummer), family = poisson, ziformula=~1, data = df_total3_order_signature)
# print the summary
summary(glmmtmb_model_zi_no_time)
# check the confidence interval
confint(glmmtmb_model_zi_no_time)
## Investigate residuals with DHARMA
# simulationOutput <- simulateResiduals(fittedModel = glmm_mnv, plot=T)
simulationOutput <- simulateResiduals(fittedModel = glmmtmb_model_zi_no_time, n=1000, plot=T)
testResiduals(simulationOutput)
# Test overdispersion
testDispersion(simulationOutput)
# Test zeroinflation
testZeroInflation(simulationOutput)
########################################################
################# Supplementary plots ##################
########################################################
# Relationship between number of iSNV and diagnosis grouping
plot_grid<-ggplot(df_total3_order, aes(x=time_diff_fromA2, y=n.x)) +
theme_bw()+
# geom_smooth(method='lm', aes(color = NA), formula=y~0+x, se=TRUE) +
geom_point(aes(color=Grouping_2023), size=6) + geom_line() +
scale_color_manual(values=c("#440154", "#21918c", "#fde725"), name = "Diagnostic category", labels = c("High grade", "Low grade", "Normal" ))+
# scale_color_manual(values=c( "#fde725", "#21918c", "#440154"), name = "Diagnostic category", labels = c("High grade", "Low grade", "Normal" ))+
facet_wrap(~ Personnummer, ncol = 7) +
theme(strip.background = element_blank(),
strip.text.x.top = element_blank())+
labs(x="Time difference (days)", y='Number of iSNV') +
theme(axis.text=element_text(size=14),
axis.title=element_text(size=16,face="bold"),
legend.title = element_text(size=16),
legend.text = element_text(size=14))
plot_grid
# Relationship between Pi and diagnosis grouping
plot_grid<-ggplot(df_total3_order, aes(x=time_diff_fromA2, y=mean_pi.y)) +
theme_bw()+
# geom_smooth(method='lm', aes(color = NA), formula=y~0+x, se=TRUE) +
geom_point(aes(color=Grouping_2023), size=6) + geom_line() +
scale_color_manual(values=c("#440154", "#21918c", "#fde725"), name = "Diagnostic category", labels = c("High grade", "Low grade", "Normal" ))+
# scale_color_manual(values=c( "#fde725", "#21918c", "#440154"), name = "Diagnostic category", labels = c("High grade", "Low grade", "Normal" ))+
facet_wrap(~ Personnummer, ncol = 7) +
theme(strip.background = element_blank(),
strip.text.x.top = element_blank())+
labs(x="Time difference (days)", y="Nucleotide diversity (\u03C0)") +
theme(axis.text=element_text(size=14),
axis.title=element_text(size=16,face="bold"),
legend.title = element_text(size=16),
legend.text = element_text(size=14))
plot_grid
# Relationship between APOBEC3 and diagnosis grouping
plot_grid<-ggplot(df_total3_order_signature, aes(x=time_diff_fromA2, y=count_TCA_TCT)) +
theme_bw()+
# geom_smooth(method='lm', aes(color = NA), formula=y~0+x, se=TRUE) +
geom_point(aes(color=Grouping_2023), size=6) + geom_line() +
scale_color_manual(values=c("#440154", "#21918c", "#fde725"), name = "Diagnostic category", labels = c("High grade", "Low grade", "Normal" ))+
# scale_color_manual(values=c( "#fde725", "#21918c", "#440154"), name = "Diagnostic category", labels = c("High grade", "Low grade", "Normal" ))+
facet_wrap(~ Personnummer, ncol = 7) +
theme(strip.background = element_blank(),
strip.text.x.top = element_blank())+
labs(x="Time difference (days)", y="Number of C>T mutations \nin T*A and T*T context") +
theme(axis.text=element_text(size=14),
axis.title=element_text(size=16,face="bold"),
legend.title = element_text(size=16),
legend.text = element_text(size=14))
plot_grid
#################
## Review test model with age
glmmtmb_model_iSNV_age<- glmmTMB(n.x ~ alder + (1 | Personnummer), family =nbinom2, data = df_total3_order_signature)
glmmtmb_model_iSNV_age2<- glmmTMB(n.x ~ Grouping_2023 * alder + (1 | Personnummer), family =nbinom2, data = df_total3_order_signature)
glmmtmb_model_APOBEC_age<- glmmTMB(count_TCA_TCT ~ alder + (1 | Personnummer), family = poisson, ziformula=~1, data = df_total3_order_signature)
glmmtmb_model_APOBEC_age2<- glmmTMB(count_TCA_TCT ~ Grouping_2023 * alder + (1 | Personnummer), family = poisson, ziformula=~1, data = df_total3_order_signature)
summary(glmmtmb_model_iSNV_age)
summary(glmmtmb_model_APOBEC_age)
summary(glmmtmb_model_iSNV_age2)
summary(glmmtmb_model_APOBEC_age2)
# Count the number of participant with the same or different diagnostic over time
diagnostic_counts <- df_total3_order %>%
group_by(Personnummer) %>%
summarize(num_diagnostic_categories = n_distinct(Grouping_2023))
# same diagnostic category
same_diagnostic_count <- diagnostic_counts %>%
filter(num_diagnostic_categories == 1) %>%
nrow()
# different diagnostic categories
different_diagnostic_count <- diagnostic_counts %>%
filter(num_diagnostic_categories > 1) %>%
nrow()
# Print the results
cat("Number of patients with all samples in the same diagnostic category:", same_diagnostic_count, "\n")
cat("Number of patients with samples in different diagnostic categories:", different_diagnostic_count, "\n")
# Create table with minor base frequency, mean pi and number if iSNV for each diagnostic group
aggregate(df_total3_order$minor_base_freq~df_total3_order$Grouping_2023, FUN=mean)
aggregate(df_total3_order$mean_pi.y~df_total3_order$Grouping_2023, FUN=mean)
aggregate(df_total3_order$n.x~df_total3_order$Grouping_2023, FUN=mean)