forked from liampshaw/Pathogen-host-range
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pathogen-host-range.Rmd
1530 lines (1287 loc) · 75.2 KB
/
Pathogen-host-range.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: The phylogenetic range of bacterial and viral pathogens of vertebrates
output:
html_document:
css: custom.css
fig_height: 8
fig_width: 12
toc: true
fig_caption: true
code_folding: hide
pdf_document: default
editor_options:
chunk_output_type: console
---
**Paper**: The phylogenetic range of bacterial and viral pathogens of vertebrates
preprint: https://doi.org/10.1101/670315
**Authors**: Liam P. Shaw\*, Alethea Wang\*, David Dylus, Magda Meier, Grega Pogacnik, Christophe Dessimoz, Francois Balloux (\* co-first authors)
# Introduction
This is a supplementary R Markdown workbook (Supplementary Text 1) containing code to reproduce all main figures and analyses in the associated paper, as well as several supplementary files. Code can be shown or hidden overall (see top right) or for individual sections.
Code can be altered to change any of the figures. Some explanatory text is provided, but not much. Any questions can be addressed via email ([email protected]).
Unless otherwise stated, data related to viruses is plotted in <span style="color:red">red</span> and bacteria in black. The code should take <10 minutes to run on a standard laptop.
**Note:** At several points code is adapted/reused from the supplementary code repository made available by Olival et. al. (2017):
* Paper: *Host and Viral Traits Predict Zoonotic Spillover from Mammals* https://doi.org/10.1038/nature22975
* Code repository: https://github.com/ecohealthalliance/HP3
* MIT License: https://opensource.org/licenses/MIT
This mainly applies to several scripts in the `scripts' directory relating to the fitting and plotting of generalized additive models (GAMs) -- more information is included where appropriate. All code here is also made available under an MIT License.
# Preliminary steps
This section contains library requirements and defines two useful functions:
* PHB (Phylogenetic Host Breadth) - function of the cophenetic distance between the hosts of a pathogen species (default: mean).
* THR (Taxonomic Host Range) - function of the cophenetic distance between all hosts of a particular taxonomic group (default: mean).
```{r Setup, echo=FALSE, message=FALSE, cache=TRUE}
options(warn=-1) # Turn off warnings
def.chunk.hook <- knitr::knit_hooks$get("chunk")
knitr::knit_hooks$set(chunk = function(x, options) {
x <- def.chunk.hook(x, options)
ifelse(options$size != "normalsize", paste0("\n \\", options$size,"\n\n", x, "\n\n \\normalsize"), x)
})
# Libraries
library(broman)
library(ape)
library(ggplot2)
library(dplyr)
library(reshape2)
library(tidyr)
library(grid)
library(broman)
library(gridExtra)
library(cowplot)
cachedata = TRUE
cachetests = TRUE
echoing = TRUE
warnings = FALSE
# To (hopefully) avoid the accidental classic 'string/factor' bug
options(stringsAsFactors=F)
# Default plot theme
theme_basic <- function () {
theme_bw(base_size=14) %+replace%
theme(
axis.text=element_text(colour="black")
) %+replace%
theme(
panel.grid=element_blank()
)
}
# Function: Phylogenetic Host Breadth
PHB <- function(pathogen, cophenetic.matrix=cophenetic_matrix, m=pathogen_vs_host_db, FUN=mean){
species.list <- m[m$Species==pathogen,"HostSpeciesPHB"]
species.matrix <- cophenetic.matrix[species.list, species.list]
species.matrix <- species.matrix[upper.tri(species.matrix)]
if (length(species.matrix)>1){
phb.function <- FUN(species.matrix)
}
else{
phb.function <- 0
}
return(phb.function)
}
# Function: Taxonomic Host Range
THR <- function(taxgroup, cophenetic.matrix = cophenetic_matrix,
m = pathogen_vs_host_db, FUN = mean) {
species.list <- pathogen_vs_host_db[pathogen_vs_host_db$HostOrder==taxgroup,
"HostSpeciesPHB"]
thr.function <- FUN(cophenetic.matrix[species.list, species.list])
return(thr.function)
}
```
First, the data is read in: the database of host-pathogen associations (tidy format i.e. one association per row); and the host phylogenetic tree based on mitochondrial gene alignments.
```{r Read_in_data, cache=cachedata}
# Host-pathogen association database
pathogen_vs_host_db <- read.csv('data/PathogenVsHostDB-2019-05-30.csv', stringsAsFactors = F)
# Host phylogenetic tree
host_tree <- read.tree('data/HostPhylogeneticTree-2019-05-30.nwk')
# Uncomment to use only species which do not disrupt monophyly (n=40)
#host_tree <- read.tree('HostPhylogeneticTree-2019-05-30-pruned.nwk')
# Cophenetic matrix (from phylogenetic tree)
cophenetic_matrix <- cophenetic.phylo(host_tree)
# Keep only overlap
host_tree <- drop.tip(host_tree, host_tree$tip.label[which(!host_tree$tip.label %in% pathogen_vs_host_db$HostSpeciesPHB)])
pathogen_vs_host_db <- pathogen_vs_host_db[which(pathogen_vs_host_db$HostSpeciesPHB %in% host_tree$tip.label),]
# Subset database into virus and bacteria
virus <- pathogen_vs_host_db[which(pathogen_vs_host_db$Type=="Virus"),]
bacteria <- pathogen_vs_host_db[which(pathogen_vs_host_db$Type=="Bacteria"),]
```
### Initial statistics
To begin with, some overall statistics which are cited in the main text.
```{r Initial_statistics_host_orders, results=FALSE, cache=cachedata}
# List of host orders
host_orders <- unique(as.character(pathogen_vs_host_db$HostOrder))
n.host.orders <- length(host_orders)
```
```{r Initial_statistics_interhost, cache=cachedata}
# Calculate THR for each host order
order_thr <- sapply(host_orders, function(x) THR(x, FUN=max))
median.inter.host.distance.order <- median(order_thr)
```
The number of host orders is `r n.host.orders`.
The medium maximum inter-host distance within an order is `r require(broman); myround(median.inter.host.distance.order, 3)`.
#### Vector-borne
We find that viruses are significantly more likely to be vector-borne compared with bacteria.
```{r Initial_statistics_2, cache=cachedata}
vector.tab <- table(pathogen_vs_host_db[which(!duplicated(pathogen_vs_host_db$Species)),"VectorBorne"], pathogen_vs_host_db[which(!duplicated(pathogen_vs_host_db$Species)),"Type"])
vector.tab <- vector.tab[c("No", "Yes"),]
chisq.test(vector.tab)
```
#### Removing humans from the database
We want to think about pathogen range etc. after removing them from the database and the host tree. We recalculate PHB after doing this.
```{r Remove_humans, cache=cachedata}
# Removing humans from database
pathogen_vs_host_db_no_humans <- pathogen_vs_host_db[which(pathogen_vs_host_db$HostSpeciesPHB!="Homosapiens"),]
t.no.humans <- drop.tip(host_tree, tip = host_tree$tip.label[!host_tree$tip.label %in% pathogen_vs_host_db_no_humans$HostSpeciesPHB])
d.no.humans <- cophenetic.phylo(t.no.humans)
# Bacteria PHB
bacteria.names <- names(sort(table(bacteria$Species), decreasing=TRUE))
bacteria.no.humans.phbs <- as.numeric(sapply(bacteria.names, function(x) PHB(x,
m=pathogen_vs_host_db_no_humans,
cophenetic.matrix = d.no.humans)))
virus.names <- names(sort(table(virus$Species), decreasing=TRUE))
virus.no.humans.phbs <- as.numeric(sapply(virus.names, function(x) PHB(x, m=pathogen_vs_host_db_no_humans,
cophenetic.matrix = d.no.humans)))
virus.no.humans.phbs.range <- as.numeric(sapply(virus.names, function(x) PHB(x, m=pathogen_vs_host_db_no_humans,
cophenetic.matrix = d.no.humans, FUN = max)))
```
#### Summary statistics
It is useful to have data frames with the summary statistics for each pathogen species (i.e. one row per pathogen), calculated from the pathogen-host association database (one row per association). We separate this into viruses and bacteria, as we treat them separately.
### Viruses
```{r Virus_data_frame, results=FALSE, cache=cachedata}
options(warn=-1) # Turn off warnings
virus.names <- names(sort(table(virus$Species), decreasing=TRUE))
n.hosts <- as.numeric(sort(table(virus$Species), decreasing=TRUE))
virus.phbs <- sapply(virus.names, function(x) PHB(x))
virus.unique <- virus[which(!duplicated(virus$Species)),]
rownames(virus.unique) <- virus.unique$Species
proteins <- as.numeric(virus.unique[virus.names, "ProteinCount"])
virus.families <- virus.unique[virus.names, "Family"]
# Combine into a data frame of statistics for each viral pathogen
virus.df <- data.frame(cbind(virus.phbs, proteins))
genome.size <- as.numeric(virus.unique[virus.names, "Gsize"])
virus.df$genome.size <- genome.size
virus.df$family <- virus.families
#
n.hosts <- as.numeric(sort(table(virus$Species), decreasing=TRUE))
virus.df$n.hosts <- n.hosts
genome.type <- virus.unique[virus.names, "Genome"]
virus.df$genome.type <- genome.type # Genome type
# Genome type (just RNA/DNA)
genome.type.rna.dna <- ifelse(genome.type %in% c("(-) ssRNA", "(+) ssRNA",
"ssRNA-RT", "dsRNA"), "RNA",
ifelse(genome.type %in% c("dsDNA", "dsDNA-RT", "ssDNA"),
"DNA", ""))
virus.df$genome.type.rna.dna <- genome.type.rna.dna
zoonotic <- ifelse(virus.unique[virus.names, "Zoonotic"]=="Yes", "Zoonotic", "Not zoonotic")
virus.df$n.hosts <- n.hosts # Number of hosts
virus.df$zoonotic <- zoonotic # Zoonotic
virus.df$virus.phbs.no.human <- virus.no.humans.phbs # Add non-human mean PHB
virus.df$virus.phbs.max.no.human <- virus.no.humans.phbs.range # Add non-human maximum PHB
virus.df$vector.borne <- virus.unique[virus.names, "VectorBorne"]
```
### Bacteria
```{r Bacteria_data_frame, cache=cachedata}
# Get bacterial pathogen species names
bacteria.names <- names(sort(table(bacteria$Species), decreasing=TRUE))
# Get number of hosts for each pathogen
n.hosts <- as.numeric(sort(table(bacteria$Species), decreasing=TRUE))
# Get mean PHB for each pathogen
bacteria.phbs <- as.numeric(sapply(bacteria.names, function(x) PHB(x)))
# Dataframe of unique bacterial pathogen species
bacteria.unique <- bacteria[which(!duplicated(bacteria$Species)),]
rownames(bacteria.unique) <- bacteria.unique$Species
# Extract relevant lifestyle variables:
# motility, cell, Gram stain,
# spore formation, oxygen requirements, zoonotic
motility <- bacteria.unique[bacteria.names, "Motility"]
cell <- bacteria.unique[bacteria.names, "Cell"]
gram <- bacteria.unique[bacteria.names, "GramStain"]
spore <- bacteria.unique[bacteria.names, "Spore"]
oxygen <- bacteria.unique[bacteria.names, "Oxygen"]
zoonotic <- ifelse(bacteria.unique[bacteria.names, "Zoonotic"]=="Yes", "Zoonotic", "Not zoonotic")
# Combine this information into a single data frame of statistics for each bacterial pathogen
bacteria.df <- data.frame(cbind(bacteria.names, n.hosts, bacteria.phbs,
motility, gram, spore, oxygen, zoonotic, cell))
bacteria.df$bacteria.phbs <- as.numeric(as.character(bacteria.df$bacteria.phbs)) # Make sure numeric variable
bacteria.df$bacteria.phbs.no.human <- bacteria.no.humans.phbs # Add in PHB without humans
bacteria.df$family <- bacteria.unique[bacteria.names, "Family"] # Add taxonomic family
bacteria.df$genus <- bacteria.unique[bacteria.names, "Genus"] # Add taxonomic genus
bacteria.df$vector.borne <- bacteria.unique[bacteria.names, "VectorBorne"]
```
# Figures
## Figure 1
![](figures/Figure-1-schematic-overview.png)
This is the schematic overview figure, and was made manually in Inkscape using images from FlatIcon (see references in main manuscript).
## Figure 2
We get a dataset of just the unique pathogens and then calculate the mean phylogenetic host breadth (PHB) of each pathogen. We then use this to plot a histogram.
```{r Figure_PHB_histogram, results='show', cache=cachedata}
options(warn=-1) # Turn off warnings
# Get unique pathogens
unique_pathogens <- data.frame(Species=pathogen_vs_host_db[!duplicated(pathogen_vs_host_db$Species),"Species"],
Type=pathogen_vs_host_db[!duplicated(pathogen_vs_host_db$Species),"Type"])
unique_pathogens$Species <- as.character(unique_pathogens$Species)
# Calculate PHB
unique_pathogens$PHB <- sapply(unique_pathogens$Species, function(x) PHB(x, cophenetic.matrix = cophenetic_matrix, m = pathogen_vs_host_db))
unique_pathogens$PHB.median <- sapply(unique_pathogens$Species, function(x) PHB(x, cophenetic.matrix = cophenetic_matrix, m = pathogen_vs_host_db, FUN=median))
unique_pathogens$PHB.max <- sapply(unique_pathogens$Species, function(x) PHB(x, cophenetic.matrix = cophenetic_matrix, m = pathogen_vs_host_db, FUN = max))
# Number of total hosts
host_species_counts <- data.frame(pathogen_vs_host_db %>% group_by(Species) %>% summarise(count=length(HostSpeciesPHB)))
rownames(host_species_counts) <- host_species_counts$Species
unique_pathogens$N.hosts <- host_species_counts[unique_pathogens$Species,"count"]
# Gather data
pathogen_range_histo <- data.frame( phb = 1:50/30-0.03,
bacteria = hist(filter(unique_pathogens, Type == "Bacteria")$PHB, breaks = 0:50/30, freq = TRUE, plot = FALSE)$count,
virus = hist(filter(unique_pathogens, Type == "Virus")$PHB, breaks = 0:50/30, freq = TRUE, plot = FALSE)$count) %>% gather(type, count, -phb)
pathogen_range_histo$count.norm <- pathogen_range_histo$count/sum(pathogen_range_histo$count)
pathogen_range_histo$type <- ifelse(pathogen_range_histo$type=="bacteria", "Bacteria", "Virus")
pathogen_range_histo.total <- data.frame(pathogen_range_histo %>% group_by(phb) %>% summarise(count=sum(count)))
# Pathogen range histogram, excluding zeros
pathogen_range_histo$type <- ordered(pathogen_range_histo$type, levels=c("Virus", "Bacteria"))
levels(pathogen_range_histo$type) <- c("(a) Viruses", "(b) Bacteria")
p.histogram.figure.2 <- ggplot(pathogen_range_histo, aes(fill=type, x=phb, y=count))+
geom_histogram(colour="black", aes(fill="All"), data=pathogen_range_histo.total, stat="identity", alpha=0.4)+
ylim(c(0,75))+geom_bar(stat="identity")+
facet_wrap(~type, ncol=1)+
scale_fill_manual(values=c("#de2d26", "#252525", "#f7f7f7"))+
xlim(c(0,1))+theme_basic()+
xlab("Mean PHB")+
ylab("Frequency")+
labs(fill="Pathogen type")+
geom_vline(xintercept = median(filter(unique_pathogens, Type == "Bacteria")$PHB[which(filter(unique_pathogens, Type == "Bacteria")$PHB!=0)]),
colour="white", size=2, alpha=0.8)+
geom_vline(xintercept = median(filter(unique_pathogens, Type == "Bacteria")$PHB[which(filter(unique_pathogens, Type == "Bacteria")$PHB!=0)]),
colour="black", size=1, linetype="dashed")+ # Second to make colour clear
geom_vline(xintercept = median(filter(unique_pathogens, Type == "Virus")$PHB[which(filter(unique_pathogens, Type == "Virus")$PHB!=0)]),
colour="white", size=2, alpha=0.8)+
geom_vline(xintercept = median(filter(unique_pathogens, Type == "Virus")$PHB[which(filter(unique_pathogens, Type == "Virus")$PHB!=0)]),
colour="red", size=1, linetype="dashed")+ # Second to make colour clear
theme(axis.text=element_text(size=16),
legend.text =element_text(colour="black", size=16),
legend.title=element_text(colour="black", size=18),
strip.text.x=element_text(colour="black", size=22),
axis.title=element_text(colour="black", size=22))+
theme(legend.position="none")
# Perform Wilcox test (for the caption of the figure)
wilcox.test(unique_pathogens[which(unique_pathogens$PHB>0 & unique_pathogens$Type=="Bacteria"),"PHB"], unique_pathogens[which(unique_pathogens$PHB>0 & unique_pathogens$Type=="Virus"),"PHB"])
median(unique_pathogens[which(unique_pathogens$PHB>0 & unique_pathogens$Type=="Bacteria"),"PHB"])
median(unique_pathogens[which(unique_pathogens$PHB>0 & unique_pathogens$Type=="Virus"),"PHB"])
# Show plot
p.histogram.figure.2
# Save figure
pdf("figures/Figure-2-histogram-PHB.pdf", width=12, height=8)
p.histogram.figure.2
dev.off()
png("figures/Figure-2-histogram-PHB.png", width=1200, height=800)
p.histogram.figure.2
dev.off()
```
### Subsampling to non-human hosts
To examine whether sampling bias could be influencing this result, we look at the effect of subsampling to only non-human hosts (both domestic and non-domestic).
```{r Figure_PHB_histogram_subsample_function, cache=cachedata}
options(warn=-1) # Turn off warnings
# Function to plot PHB histogram for a given dataset
plotHistogram <- function(association_dataset, cophenetic_distances){
# Get unique pathogens
unique_pathogens.subsampled <- data.frame(Species=association_dataset[!duplicated(association_dataset$Species),"Species"],
Type=association_dataset[!duplicated(association_dataset$Species),"Type"])
unique_pathogens.subsampled$Species <- as.character(unique_pathogens.subsampled$Species)
# Calculate PHB
unique_pathogens.subsampled$PHB <- sapply(unique_pathogens.subsampled$Species, function(x) PHB(x, cophenetic.matrix = cophenetic_distances, m = association_dataset))
unique_pathogens.subsampled$PHB.median <- sapply(unique_pathogens.subsampled$Species, function(x) PHB(x, cophenetic.matrix = cophenetic_distances, m = association_dataset, FUN=median))
unique_pathogens.subsampled$PHB.max <- sapply(unique_pathogens.subsampled$Species, function(x) PHB(x, cophenetic.matrix = cophenetic_distances, m = association_dataset, FUN = max))
# Number of total hosts
host_species_counts <- data.frame(association_dataset %>% group_by(Species) %>% summarise(count=length(HostSpeciesPHB)))
rownames(host_species_counts) <- host_species_counts$Species
unique_pathogens.subsampled$N.hosts <- host_species_counts[unique_pathogens.subsampled$Species,"count"]
# Gather data
pathogen_range_histo <- data.frame(phb = 1:50/30-0.03,
bacteria = hist(filter(unique_pathogens.subsampled, Type == "Bacteria")$PHB, breaks = 0:50/30, freq = TRUE, plot = FALSE)$count,
virus = hist(filter(unique_pathogens.subsampled, Type == "Virus")$PHB, breaks = 0:50/30, freq = TRUE, plot = FALSE)$count) %>% pivot_longer(cols=c("bacteria", "virus"), names_to = "type", values_to = "count")
pathogen_range_histo$count.norm <- pathogen_range_histo$count/sum(pathogen_range_histo$count)
pathogen_range_histo$type <- ifelse(pathogen_range_histo$type=="bacteria", "Bacteria", "Virus")
pathogen_range_histo.total <- data.frame(pathogen_range_histo %>% group_by(phb) %>% summarise(count=sum(count)))
# Pathogen range histogram, excluding zeros
pathogen_range_histo$type <- ordered(pathogen_range_histo$type, levels=c("Virus", "Bacteria"))
levels(pathogen_range_histo$type) <- c("(a) Viruses", "(b) Bacteria")
p.histogram <- ggplot(pathogen_range_histo, aes(fill=type, x=phb, y=count))+
geom_histogram(colour="black", aes(fill="All"), data=pathogen_range_histo.total, stat="identity", alpha=0.4)+
ylim(c(0,75))+geom_bar(stat="identity")+
facet_wrap(~type, ncol=1)+
scale_fill_manual(values=c("#de2d26", "#252525", "#f7f7f7"))+
xlim(c(0,1))+theme_basic()+
xlab("Mean PHB")+
ylab("Frequency")+
labs(fill="Pathogen type")+
geom_vline(xintercept = median(filter(unique_pathogens.subsampled, Type == "Bacteria")$PHB[which(filter(unique_pathogens.subsampled, Type == "Bacteria")$PHB!=0)]),
colour="white", size=2, alpha=0.8)+
geom_vline(xintercept = median(filter(unique_pathogens.subsampled, Type == "Bacteria")$PHB[which(filter(unique_pathogens.subsampled, Type == "Bacteria")$PHB!=0)]),
colour="black", size=1, linetype="dashed")+ # Second to make colour clear
geom_vline(xintercept = median(filter(unique_pathogens.subsampled, Type == "Virus")$PHB[which(filter(unique_pathogens.subsampled, Type == "Virus")$PHB!=0)]),
colour="white", size=2, alpha=0.8)+
geom_vline(xintercept = median(filter(unique_pathogens.subsampled, Type == "Virus")$PHB[which(filter(unique_pathogens.subsampled, Type == "Virus")$PHB!=0)]),
colour="red", size=1, linetype="dashed")+ # Second to make colour clear
theme(axis.text=element_text(size=16),
legend.text =element_text(colour="black", size=16),
legend.title=element_text(colour="black", size=18),
strip.text.x=element_text(colour="black", size=22),
axis.title=element_text(colour="black", size=22))+
theme(legend.position="none")
return(p.histogram)
}
```
```{r Figure_PHB_domestic, cache=cachedata}
options(warn=-1) # Turn off warnings
pathogen_vs_host_db_domestic <- pathogen_vs_host_db[which(pathogen_vs_host_db$Domestic=="Yes" & pathogen_vs_host_db$HostSpeciesPHB!="Homosapiens"),]
cophenetic_domestic <- cophenetic_matrix[which(rownames(cophenetic_matrix) %in% pathogen_vs_host_db_domestic$HostSpeciesPHB), which(colnames(cophenetic_matrix) %in% pathogen_vs_host_db_domestic$HostSpeciesPHB)]
p.PHB.domestic <- plotHistogram(pathogen_vs_host_db_domestic, cophenetic_domestic)
```
```{r Figure_PHB_non_domestic, cache=cachedata}
options(warn=-1) # Turn off warnings
pathogen_vs_host_db_non_domestic <- pathogen_vs_host_db[which(pathogen_vs_host_db$Domestic=="No" & pathogen_vs_host_db$HostSpeciesPHB!="Homosapiens"),]
cophenetic_non_domestic <- cophenetic_matrix[which(rownames(cophenetic_matrix) %in% pathogen_vs_host_db_non_domestic$HostSpeciesPHB), which(colnames(cophenetic_matrix) %in% pathogen_vs_host_db_non_domestic$HostSpeciesPHB)]
p.PHB.nondomestic <- plotHistogram(pathogen_vs_host_db_non_domestic, cophenetic_non_domestic)
```
```{r Figure_PHB_subsampling_combine, cache=cachedata}
cowplot::plot_grid(p.PHB.domestic+ggtitle("Domestic (n=2,270)"), p.PHB.nondomestic + ggtitle("Non-domestic (n=5,898)"))
```
## Figure 3
We produce an analagous version of Figure 1 from Olival et al. (2017) based on our dataset. Ordering of mammal orders is the same to allow easy comparison with their viral figure.
```{r viral_richness_per_host_order, cache=cachedata}
options(warn=-1) # Turn off warnings
# First make a dataset per order of mammals
# Note that we have Artiodactyla where Olival et al. have "Cetartiodactyla" - have changed this
mammal.orders <- stringi::stri_trans_totitle(c( "CINGULATA", "PILOSA","DIDELPHIMORPHIA", "EULIPOTYPHLA",
"CHIROPTERA", "PRIMATES", "RODENTIA", "CARNIVORA", "LAGOMORPHA", "PROBOSCIDEA", "DIPROTODONTIA",
"ARTIODACTYLA", "PERISSODACTYLA", "PERAMELEMORPHIA", "SCANDENTIA"))
pathogen_vs_mammal_db <- pathogen_vs_host_db_no_humans[which(pathogen_vs_host_db_no_humans$HostOrder %in% mammal.orders),]
human_pathogens <- pathogen_vs_host_db$Species[which(pathogen_vs_host_db$HostSpeciesPHB=="Homosapiens")]
pathogen_vs_mammal_db$pathogenSharedWithHumans <- ifelse(pathogen_vs_mammal_db$Species %in% human_pathogens,
"yes", "no")
boxplot.df <- data.frame(pathogen_vs_mammal_db %>%
group_by(HostSpeciesPHB, HostOrder, Type) %>%
summarise(nPathogens=length(Species),
nHumanPathogens=sum(pathogenSharedWithHumans == "yes"),
nWildMammalHosts=sum(Domestic == "No")))
boxplot.df$propHumanPathogens <- boxplot.df$nHumanPathogens/boxplot.df$nPathogens
boxplot.df$propWildMammalHosts <- boxplot.df$nWildMammalHosts/boxplot.df$nPathogens
# Add information on domesticity: is host species domestic or not?
# Need to check this as they can be different for each association
# i.e. is a species completely wild, completely domestic, or a mixture?
host.df <- pathogen_vs_host_db[which(!duplicated(pathogen_vs_host_db$HostSpeciesPHB)),]
host.df.domestic <- host.df$Domestic
names(host.df.domestic) <- host.df$HostSpeciesPHB
domestic.mammals <- names(host.df.domestic)[which(host.df.domestic=="Yes")]
wild.mammals <- names(host.df.domestic)[which(host.df.domestic=="No")]
captive.mammals <- names(host.df.domestic)[which(host.df.domestic=="Captive")]
# Order hosts using Olival et al. ordering
boxplot.df$HostOrder <- ordered(boxplot.df$HostOrder, levels=mammal.orders)
# Proportion of zoonotic pathogens for viruses
p.virus.zoonotic <- ggplot(data=boxplot.df[which(boxplot.df$Type=="Virus"),], aes( HostOrder, propHumanPathogens))+
geom_boxplot(outlier.shape = NA, fill="#fee5d9")+
theme_basic()+coord_flip()+
geom_jitter(aes(fill=propWildMammalHosts), colour="black", shape=21, width=0.15, height = 0, size=2)+
scale_fill_continuous(low="white", high="red")+
scale_x_discrete(drop=FALSE)+
theme(legend.position = "none")+
ylab("")+xlab("")+
ylab("Proportion of zoonotic viruses")+
ggtitle("(a)")+
theme(plot.title=element_text(hjust=0))
p.virus.richness <- ggplot(data=boxplot.df[which(boxplot.df$Type=="Virus"),], aes( HostOrder, nPathogens))+
geom_boxplot(outlier.shape = NA, fill="#fee5d9")+
theme_basic()+coord_flip()+
geom_jitter(aes(fill=propWildMammalHosts), colour="black", shape=21, width=0.15, height = 0, size=2)+
scale_fill_continuous(low="white", high="red", limits=c(0,1))+
ylab("")+xlab("")+
theme(legend.position = "none")+
theme(axis.text.y=element_blank())+
ylab("Total viral richness")+
ggtitle("(b)")+
theme(plot.title=element_text(hjust=0))+
ylim(c(0,60))+
labs(fill="Proportion of wild hosts")+
theme(legend.position = c(0.7,0.15),
legend.title = element_text(size=14),
legend.background = element_rect(colour="black"))
p.bacteria.zoonotic <- ggplot(data=boxplot.df[which(boxplot.df$Type=="Bacteria"),], aes( HostOrder, propHumanPathogens))+
geom_boxplot(outlier.shape = NA, fill="#f7f7f7")+
theme_basic()+coord_flip()+
geom_jitter(aes(fill=propWildMammalHosts), colour="black", shape=21, width=0.15, height = 0, size=2)+
scale_x_discrete(drop=FALSE)+
scale_fill_continuous(low="white", high="black")+
ylab("")+xlab("")+
theme(legend.position = "none")+
ylab("Proportion of zoonotic bacteria")+
ggtitle("(c)")+
theme(plot.title=element_text(hjust=0))
p.bacteria.richness <- ggplot(data=boxplot.df[which(boxplot.df$Type=="Bacteria"),], aes( HostOrder, nPathogens))+
geom_boxplot(outlier.shape = NA, fill="#f7f7f7")+
theme_basic()+coord_flip()+
geom_jitter(aes(fill=propWildMammalHosts), colour="black", shape=21, width=0.15, height = 0, size=2)+
scale_fill_continuous(low="white", high="black", limits=c(0,1))+
ylab("")+xlab("")+
scale_x_discrete(drop=FALSE)+
theme(legend.position = "none")+
theme(axis.text.y=element_blank())+
ylab("Total bacterial richness")+
ggtitle("(d)")+
theme(plot.title=element_text(hjust=0))+
ylim(c(0,60))+
labs(fill="Proportion of wild hosts")+
theme(legend.position = c(0.7,0.15),
legend.title = element_text(size=14),
legend.background = element_rect(colour="black"))
pdf(file="figures/Figure-3-richness-per-species.pdf", width=20, height=9)
cowplot::plot_grid(p.virus.zoonotic, p.virus.richness,
p.bacteria.zoonotic, p.bacteria.richness, nrow=1)
dev.off()
png(file="figures/Figure-3-richness-per-species.png", width=2000, height=900)
cowplot::plot_grid(p.virus.zoonotic, p.virus.richness,
p.bacteria.zoonotic, p.bacteria.richness, nrow=1)
dev.off()
# Show plot
cowplot::plot_grid(p.virus.zoonotic, p.virus.richness,
p.bacteria.zoonotic, p.bacteria.richness, nrow=1)
# Correlation of bacterial and viral richness per host order
boxplot.df.bacteria <- boxplot.df[which(boxplot.df$Type=="Bacteria"),]
boxplot.df.virus <- boxplot.df[which(boxplot.df$Type=="Virus"),]
# Shared hosts i.e. have both bacteria and virus associations
shared.hosts <- boxplot.df.virus$HostSpeciesPHB[which(boxplot.df.virus$HostSpeciesPHB %in% boxplot.df.bacteria$HostSpeciesPHB)]
boxplot.df.shared <- boxplot.df[which(boxplot.df$HostSpeciesPHB %in% shared.hosts),]
# Now they are sorted, so can easily separate
mammal.bacterial.viral.richness <- data.frame(species=shared.hosts,
nBacteria=boxplot.df.shared$nPathogens[which(boxplot.df.shared$Type=="Bacteria")],
propBacteriaSharedHumans=boxplot.df.shared$propHumanPathogens[which(boxplot.df.shared$Type=="Bacteria")],
nVirus=boxplot.df.shared$nPathogens[which(boxplot.df.shared$Type=="Virus")],
propVirusSharedHumans=boxplot.df.shared$propHumanPathogens[which(boxplot.df.shared$Type=="Virus")])
cor.test(mammal.bacterial.viral.richness$nBacteria, mammal.bacterial.viral.richness$nVirus)
```
## Figure 4
We fit generalized additive models (GAMs) for host traits which predict viral and bacterial richness. To do this we use a dataset of host traits collected by Olival et al. to predict pathogen richness (bacterial and viral) per species. These host traits included a phylogenetic eigenvector regression (PVR) of body mass. As Olival et al. collected information for an analysis of only viral pathogens, we found there was better overlap for viruses in our dataset (n=613 host wild mammals) than for bacteria (n=274).
```{r GAMs, cache=cachedata, message=FALSE}
options(warn=-1) # Turn off warnings
source('scripts/04-fit-GAMs-host-traits.R')
source('scripts/05-plot-GAMs-host-traits.R')
allplots
```
## Figure 5
We also fit GAMs to predict zoonotic potential. For bacteria, GAMs could include terms for host range (PHB_mean, PHB_median, or PHB_max), research effort (NCBI PubMed, Nucleotide, or SRA results), motility, sporulation, being vector-borne, oxygen requirements, and Gram stain. We excluded cellular lifestyle (intra/extracellular) as a predictor due to low numbers, and excluded pathogens of unknown motility (n=50) or sporulation (n=17). For viruses, GAMs could include terms for host range, research effort, genome size (number of proteins and length), being vector-borne, and genome type (Baltimore classification). We excluded pathogens with unknown genome size (n=253). We observed structure in some partial effect residuals in the best-fit GAMs: research effort for bacteria (Figure 6b) and host range for viruses (Figure 6d). This structure was driven by pathogen taxonomy, with families (orders) for bacteria (viruses) having different zoonotic potential; e.g. the Staphylococcaceae contain a high proportion of generalists. Attempts to include taxonomy as a categorical predictor produced best fit models which excluded all lifestyle factors (not shown), although host range and research effort were still the strongest predictors.
```{r GAMs-zoonotic-potential, cache=cachedata, message=FALSE}
detachAllPackages <- function() {
basic.packages <- c("package:stats","package:graphics","package:grDevices","package:utils","package:datasets","package:methods","package:base")
package.list <- search()[ifelse(unlist(gregexpr("package:",search()))==1,TRUE,FALSE)]
package.list <- setdiff(package.list,basic.packages)
if (length(package.list)>0) for (package in package.list) detach(package, character.only=TRUE)
}
```
```{r GAMs-2, message=FALSE, cache=cachedata, message=FALSE}
detachAllPackages()
options(warn=-1)
library(stringi)
library(parallel)
library(magrittr)
library(purrr)
library(mgcv)
library(ggplot2)
library(cowplot)
library(viridis)
library(svglite)
library(phangorn)
library(MuMIn)
library(dplyr)
source('scripts/06-fit-GAM-viral-zoonotic-potential.R')
source('scripts/07-fit-GAM-bacterial-zoonotic-potential.R')
```
```{r GAMs-2-plot, cache=cachedata}
allplots = cowplot::plot_grid(allplots_viral, allplots_bacterial, nrow=2, rel_widths = c(5.3, 5.3, 5.3))
# Save plots
png(file="figures/Figure-5-gams-zoonotic-potential.png", width = convertr::convert(183, "mm", "in")*500, convertr::convert(100, "mm", "in")*600, pointsize=7, res=600)
allplots
dev.off()
pdf(file="figures/Figure-5-gams-zoonotic-potential.pdf", width =7, height=5, pointsize=7)
allplots
dev.off()
# Show plot
allplots
```
## Figure 6
We make a plot of the host switching for bacteria and viruses together (left panel of figure).
If we denote the set of pathogens seen at least once in a host taxon a as p_a (where the taxon could be a species, genus, family etc.), we define the fraction of shared pathogens between two taxa a and b as
```{latex}
s_(a,b) = \frac{| p_{a} \union p_b |}{p_{a} \intersection p_{b}}
```
Note that this definition is symmetric in a, b. It can therefore be compared with the (mean) phylogenetic distance between taxa using a Mantel test to determine the correlation.
```{r Figure_distance_shared_pathogens_scatter, cache=cachedata}
hostShiftingResultsOrder <- function(type="Bacteria", min.rep=0){
metadata.type <- metadata[which(metadata$Type %in% type),]
# Get host orders, and keep only those with more than min.rep representatives
type.host.orders.t <- table(metadata.type$HostOrder)
type.host.orders <- names(type.host.orders.t[type.host.orders.t>min.rep])
# Prepare results to fill
results <- matrix(nrow=0, ncol=8)
# For each host order
for (reference.order in type.host.orders){
reference.order.pathogens <- unique(metadata.type[which(metadata.type$HostOrder==reference.order), "Species"])
reference.order.species <- unique(as.character(metadata.type[which(metadata.type$HostOrder==reference.order), "HostSpeciesPHB"]))
# For each host order, return percentage of pathogens shared with host order vs. mean distance
for (comparison.order in type.host.orders){
# All pathogens in that host order
pathogens <- metadata.type[which(metadata.type$HostOrder==comparison.order),"Species"]
# Percentage of those pathogens that are shared with reference order
perc.shared <- length(pathogens[pathogens %in% reference.order.pathogens])/length(pathogens)
perc.shared.unique <- length(unique(pathogens)[unique(pathogens) %in% reference.order.pathogens])/length(unique(c(pathogens, reference.order.pathogens)))
# Mean of all host order in order from reference order
comparison.order.species <- unique(as.character(metadata.type[which(metadata.type$HostOrder==comparison.order),"HostSpeciesPHB"]))
mean.distance <- mean(d[comparison.order.species, reference.order.species])
sd.distance <- sd(d[comparison.order.species, reference.order.species])
# Add to results
if (reference.order==comparison.order){
mean.distance <- NA
sd.distance <- NA
perc.shared <- NA
perc.shared.unique <- NA
}
results <- rbind(results, c(reference.order,
comparison.order,
mean.distance,
sd.distance,
perc.shared,
perc.shared.unique,
length(reference.order.species),
length(comparison.order.species) ))
}
}
# Naming of data frame, add names of host orders
results <- as.data.frame(results, stringsAsFactors=FALSE)
colnames(results) <- c("order.1",
"order.2",
"mean.distance",
"sd.distance",
"perc.shared",
"perc.shared.unique",
"n.associations.order.1",
"n.associations.order.2")
results$perc.shared <- as.numeric(results$perc.shared)
results$perc.shared.unique <- as.numeric(results$perc.shared.unique)
results$mean.distance <- as.numeric(results$mean.distance)
results$sd.distance <- as.numeric(results$sd.distance)
return(results)
}
hostShiftingResultsFamily <- function(type="Bacteria", min.rep=0){
metadata.type <- metadata[which(metadata$Type %in% type),]
# Get host families, and keep only those with more than min.rep representatives
type.host.families.t <- table(metadata.type$HostFamily)
type.host.families <- names(type.host.families.t[type.host.families.t>min.rep])
# Get list of host.species
host.table <- table(metadata.type$HostSpeciesPHB)
host.species <- names(host.table)[which(host.table>min.rep)]
results <- matrix(nrow=0, ncol=8)
# For each host family
for (reference.family in type.host.families){
reference.family.pathogens <- unique(metadata.type[which(metadata.type$HostFamily==reference.family), "Species"])
reference.family.species <- unique(as.character(metadata.type[which(metadata.type$HostFamily==reference.family), "HostSpeciesPHB"]))
# For each host order, return percentage of pathogens shared with host family vs. mean distance
for (comparison.family in type.host.families){
# All pathogens in that host family
pathogens <- metadata.type[which(metadata.type$HostFamily==comparison.family),"Species"]
# Percentage of those pathogens that are shared with reference order
perc.shared <- length(pathogens[pathogens %in% reference.family.pathogens])/length(pathogens)
perc.shared.unique <- length(unique(pathogens)[unique(pathogens) %in% reference.order.pathogens])/length(unique(c(pathogens, reference.order.pathogens)))
# Mean of all host order in order from reference order
comparison.family.species <- unique(as.character(metadata.type[which(metadata.type$HostFamily==comparison.family),"HostSpeciesPHB"]))
mean.distance <- mean(d[comparison.family.species, reference.family.species])
sd.distance <- sd(d[comparison.family.species, reference.family.species])
# Add to results
results <- rbind(results, c(reference.family,
comparison.family,
mean.distance,
sd.distance,
perc.shared,
perc.shared.unique,
length(metadata.type[which(metadata.type$HostFamily==reference.family), "HostSpeciesPHB"]),
length(metadata.type[which(metadata.type$HostFamily==comparison.family), "HostSpeciesPHB"]) ))
}
}
# Naming of data frame, add names of host orders
results <- as.data.frame(results, stringsAsFactors=FALSE)
colnames(results) <- c("family.1",
"family.2",
"mean.distance",
"sd.distance",
"perc.shared",
"perc.shared.unique",
"n.associations.family.1",
"n.associations.order.2")
results$perc.shared <- as.numeric(results$perc.shared)
results$perc.shared.unique <- as.numeric(results$perc.shared.unique)
results$mean.distance <- as.numeric(results$mean.distance)
results$sd.distance <- as.numeric(results$sd.distance)
return(results)
}
hostShiftingResultsSpecies <- function(type="Bacteria", min.rep=0){
metadata.type <- metadata[which(metadata$Type %in% type),]
# Get host orders, and keep only those with more than min.rep representatives
type.host.species.t <- table(metadata.type$HostSpeciesPHB)
type.host.species <- names(type.host.species.t[type.host.species.t>min.rep])
# Get list of host.species
host.species <- type.host.species
results <- matrix(nrow=0, ncol=8)
# For each host species
for (reference.species in host.species){
reference.species.pathogens <- unique(metadata.type[which(metadata.type$HostSpeciesPHB==reference.species), "Species"])
# For each host species, return percentage of pathogens shared with host species vs. mean distance
for (comparison.species in host.species){
# All pathogens in that host species
pathogens <- metadata.type[which(metadata.type$HostSpeciesPHB==comparison.species),"Species"]
# Percentage of those pathogens that are shared with reference species
perc.shared <- length(pathogens[pathogens %in% reference.species.pathogens])/length(pathogens)
perc.shared.unique <- length(unique(pathogens)[unique(pathogens) %in% reference.species.pathogens])/length(unique(c(pathogens, reference.species.pathogens)))
# Mean of all host species in order from reference order
mean.distance <- mean(d[comparison.species, reference.species])
sd.distance <- sd(d[comparison.species, reference.species])
# Add to results
results <- rbind(results, c(reference.species,
comparison.species,
mean.distance,
sd.distance,
perc.shared,
perc.shared.unique,
length(reference.species),
length(comparison.species) ))
}
}
# Naming of data frame, add names of host orders
results <- as.data.frame(results, stringsAsFactors=FALSE)
colnames(results) <- c("species.1",
"species.2",
"mean.distance",
"sd.distance",
"perc.shared",
"perc.shared.unique",
"n.hosts.species.1",
"n.hosts.species.2")
results$perc.shared <- as.numeric(results$perc.shared)
results$perc.shared.unique <- as.numeric(results$perc.shared.unique)
results$mean.distance <- as.numeric(results$mean.distance)
results$sd.distance <- as.numeric(results$sd.distance)
return(results)
}
hostShiftingPlot <- function(results, title.string=""){
# Correlation test
correlation <- cor.test(results$mean.distance, results$perc.shared, method = "spearman")
# Label for correlation statistics
corr.label <- data.frame(
mean.distance = 0,
perc.shared = 0.1,
label = paste("Spearman's rho = ", myround(correlation$estimate, 3),
"\n p = ", myround(correlation$p.value, 3)),
host.order="",
n.hosts="NA*")
# Plot results
p <- ggplot(results, aes(x=mean.distance, y=perc.shared.unique))+
geom_point( fill="black", alpha=0.2)+
stat_smooth(method="loess", aes(group=1), se = FALSE, size=2, alpha=0.5)+
xlim(c(0,max(results$mean.distance)))+
ylim(c(0,1))+
xlab(paste("Mean phylogenetic distance"))+
ylab(paste("Fraction of shared pathogens"))+
theme_basic()+
labs(size="Number of host-pathogen associations")+
theme(axis.text=element_text(size=16),
legend.text =element_text(colour="black", size=16),
legend.title=element_text(colour="black", size=18),
strip.text.x=element_text(colour="black", size=22),
axis.title=element_text(colour="black", size=22))
return(p)
}
metadata <- pathogen_vs_host_db
d <- cophenetic_matrix
results.orders.10 <- hostShiftingResultsOrder(type=c("Bacteria", "Virus"), min.rep=10)
p.pathogen.sharing.orders.10 <- hostShiftingPlot(results.orders.10)+xlab("Mean phylogenetic distance between orders")
results.orders.5 <- hostShiftingResultsOrder(type=c("Bacteria", "Virus"), min.rep=5)
p.pathogen.sharing.orders.5 <- hostShiftingPlot(results.orders.5)
results.species.10 <- hostShiftingResultsSpecies(type=c("Bacteria", "Virus"), min.rep=10)
# plot, but exclude comparisons of species with themselves, and the outliers
p.pathogen.sharing.species.10 <- hostShiftingPlot(results.species.10[which(results.species.10$mean.distance!=0 &
results.species.10$mean.distance<1.8),])
```
We also carry out a Mantel test for correlation of the two distance matrices.
```{r mantel_test_correlation_distace, cache=cachedata}
mean.dist <- acast(species.1 ~ species.2, value.var = "mean.distance", data=results.species.10)
perc.shared <- acast(species.1 ~ species.2, value.var = "perc.shared.unique", data=results.species.10)
mantel.test(mean.dist, perc.shared)
```
We construct the right panel of the figure. We make a separate data frame for bacteria and for viruses, then combine these together and plot the results.
```{r Figure_distance_shared_pathogens_decay, cache=cachedata}
host.orders <- table(metadata$HostOrder)
min.rep.10 <- names(host.orders)[host.orders>10]
hostShiftingPlotDataHuman <- function(type=c("Bacteria", "Virus"), reference.species="Homosapiens", host.orders = min.rep.10, title.string=""){
# Limit to only pathogens of interest (bacteria, viruses, or both)
metadata.type <- metadata[which(metadata$Type %in% type),]
# Get host orders, and keep only those with more than min.rep representatives
type.host.orders <- min.rep.10
# Get list of reference.species pathogens
reference.species.pathogens <- unique(metadata.type[which(metadata.type$HostSpeciesPHB==reference.species), "Species"])
# For each host order, return percentage of pathogens shared with humans vs. mean distance of host species from reference species
results <- matrix(nrow=0, ncol=3)
for (b in type.host.orders){
# All pathogens in that host order, excluding the reference species
pathogens <- unique(metadata.type[which(metadata.type$HostOrder==b & metadata.type$HostSpeciesPHB!=reference.species),"Species"])
# Percentage of those pathogens that are shared with humans
perc.shared <- length(pathogens[pathogens %in% reference.species.pathogens])/length(pathogens)
# Mean of all host species in order from reference species. Note that we consider all species,
# not just those that have a pathogen association of the right type
host.species <- as.character(metadata[which(metadata$HostOrder==b),"HostSpeciesPHB"])
mean.distance <- mean(d[host.species, reference.species])
# Add to results
results <- rbind(results, c(perc.shared, mean.distance, length(unique(host.species))))
}
# Naming of data frame, add names of host orders
results <- as.data.frame(results)
colnames(results) <- c("perc.shared", "mean.distance", "n.hosts")
results$host.order <- type.host.orders
# Correlation test
correlation <- cor.test(results$mean.distance, results$perc.shared, method = "spearman")
# Label for correlation statistics
corr.label <- data.frame(
mean.distance = 0,
perc.shared = 0.1,
label = paste("Spearman's rho = ", myround(correlation$estimate, 3),
"\n p = ", myround(correlation$p.value, 3)),
host.order="",
n.hosts="NA*")
# Return results
return(results)
}
human.sharing.bacteria <- hostShiftingPlotDataHuman("Bacteria")
human.sharing.bacteria$type <- "Bacteria"
human.sharing.virus <- hostShiftingPlotDataHuman("Virus")
human.sharing.virus$type <- "Virus"
plot.df <- rbind(human.sharing.bacteria, human.sharing.virus)
# The parameter starting values for the sigmoidal fit are from fitting the sigmoid the bacteria data only with nls:
# fit <- nls(perc.shared ~ SSlogis(mean.distance, Asym, xmid, scal), data = human.sharing.bacteria)
p.human.sharing <- ggplot(plot.df, aes(x=mean.distance, y=perc.shared))+
geom_point(aes(size=n.hosts, fill=type), colour="black", pch=21, alpha=0.5)+
xlim(c(0,max(plot.df$mean.distance)))+
geom_smooth(method="nls", aes(group=type, colour=type),size=2, formula= y ~ Asym/(1+exp((xmid-x)/scal)), method.args = list(start=c(Asym=0.7, xmid=1.31, scal=-0.33)), se=FALSE)+
scale_fill_manual(values=c("black", "red"))+
scale_colour_manual(values=c("black", "red"))+
scale_size_continuous(range = c(2,10))+
ylim(c(0,1))+
labs(size="Number of\nhost-pathogen\nassociations", fill="Pathogen type", colour="Pathogen type")+
xlab(paste("Mean phylogenetic distance to Homo sapiens"))+
ylab(paste("Fraction of shared pathogens with Homo sapiens"))+
theme_basic()+
theme(axis.text=element_text(size=16),
legend.text =element_text(colour="black", size=16),
legend.title=element_text(colour="black", size=18),
strip.text.x=element_text(colour="black", size=22),
axis.title=element_text(colour="black", size=22))
```
We combine the left and right panels together to make a preliminary version of the figure. The paper version was produced manually from this preliminary version in [Inkscape](https://inkscape.org/), using animal silhouettes from [phylopic.org](https://phylopic.org).
```{r Fig_host_shifting_plot, cache=cachedata}
pdf("figures/Figure-6-pathogen-sharing.pdf", width=17, height=10)
cowplot::plot_grid(p.pathogen.sharing.orders.10+ggtitle("(a)"), p.human.sharing+ggtitle("(b)"))
dev.off()
png("figures/Figure-6-pathogen-sharing.png", width=1700, height=1000, pointsize = 14)
cowplot::plot_grid(p.pathogen.sharing.orders.10, p.human.sharing)
dev.off()
# Show plot
cowplot::plot_grid(p.pathogen.sharing.orders.10, p.human.sharing)
```
It is useful to be able to match the points and names together, which can be done with the plot below.
```{r Fig_host_shifting_plot_match_names, cache=cachedata}
ggplot(plot.df, aes(x=mean.distance, y=perc.shared))+
geom_text(aes(label=host.order, colour=type), nudge_x = 0,nudge_y=0, size=1, angle=45)+
stat_smooth(method="loess", aes(group=type, colour=type),size=2, se = FALSE, formula = y ~ exp(x)/(exp(x)+1))+
xlim(c(0,max(plot.df$mean.distance)))+
scale_fill_manual(values=c("black", "red"))+
scale_colour_manual(values=c("black", "red"))+
scale_size_continuous(range = c(2,10))+
ylim(c(0,1))+
labs(size="Number of\nhost-pathogen\nassociations", fill="Pathogen type", colour="Pathogen type")+
xlab(paste("Mean phylogenetic distance to Homo sapiens"))+
ylab(paste("Fraction of shared pathogens with Homo sapiens"))+
theme_basic()+geom_point(aes(colour=type))+facet_wrap(~type)
ggsave("figures/Figure-6-pathogen-sharing-right-panel-names.pdf")
```
# Supplementary Figures
## Supplementary Figure 1
We compare our mitochondrial gene phylogeny with the *cytb* phylogeny of Olival et al.
```{r tree_comparison_olival, cache=cachedata}
olival.tree <- read.tree('data/olival_cytb_supertree.tree')
# Convert tip labels
olival.tree$tip.label <- gsub("_", "", olival.tree$tip.label)
# Only consider shared species
olival.tree.shared <- drop.tip(olival.tree,
tip=olival.tree$tip.label[which(!olival.tree$tip.label %in% host_tree$tip.label)])
olival.tree.cophenetic <- cophenetic(olival.tree.shared)
this.study.tree.cophenetic <- d[rownames(olival.tree.cophenetic), rownames(olival.tree.cophenetic)]
# Plot cophenetic distances
tree.df <- data.frame(cbind(as.vector(olival.tree.cophenetic), as.vector(this.study.tree.cophenetic)))
p.olival.cophenetic.comparison <- ggplot(tree.df, aes(X1, X2))+
geom_point(size=0.25, alpha=0.1)+
theme_basic()+
xlab("Cophenetic distance (Olival et al. 2017)")+
ylab("Cophenetic distance (this study)")+
geom_abline(linetype='dashed')+
stat_smooth(method="lm", colour="red")
p.olival.cophenetic.comparison
png("figures/Supplementary-Figure-1-Cophenetic-Tree-Comparison.png", width=800, height=1200)
p.olival.cophenetic.comparison
dev.off()
```
## Supplementary Figure 2
Mean and maximum PHB are well-correlated for most pathogens.
```{r Supp_Fig_2, cache=cachedata}
p.mean.max.phb <- ggplot(unique_pathogens, aes(x=PHB, y=PHB.max))+geom_point(aes(size=N.hosts, colour=Type))+theme_basic()+
facet_wrap(~Type)+
scale_color_manual(values=c("black", "red"))+
geom_abline(slope=1, intercept=0)
p.mean.max.phb
pdf("figures/Supplementary-Figure-2-PHB-correlation.pdf", width=12, height=8)
p.mean.max.phb
dev.off()
```
## Supplementary Figure 3
Shows how PHB varies within bacterial families.
```{r Figure_PHB_by_pathogen_family, cache=cachedata}
# Family
min.abundance <- 20
df.generalist.prop.family <- data.frame(bacteria.df %>% group_by(family) %>% summarise(prop.generalist=length(n.hosts[which(n.hosts!=1)])/length(n.hosts), species=length(n.hosts)))