-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_variantCalling_day84_c.Rmd
2074 lines (1305 loc) · 87.3 KB
/
03_variantCalling_day84_c.Rmd
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Analysis of Mutations Identified on Day 84"
author: "Ricardo Ramiro"
date: "23/07/2020"
output:
html_document:
code_folding: show
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE,
message=FALSE,
error = TRUE,
warning=FALSE,
paged.print=FALSE,
fig.align = "center")
knitr::opts_hooks$set(eval = function(options) {
if (options$engine == "bash") {
options$eval <- FALSE
}
options
})
```
### Run breseq v0.34.1 to call mutations in B. thetaiotaomicron populations
```{bash}
# activate conda environment where breseq is installed
conda activate breseq0.34.1
# change permissions on script to run it
chmod u+x breseq_script_D84_prokka_withMQpara_v3ref_c.sh
# run script (this is set up to run multiple samples in parallel)
nohup bash breseq_script_D84_prokka_withMQpara_v3ref_c.sh &
```
### get output.gd files into a single folder
```{bash, eval=FALSE, echo=FALSE}
# go to dir
cd analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref
# make new dir to put all gd files
mkdir breseq_gdOutput_day84
# copy and rename gd files
for DIR in D84*; do
cd "$DIR"/output
cp output.gd ../../breseq_gdOutput_day84/$DIR.gd
cd ../..
done
```
### use gdtools compare to get mutation tables
```{bash}
# reference sequence
REF=analysis/genomics/TDA1000/TDA1000_hybridAssembly/results/assemblies/prokka_annotation/TDA1000_chromosome_plasmid_v3.gff3
# move to folder with gd files
cd breseq_gdOutput_day84
# generate html file comparing all runs of the ancestral
gdtools COMPARE -r $REF --repeat-header 0 -o ../downstreamAnalysis/breseq_output_day84.html `ls *.gd`
```
The above html files were then copied in to google sheets of the same name. Two tabs were created:
- raw: just copy paste
- edited. In this sheet I did the following:
- google sheets considers anything that starts as a plus sign as a formula and gives an error. This is important in column C (mutation). To change this behaviour I replaced, using REGEX,
"=\+" by "'+". The apostrophe allows the plus sign to be the first character
- when the same gene is mutated with both SNPs and deletions, breseq will add Δ in the frequency field for samples that got deletions. I replaced all these Δ by nothing
- replaced "\?" by noting
- I changed the format of the frequencies of the mutations. These are expressed as %, I changed to decimal
- some positions are written as ######:# (e.g. 314,132:1 or 314,132:2). These lead to problems when making position column as numeric. Thus, I replaced "\:[0-9]" by nothing
- in the data from day 84 there were a few mutations in overlapping genes, with two gene names per cell, which would have caused problems in the plotting of the data. However, none of these were parallel, so these are mostly irrelevant for our analysis, so I didn't do anything about it
- change the name of the column "seq id" to "seq_id"
- in the gene column, I replaced all spaces, ← and → by nothing
### general data manipulation
```{r}
#load libraries
library(tidyverse)
library(readxl)
library(cowplot)
library(plotly)
library(patchwork)
# load data
d84<-read_xlsx("analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/breseq_output_day84.xlsx",sheet=2)
# change order of columns, such that metadata is all on the left of the data frame
d84<-d84[c(1:3,45:47,4:44)]
# transform data to long format
d84_long<-gather(d84,key=sample,value=frequency,D84_C10:D84_M9)
# separate the sample column to its component day and mouse codes
d84_long<-d84_long %>% separate(sample, c("day", "mouse"),sep="_",remove=F)
# remove D from day
d84_long$day<-str_remove(d84_long$day,"D")
# remove NAs from the frequency column
d84_long<-d84_long[!is.na(d84_long$frequency),]
#transform position to numeric
d84_long$position<-as.numeric(d84_long$position)
# get the first letter from each mouse code, as this indicates the diet
d84_long$trt<-str_sub(d84_long$mouse,1,1)
# create a data frame matching the 1-letter codes to the codes used in the paper
trt_diet<-data.frame(trt=c("C","M","F"),diet=c("WD","SD","AD"))
# match the 1-letter diet codes to those to use in the paper
d84_long<-left_join(d84_long,trt_diet)
# create a new column for easy identification of mutations
d84_long$seqid_position_mutation_gene<-paste0(d84_long$seq_id,"_",d84_long$position,"_",d84_long$mutation,"_",d84_long$gene)
# write the long format dataframe to a new file
# writexl::write_xlsx(d84_long,"analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/breseq_output_day84_longFormat_c.xlsx")
```
## detect and remove low confidence mutations
### check which regions of the genome have multiple mutations across >2/3 of the mice
##### count number of mutations in windows of 1000bp per mouse
1000 bp was used as the window as this is the expected average gene size for bacteria (e.g. https://www.pnas.org/content/101/9/3160)
```{r}
# load packages
library(rollply)
library(ggbeeswarm)
# count the number of mutations in windows of 1000bp (approximately the gene size in bacteria)
d84_long.smoothed <- d84_long %>% group_by(seq_id,mouse,diet) %>%
rollply(., ~ position, wdw.size = 1000, summarize, mutation_no = n(), ) %>%
arrange(seq_id,position)
#remove regions without mutations
d84_long.smoothed<-d84_long.smoothed[!d84_long.smoothed$mutation_no==0,]
```
#### do boxplots with the whiskers being at 99% CI, instead of the 1.5IQR
Boxplot for the chromosome
```{r}
#function for 99% CI
quantiles_99 <- function(x) {
r <- quantile(x, probs=c(0.01, 0.25, 0.5, 0.75, 0.99))
names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
r
}
# plot for the chromosone
p<-ggplot(d84_long.smoothed[d84_long.smoothed$seq_id=="chromosome",],
aes(x=mouse,y=mutation_no))+
guides(fill=F) +
stat_summary(fun.data = quantiles_99, geom="boxplot")+
geom_quasirandom(aes(label=position,label2=seq_id))+
xlab("Mouse")+ylab("Mutation Count")+
cowplot::theme_cowplot()+
background_grid(major="xy")+
theme(legend.position = "none")+
facet_grid(~diet,scales="free_x",drop=T)+
ggtitle("chromosome")
ggplotly(p)
```
Boxplot for the plasmid
```{r}
# plot for the plasmid
p<-ggplot(d84_long.smoothed[d84_long.smoothed$seq_id=="plasmid",],
aes(x=mouse,y=mutation_no))+
guides(fill=F) +
stat_summary(fun.data = quantiles_99, geom="boxplot")+
geom_quasirandom(aes(label=position,label2=seq_id))+
xlab("Mouse")+ylab("Mutation Count")+
cowplot::theme_cowplot()+
background_grid(major="xy")+
scale_y_log10()+
theme(legend.position = "none")+
facet_grid(~diet,scales="free_x",drop=T)+
ggtitle("plasmid")
ggplotly(p)
```
Plots for the chromosome showed that any 1000bp region with more than 3 mutations is beyond the 99% CI, so below I filter for those gene regions that have 3 or more mutations
#### summarize mutation counts per position range
```{r}
# keep all regions that have three or more mutations
d84_long.smoothed_above3<-d84_long.smoothed[d84_long.smoothed$mutation_no>=3,]
#position is a non-integer, round it to an integer
d84_long.smoothed_above3$position<-round(d84_long.smoothed_above3$position,0)
#get the unique position-seq_id combinations
d84_long.smoothed_above3_unique<-d84_long.smoothed_above3[c(1,2)] %>% distinct()
# get a table that summarizes, for each position, the number of mice that has a mutation in it and and the number of mutations per mouse
d84_long.smoothed_above3_mouseCount<-d84_long.smoothed_above3 %>%
group_by(seq_id,position,mouse,diet=fct_explicit_na(diet)) %>%
summarise(mutation_no=sum(mutation_no)) %>%
group_by(seq_id,position) %>%
summarize(mouse_no=n(),min_mutations_perMouse=min(mutation_no),max_mutations_perMouse=max(mutation_no),mean_mutations_perMouse=mean(mutation_no)) %>%
arrange(-mouse_no)
#get regions that are mutated in at least 2/3 of the mice. Those are further inspected.
d84_long.smoothed_above3_mouseCount_subset<-d84_long.smoothed_above3_mouseCount %>% subset(mouse_no>=27) %>% arrange(seq_id,position)
# knitr::kable(d84_long.smoothed_above3_mouseCount_subset,format = "markdown")
```
This generates 1 regions that get >3 mutations in 39 or 40 mice. This region was then manually redefined to specific positions by looking at mutations in the same genes:
|seq_id | position| mouse_no| min_mutations_perMouse| max_mutations_perMouse| mean_mutations_perMouse|refined_position_byGeneID|Ancestral | genes_inRegion
|:----------|--------:|--------:|----------------------:|----------------------:|-----------------------:|:------------------------|:------------------------|:--------------
|chromosome | 1096287| 40| 3| 9| 6.750000|1095828 - 1098878 |many muts in TDA1000/1/2 |TDA1000_00850 ←
|chromosome | 1097287| 39| 4| 9| 6.871795|1095828 - 1098878 |many muts in TDA1000/1/2 |TDA1000_00851 →
#### Create and export a list of all the mutations in this repetitive region
```{r}
# create a data frame that has the number of mice that each mutation in the repetitive region
d84_long_rep_region<-d84_long %>% subset(seq_id=="chromosome" & position>1095828-1 & position<1098878+1) %>% group_by(seq_id,position,mutation,annotation,gene,description) %>% summarize(sample_count=n())
# export data
# writexl::write_xlsx(d84_long_rep_region,"analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/regions_multipleMutations_d84_uniqueMutations_c.xlsx")
```
## make file with low confidence mutational targets (ancestral with High mapping quality)
```{r}
#load the data
regionsMult <- read_excel("analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/regions_multipleMutations_d84_uniqueMutations_c.xlsx")
mutsAnc <- read_excel("analysis/genomics/ancestrals_breseq/downstreamAnalysis/ancestral_mutations_polymorphismHighMappingQualMQ.xlsx")
# add a column with the type of low confidence mutation
regionsMult$type<-rep("region_MultipleMutations",dim(regionsMult)[1])
mutsAnc$type<-rep("Ancestral",dim(mutsAnc)[1])
# sample_count in mutsAnc was for the ancestrals and we need this for the samples
mutsAnc$seqid_position_mutation_gene<-paste(mutsAnc$seq_id,
mutsAnc$position,
mutsAnc$mutation,
mutsAnc$gene,
sep = "_")
d84_long_numberMice_withMutation<-d84_long %>%
group_by(seq_id,position,mutation,annotation, gene,description) %>%
summarise(sample_count=n())
d84_long_numberMice_withMutation$seqid_position_mutation_gene<-paste(d84_long_numberMice_withMutation$seq_id,
d84_long_numberMice_withMutation$position,
d84_long_numberMice_withMutation$mutation,
d84_long_numberMice_withMutation$gene,
sep = "_")
mutsAnc_d84<-d84_long_numberMice_withMutation %>% filter(seqid_position_mutation_gene %in% unique(mutsAnc$seqid_position_mutation_gene))
mutsAnc_notInd84<-mutsAnc %>% filter(!seqid_position_mutation_gene %in% unique(d84_long_numberMice_withMutation$seqid_position_mutation_gene))
# mutations present in the ancestrals but not in day84 get a 0 count
mutsAnc_notInd84$sample_count<-0
#mutations present in the ancestrals and day84 must get the Ancestral type
mutsAnc_d84$type<-"Ancestral"
#bind the two dataframes above
mutsAnc<-bind_rows(mutsAnc_d84[c(1:7,9,8)],mutsAnc_notInd84)
# bind all three dataframes
low_confidence_mutations<-bind_rows(regionsMult, mutsAnc[1:8])
# get a list of unique mutations
low_confidence_mutations_unique<-low_confidence_mutations %>%
group_by(seq_id,position,mutation,annotation,gene,description) %>%
mutate(type = paste(type, collapse=","),
sample_count = paste(sample_count, collapse=",")) %>%
distinct()
```
```{r}
# load annotation data
annotation<-readxl::read_excel("analysis/genomics/TDA1000/TDA1000_hybridAssembly/results/assemblies/prokka_annotation/annotation_table_short.xlsx")
# there were a few mutations that correspond to a deletion, the labels for these needed to be edited
low_confidence_mutations_unique$gene<-str_replace_all(low_confidence_mutations_unique$gene,"\\[", "")
low_confidence_mutations_unique$gene<-str_replace_all(low_confidence_mutations_unique$gene,"\\]", "")
low_confidence_mutations_unique$gene<-str_replace_all(low_confidence_mutations_unique$gene,"–", "/")
# create two new columns with gene names (for when mutations are in intergenic regions)
low_confidence_mutations_unique<-low_confidence_mutations_unique %>% separate(gene, c("gene_1", "gene_2"),sep="/",remove=F)
#merge data frame of mutations with annotations (for gene 1)
low_confidence_mutations_unique<-left_join(low_confidence_mutations_unique,annotation,by=c("gene_1"="locus_tag_prokka"))
low_confidence_mutations_unique<-low_confidence_mutations_unique %>% rename_at(11, ~paste0(., "_1"))
#merge data frame of mutations with annotations (for gene 2)
low_confidence_mutations_unique<-left_join(low_confidence_mutations_unique,annotation,by=c("gene_2"="locus_tag_prokka"))
low_confidence_mutations_unique<-low_confidence_mutations_unique %>% rename_at(12, ~paste0(., "_2"))
# merge locus that are intergenic mutations
low_confidence_mutations_unique$locus_name<-ifelse(is.na(low_confidence_mutations_unique$locus_tag_db_2),low_confidence_mutations_unique$locus_tag_db_1,
paste0(low_confidence_mutations_unique$locus_tag_db_1,"/",low_confidence_mutations_unique$locus_tag_db_2))
low_confidence_mutations_unique$locus_name<-ifelse(low_confidence_mutations_unique$seq_id=="plasmid",low_confidence_mutations_unique$gene,low_confidence_mutations_unique$locus_name)
#change order of columns so that the BT locus_names are close to the gene names
low_confidence_mutations_unique<-low_confidence_mutations_unique %>% select(1:5,locus_name,8,10,9) %>%
arrange(seq_id,position)
#export data
# writexl::write_xlsx(low_confidence_mutations_unique,"analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/low_confidence_mutations_unique_c.xlsx")
# get list of low confidence mutational targets
low_confidence_mutationalTargets<-low_confidence_mutations_unique[c(1,5:7)] %>% distinct()
# writexl::write_xlsx(low_confidence_mutationalTargets,"analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/low_confidence_mutationalTargets_c.xlsx")
DT::datatable(low_confidence_mutations_unique, extensions = "Buttons",
options = list(pageLength = 10,dom = "Blfrtip", buttons = "csv"))
```
## remove low confidence mutations from mutation list
```{r}
# load low confidence mutations
low_confidence_mutations_unique<-read_xlsx("analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/low_confidence_mutations_unique_c.xlsx")
#create a column code for filtering
low_confidence_mutations_unique$seqid_position_mutation_gene<-paste(low_confidence_mutations_unique$seq_id,
low_confidence_mutations_unique$position,
low_confidence_mutations_unique$mutation,
low_confidence_mutations_unique$gene,
sep = "_")
#create vector with low quality mutation codes
low_confidence_mutations_unique<-low_confidence_mutations_unique$seqid_position_mutation_gene
# remove all mutations that match above code
d84_long<-d84_long %>% subset(!seqid_position_mutation_gene %in% low_confidence_mutations_unique)
# reorder levels of diet
d84_long$diet<-factor(d84_long$diet,levels=c("SD","WD","AD"))
# write the long format dataframe to a new file
# writexl::write_xlsx(d84_long,"analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/breseq_output_day84_longFormat_woutLowConfMutations_c.xlsx")
```
## add annotations to table (e.g. BT gene names, functions, etc)
```{r}
#create new dataframe with annotations
d84_long_annot<-d84_long
# there were a few mutations that correspond to a deletion, the labels for these needed to be edited
d84_long_annot$gene<-str_replace_all(d84_long_annot$gene,"\\[", "")
d84_long_annot$gene<-str_replace_all(d84_long_annot$gene,"\\]", "")
d84_long_annot$gene<-str_replace_all(d84_long_annot$gene,"–", "/")
# create two new columns with gene names (for when mutations are in intergenic regions)
d84_long_annot<-d84_long_annot %>% separate(gene, c("gene_1", "gene_2"),sep="/",remove=F)
#merge data frame of parallel mutations with annotations (for gene 1)
d84_long_annot<-left_join(d84_long_annot,annotation,by=c("gene_1"="locus_tag_prokka"))
d84_long_annot<-d84_long_annot %>% rename_at(16, ~paste0(., "_1"))
#merge data frame of parallel mutations with annotations (for gene 2)
d84_long_annot<-left_join(d84_long_annot,annotation,by=c("gene_2"="locus_tag_prokka"))
d84_long_annot<-d84_long_annot %>% rename_at(17, ~paste0(., "_2"))
# merge locus that are intergenic mutations
d84_long_annot$locus_name<-ifelse(is.na(d84_long_annot$locus_tag_db_2),d84_long_annot$locus_tag_db_1,
paste0(d84_long_annot$locus_tag_db_1,"/",d84_long_annot$locus_tag_db_2))
# keep gene name for plasmid mutations
d84_long_annot$locus_name<-ifelse(d84_long_annot$seq_id=="plasmid",d84_long_annot$gene,
d84_long_annot$locus_name)
# there are two loci with overlapping genes: TDA1000_02181 TDA1000_02182 and TDA1000_00472 TDA1000_00473
d84_long_annot$locus_name<-ifelse(str_detect(d84_long_annot$gene,"TDA1000_02181"),d84_long_annot$gene,
d84_long_annot$locus_name)
d84_long_annot$locus_name<-ifelse(str_detect(d84_long_annot$gene,"TDA1000_00472"),"BT_2879 BT_2880",
d84_long_annot$locus_name)
#change order of columns so that the BT locus_names are close to the gene names
d84_long_annot<-d84_long_annot[c(1:5,18,8:17)]
# write the long format dataframe to a new file
# writexl::write_xlsx(d84_long_annot,"analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/breseq_output_day84_longFormat_woutLowConfMutations_withAnnotations_c.xlsx")
```
# Count the number of mutations per mouse
## Total number of mutations
Most mice have between 20 and 70 mutations, except 3 mice: M1, M3 and C4 which have 274, 230 and 156 mutations (respectively).
- Mouse M1: has one mutation in mutS at 0.7%. If most mutations arise in these mutator backgrounds, we would predict most mutations would be below 1%. Indeed, 171 are <=1% and 250 are <=2%.
- Mouse M3: not immediately clear which mutations could lead to mutators. 205 mutations are <=2%
- Mouse C4: not immediately clear which mutations could lead to mutators. 86 mutations are <=2%
```{r}
# create data
d84_mutationCount<-d84_long %>% group_by(mouse,diet) %>% summarize(mutation_tot=n())
d84_mutationCount$diet<-factor(d84_mutationCount$diet,levels=c("SD","WD","AD"))
d84_mutationCount$mouse2<-str_sub(d84_mutationCount$mouse,start = 2,end = 3) %>%
as.numeric() %>% as.factor()
p<-ggplot(d84_mutationCount,aes(x=diet,y=mutation_tot,fill=diet))+
geom_boxplot(outlier.shape = NA, alpha=0.6)+
geom_quasirandom(aes(label=mouse),shape=21,size=3)+
xlab("Diet")+ylab("Mutation count")+
scale_fill_manual(values=c("#4575b4","#d73027","#878787"))+
cowplot::theme_cowplot()+
background_grid(major="y")+
theme(legend.position = "none")+
ylim(0,300)
p
#interactive/downloadable table
DT::datatable(data = d84_mutationCount, extensions = "Buttons",
options = list(dom = "Blfrtip", buttons = c("csv","excel")))
```
## Mutation count per mouse - Fig. 2A
```{r}
p<-ggplot(d84_mutationCount,aes(x=mouse2,y=mutation_tot,fill=diet))+
geom_col(alpha=0.5)+
xlab("Mouse")+ylab("Mutation counts per mouse")+
scale_fill_manual(values=c("#4575b4","#d73027","#878787"))+
cowplot::theme_cowplot()+
background_grid(major="y",minor="y")+
theme(legend.position = "none")+
facet_wrap(~diet,scales = "free_x")+
ylim(0,300)
p
```
## Mutation frequency per mouse - Fig. 2A
```{r}
d84_long$mouse2<-str_sub(d84_long$mouse,start = 2,end = 3) %>%
as.numeric() %>% as.factor()
p<-ggplot(d84_long,aes(x=mouse2,y=frequency,fill=diet))+
geom_quasirandom(aes(label=mouse),shape=21)+
xlab("Mouse")+ylab("Mutation frequency")+
scale_fill_manual(values=c("#4575b4","#d73027","#878787"))+
cowplot::theme_cowplot()+
background_grid(major="y",minor="y")+
theme(legend.position = "none")+
facet_wrap(~diet,scales = "free_x")+
scale_y_log10()
p
```
## Mutation Spectra - Fig. S3B
Black points: frequency of coding SNPs that are non-synonymous (relative to synonymous)
Grey points: frequency of mutations that are in coding regions (relative to intergenic)
```{r,fig.width=10,fig.height=6}
library(RColorBrewer)
# dataframe with the number of mutations per type per mouse
d84_mutationCount_perType<-d84_long %>% group_by(diet,gene,mutation,annotation,mouse) %>% summarize(mutation_no=n())
#### rename mutation types ####
# get list of unique mutations and sort it alphagbetically
mutation<-unique(d84_mutationCount_perType$mutation)
mutation_types_df<-data.frame(mutation) %>% arrange(mutation)
#count the number of characters in each mutation type (this is useful because only snps have 3)
mutation_types_df$str_no<-str_count(mutation_types_df$mutation)
# create two data frames, one for SNPs and another for non-snps
mutation_types_df_snps<-mutation_types_df %>% subset(str_no==3)
mutation_types_df_non_snps<-mutation_types_df %>% subset(!str_no==3)
# add general mutation type for snps
snps<-data.frame(mutation =c("A→C" ,"T→G" ,"A→G" ,"T→C" ,"A→T" ,"T→A" ,"C→A" ,"G→T" ,"C→G" ,"G→C" ,"C→T" ,"G→A"),
mutation2=c("AT:CG","AT:CG","AT:GC","AT:GC","AT:TA","AT:TA","CG:AT","CG:AT","CG:GC","CG:GC","CG:TA","CG:TA"))
mutation_types_df_snps<-left_join(mutation_types_df_snps,snps)
# add general mutation type for other mutations
mutation_types_df_non_snps$mutation2<-ifelse(str_detect(mutation_types_df_non_snps$mutation,"IS"),"IS","indel")
#join the two dataframes of mutation types
mutation_types_df<-bind_rows(mutation_types_df_snps,mutation_types_df_non_snps)
# add new mutation types to dataframe of counts
d84_mutationCount_perType<-left_join(d84_mutationCount_perType,mutation_types_df %>% select(mutation,mutation2))
# add a region type column for coding/intergenic mutations
d84_mutationCount_perType$region_type<-ifelse(str_detect(d84_mutationCount_perType$annotation,"intergenic"),"intergenic","coding")
#some genes had no annotation, so I edited those
d84_mutationCount_perType$region_type<-ifelse(d84_mutationCount_perType$gene %in% c("[TDA1000_04919]","[TDA1000_00406]"),"coding",d84_mutationCount_perType$region_type)
#### create a new dataframe of counts for mutation spectra ####
d84_mutationCount_perType_spectra<-
d84_mutationCount_perType %>%
group_by(diet,mouse,mutation2) %>%
summarize(count=sum(mutation_no)) %>%
group_by(diet,mouse) %>%
mutate(total=sum(count))
#calculate mutation frequency
d84_mutationCount_perType_spectra$frequency<-d84_mutationCount_perType_spectra$count/d84_mutationCount_perType_spectra$total
# remove letters from mouse names, so you can order bars by mouse number more easily
d84_mutationCount_perType_spectra$mouse2<-str_sub(d84_mutationCount_perType_spectra$mouse,start = 2,end = 3) %>%
as.numeric() %>% as.factor()
#reorder diet levels
d84_mutationCount_perType_spectra$diet<-factor(d84_mutationCount_perType_spectra$diet,levels=c("SD","WD","AD"))
# writexl::write_xlsx(d84_mutationCount_perType_spectra,"analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/D84_mutationalSpectra.xlsx")
#### create a new dataframe of counts for mutation region ####
d84_mutationCount_perType_region<-
d84_mutationCount_perType %>%
group_by(diet,mouse,region_type) %>%
summarize(count=sum(mutation_no)) %>%
group_by(diet,mouse) %>%
mutate(total=sum(count))
#calculate mutation frequency
d84_mutationCount_perType_region$frequency<-d84_mutationCount_perType_region$count/d84_mutationCount_perType_region$total
# remove letters from mouse names, so you can order bars by mouse number more easily
d84_mutationCount_perType_region$mouse2<-str_sub(d84_mutationCount_perType_region$mouse,start = 2,end = 3) %>%
as.numeric() %>% as.factor()
#reorder diet levels
d84_mutationCount_perType_region$diet<-factor(d84_mutationCount_perType_region$diet,levels=c("SD","WD","AD"))
#### create new dataframe of counts for syn/non-syn SNPS ####
d84_mutationCount_perType_coding_snps<-d84_mutationCount_perType %>% filter(region_type=="coding" & mutation %in% snps$mutation) %>%
separate(annotation,c("aa","codon"),sep=" ",remove=F)
# remove non-coding (2mutations) and pseudogene (1 mutation)
d84_mutationCount_perType_coding_snps<-d84_mutationCount_perType_coding_snps %>% subset(!aa %in% c("pseudogene","noncoding"))
#get ancestral aminoacid
d84_mutationCount_perType_coding_snps$ancestral_aa<-str_sub(d84_mutationCount_perType_coding_snps$aa,1,1)
#get evolved aminoacid
d84_mutationCount_perType_coding_snps$evolved_aa<-str_sub(d84_mutationCount_perType_coding_snps$aa,-1,-1)
#new column for synonymous or non-synonymous
d84_mutationCount_perType_coding_snps$snp_type<-ifelse(d84_mutationCount_perType_coding_snps$ancestral_aa==d84_mutationCount_perType_coding_snps$evolved_aa,
"synonymous","non-synonymous")
## save mutational spectra coding snps
# writexl::write_xlsx(d84_mutationCount_perType_coding_snps,"analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/D84_mutationalSpectra_codingSNPs.xlsx")
#dataframe with count of snp_type per mouse
d84_mutationCount_perType_coding_snps_count<-
d84_mutationCount_perType_coding_snps %>%
group_by(mouse,diet,snp_type) %>%
summarize(count=sum(mutation_no)) %>%
group_by(mouse,diet) %>%
mutate(total=sum(count))
#get frequency of each type of mutations
d84_mutationCount_perType_coding_snps_count$frequency<-d84_mutationCount_perType_coding_snps_count$count/d84_mutationCount_perType_coding_snps_count$total
# remove letters from mouse names, so you can order bars by mouse number more easily
d84_mutationCount_perType_coding_snps_count$mouse2<-str_sub(d84_mutationCount_perType_coding_snps_count$mouse,start = 2,end = 3) %>%
as.numeric() %>% as.factor()
#reorder diet levels
d84_mutationCount_perType_coding_snps_count$diet<-factor(d84_mutationCount_perType_coding_snps_count$diet,levels=c("SD","WD","AD"))
#plot mutation spectra per mouse
ggplot()+
geom_bar(data=d84_mutationCount_perType_spectra,aes(x=mouse2,y=frequency,fill=mutation2),position = "stack", stat="identity")+
geom_point(data=na.omit(d84_mutationCount_perType_region[d84_mutationCount_perType_region$region_type=="coding",]),aes(x=mouse2,y=frequency),shape=21,fill="#d9d9d9",size=3)+
geom_point(data=na.omit(d84_mutationCount_perType_coding_snps_count[d84_mutationCount_perType_coding_snps_count$snp_type=="non-synonymous",]),aes(x=mouse2,y=frequency),color="#d9d9d9",fill="black",shape=21,size=3)+
facet_wrap(~diet,scales="free_x")+
scale_fill_manual(values=c("#92c5de","#2166ac","#fddbc7","#f4a582","#d6604d","#b2182b","#fee090","black"),
name="Mutation\ntype")+
theme_cowplot()+
theme(axis.text.x = element_text(angle=90,vjust=0.5))+
ylab("Mutation frequency")+
xlab("Mouse")
```
Table for mutation spectra
```{r}
#interactive/downloadable table
DT::datatable(data = d84_mutationCount_perType_spectra, extensions = "Buttons",
options = list(dom = "Blfrtip", buttons = "csv"))
```
Table for mutations per region type
```{r}
#interactive/downloadable table
DT::datatable(data = d84_mutationCount_perType_region, extensions = "Buttons",
options = list(dom = "Blfrtip", buttons = "csv"))
```
Table for mutations per SNP type
```{r}
#interactive/downloadable table
DT::datatable(data = d84_mutationCount_perType_coding_snps_count, extensions = "Buttons",
options = list(dom = "Blfrtip", buttons = "csv"))
```
# get parallel mutational targets
These were defined at the gene level and included any gene or intergenic region that was mutated in at least 2 mice and in which at least one of the mice had that gene mutated at a frequency of >0.05 (so the maximum frequency needs to be above 0.05). As there are three mice that likely have mutators in the population, I obtained the list of parallel mutational targets both including and excluding these mice. A total of 77 genes were identified as parallel.
#### what mutational targets are only parallel if mutator mice are included?
```{r}
d84_long<-readxl::read_xlsx("analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/breseq_output_day84_longFormat_woutLowConfMutations_c.xlsx")
#get dataframe with mutation frequency per gene, per mouse
d84_long_freqGene<-d84_long %>% group_by(seq_id,gene,description,sample,day,mouse,trt,diet) %>%
summarize(freq_gene=sum(frequency))
#get dataframe with maximum frequency and number of mice that carry mutation
parallel_genes_df<-d84_long_freqGene %>% group_by(seq_id,gene) %>%
summarise(max_freq=max(freq_gene),mouse_count=length(mouse)) %>%
subset(mouse_count>=2) %>% subset(max_freq>=0.05)
# get list of paralell genes
parallel_genes<-parallel_genes_df$gene
########Repeat above procedure without mutator mice##########
#get dataframe with mutation frequency per gene, per mouse
d84_long_freqGene_woutMutators<-d84_long[!d84_long$mouse %in% c("M1","M3","C4"),] %>% group_by(seq_id,gene,description,sample,day,mouse,trt,diet) %>%
summarize(freq_gene=sum(frequency))
#get dataframe with maximum frequency and number of mice that carry mutation
parallel_genes_woutMutators_df<-d84_long_freqGene_woutMutators %>%
group_by(seq_id,gene) %>%
summarise(max_freq=max(freq_gene),mouse_count=length(mouse)) %>%
subset(mouse_count>=2) %>% subset(max_freq>=0.05)
# get list of paralell genes
parallel_genes_woutMutators<-parallel_genes_woutMutators_df$gene
################
# get genes that are parallel only when mutator mice are included
parallel_genes_onlyMutators<-setdiff(parallel_genes,parallel_genes_woutMutators)
#get data frame for genes that are parallel when mutator mice are included
DT::datatable(parallel_genes_df %>% subset(gene %in% parallel_genes_onlyMutators) %>% left_join(.,d84_long_annot[c("gene","locus_name")] %>% distinct()))
```
From here, I keep going with the list of parallel genes that are not dependent on the mutator mice, as there were only three of these and all are low prevalence and low abundance.
#### get table of mutation frequencies for parallel mutations
```{r}
# read data
d84_long_annot<-read_xlsx("analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/breseq_output_day84_longFormat_woutLowConfMutations_withAnnotations_c.xlsx")
#get dataframe of parallel mutations and keep only essential columns
d84_long_parallel<-d84_long_annot[c(1:14)] %>% subset(gene %in% parallel_genes_woutMutators)
# writexl::write_xlsx(d84_long_parallel,"analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/breseq_output_day84_longFormat_parallelMutations_withAnnotations_c.xlsx")
```
### Number of parallel mutations and porportion of parallel mutations (relative to total) - Fig. 2B and S3A
```{r}
# create data for the parallel mutations
d84_parallel_mutationCount<-d84_long_parallel %>% group_by(mouse,diet) %>% summarize(mutation_tot_para=n())
d84_parallel_mutationCount$diet<-factor(d84_parallel_mutationCount$diet,levels=c("SD","WD","AD"))
p1<-ggplot(d84_parallel_mutationCount,aes(x=diet,y=mutation_tot_para,fill=diet))+
geom_boxplot(outlier.shape = NA, alpha=0.6)+
geom_quasirandom(aes(label=mouse),shape=21,size=3)+
xlab("Diet")+ylab("Mutation count (parallel)")+
scale_fill_manual(values=c("#4575b4","#d73027","#878787"))+
cowplot::theme_cowplot()+
background_grid(major="y")+
theme(legend.position = "none")+
ylim(0,50)
# get the proportion of parallel mutations per mouse
d84_mutationCount<-left_join(d84_mutationCount,d84_parallel_mutationCount)
d84_mutationCount$freq<-d84_mutationCount$mutation_tot_para/d84_mutationCount$mutation_tot
p2<-ggplot(d84_mutationCount,aes(x=diet,y=freq,fill=diet))+
geom_boxplot(outlier.shape = NA, alpha=0.6)+
geom_quasirandom(aes(label=mouse),shape=21,size=3)+
xlab("Diet")+ylab("Fraction of parallel muttions")+
scale_fill_manual(values=c("#4575b4","#d73027","#878787"))+
cowplot::theme_cowplot()+
background_grid(major="y")+
theme(legend.position = "none")+
ylim(0,1)
p1+p2
```
#### get table of mutation frequencies for parallel mutations summed per gene at day 84
```{r}
#get table with mutation frequencies summed per gene
d84_long_parallel_freqGene<-d84_long_parallel %>%
group_by(seq_id,gene,locus_name,description,sample,day,mouse,trt,diet) %>%
summarize(freq_gene=sum(frequency))
# writexl::write_xlsx(d84_long_parallel_freqGene,"analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/breseq_output_day84_longFormat_parallelMutations_freqPerGene_c.xlsx")
```
#### get table of the prevalence at which each mutational target is mutated at day 84 (across mice)
```{r}
# get the number of mice with a gene mutated per diet
d84_parallelMutationCounts_perDiet<-d84_long_parallel_freqGene %>%
group_by(seq_id,gene,locus_name,description,diet) %>%
summarize(mouse_no=length(mouse)) %>% tidyr::spread(.,diet,mouse_no)
d84_parallelMutationCounts_perDiet[is.na(d84_parallelMutationCounts_perDiet)]<-0
# writexl::write_xlsx(d84_parallelMutationCounts_perDiet,"analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/breseq_output_day84_longFormat_parallelMutations_prevalence_c.xlsx")
```
# Interactive plots of mutation distribution along the genome
## all Mutations
```{r}
library(plotly)
# get mouse nubmers as numbers
d84_long_annot$mouse_no<-as.factor(substr(d84_long_annot$mouse,2,3))
#reoder diet factor levels
d84_long_annot$diet<-factor(d84_long_annot$diet,levels=c("SD","WD","AD"))
p_chr<-ggplot(d84_long_annot[d84_long_annot$seq_id=="chromosome",],aes(x=position,y=frequency,color=mouse_no))+
geom_point(aes(label=gene,label2=description,label3=locus_name))+
scale_color_viridis_d("Mouse")+
facet_grid(diet~seq_id,scales = "free_x")+theme_cowplot()+background_grid(major="xy")
ggplotly(p_chr)
p_plas<-ggplot(d84_long_annot[d84_long_annot$seq_id=="plasmid",],aes(x=position,y=frequency,color=mouse_no))+
geom_point(aes(label=gene,label2=description))+
facet_grid(diet~seq_id,scales = "free_x")+theme_cowplot()+background_grid(major="xy")+
scale_color_viridis_d("Mouse")
ggplotly(p_plas)
```
## parallel Mutations
```{r}
library(plotly)
# get mouse nubmers as numbers
d84_long_parallel$mouse_no<-as.factor(substr(d84_long_parallel$mouse,2,3))
#reoder diet factor levels
d84_long_parallel$diet<-factor(d84_long_parallel$diet,levels=c("SD","WD","AD"))
p_chr<-ggplot(d84_long_parallel[d84_long_parallel$seq_id=="chromosome",],aes(x=position,y=frequency,color=mouse_no))+
geom_point(aes(label=gene,label2=description,label3=locus_name))+
scale_color_viridis_d("Mouse")+
facet_grid(diet~seq_id,scales = "free_x")+theme_cowplot()+background_grid(major="xy")
ggplotly(p_chr)
p_plas<-ggplot(d84_long_parallel[d84_long_parallel$seq_id=="plasmid",],aes(x=position,y=frequency,color=mouse_no))+
geom_point(aes(label=gene,label2=description))+
facet_grid(diet~seq_id,scales = "free_x")+theme_cowplot()+background_grid(major="xy")+
scale_color_viridis_d("Mouse")
ggplotly(p_plas)
```
## plot number of parallel mutations per mouse at day 84 - Fig. 2B
```{r,fig.width=10,fig.height=6}
# create data for the parallel mutations
d84_parallel_mutationCount<-d84_long_parallel %>% group_by(mouse,diet) %>% summarize(mutation_tot_para=n())
d84_parallel_mutationCount$diet<-factor(d84_parallel_mutationCount$diet,levels=c("SD","WD","AD"))
p<-ggplot(d84_parallel_mutationCount,aes(x=diet,y=mutation_tot_para,fill=diet))+
geom_boxplot(outlier.shape = NA, alpha=0.6)+
geom_quasirandom(aes(label=mouse),shape=21,size=3)+
xlab("Diet")+ylab("Mutation count (parallel)")+
scale_fill_manual(values=c("#4575b4","#d73027","#878787"))+
cowplot::theme_cowplot()+
background_grid(major="y")+
theme(legend.position = "none")+
ylim(0,50)
p
```
# plots of parallel mutational targets as a circular genome - Fig. 2C
```{r,fig.width=10,fig.height=10}
# prevalence of parallel mutations per gene per diet for genes mutated in more than 2 mice AND with mutation freqs>0.05
d84_long_parallel_freqGene<-read_xlsx("analysis/genomics/evolved_populations_breseq/D84_prokkaREF_MQ20_BQ30_v3ref/downstreamAnalysis/breseq_output_day84_longFormat_parallelMutations_freqPerGene_c.xlsx")
d84_parallelMutationCounts_perDiet_2mice0.05<-d84_long_parallel_freqGene %>%
filter(freq_gene>0.05) %>%
group_by(seq_id,gene,locus_name,description,diet) %>%
summarize(mouse_no=length(mouse)) %>% tidyr::spread(.,diet,mouse_no)
d84_parallelMutationCounts_perDiet_2mice0.05[is.na(d84_parallelMutationCounts_perDiet_2mice0.05)]<-0
d84_parallelMutationCounts_perDiet_2mice0.05<-d84_parallelMutationCounts_perDiet_2mice0.05 %>% mutate(tot_mice = rowSums(across(AD:WD)))
d84_parallelMutationCounts_perDiet_2mice0.05<-d84_parallelMutationCounts_perDiet_2mice0.05 %>% filter(tot_mice>=2)
###########
# create long format data frame from prevalence of parallel mutations per gene per diet. This will be useful later
d84_parallelMutationCounts_perDiet_long<- d84_parallelMutationCounts_perDiet_2mice0.05[1:7] %>% gather(.,key=diet,value = count,AD:WD)
#add a column for just presence/absence of mutations
d84_parallelMutationCounts_perDiet_long$presenceAbsence<-ifelse(d84_parallelMutationCounts_perDiet_long$count==0,0,1)
# add a column for average position of mutations in a gene, to get the position of each gene
d84_parallelMutationCounts_perDiet_long<-left_join(d84_parallelMutationCounts_perDiet_long,d84_long_parallel %>% group_by(gene) %>% summarise(position_av=round(mean(position),0)))
# create mock clone and add its info to data frame. This is useful to edit the size of the central white circle
newrow<-data.frame(seq_id=NA,gene=NA,locus_name=NA,description=NA,diet="A",count=0,presenceAbsence=0,position_av=0)
d84_parallelMutationCounts_perDiet_long<-bind_rows(d84_parallelMutationCounts_perDiet_long,newrow)
# transform diet to factor
d84_parallelMutationCounts_perDiet_long$diet<-as.factor(d84_parallelMutationCounts_perDiet_long$diet)