-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR_phylogeny_plotting.R
3490 lines (3009 loc) · 177 KB
/
R_phylogeny_plotting.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
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
#if (!require("BiocManager", quietly = TRUE))
# install.packages("BiocManager")
#BiocManager::install("ggtree")
install.packages("remotes")
remotes::install_github("djw533/micro.gen.extra")
library(ape)
library(phytools)
library(ggtree)
library(aplot)
library(treeio)
library(stringr)
library(ggbreak)
library(ggpubr)
library(ggplot2)
library(forcats)
library(dplyr)
library(micro.gen.extra)
raw_tree <- ape::read.tree("~/cluster1/projects/Penicillium/phylogenetics/busco_aa_mafft_trimal.concat.veryfasttree_doubleprecision_treefile")
ggtree(raw_tree)
##this step takes the full phylogeny, first scales down the size of the roqueforti region then collapses it into a triangle which represents the length of the branches min and max at the different triangle tips
##it also readds the tip labels in order to overwrite the previously added tips which gets messed up with the collapsing and re-scaling
#scaleClade(p1, 698, .1) %>% collapse(698, 'mixed', fill="darkgreen") + geom_tiplab(size=5, as_ylab = TRUE)
##additionally can add a right sided label to the node too
#p1+
# geom_hilight(node=698, type = "gradient", gradient.direction = 'rt',alpha = .5, to.bottom=T, fill="steelblue", extend=1)+
# geom_cladelab(node=698, label="roqueforti", angle=0, fontsize=4, vjust=.5)+
# geom_hilight(node=545, type = "gradient", gradient.direction = 'rt',alpha = .5, to.bottom=T, fill="darkgreen", extend=1)+
# geom_cladelab(node=545, label="Camemberti", angle=0, fontsize=4, vjust=.5)
###TO ROTATE A NODE (MUST USE THE GGTREE SPECIFIC TAG DUE TO CONFLICTS)
#ggtree::rotate(p1, 644)
#######extracting all of the nodes for each species
##rerooting tree based on the edge before fumigatus
nodes=grep("fumigatus", raw_tree$tip.label)
root=MRCA(raw_tree, nodes)
rooted_tree=ape::root.phylo(raw_tree, node = root-1)
rooted_tree=TreeTools::Preorder(rooted_tree)
##just change the species for one strain of citrinum (need to do this at the base of all the data too)
#rooted_tree$tiplabel=gsub('Psteckii.P2648', 'Pcitrinum.P2648', rooted_tree$tiplabel)
##get a list of all the species by taking everything before the first dot (they have all been named properly for this sort of purpose)
species= unique(do.call('rbind', strsplit(as.character(rooted_tree$tip.label),'.',fixed=TRUE))[,1] )
##create empty dataframe
sp_clades=as.data.frame(c())
for (sp in species) {
nodes=grep(paste(sp), rooted_tree$tip.label)
clade=MRCA(rooted_tree, nodes)
output=print(paste(sp,clade))
sp_clades=rbind(sp_clades, output)
}
##rename header temporarily
colnames(sp_clades) = "temp"
##split the column into two seperate columns with appropriate headers
sp_clades2=sp_clades %>% tidyr::separate(temp, c('species', 'node'))
##remove the unnamed, rubens, crustosum and simplicissimum
sp_clades2=subset(sp_clades2, species != "Prubens" & species != "Pherquei" & species != "Punnamed" )
##rename chrysogenum to chrysogenum/rubens
sp_clades2$species=gsub('Pchrysogenum', 'Pchrysogenum/Prubens', sp_clades2$species)
sp_clades2$species=gsub('Pmalachiteum', 'Pmalachiteum/Pherquei', sp_clades2$species)
#sp_clades2$species=gsub('Psolitum', 'Psolitum/Pcrustosum', sp_clades2$species)
#sp_clades2$species=gsub('Pjanthinellum', 'Pjanthinellum/Psimplicissimum', sp_clades2$species)
##extract each column as a list MIGHT NOT NEED THIS
species2=sp_clades2[,1]
clades=sp_clades2[,2]
##force a strict order for the plotting to maintain it
sp_clades2$species <- factor(sp_clades2$species, levels = unique(sp_clades2$species))
sp_clades2$node <- factor(sp_clades2$node, levels = unique(sp_clades2$node))
sp_clades3=data.frame(node=as.numeric(paste(clades)), species=species2)
##extract each column as a list MIGHT NOT NEED THIS
species2=sp_clades3[,1]
clades=sp_clades3[,2]
##now plot with higlighting values on top of rerooted tree by using the clades and species
##added a line around each species defined region
##currently have the tips/genome labelled to just to help
ggtree(rooted_tree, size=0.2) +
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% sp_clades2$node, fill=node), colour="black", linetype="dotted", size=0.25 ,alpha = 0.5, to.bottom=T, extend=0.01)+
geom_tiplab(size=2, as_ylab = TRUE)+
scale_fill_viridis_b()+
geom_rootedge(rootedge = 0.01, linewidth=0.2)+
geom_treescale(x=0.1, y=150, width=0.05, color='black')
##hilighting the species using cladelab too
ggtree(rooted_tree, size=0.2) +
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% sp_clades2$node, fill=node),alpha = 0.5, to.bottom=T, extend=0.01)+
geom_rootedge(rootedge = 0.01, linewidth=0.2)+
geom_cladelab(data=sp_clades3 , mapping=aes(node=node, label=species), fontsize = 1, barcolour="orange" )+
geom_treescale(x=0.1, y=150, width=0.05, color='black', offset = 5)
##highlighting the clades and placing the genome name at the tip at a reasonable size
ggtree(rooted_tree, size=0.2) +
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% sp_clades2$node, fill=node), colour="black", linetype="dotted", size=0.25 ,alpha = 0.5, to.bottom=T, extend=0.01)+
geom_tiplab(size=.25)+
scale_fill_viridis_b()+
geom_rootedge(rootedge = 0.01, linewidth=0.2)+
geom_treescale(x=0.1, y=150, width=0.05, color='black')
###now can add the starship information
##reading in DUF3435 data
captains=read.csv(file="cluster2/projects/Penicillium/starfish/allgenomes.genome_strain.TEMP_STARFISH_RESULTS.PLUS_PHYLOGENY_COMMENTS.PLUS_MYBs.ONLY_IN_PHYLO.tsv", header=T, sep='\t')
###need to install aplot and then use this to align a plot of the cpatins againt the tree
ggtree(rooted_tree, size=0.2) +
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% sp_clades2$node, fill=node),alpha = 0.5, to.bottom=T, extend=0.01)+
scale_fill_viridis_b()+
geom_rootedge(rootedge = 0.01, linewidth=0.2)+
geom_cladelab(data=sp_clades3 , mapping=aes(node=node, label=species), fontsize = 1, barcolour="grey" )+
geom_treescale(x=0.1, y=150, width=0.05, color='black', offset = 5)
##alt tree with bigger text and no different colours for the boxed and a box edge colour
ggtree(rooted_tree, size=1) +
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% sp_clades2$node),alpha = 0.1, to.bottom=T, extend=0.01, linetype="dotted", colour="firebrick")+
geom_rootedge(rootedge = 0.01, linewidth=1)+
geom_cladelab(data=sp_clades3 , mapping=aes(node=node, label=species), fontsize = 3, barcolour="grey" )+
geom_treescale(x=0.1, y=150, width=0.05, color='black', offset = 5)
##extract name of genomes in tree
##then subset captains data frame for only those genomes
genomes=rooted_tree$tip.label
captains=captains[grepl(paste(genomes, collapse="|"), captains$genome2),]
###the colour scheme for the isolation catagories=
## orange = clinical
## green = environment
## yellow = env-clinical
## purple = env-saline (only in aspergillus)
## light-blue = env-food
## blue = food-production
## black = unknown
## grey = NA
##plot tree to align with the starships
g=ggtree(rooted_tree, size=0.2) +
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% sp_clades2$node, fill=node),alpha = 0.5, to.bottom=T, extend=0.01)+
geom_rootedge(rootedge = 0.01, linewidth=0.2)+
geom_cladelab(data=sp_clades3 , mapping=aes(node=node, label=species), fontsize = 1, barcolour="orange" )+
geom_treescale(x=0.1, y=150, width=0.05, color='black', offset = 5)
##plotting the starships
p1=ggplot(captains, aes(genome2, captains))+
geom_col(aes(fill=isolation_simple, group=genome2))+
coord_flip()+
theme_tree2()+
scale_fill_manual(values = c("#E69F00", "#009E73","#F0E442","#56B4E9", "#0072B2", "#000000"))
p1 %>% insert_left(g, width=5)
##regerenate the tree, removing the aspergillus branches after using them to root
aspsp=c("Afumigatus.E142_GCA_949125165.1", "Afumigatus.A1160_ASM2422042v1", "Afumigatus.A1163_ASM15014v1", "Afumigatus.Af293_ASM265v1", "Afumigatus.Afir964_ASM2875220v1", "Afumigatus.C6_GCA_949125545.1", "Afumigatus.C87_GCA_949125185.1", "Aawamori.IFM58123_GCA_003850985.1", "Aniger.ATCC13157_ATCC13157_v1", "Aniger.KJC3_ASM2978390v1", "Aniger.H915-1_ASM174190v1", "Aniger.WU-2020_GCA_024862975.1", "Aniger.KYF3_ASM2978392v1", "Aniger.JA-B-2022_ASM2958203v1", "Aluchuensis.IFO4308_GCA_016861625.1", "Aluchuensis.RIB2601_GCA_016865315.1", "Atubingensis.C2-2_ASM1061485v1","Atubingensis.WU-2223L_ASM1334032v1","Aterreus.ATCC20542_ASM1680841v1","Aterreus.M6925_ASM983442v1","Aflavus.A5P1_ASM2958205v1","Aflavus.CA14_ASM1478422v2","Aflavus.AF13_ASM1411748v1","Aflavus.NRRL3357-2_ASM901741v1","Aflavus.NRRL3357_ASM1411746v1","Aoryzae.RIB40_ASM18445v3","Aoryzae.KSS2_ASM803225v1","Aoryzae.SU-16_ASM985666v1","Aoryzae.BCC7051_ASM200794v1","Aoryzae.KBP3_ASM803205v1","Aparasiticus.MRI410_ASM2850576v1","Asojae.SMF134_ASM827498v1")
rooted_trim_tree=drop.tip(rooted_tree, aspsp)
##get a list of all the species by taking everything before the first dot (they have all been named properly for this sort of purpose)
species= unique(do.call('rbind', strsplit(as.character(rooted_trim_tree$tip.label),'.',fixed=TRUE))[,1] )
##create empty dataframe
sp_clades=as.data.frame(c())
for (sp in species) {
nodes=grep(paste(sp), rooted_trim_tree$tip.label)
clade=MRCA(rooted_trim_tree, nodes)
output=print(paste(sp,clade))
sp_clades=rbind(sp_clades, output)
}
##rename header temporarily
colnames(sp_clades) = "temp"
##split the column into two seperate columns with appropriate headers
sp_clades2=sp_clades %>% tidyr::separate(temp, c('species', 'node'))
##remove the unnamed, rubens, crustosum and simplicissimum
sp_clades2=subset(sp_clades2, species != "Prubens" & species != "Pherquei" & species != "Punnamed" )
##rename chrysogenum to chrysogenum/rubens
sp_clades2$species=gsub('Pchrysogenum', 'Pchrysogenum/Prubens', sp_clades2$species)
sp_clades2$species=gsub('Pmalachiteum', 'Pmalachiteum/Pherquei', sp_clades2$species)
#sp_clades2$species=gsub('Psolitum', 'Psolitum/Pcrustosum', sp_clades2$species)
#sp_clades2$species=gsub('Pjanthinellum', 'Pjanthinellum/Psimplicissimum', sp_clades2$species)
##extract each column as a list MIGHT NOT NEED THIS
species2=sp_clades2[,1]
clades=sp_clades2[,2]
##force a strict order for the plotting to maintain it
sp_clades2$species <- factor(sp_clades2$species, levels = unique(sp_clades2$species))
sp_clades2$node <- factor(sp_clades2$node, levels = unique(sp_clades2$node))
sp_clades3=data.frame(node=as.numeric(paste(clades)), species=species2)
##extract each column as a list MIGHT NOT NEED THIS
species2=sp_clades3[,1]
clades=sp_clades3[,2]
ggtree(rooted_trim_tree, size=0.1)+
geom_rootedge(rootedge = 0.02, linewidth=0.2)+
geom_treescale(x=0.15, y=200, width=0.01, color='black', offset = 5)+
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% sp_clades2$node, fill=node),alpha = 0.5, to.bottom=T, extend=0.01)+
geom_cladelab(data=sp_clades3 , mapping=aes(node=node, label=species), fontsize = 1, barcolour="orange" )
g=ggtree(rooted_trim_tree, size=0.1)+
geom_rootedge(rootedge = 0.02, linewidth=0.2)+
geom_treescale(x=0.15, y=200, width=0.01, color='black', offset = 5)+
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% sp_clades2$node, fill=node),alpha = 0.5, to.bottom=T, extend=0.01)+
geom_cladelab(data=sp_clades3 , mapping=aes(node=node, label=species), fontsize = 1, barcolour="orange" )
genomes=rooted_trim_tree$tip.label
captains2=captains[grepl(paste(genomes, collapse="|"), captains$genome2), ]
p1=ggplot(captains2, aes(genome2, captains))+
geom_col(aes(fill=isolation_simple, group=genome2), show.legend = F)+
coord_flip()+
theme_tree2()+
scale_fill_manual(values = c("#E69F00", "#009E73","#F0E442","#56B4E9", "#0072B2", "#000000"))
p1 %>% insert_left(g, width=2)
##plotting all together just split by isolation orgigins
ggplot(subset(captains2, isolation_simple != "" & isolation_simple != "unknown"), aes(x=isolation_simple, y=captains))+
gghalves::geom_half_boxplot(side = "r", outlier.shape = "", width=0.4, position=position_nudge(x = .1))+
ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
geom_jitter(width=0.05, alpha=0.6, aes(colour=isolation_simple), show.legend = FALSE)+
scale_colour_manual(values = c("#E69F00", "#009E73","#F0E442","#56B4E9", "#0072B2", "#000000"))+
theme_pubr(x.text.angle = 35)+
labs(x="")
####one last time, generating trees,
###but this time using the captain information file to remove a bunch of genomes
###removing genomes that are not able to be made public for roqueforti, solitum and crustosum
toremove=subset(captains, species == "Proqueforti" & supp == "" | species == "Psolitum" & supp == "" | species == "Pcrustosum" & supp == "" )$genome2
captains2_public=subset(captains2, species == "Proqueforti" & supp != "" | species == "Psolitum" & supp != "" | species == "Pcrustosum" & supp != "" | species != "Proqueforti" & species != "Psolitum" & species != "Pcrustosum" & supp == "" )
rooted_trim_tree_public=drop.tip(rooted_trim_tree, toremove, trim.internal = TRUE, subtree = FALSE, root.edge = 0, rooted = is.rooted(rooted_trim_tree))
species= unique(do.call('rbind', strsplit(as.character(rooted_trim_tree_public$tip.label),'.',fixed=TRUE))[,1] )
sp_clades=as.data.frame(c())
for (sp in species) {
nodes=grep(paste(sp), rooted_trim_tree_public$tip.label)
clade=MRCA(rooted_trim_tree_public, nodes)
output=print(paste(sp,clade))
sp_clades=rbind(sp_clades, output)
}
colnames(sp_clades) = "temp"
sp_clades2=sp_clades %>% tidyr::separate(temp, c('species', 'node'))
sp_clades2=subset(sp_clades2, species != "Prubens" & species != "Pherquei" & species != "Punnamed" )
sp_clades2$species=gsub('Pchrysogenum', 'Pchrysogenum/Prubens', sp_clades2$species)
sp_clades2$species=gsub('Pmalachiteum', 'Pmalachiteum/Pherquei', sp_clades2$species)
species2=sp_clades2[,1]
clades=sp_clades2[,2]
sp_clades2$species <- factor(sp_clades2$species, levels = unique(sp_clades2$species))
sp_clades2$node <- factor(sp_clades2$node, levels = unique(sp_clades2$node))
sp_clades3=data.frame(node=as.numeric(paste(clades)), species=species2)
species2=sp_clades3[,1]
clades=sp_clades3[,2]
ggtree(rooted_trim_tree_public, size=0.1)+
geom_rootedge(rootedge = 0.02, linewidth=0.2)+
geom_treescale(x=0.15, y=200, width=0.01, color='black', offset = 5)+
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% sp_clades2$node, fill=node),alpha = 0.5, to.bottom=T, extend=0.01)+
geom_cladelab(data=sp_clades3 , mapping=aes(node=node, label=species), fontsize = 1, barcolour="orange" )
###saved as A4 pdf ('phylogeny_complete.penicillium_public')
##same as above but with the node support values for all branches added (need to manually remove those inside a species branching)
ggtree(rooted_trim_tree_public, size=0.1)+
geom_rootedge(rootedge = 0.02, linewidth=0.2)+
geom_treescale(x=0.15, y=200, width=0.01, color='black', offset = 5)+
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% sp_clades2$node, fill=node),alpha = 0.5, to.bottom=T, extend=0.01)+
geom_cladelab(data=sp_clades3 , mapping=aes(node=node, label=species), fontsize = 1, barcolour="orange" )+
geom_text2(aes(label=label, subset=!isTip), hjust=-.2, size=0.5)
###saved as A4 pdf ('phylogeny_complete.penicillium_public.support')
##plot the same phylogeny but with only boxes around the species levels and with the genome name next to the tip
ggtree(rooted_trim_tree_public, size=0.2) +
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% sp_clades2$node, fill=node), colour="black", linetype="dotted", size=0.25 ,alpha = 0.5, to.bottom=T, extend=0.01)+
geom_tiplab(size=.25)+
scale_fill_viridis_b()+
geom_rootedge(rootedge = 0.01, linewidth=0.2)+
geom_treescale(x=0.1, y=150, width=0.05, color='black')
###saved as A4 pdf ('phylogeny_complete.penicillium_public.genome_lab')
g=ggtree(rooted_trim_tree_public, size=0.1)+
geom_rootedge(rootedge = 0.02, linewidth=0.2)+
geom_treescale(x=0.15, y=200, width=0.01, color='black', offset = 5)+
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% sp_clades2$node, fill=node),alpha = 0.5, to.bottom=T, extend=0.01)+
geom_cladelab(data=sp_clades3 , mapping=aes(node=node, label=species), fontsize = 1, barcolour="orange" )
p1=ggplot(captains2_public, aes(genome2, captains))+
geom_col(aes(fill=isolation_simple, group=genome2), show.legend = F)+
coord_flip()+
theme_tree2()+
scale_fill_manual(values = c("#E69F00", "#009E73","#F0E442","#56B4E9", "#0072B2", "#000000"))
p1 %>% insert_left(g, width=2)
###saved as A4 pdf ('Penicillium_phylogeny_public.captains_isolation')
##CAN ALSO SAVE THE ABOVE TREE FOR ANOTHER PLOT WITH ASPERGILLUS ALONGSIDE
PENICILLIUMsupport=ggtree(rooted_trim_tree_public, size=0.1)+
geom_rootedge(rootedge = 0.02, linewidth=0.2)+
geom_treescale(x=0.15, y=200, width=0.01, color='black', offset = 5)+
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% sp_clades2$node, fill=node),alpha = 0.5, to.bottom=T, extend=0.01)+
geom_cladelab(data=sp_clades3 , mapping=aes(node=node, label=species), fontsize = 1, barcolour="orange" )+
geom_text2(aes(label=label, subset=!isTip), hjust=-.2, size=0.5)
PENICILLIUM=ggtree(rooted_trim_tree_public, size=0.1)+
geom_rootedge(rootedge = 0.02, linewidth=0.2)+
geom_treescale(x=0.15, y=200, width=0.01, color='black', offset = 5)+
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% sp_clades2$node, fill=node),alpha = 0.5, to.bottom=T, extend=0.01)+
geom_cladelab(data=sp_clades3 , mapping=aes(node=node, label=species), fontsize = 1, barcolour="orange" )
##alternative plot with a smaller bar graph and reducing the x axis to remove the long (and false) branches
p1=ggplot(captains2_public, aes(genome2, captains))+
geom_col(aes(fill=isolation_simple, group=genome2), show.legend = F)+
coord_flip()+
theme_tree2()+
scale_fill_manual(values = c("#E69F00", "#009E73","#F0E442","#56B4E9", "#0072B2", "#000000"))+
ylim(0,50)
p1 %>% insert_left(g, width=5)
##and saving as another i,age to join with aspergillus
PENICILLIUM2support=p1 %>% insert_left(PENICILLIUMsupport, width=5)
PENICILLIUM2=p1 %>% insert_left(PENICILLIUM, width=5)
##plotting all together just split by isolation origins
##removing unknown, NA and clinical (only had 1 clinical sample)
##also removing the outliers (that look to be truly errors) of janthinelleum
ggplot(subset(captains2_public, isolation_simple != "" & isolation_simple != "unknown" & isolation_simple != "clinical" & captains < 50), aes(x=isolation_simple, y=captains))+
gghalves::geom_half_boxplot(side = "r", outlier.shape = "", width=0.4, position=position_nudge(x = .1))+
ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
geom_jitter(width=0.05, alpha=0.6, aes(colour=isolation_simple), show.legend = FALSE)+
scale_colour_manual(values = c("#009E73","#F0E442","#56B4E9", "#0072B2", "#000000"))+
theme_pubr(x.text.angle = 35)+
labs(x="")
###saved as svg 300x600 ('Penicillium_no_phylogeny.captains_isolation.svg')
##can randomly sample a subset of sets within the 'isolation_simple' group
captains2_public_random=do.call(rbind,replicate(1000, subset(captains2_public, isolation_simple != "" & isolation_simple != "unknown" & isolation_simple != "clinical" & isolation_simple != "environment-clinical" & isolation_simple != "environment-food" ) %>% group_by(isolation_simple) %>% slice_sample(n=25) %>% summarise(mean_captains=mean(captains)), simplify=FALSE))
##plot this
ggplot(subset(captains2_public_random, isolation_simple != "" & isolation_simple != "unknown" & isolation_simple != "clinical"), aes(x=isolation_simple, y=mean_captains))+
gghalves::geom_half_boxplot(side = "r", outlier.shape = "", width=0.4, position=position_nudge(x = .1))+
ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
geom_jitter(width=0.05, alpha=0.6, aes(colour=isolation_simple), show.legend = FALSE)+
scale_colour_manual(values = c("#009E73", "#0072B2", "#000000"))+
theme_pubr(x.text.angle = 45)+
labs(x="")
###saved as svg 250x450 ('Penicillium_public_randomisation.captains_isolation')
#########CASEI/BIF/CAM ###############
###will want to take subsets to align alongside other data
##can use the fasciculata1 genome graph group to begin
nodes=grep("caseifulvum|biforme|camemberti", rooted_tree$tip.label)
##this can add a grouping label to the full tree
rooted_tree = groupOTU(rooted_tree, nodes)
##now can subset based on the node of the MRCA
##give the nodes based on the grep above using species names
##can also replace the variable 'nodes' with just a node number
clade = MRCA(rooted_tree, nodes)
subset_tree = tree_subset(rooted_tree, clade, levels_back = 2)
##rotate the node of biformer and casei/camemberti
##now redo all the steps for the hilighting of species etc
##get a list of all the species by taking everything before the first dot (they have all been named properly for this sort of purpose)
subset_species=unique(do.call('rbind', strsplit(as.character(subset_tree$tip.label),'.',fixed=TRUE))[,1] )
##create empty dataframe
subset_sp_clades=as.data.frame(c())
for (sp in subset_species) {
nodes=grep(paste(sp), subset_tree$tip.label)
clade=MRCA(subset_tree, nodes)
output=print(paste(sp,clade))
subset_sp_clades=rbind(subset_sp_clades, output)
}
##rename header temporarily
colnames(subset_sp_clades) = "temp"
##split the column into two seperate columns with appropriate headers
subset_sp_clades2=subset_sp_clades %>% tidyr::separate(temp, c('species', 'node'))
##extract each column as a list MIGHT NOT NEED THIS
subset_species2=subset_sp_clades2[,1]
subset_clades=subset_sp_clades2[,2]
##force a strict order for the plotting to maintain it
subset_sp_clades2$species <- factor(subset_sp_clades2$species, levels = unique(subset_sp_clades2$species))
subset_sp_clades2$node <- factor(subset_sp_clades2$node, levels = unique(subset_sp_clades2$node))
subset_sp_clades3=data.frame(node=as.numeric(paste(subset_clades)), species=subset_species2)
##extract each column as a list MIGHT NOT NEED THIS
subset_species2=subset_sp_clades3[,2]
subset_clades=subset_sp_clades3[,1]
###now plot all together
##need to always adjust the fixed values such as geom_hilight extend, treescale position and size and geom_rootedge length
#g=ggtree(subset_tree, size=0.2) +
# guides(fill = "none")+
# geom_hilight(mapping=aes(subset=node %in% subset_sp_clades2$node, fill=node),alpha = 0.5, to.bottom=T, extendto=1, colour="black", linetype="dotted", size=0.25)+
# scale_fill_viridis_b()+
# geom_rootedge(rootedge = 0.0005, linewidth=0.2)+
# geom_cladelab(data=subset_sp_clades3 , mapping=aes(node=node, label=species), fontsize = 2, barcolour="grey" )+
# geom_treescale(x=0.0001, y=100, width=0.001, color='black', offset = 5)+
# ggplot2::xlim(-.0005, 0.0052)
##without colours highlighting the clades
g=ggtree(subset_tree, size=0.2) +
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% subset_sp_clades2$node),alpha = 0.1, to.bottom=T, linetype="dotted", colour="firebrick")+
geom_rootedge(rootedge = 0.0005, linewidth=0.2)+
geom_cladelab(data=subset_sp_clades3 , mapping=aes(node=node, label=species), fontsize = 2, barcolour="grey", angle=270, vjust=-0.5, hjust=0.5 )+
geom_treescale(x=0.0001, y=100, width=0.001, color='black', offset = 2)+
ggplot2::xlim(-.0005, 0.0052)
##extract name of genomes in tree
##then subset captains data frame for only those genomes
genomes=subset_tree$tip.label
captains_subset=captains[grepl(paste(genomes, collapse="|"), captains$genome2), ]
p1=ggplot(captains_subset, aes(genome2, captains))+
geom_col(aes(fill=isolation_simple, group=genome2), show.legend = F)+
coord_flip()+
theme_tree2()+
scale_fill_manual(values = c("#009E73","#56B4E9", "#0072B2", "#000000"))+
labs(fill="isolation")
p1 %>% insert_left(g)
##saved as A5
##now plotting grouped captain counts
##first order according to phylogeny
captains_subset$species=factor(captains_subset$species, levels=c('Ppalitans', 'Pfuscoglaucum', 'Pcaseifulvum', 'Pcamemberti', 'Pbiforme'))
#ggplot(captains_subset, aes(x=species, y=captains))+
# geom_boxplot(outlier.shape = "", width=0.3)+
# geom_jitter(width=0.075, alpha=0.5, aes(colour=isolation_simple))+
# ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
# theme_pubr(x.text.angle = 65)
ggplot(captains_subset, aes(x=species, y=captains))+
gghalves::geom_half_boxplot(side = "r", outlier.shape = "", width=0.4, position=position_nudge(x = .1))+
ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
geom_jitter(width=0.05, alpha=0.6, aes(colour=isolation_simple), show.legend = FALSE)+
scale_colour_manual(values = c("#009E73","#56B4E9", "#0072B2", "#000000"))+
theme_pubr()+
coord_flip()+
labs(x="")
##split by isolation environment WILL NOT USE THIS
ggplot(captains_subset, aes(x=isolation_simple, y=captains))+
gghalves::geom_half_boxplot(side = "r", outlier.shape = "", width=0.4, position=position_nudge(x = .1))+
geom_jitter(width=0.05, alpha=0.6, aes(colour=isolation_simple), show.legend = FALSE)+
scale_colour_manual(values = c("#009E73","#56B4E9", "#0072B2", "#000000"))+
ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
theme_pubr(x.text.angle = 65)+
facet_grid(species ~ .)
###trying to generate a cleaner image for presentation by collapsing nodes and only showing the boxplots
##this tree will have no labels initially, only manually done by by after collapsing etc
g=ggtree(subset_tree, size=0.5) +
guides(fill = "none")+
geom_treescale(x=0.0001, y=31, width=0.001, color='black', offset = 0.5)+
ggplot2::xlim(-.0005, 0.0052)
g2=scaleClade(g, 204, .1) %>%
scaleClade(296, .1) %>%
scaleClade(286, .1) %>%
scaleClade(156, .1) %>%
scaleClade(177, .1) %>%
collapse(204, 'mixed', fill="firebrick", alpha=0.75) %>%
collapse(296, 'mixed', fill="firebrick", alpha=0.75) %>%
collapse(286, 'mixed', fill="firebrick", alpha=0.75) %>%
collapse(177, 'mixed', fill="firebrick", alpha=0.75) %>%
collapse(156, 'mixed', fill="firebrick", alpha=0.75) +
geom_cladelabel(node=204, label="P.biforme", color='black', fontsize=3)+
geom_cladelabel(node=296, label="P.camemberti", color='black', fontsize=3)+
geom_cladelabel(node=286, label="P.caseifulvum", color='black', fontsize=3)+
geom_cladelabel(node=177, label="P.fuscoglaucum", color='black', fontsize=3)+
geom_cladelabel(node=156, label="P.palitans", color='black', fontsize=3)+
geom_rootedge(rootedge = -0.0005, linewidth=0.5)
##same plot as from above
p2=ggplot(captains_subset, aes(x=species, y=captains))+
gghalves::geom_half_boxplot(side = "r", outlier.shape = "", width=0.4, position=position_nudge(x = .1))+
ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
geom_jitter(width=0.05, alpha=0.6, aes(colour=isolation_simple), show.legend = FALSE)+
scale_colour_manual(values = c("#009E73","#56B4E9", "#0072B2", "#000000"))+
theme_pubr()+
coord_flip()+
labs(x="")
multiplot(g2, p2, ncol = 2)
##saved as pdf, 3x11.69 inches, landscape (so short and wide)
######### SOLITUM ###############
###will want to take subsets to align alongside other data
##can use the fasciculata1 genome graph group to begin
nodes=grep("solitum", rooted_tree$tip.label)
##this can add a grouping label to the full tree
rooted_tree = groupOTU(rooted_tree, nodes)
##now can subset based on the node of the MRCA
##give the nodes based on the grep above using species names
##can also replace the variable 'nodes' with just a node number
clade = MRCA(rooted_tree, nodes)
subset_tree = tree_subset(rooted_tree, clade, levels_back = 1)
##rotate the node of biformer and casei/camemberti
##now redo all the steps for the hilighting of species etc
##get a list of all the species by taking everything before the first dot (they have all been named properly for this sort of purpose)
subset_species=unique(do.call('rbind', strsplit(as.character(subset_tree$tip.label),'.',fixed=TRUE))[,1] )
##create empty dataframe
subset_sp_clades=as.data.frame(c())
for (sp in subset_species) {
nodes=grep(paste(sp), subset_tree$tip.label)
clade=MRCA(subset_tree, nodes)
output=print(paste(sp,clade))
subset_sp_clades=rbind(subset_sp_clades, output)
}
##rename header temporarily
colnames(subset_sp_clades) = "temp"
##split the column into two seperate columns with appropriate headers
subset_sp_clades2=subset_sp_clades %>% tidyr::separate(temp, c('species', 'node'))
##extract each column as a list MIGHT NOT NEED THIS
subset_species2=subset_sp_clades2[,1]
subset_clades=subset_sp_clades2[,2]
##force a strict order for the plotting to maintain it
subset_sp_clades2$species <- factor(subset_sp_clades2$species, levels = unique(subset_sp_clades2$species))
subset_sp_clades2$node <- factor(subset_sp_clades2$node, levels = unique(subset_sp_clades2$node))
subset_sp_clades3=data.frame(node=as.numeric(paste(subset_clades)), species=subset_species2)
##extract each column as a list MIGHT NOT NEED THIS
subset_species2=subset_sp_clades3[,2]
subset_clades=subset_sp_clades3[,1]
###now plot all together
##need to always adjust the fixed values such as geom_hilight extend, treescale position and size and geom_rootedge length
#g=ggtree(subset_tree, size=0.2) +
# guides(fill = "none")+
# geom_hilight(mapping=aes(subset=node %in% subset_sp_clades2$node, fill=node),alpha = 0.5, to.bottom=T, extendto=1, colour="black", linetype="dotted", size=0.25)+
# scale_fill_viridis_b()+
# geom_rootedge(rootedge = 0.0005, linewidth=0.2)+
# geom_cladelab(data=subset_sp_clades3 , mapping=aes(node=node, label=species), fontsize = 2, barcolour="grey" )+
# geom_treescale(x=0.0001, y=100, width=0.001, color='black', offset = 5)+
# ggplot2::xlim(-.0005, 0.0052)
##without colours highlighting the clades
g=ggtree(subset_tree, size=0.2) +
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% subset_sp_clades2$node),alpha = 0.1, to.bottom=T, linetype="dotted", colour="firebrick")+
geom_rootedge(rootedge = 0.0005, linewidth=0.2)+
geom_cladelab(data=subset_sp_clades3 , mapping=aes(node=node, label=species), fontsize = 2, barcolour="grey", angle=270, vjust=-0.5, hjust=0.5 )+
geom_treescale(x=0.005, y=45, width=0.001, color='black', offset = 1)+
ggplot2::xlim(-.0005, 0.02)
##extract name of genomes in tree
##then subset captains data frame for only those genomes
genomes=subset_tree$tip.label
captains_subset=captains[grepl(paste(genomes, collapse="|"), captains$genome2), ]
p1=ggplot(captains_subset, aes(genome2, captains))+
geom_col(aes(fill=isolation_simple, group=genome2), show.legend = F)+
coord_flip()+
theme_tree2()+
scale_fill_manual(values = c("#009E73","#F0E442","#56B4E9", "#0072B2", "#000000"))+
labs(fill="isolation")
p1 %>% insert_left(g)
##saved as A5
##now plotting grouped captain counts
##first order according to phylogeny
captains_subset$species=factor(captains_subset$species, levels=c('Psolitum','Pcrustosum'))
#ggplot(captains_subset, aes(x=species, y=captains))+
# geom_boxplot(outlier.shape = "", width=0.3)+
# geom_jitter(width=0.075, alpha=0.5, aes(colour=isolation_simple))+
# ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
# theme_pubr(x.text.angle = 65)
ggplot(captains_subset, aes(x=species, y=captains))+
gghalves::geom_half_boxplot(side = "r", outlier.shape = "", width=0.4, position=position_nudge(x = .1))+
ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
geom_jitter(width=0.05, alpha=0.6, aes(colour=isolation_simple), show.legend = FALSE)+
scale_colour_manual(values = c("#009E73","#F0E442","#56B4E9", "#0072B2", "#000000"))+
theme_pubr()+
coord_flip()+
labs(x="")
##split by isolation environment
ggplot(captains_subset, aes(x=isolation_simple, y=captains))+
gghalves::geom_half_boxplot(side = "r", outlier.shape = "", width=0.4, position=position_nudge(x = .1))+
geom_jitter(width=0.05, alpha=0.6, aes(colour=isolation_simple), show.legend = FALSE)+
scale_colour_manual(values = c("#009E73","#F0E442","#56B4E9", "#0072B2", "#000000"))+
ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
theme_pubr(x.text.angle = 65)+
facet_grid(species ~ .)
##for the figure save as
###trying to generate a cleaner image for presentation by collapsing nodes and only showing the boxplots
##this tree will have no labels initially, only manually done by by after collapsing etc
g=ggtree(subset_tree, size=0.5) +
guides(fill = "none")+
geom_treescale(x=0.005, y=19, width=0.001, color='black', offset = .15)+
ggplot2::xlim(-.0005, 0.02)
g2=scaleClade(g, 62, .1) %>%
scaleClade(89, .1) %>%
collapse(89, 'mixed', fill="firebrick", alpha=0.75) %>%
collapse(62, 'mixed', fill="firebrick", alpha=0.75) +
geom_cladelabel(node=89, label="P.crustosum", color='black', fontsize=3)+
geom_cladelabel(node=62, label="P.solitum", color='black', fontsize=3)+
geom_rootedge(rootedge = -0.0005, linewidth=0.5)
##same plot as from above
p2=ggplot(captains_subset, aes(x=species, y=captains))+
gghalves::geom_half_boxplot(side = "r", outlier.shape = "", width=0.4, position=position_nudge(x = .1))+
ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
geom_jitter(width=0.05, alpha=0.6, aes(colour=isolation_simple), show.legend = FALSE)+
scale_colour_manual(values = c("#009E73","#F0E442","#56B4E9", "#0072B2", "#000000"))+
theme_pubr()+
coord_flip()+
labs(x="")
multiplot(g2, p2, ncol = 2)
##saved as pdf, 3x11.69 inches, landscape (so short and wide)
##for the same plot to add to the side
##same plot as above splitting the isolation origins of solitum
##except changing the x axis label angle and removing the x axis title
ggplot(subset(captains_subset, species=="Psolitum" & isolation_simple != "NA"), aes(x=isolation_simple, y=captains))+
gghalves::geom_half_boxplot(side = "r", outlier.shape = "", width=0.4, position=position_nudge(x = .1))+
geom_jitter(width=0.05, alpha=0.6, aes(colour=isolation_simple), show.legend = FALSE)+
scale_colour_manual(values = c("#009E73","#56B4E9", "#0072B2", "#000000"))+
ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
theme_pubr(x.text.angle = 35)+
labs(x="")
##saved as svg 200x350 captains_boxplot.isolation.sol_only
#######REDOING THE WHOLE SOLITUM/CRUSTOSUM SET BUT THIS TIME WITH THE PUBLIC REDUCED DATASET
#########ROQUEFORTI ###############
###will want to take subsets to align alongside other data
##can use the fasciculata1 genome graph group to begin
nodes=grep("roqueforti", rooted_tree$tip.label)
##this can add a grouping label to the full tree
rooted_tree = groupOTU(rooted_tree, nodes)
##now can subset based on the node of the MRCA
##give the nodes based on the grep above using species names
##can also replace the variable 'nodes' with just a node number
clade = MRCA(rooted_tree, nodes)
subset_tree = tree_subset(rooted_tree, clade, levels_back = 2)
##rotate the node of biformer and casei/camemberti
##now redo all the steps for the hilighting of species etc
##get a list of all the species by taking everything before the first dot (they have all been named properly for this sort of purpose)
subset_species=unique(do.call('rbind', strsplit(as.character(subset_tree$tip.label),'.',fixed=TRUE))[,1] )
##create empty dataframe
subset_sp_clades=as.data.frame(c())
for (sp in subset_species) {
nodes=grep(paste(sp), subset_tree$tip.label)
clade=MRCA(subset_tree, nodes)
output=print(paste(sp,clade))
subset_sp_clades=rbind(subset_sp_clades, output)
}
##rename header temporarily
colnames(subset_sp_clades) = "temp"
##split the column into two seperate columns with appropriate headers
subset_sp_clades2=subset_sp_clades %>% tidyr::separate(temp, c('species', 'node'))
##extract each column as a list MIGHT NOT NEED THIS
subset_species2=subset_sp_clades2[,1]
subset_clades=subset_sp_clades2[,2]
##force a strict order for the plotting to maintain it
subset_sp_clades2$species <- factor(subset_sp_clades2$species, levels = unique(subset_sp_clades2$species))
subset_sp_clades2$node <- factor(subset_sp_clades2$node, levels = unique(subset_sp_clades2$node))
subset_sp_clades3=data.frame(node=as.numeric(paste(subset_clades)), species=subset_species2)
##extract each column as a list MIGHT NOT NEED THIS
subset_species2=subset_sp_clades3[,2]
subset_clades=subset_sp_clades3[,1]
###now plot all together
##need to always adjust the fixed values such as geom_hilight extend, treescale position and size and geom_rootedge length
#g=ggtree(subset_tree, size=0.2) +
# guides(fill = "none")+
# geom_hilight(mapping=aes(subset=node %in% subset_sp_clades2$node, fill=node),alpha = 0.5, to.bottom=T, extendto=1, colour="black", linetype="dotted", size=0.25)+
# scale_fill_viridis_b()+
# geom_rootedge(rootedge = 0.0005, linewidth=0.2)+
# geom_cladelab(data=subset_sp_clades3 , mapping=aes(node=node, label=species), fontsize = 2, barcolour="grey" )+
# geom_treescale(x=0.0001, y=100, width=0.001, color='black', offset = 5)+
# ggplot2::xlim(-.0005, 0.0052)
##without colours highlighting the clades
g=ggtree(subset_tree, size=0.2) +
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% subset_sp_clades2$node),alpha = 0.1, to.bottom=T, linetype="dotted", colour="firebrick")+
geom_rootedge(rootedge = 0.0005, linewidth=0.2)+
geom_cladelab(data=subset_sp_clades3 , mapping=aes(node=node, label=species), fontsize = 2, barcolour="grey", angle=270, vjust=-0.5, hjust=0.5 )+
geom_treescale(x=0.0001, y=100, width=0.001, color='black', offset = 5)+
ggplot2::xlim(-.0005, 0.012)
##extract name of genomes in tree
##then subset captains data frame for only those genomes
genomes=subset_tree$tip.label
captains_subset=captains[grepl(paste(genomes, collapse="|"), captains$genome2), ]
p1=ggplot(captains_subset, aes(genome2, captains))+
geom_col(aes(fill=isolation_simple, group=genome2), show.legend = F)+
coord_flip()+
theme_tree2()+
scale_fill_manual(values = c("#009E73","#56B4E9", "#0072B2", "#000000"))+
labs(fill="isolation")
p1 %>% insert_left(g)
##saved as A5
##now plotting grouped captain counts
##first order according to phylogeny
captains_subset$species=factor(captains_subset$species, levels=c('Pcarneum', 'Ppsychrosexualis', 'Proqueforti'))
#ggplot(captains_subset, aes(x=species, y=captains))+
# geom_boxplot(outlier.shape = "", width=0.3)+
# geom_jitter(width=0.075, alpha=0.5, aes(colour=isolation_simple))+
# ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
# theme_pubr(x.text.angle = 65)
ggplot(captains_subset, aes(x=species, y=captains))+
gghalves::geom_half_boxplot(side = "r", outlier.shape = "", width=0.4, position=position_nudge(x = .1))+
ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
geom_jitter(width=0.05, alpha=0.6, aes(colour=isolation_simple), show.legend = FALSE)+
scale_colour_manual(values = c("#009E73","#56B4E9", "#0072B2", "#000000"))+
theme_pubr()+
coord_flip()+
labs(x="")
##split by isolation environment
ggplot(captains_subset, aes(x=isolation_simple, y=captains))+
gghalves::geom_half_boxplot(side = "r", outlier.shape = "", width=0.4, position=position_nudge(x = .1))+
ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
geom_jitter(width=0.05, alpha=0.6, aes(colour=isolation_simple), show.legend = FALSE)+
scale_colour_manual(values = c("#009E73","#56B4E9", "#0072B2", "#000000"))+
theme_pubr()+
coord_flip()+
labs(x="")+
facet_grid(~fct_rev(species) ~ . , )
###trying to generate a cleaner image for presentation by collapsing nodes and only showing the boxplots
##this tree will have no labels initially, only manually done by by after collapsing etc
g=ggtree(subset_tree, size=0.5) +
guides(fill = "none")+
geom_treescale(x=0.0006, y=35, width=0.001, color='black', offset = 1)+
ggplot2::xlim(-.0005, 0.012)
g2=scaleClade(g, 258, .1) %>%
scaleClade(508, .1) %>%
scaleClade(509, .1) %>%
collapse(258, 'mixed', fill="firebrick", alpha=0.75) %>%
collapse(508, 'mixed', fill="firebrick", alpha=0.75) %>%
collapse(509, 'mixed', fill="firebrick", alpha=0.75) +
geom_cladelabel(node=258, label="P.roqueforti", color='black', fontsize=3)+
geom_cladelabel(node=508, label="P.psychrosexualis", color='black', fontsize=3)+
geom_cladelabel(node=509, label="P.carneum", color='black', fontsize=3)+
geom_rootedge(rootedge = -0.0005, linewidth=0.5)
##same plot as from above
p2=ggplot(captains_subset, aes(x=species, y=captains))+
gghalves::geom_half_boxplot(side = "r", outlier.shape = "", width=0.4, position=position_nudge(x = .1))+
ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
geom_jitter(width=0.05, alpha=0.6, aes(colour=isolation_simple), show.legend = FALSE)+
scale_colour_manual(values = c("#009E73","#56B4E9", "#0072B2", "#000000"))+
theme_pubr()+
coord_flip()+
labs(x="")
multiplot(g2, p2, ncol = 2)
##saved as pdf, 3x11.69 inches, landscape (so short and wide)
##for the same plot to add to the side
##same plot as above splitting the isolation origins of solitum
##except changing the x axis label angle and removing the x axis title
captains_subset$cluster=factor(captains_subset$cluster, levels=c('Termignon','non-Roquefort' , 'contaminants', 'silage3', 'silage2', 'silage', 'wood', 'Roquefort'))
ggplot(subset(captains_subset, cluster != ""), aes(x=cluster, y=captains))+
gghalves::geom_half_boxplot(side = "r", outlier.shape = "", width=0.4, position=position_nudge(x = .1))+
ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
geom_jitter(width=0.05, alpha=0.6, aes(colour=isolation_simple), show.legend = FALSE)+
scale_colour_manual(values = c("#009E73","#56B4E9", "#0072B2", "#000000"))+
theme_pubr()+
labs(x="")+
coord_flip()
##saved as svg 200x350 captains_boxplot.isolation.roq_only??
###############
###above it is not clear how roqueforti's structure impacts the starship distribution so we can re subset for just roqueforti and split by cluster
nodes=grep("roqueforti", rooted_tree$tip.label)
##this can add a grouping label to the full tree
rooted_tree = groupOTU(rooted_tree, nodes)
##now can subset based on the node of the MRCA
##give the nodes based on the grep above using species names
##can also replace the variable 'nodes' with just a node number
clade = MRCA(rooted_tree, nodes)
subset_tree = tree_subset(rooted_tree, clade, levels_back = 0)
##rotate the node of biformer and casei/camemberti
##now redo all the steps for the hilighting of species etc
##get a list of all the species by taking everything before the first dot (they have all been named properly for this sort of purpose)
subset_species=unique(do.call('rbind', strsplit(as.character(subset_tree$tip.label),'.',fixed=TRUE))[,1] )
##create empty dataframe
subset_sp_clades=as.data.frame(c())
for (sp in subset_species) {
nodes=grep(paste(sp), subset_tree$tip.label)
clade=MRCA(subset_tree, nodes)
output=print(paste(sp,clade))
subset_sp_clades=rbind(subset_sp_clades, output)
}
##rename header temporarily
colnames(subset_sp_clades) = "temp"
##split the column into two seperate columns with appropriate headers
subset_sp_clades2=subset_sp_clades %>% tidyr::separate(temp, c('species', 'node'))
##extract each column as a list MIGHT NOT NEED THIS
subset_species2=subset_sp_clades2[,1]
subset_clades=subset_sp_clades2[,2]
##force a strict order for the plotting to maintain it
subset_sp_clades2$species <- factor(subset_sp_clades2$species, levels = unique(subset_sp_clades2$species))
subset_sp_clades2$node <- factor(subset_sp_clades2$node, levels = unique(subset_sp_clades2$node))
subset_sp_clades3=data.frame(node=as.numeric(paste(subset_clades)), species=subset_species2)
##extract each column as a list MIGHT NOT NEED THIS
subset_species2=subset_sp_clades3[,2]
subset_clades=subset_sp_clades3[,1]
###now plot all together
##need to always adjust the fixed values such as geom_hilight extend, treescale position and size and geom_rootedge length
#g=ggtree(subset_tree, size=0.2) +
# guides(fill = "none")+
# geom_hilight(mapping=aes(subset=node %in% subset_sp_clades2$node, fill=node),alpha = 0.5, to.bottom=T, extendto=1, colour="black", linetype="dotted", size=0.25)+
# scale_fill_viridis_b()+
# geom_rootedge(rootedge = 0.0005, linewidth=0.2)+
# geom_cladelab(data=subset_sp_clades3 , mapping=aes(node=node, label=species), fontsize = 2, barcolour="grey" )+
# geom_treescale(x=0.0001, y=100, width=0.001, color='black', offset = 5)+
# ggplot2::xlim(-.0005, 0.0052)
##without colours highlighting the clades
g=ggtree(subset_tree, size=0.2) +
guides(fill = "none")+
geom_hilight(mapping=aes(subset=node %in% subset_sp_clades2$node),alpha = 0.1, to.bottom=T, linetype="dotted", colour="firebrick")+
geom_rootedge(rootedge = 0.0005, linewidth=0.2)+
geom_cladelab(data=subset_sp_clades3 , mapping=aes(node=node, label=species), fontsize = 2, barcolour="grey", angle=270, vjust=-0.5, hjust=0.5 )+
geom_treescale(x=0.0001, y=200, width=0.001, color='black', offset = 5)+
ggplot2::xlim(-.0005, 0.0035)
genomes=subset_tree$tip.label
captains_subset=captains[grepl(paste(genomes, collapse="|"), captains$genome2), ]
p1=ggplot(captains_subset, aes(genome2, captains))+
geom_col(aes(fill=cluster, group=genome2))+
coord_flip()+
theme_tree2()+
labs(fill="cluster")+
scale_fill_lancet()
p1 %>% insert_left(g)
##now plot the clusters grouped
captains_subset$cluster=factor(captains_subset$cluster, levels=c('Termignon','non-Roquefort' , 'contaminants', 'silage3', 'silage2', 'silage', 'wood', 'Roquefort'))
ggplot(subset(captains_subset, species== "Proqueforti" & cluster != "undefined" & cluster != ""), aes(x=cluster, y=captains))+
gghalves::geom_half_boxplot(side = "r", outlier.shape = "", width=0.4, position=position_nudge(x = .1))+
ggpubr::geom_pwc( label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
geom_jitter(width=0.05, alpha=0.6, aes(colour=isolation_simple), show.legend = FALSE)+
scale_colour_manual(values = c("#009E73","#56B4E9", "#0072B2", "#000000"))+
theme_pubr()+
labs(x="")+
coord_flip()
###try to label, collapse etc a tree using the clusters of roquefort populations
##defined the clusters
subset_cluster=unique(do.call('rbind', strsplit(as.character(subset(captains_subset, cluster != "undefined")$cluster),'.',fixed=TRUE))[,1] )
##create empty dataframe
subset_sp_cluster=as.data.frame(c())
for (cl in subset_cluster) {
nodes <- c()
genome=subset(captains_subset, cluster == cl)[,3]
print(paste(genome))
for(gen in genome) {
node=grep(paste(gen), subset_tree$tip.label)
nodes=append(nodes, node)
}
print(nodes)
clade=MRCA(subset_tree, nodes)
output=print(paste(cl,clade))
subset_sp_cluster=rbind(subset_sp_cluster, output)
}
colnames(subset_sp_cluster) = "temp"
##split the column into two seperate columns with appropriate headers
subset_sp_cluster2=subset_sp_cluster %>% tidyr::separate(sep=" ", temp, c('cluster', 'node'))
##extract each column as a list MIGHT NOT NEED THIS
subset_cluster2=subset_sp_cluster2[,1]
subset_clades=subset_sp_cluster2[,2]
##force a strict order for the plotting to maintain it
subset_sp_cluster2$cluster <- factor(subset_sp_cluster2$cluster, levels = unique(subset_sp_cluster2$cluster))
subset_sp_cluster2$node <- factor(subset_sp_cluster2$node, levels = unique(subset_sp_cluster2$node))
subset_sp_cluster3=data.frame(node=as.numeric(paste(subset_clades)), cluster=subset_cluster2)
##extract each column as a list MIGHT NOT NEED THIS
subset_cluster2=subset_sp_cluster3[,2]
subset_clades=subset_sp_cluster3[,1]
##with the cluster labels
ggtree(subset_tree, size=0.2) +
guides(fill = "none")+
geom_rootedge(rootedge = 0.0005, linewidth=0.2)+
geom_cladelab(data=subset_sp_cluster3 , mapping=aes(node=node, label=cluster), fontsize = 2, barcolour="grey")+
geom_treescale(x=0.0001, y=200, width=0.001, color='black', offset = 5)+
ggplot2::xlim(-.0005, 0.0035)
##now without cluster labels and collapsing for a simplified version to place next to the captain count
g=ggtree(subset_tree, size=0.5) +
guides(fill = "none")+
geom_treescale(x=0.0000001, y=80, width=0.001, color='black', offset = 2)+
ggplot2::xlim(-.0005, 0.0035)
g2=scaleClade(g, 389, .1) %>%
scaleClade(416, .1) %>%
scaleClade(255, .1) %>%
scaleClade(328, .1) %>%
scaleClade(320, .1) %>%
scaleClade(339, .1) %>%
scaleClade(478, .1) %>%
collapse(389, 'mixed', fill="firebrick", alpha=0.75) %>%
collapse(416, 'mixed', fill="firebrick", alpha=0.75) %>%
collapse(255, 'mixed', fill="firebrick", alpha=0.75) %>%
collapse(328, 'mixed', fill="firebrick", alpha=0.75) %>%
collapse(320, 'mixed', fill="firebrick", alpha=0.75) %>%
collapse(339, 'mixed', fill="firebrick", alpha=0.75) %>%
collapse(478, 'mixed', fill="firebrick", alpha=0.75) +
geom_cladelabel(node=389, label="wood", color='black', fontsize=3)+
geom_cladelabel(node=416, label="Roquefort", color='black', fontsize=3)+
geom_cladelabel(node=255, label="non-Roquefort", color='black', fontsize=3)+
geom_cladelabel(node=328, label="contaminants", color='black', fontsize=3)+
geom_cladelabel(node=320, label="Termignon", color='black', fontsize=3)+
geom_cladelabel(node=339, label="silage2", color='black', fontsize=3)+
geom_cladelabel(node=478, label="silage", color='black', fontsize=3)+
geom_rootedge(rootedge = 0.0005, linewidth=0.5)
##same plot as from above
captains_subset$cluster=factor(captains_subset$cluster, levels=c('Termignon','non-Roquefort' , 'contaminants', 'silage2', 'silage', 'wood', 'Roquefort'))
p2=ggplot(subset(captains_subset, species== "Proqueforti" & cluster != "undefined" & cluster != ""), aes(x=cluster, y=captains))+
gghalves::geom_half_boxplot(side = "r", outlier.shape = "", width=0.4, position=position_nudge(x = .1))+
ggpubr::geom_pwc(label='p.adj.signif', p.adjust.method = "bonferroni", vjust = .5, hide.ns = T)+
geom_jitter(width=0.05, alpha=0.6, aes(colour=isolation_simple), show.legend = FALSE)+
scale_colour_manual(values = c("#009E73","#56B4E9", "#0072B2", "#000000"))+
theme_pubr()+
labs(x="")+
coord_flip()
multiplot(g2, p2, ncol = 2)
##saved as pdf, 3x11.69 inches, landscape (so short and wide)
#######REDOING THE WHOLE ROQUEFORTI SET BUT THIS TIME WITH THE PUBLIC REDUCED DATASET
###will want to take subsets to align alongside other data
##can use the fasciculata1 genome graph group to begin
nodes=grep("roqueforti", rooted_trim_tree_public$tip.label)
##this can add a grouping label to the full tree
rooted_trim_tree_public = groupOTU(rooted_trim_tree_public, nodes)
##now can subset based on the node of the MRCA
##give the nodes based on the grep above using species names
##can also replace the variable 'nodes' with just a node number
clade = MRCA(rooted_trim_tree_public, nodes)
subset_tree = tree_subset(rooted_trim_tree_public, clade, levels_back = 2)
##rotate the node of biformer and casei/camemberti
##now redo all the steps for the hilighting of species etc
##get a list of all the species by taking everything before the first dot (they have all been named properly for this sort of purpose)
subset_species=unique(do.call('rbind', strsplit(as.character(subset_tree$tip.label),'.',fixed=TRUE))[,1] )
##create empty dataframe
subset_sp_clades=as.data.frame(c())
for (sp in subset_species) {
nodes=grep(paste(sp), subset_tree$tip.label)
clade=MRCA(subset_tree, nodes)
output=print(paste(sp,clade))
subset_sp_clades=rbind(subset_sp_clades, output)
}
##rename header temporarily
colnames(subset_sp_clades) = "temp"
##split the column into two seperate columns with appropriate headers
subset_sp_clades2=subset_sp_clades %>% tidyr::separate(temp, c('species', 'node'))
##extract each column as a list MIGHT NOT NEED THIS