-
Notifications
You must be signed in to change notification settings - Fork 0
/
SARS-CoV-2_Markdown.Rmd
2868 lines (2542 loc) · 104 KB
/
SARS-CoV-2_Markdown.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: "SARS-CoV-2 Markdown"
author: "Nicholas P Dylla"
date: "`r Sys.Date()`"
output: html_document
editor_options:
chunk_output_type: console
header-includes:
- |
```{=latex}
\usepackage{fvextra}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{
showspaces = false,
showtabs = false,
breaklines,
commandchars=\\\{\}
}
```
---
# Table of Contents
1. Load Packages
2. Load Metabolomic and Metagenomic Data
3. Main Figures and Tables
1. Figure 1
2. Figure 3
3. Figure 4
4. Figure 5
5. Figure 6
6. Table 2
7. Table 3
8. Table 4
9. Table 5
4. Supplemtal Figures and Tables
1. Supplemental Figure 1
2. Supplemental Figure 2
5. Save Data Image
***
### Load packages and markdown options
```{r package-loading, message=FALSE, warning=FALSE}
# BiocManager::install('EnhancedVolcano')
# BiocManager::install('microbiomeMarker')
pacman::p_load(
tidyverse, # Data wrangling and visualization
purrr, # Functional programming
ggrepel, # Visualization, repels labels on plots
knitr, # To change R markdown PDF options
cutpointr, # Calculate cutpoints
cowplot, # Plot ggplots together
caret, # Machine learning
umap, # UMAP
ggsci, # Color palette
phyloseq, # Manage genomic data
microbiomeMarker, # LEfSe analysis
grid, # Work with graphical objects
gridExtra, # Plot multiple ggplots
yingtools2, # Custom functions for data manipulation
ggpirate, # ggplot version of Pirate plot
stringr, # Work with character strings
magick, # Import pdf into ggplot
ComplexHeatmap, # Heatmap
circlize, # Color generator for heatmap
survival, # Survival analysis
survminer, # Visualize survival analysis
gtsummary, # Table visualization
gt, # Export gtsummary table
pROC, # ROC analysis
grDevices, # Alternative pdf (cairo_pdf) to save special characters
tableone, # Create table ones
rstatix, # Tidyverse statistics
EnhancedVolcano, # Volcano plot
bestglm, # Logistic regression
devtools, # Source functions from GitHub
install = F
)
# ggplot theme shortcuts
et <- element_text
eb <- element_blank
er <- element_rect
opts_chunk$set(tidy.opts = list(width.cutoff = 60), tidy = TRUE)
```
### Load in Metabolomic and Metagenomic Data
```{r load-in-data, message=FALSE, warning=FALSE}
#1) Load R image
# load("./Data/SARS-CoV-2_Modeling.RData")
# OR #
#2) Individually Load R Objects
#### START ####
# Covid lookup table
covid_lookup <- readRDS("./Data/covid_lookup_patientID.rds")
# Covid transition table
covid_transition <- readRDS("./Data/covid_transition_patientID.rds")
# Table one data
tableone_vars <- readRDS("./Data/tableone_variables.rds")
tableone_cats <- readRDS("./Data/tableone_categories.rds")
# Metagenomic Data
covid_kraken <- readRDS("./Data/covid_kraken_patientID.rds")
## Save kraken data as CSV as response to reviewer's comments
covid_kraken %>%
arrange(patient_ID, desc(pctseqs), Kingdom, Phylum, Class, Order, Family, Genus, Species) %>%
group_by(patient_ID) %>%
filter(pctseqs >= 0.0001) %>%
write.csv(., "./Results/covid_kraken_taxonomy.csv", row.names = F)
mat <- readRDS("./Data/shotgun_matrix_patientID.rds")
tax_lookup <- readRDS("./Data/tax_lookup.rds")
# EggNog emapper
emapper <- readRDS("./Data/emapper_patientID.rds")
# BAI related genes
bai <- readRDS("./Data/bai_patientID.rds")
# DAT related genes
dat <- readRDS("./Data/dat_patientID.rds")
# Butyrate related genes
lca <- readRDS("./Data/butyrate_patientID.rds")
# CARD related genes lookup
card_dict <- readRDS("./Data/card_dict.rds")
# CARD data for covid patients
card <- readRDS("./Data/card_patientID.rds")
# Bacteriocin related genes
bactocin <- readRDS("./Data/bactocin_patientID.rds")
# Length of stay data
covid_flow <- readRDS("./Data/covid_flow_patientID.rds")
# Qualitative Metabolomic Data
metab_qual_raw <- readRDS("./Data/metab_qual_raw_patientID.rds")
# Quantitative Metabolomic Data
metab_quant <- readRDS("./Data/metab_quant_patientID.rds")
# vital_status == 0, ALIVE
# vital_status == 1, DECEASED
#### END ####
# Load color palette from GitHub
source_url("https://github.com/yingeddi2008/DFIutility/blob/master/getRdpPal.R?raw=TRUE")
pal <- getRdpPal(covid_kraken)
```
### Figure 1
```{r, figure-1, fig.dim=c(10,12), message=FALSE, warning=FALSE}
#### Relative Abundance Plot START ####
# Obtain proteobacteria abundances per sample
proteo <-
covid_kraken %>%
filter(Phylum == "Proteobacteria") %>%
count(patient_ID, wt = pctseqs, name = "Proteobacteria")
# Create ggplot ordered by proteobacteria abundance
gg_proteo <- covid_kraken %>%
left_join(proteo) %>%
arrange(Kingdom, Phylum, Class, Order, Family, Genus) %>%
mutate(Genus = factor(Genus, levels = unique(Genus))) %>%
group_by(patient_ID) %>%
arrange(Genus) %>%
mutate(cum.pct = cumsum(pctseqs),
y.text = (cum.pct + c(0, cum.pct[-length(cum.pct)]))/2) %>%
ungroup() %>%
dplyr::select(-cum.pct) %>%
mutate(Genus2=Genus) %>%
separate(Genus2, into=c("k","p","c","o","f","g","s"),sep="\\|") %>%
mutate(s=gsub(" sp\\.","",s),
tax.label=ifelse(pctseqs >= 0.25,as.character(gsub(" ","\n",g)),"")) %>%
left_join(covid_lookup %>%
select(patient_ID, vital_des)) %>%
mutate(vital_des = as.factor(vital_des),
vital_des = factor(vital_des, levels = c("Deceased", "Alive"))) %>%
ggplot(aes(x=reorder(patient_ID, -Proteobacteria),y=pctseqs)) +
geom_bar(aes(fill=Genus),stat="identity") +
# geom_text(aes(y=1-y.text,label=tax.label),size=2,
# angle=90,
# lineheight=0.6) +
theme_bw() +
theme(legend.position = "none",
# axis.title.x = eb(),
axis.text.x=eb(),
# axis.text.x=et(angle = 90, color = "black"),
strip.text.x= et(angle=0,size=14),
strip.background = element_rect(fill = "white"),
axis.title.y = et(color = "black", size = 12),
axis.text.y = et(color = "black", size = 10)) +
scale_fill_manual(values=pal) +
scale_y_continuous(expand = expansion(mult = c(0.005,0.005)))+
ylab("Relative Abundance (%)\n") +
xlab("")+
facet_grid(.~vital_des, space = "free_x", scales = "free_x")
# Color facets
proteo_grob <- ggplot_gtable(ggplot_build(gg_proteo))
strip_both <- which(grepl('strip-', proteo_grob$layout$name))
fills <- rev(ggsci::pal_igv("default")(2))
k <- 1
for (i in strip_both) {
j <- which(grepl('rect', proteo_grob$grobs[[i]]$grobs[[1]]$childrenOrder))
l <- which(grepl('titleGrob', proteo_grob$grobs[[i]]$grobs[[1]]$childrenOrder))
proteo_grob$grobs[[i]]$grobs[[1]]$children[[j]]$gp$col <- fills[k]
proteo_grob$grobs[[i]]$grobs[[1]]$children[[l]]$children[[1]]$gp$col <- fills[k]
k <- k+1
}
pdf("./Results/shotgun_relative_abundance.pdf", height = 6, width = 12)
grid.draw(proteo_grob)
dev.off()
# Figure 1A
grid.draw(proteo_grob)
grid.text("Figure 1A", x = 0.1, y = 1)
#### Relative Abundance Plot END ####
#### Alpha Diversity Plot Inverse Simpson START ####
# Filter matrix to only contain percent sequences (pctseq) >= 0.0001 (0.01%)
mat_filt <- mat %>%
pivot_longer(-patient_ID, names_to = "taxid", values_to = "pctseqs") %>%
group_by(patient_ID) %>%
filter(pctseqs >= 0.0001) %>%
pivot_wider(patient_ID, names_from = "taxid", values_from = "pctseqs", values_fill = 0) %>%
as.data.frame()
mat_invsim <- mat_filt
row.names(mat_invsim) <- mat_invsim$patient_ID
mat_invsim <- mat_invsim %>% select(-patient_ID)
mat_invsim_t <- mat_invsim %>% t()
alpha_invsim <- vegan::diversity(mat_invsim,index="invsimpson") %>%
as.data.frame()
colnames(alpha_invsim)[1] <- "InvSimpson"
alpha_invsim$patient_ID <- row.names(alpha_invsim)
# Obtain values for mean alpha diversity for alive and deceased
alpha_invsim %>%
left_join(covid_lookup %>%
select(patient_ID, vital_des)) %>%
group_by(vital_des) %>%
summarise(mean = mean(InvSimpson))
# Obtain stats for alpha diversity
alpha_invsim_stats <-
alpha_invsim %>%
left_join(covid_lookup %>%
select(patient_ID, vital_des)) %>%
rstatix::wilcox_test(InvSimpson~vital_des)
pirate_colors <- rev(ggsci::pal_igv("default")(2))
set.seed(456)
gg_alpha_invsim <- alpha_invsim %>%
left_join(covid_lookup %>%
select(patient_ID, vital_des)) %>%
mutate(vital_des = as.factor(vital_des),
vital_des = factor(vital_des, levels = c("Deceased", "Alive"))) %>%
ggplot(., aes(x = vital_des, y = InvSimpson, colour = vital_des, fill = vital_des)) +
geom_pirate(cis_params = list(fill = "white", alpha = 0.5),
bars_params = list(alpha = 0.65),
lines_params = list(size = 0.5),
points_params = list(fill = "black", size = 3.5),
jitter_width = 0.75,
cis = TRUE,
violins = FALSE) +
annotate("text", x = 1.15, y = 39.5, label = paste0("Wilcoxon, W = ", alpha_invsim_stats$statistic, ", p = ", alpha_invsim_stats$p),
size = 2.3) +
theme_bw() +
theme(
panel.grid = eb(),
axis.title.y = et(size = 14, color = "black"),
axis.title.x = eb(),
axis.text = et(size = 12, color = "black"),
plot.margin = margin(t = 5, # Top margin
r = 5, # Right margin
b = 5, # Bottom margin
l = 5) # Left margin
# plot.margin = margin(t = 0.2, # Top margin
# r = 35, # Right margin
# b = 0.2, # Bottom margin
# l = 35) # Left margin
) +
ylab("Alpha Diversity\n(Inverse Simpson Index)\n") +
scale_fill_manual(values = pirate_colors) +
scale_color_manual(values = pirate_colors) +
scale_y_continuous(breaks = seq(0,40,5))
# Figure 1B: Top Left Panel
gg_alpha_invsim + ggtitle("Figure 1B: Top Left Panel")
pdf("./Results/shotgun_pirate_alpha_diversity_invsim_vital.pdf", height = 6, width = 7)
gg_alpha_invsim
dev.off()
#### Alpha Diversity Plot Inverse Simpson END ####
#### Alpha Diversity Plot Shannon START ####
mat_shannon <- mat_filt
row.names(mat_shannon) <- mat_shannon$patient_ID
mat_shannon <- mat_shannon %>% select(-patient_ID)
mat_shannon_t <- mat_shannon %>% t()
alpha_shannon <- vegan::diversity(mat_shannon,index="shannon") %>%
as.data.frame()
colnames(alpha_shannon)[1] <- "Shannon"
alpha_shannon$patient_ID <- row.names(alpha_shannon)
# Obtain values for mean alpha diversity for alive and deceased
alpha_shannon %>%
left_join(covid_lookup %>%
select(patient_ID, vital_des)) %>%
group_by(vital_des) %>%
summarise(mean = mean(Shannon))
# Obtain stats for alpha diversity
alpha_shannon_stats <-
alpha_shannon %>%
left_join(covid_lookup %>%
select(patient_ID, vital_des)) %>%
rstatix::wilcox_test(Shannon~vital_des)
pirate_colors <- rev(ggsci::pal_igv("default")(2))
set.seed(456)
gg_alpha_shannon <- alpha_shannon %>%
left_join(covid_lookup %>%
select(patient_ID, vital_des)) %>%
mutate(vital_des = as.factor(vital_des),
vital_des = factor(vital_des, levels = c("Deceased", "Alive"))) %>%
ggplot(., aes(x = vital_des, y = Shannon, colour = vital_des, fill = vital_des)) +
geom_pirate(cis_params = list(fill = "white", alpha = 0.5),
bars_params = list(alpha = 0.65),
lines_params = list(size = 0.5),
points_params = list(fill = "black", size = 3.5),
jitter_width = 0.75,
cis = TRUE,
violins = FALSE) +
annotate("text", x = 1.15, y = 5, label = paste0("Wilcoxon, W = ", alpha_shannon_stats$statistic, ", p = ", alpha_shannon_stats$p),
size = 2.3) +
theme_bw() +
theme(
panel.grid = eb(),
axis.title.y = et(size = 14, color = "black"),
axis.title.x = eb(),
axis.text = et(size = 12, color = "black"),
plot.margin = margin(t = 5, # Top margin
r = 5, # Right margin
b = 5, # Bottom margin
l = 5) # Left margin
# plot.margin = margin(t = 0.2, # Top margin
# r = 35, # Right margin
# b = 0.2, # Bottom margin
# l = 35) # Left margin
) +
ylab("Alpha Diversity\n(Shannon Index)\n") +
scale_fill_manual(values = pirate_colors) +
scale_color_manual(values = pirate_colors) +
scale_y_continuous(breaks = seq(0,6,1))
# Figure 1B: Top Right Panel
gg_alpha_shannon + ggtitle("Figure 1B: Top Right Panel")
pdf("./Results/shotgun_pirate_alpha_diversity_shannon_vital.pdf", height = 6, width = 7)
gg_alpha_shannon
dev.off()
#### Alpha Diversity Plot Shannon END ####
#### Alpha Diversity Plot Richness START ####
mat_richness <- mat_filt
row.names(mat_richness) <- mat_richness$patient_ID
mat_richness <- mat_richness %>% select(-patient_ID)
mat_richness_t <- mat_richness %>% t()
alpha_richness <- vegan::specnumber(mat_richness) %>%
as.data.frame()
colnames(alpha_richness)[1] <- "Richness"
alpha_richness$patient_ID <- row.names(alpha_richness)
# Obtain values for mean alpha diversity for alive and deceased
alpha_richness %>%
left_join(covid_lookup %>%
select(patient_ID, vital_des)) %>%
group_by(vital_des) %>%
summarise(mean = mean(Richness))
# Obtain stats for alpha diversity
alpha_richness_stats <-
alpha_richness %>%
left_join(covid_lookup %>%
select(patient_ID, vital_des)) %>%
rstatix::wilcox_test(Richness~vital_des)
pirate_colors <- rev(ggsci::pal_igv("default")(2))
set.seed(456)
gg_alpha_richness <- alpha_richness %>%
left_join(covid_lookup %>%
select(patient_ID, vital_des)) %>%
mutate(vital_des = as.factor(vital_des),
vital_des = factor(vital_des, levels = c("Deceased", "Alive"))) %>%
ggplot(., aes(x = vital_des, y = Richness, colour = vital_des, fill = vital_des)) +
geom_pirate(cis_params = list(fill = "white", alpha = 0.5),
bars_params = list(alpha = 0.65),
lines_params = list(size = 0.5),
points_params = list(fill = "black", size = 3.5),
jitter_width = 0.75,
cis = TRUE,
violins = FALSE) +
annotate("text", x = 1.15, y = 325, label = paste0("Wilcoxon, W = ", alpha_richness_stats$statistic, ", p = ", alpha_richness_stats$p),
size = 2.3) +
theme_bw() +
theme(
panel.grid = eb(),
axis.title.y = et(size = 14, color = "black"),
axis.title.x = eb(),
axis.text = et(size = 12, color = "black"),
plot.margin = margin(t = 5, # Top margin
r = 5, # Right margin
b = 5, # Bottom margin
l = 5) # Left margin
# plot.margin = margin(t = 0.2, # Top margin
# r = 35, # Right margin
# b = 0.2, # Bottom margin
# l = 35) # Left margin
) +
ylab("Alpha Diversity\n(Species Richness)\n") +
scale_fill_manual(values = pirate_colors) +
scale_color_manual(values = pirate_colors) +
scale_y_continuous(breaks = seq(0,350,50))
# Figure 1B: Bottom Left Panel
gg_alpha_richness + ggtitle("Figure 1B: Bottom Left Panel")
pdf("./Results/shotgun_pirate_alpha_diversity_richness_vital.pdf", height = 6, width = 7)
gg_alpha_richness
dev.off()
#### Alpha Diversity Plot Richness END ####
#### Alpha Diversity Plot Evenness START ####
mat_evenness <- mat_filt
row.names(mat_evenness) <- mat_evenness$patient_ID
mat_evenness <- mat_evenness %>% select(-patient_ID)
mat_evenness_t <- mat_evenness %>% t()
h <- vegan::diversity(mat_evenness)
s <- vegan::specnumber(mat_filt)
alpha_evenness <- h/log(s)
alpha_evenness <- as.data.frame(alpha_evenness)
colnames(alpha_evenness)[1] <- "Evenness"
alpha_evenness$patient_ID <- row.names(alpha_evenness)
# Obtain values for mean alpha diversity for alive and deceased
alpha_evenness %>%
left_join(covid_lookup %>%
select(patient_ID, vital_des)) %>%
group_by(vital_des) %>%
summarise(mean = mean(Evenness))
# Obtain stats for alpha diversity
alpha_evenness_stats <-
alpha_evenness %>%
left_join(covid_lookup %>%
select(patient_ID, vital_des)) %>%
rstatix::wilcox_test(Evenness~vital_des)
pirate_colors <- rev(ggsci::pal_igv("default")(2))
set.seed(456)
gg_alpha_evenness <- alpha_evenness %>%
left_join(covid_lookup %>%
select(patient_ID, vital_des)) %>%
mutate(vital_des = as.factor(vital_des),
vital_des = factor(vital_des, levels = c("Deceased", "Alive"))) %>%
ggplot(., aes(x = vital_des, y = Evenness, colour = vital_des, fill = vital_des)) +
geom_pirate(cis_params = list(fill = "white", alpha = 0.5),
bars_params = list(alpha = 0.65),
lines_params = list(size = 0.5),
points_params = list(fill = "black", size = 3.5),
jitter_width = 0.75,
cis = TRUE,
violins = FALSE) +
annotate("text", x = 1.15, y = 0.85, label = paste0("Wilcoxon, W = ", alpha_evenness_stats$statistic, ", p = ", alpha_evenness_stats$p),
size = 2.3) +
theme_bw() +
theme(
panel.grid = eb(),
axis.title.y = et(size = 14, color = "black"),
axis.title.x = eb(),
axis.text = et(size = 12, color = "black"),
plot.margin = margin(t = 5, # Top margin
r = 5, # Right margin
b = 5, # Bottom margin
l = 5) # Left margin
# plot.margin = margin(t = 0.2, # Top margin
# r = 35, # Right margin
# b = 0.2, # Bottom margin
# l = 35) # Left margin
) +
ylab("Alpha Diversity\n(Species Evenness)\n") +
scale_fill_manual(values = pirate_colors) +
scale_color_manual(values = pirate_colors) +
scale_y_continuous(breaks = seq(0,1,0.1))
# Figure 1B: Bottom Right Panel
gg_alpha_evenness + ggtitle("Figure 1B: Bottom Right Panel")
pdf("./Results/shotgun_pirate_alpha_diversity_evenness_vital.pdf", height = 6, width = 7)
gg_alpha_evenness
dev.off()
#### Alpha Diversity Plot Observed END ####
#### Combine Alpha Diversity Plots START ####
gg_alpha_total <- cowplot::plot_grid(
gg_alpha_invsim + ylab("Inverse Simpson Index") + theme(axis.text.x = eb(),
axis.text.y = et(size = 8),
axis.title.y = et(size = 11)),
gg_alpha_shannon + ylab("Shannon Index") + theme(axis.text.x = eb(),
axis.text.y = et(size = 8),
axis.title.y = et(size = 11)),
gg_alpha_richness + ylab("Species Richness")+ theme(axis.text = et(size = 8),
axis.title.y = et(size = 11)),
gg_alpha_evenness + ylab("Species Evenness")+ theme(axis.text = et(size = 8),
axis.title.y = et(size = 11)),
align = "hv")
gg_alpha_total
#### Combine Alpha Diversity Plots END ####
#### UMAP Plot 1 START ####
umap_mat <- mat
umap_mat <- umap_mat %>%
as.data.frame() %>%
column_to_rownames(var = "patient_ID")
custom_config <- umap.defaults
custom_config$n_neighbors <- as.integer(nrow(umap_mat) * 0.1)
custom_config$metric <- 'manhattan'
set.seed(6)
umap_mat2 <- umap(umap_mat, config = custom_config)
umap_mat_plot <- umap_mat2$layout %>%
as.data.frame() %>%
mutate(patient_ID = row.names(.)) %>%
left_join(covid_lookup %>%
select(patient_ID, vital_des, race, sex, Tobacco, Shock, ends_with("doms"))) %>%
ggplot(aes(x = V1, y = V2, fill = vital_des))+
geom_point(size = 3.25, alpha = 0.8, shape = 21, color = "black")+
stat_ellipse(inherit.aes = T, aes(color = vital_des), type = "norm", level = 0.75) +
stat_ellipse(inherit.aes = T, geom = "polygon",
aes(fill = vital_des),
color = "black",
alpha = 0.35,
type = "euclid", level = 0.25, show.legend = F) +
theme_bw() +
theme(panel.grid = eb(),
axis.title = et(color = "black", size = 14),
axis.text = et(color = "black", size = 12),
legend.title = et(color = "black", size = 14),
legend.text = et(color = "black", size = 12),
plot.margin = margin(t = 0, # Top margin
r = 50, # Right margin
b = 0, # Bottom margin
l = 60)) + # Left margin
# ggtitle(paste0("Shotgun Taxonomy: UMAP \nCOVID-19 Alive vs Deceased \n", "n = ", nrow(umap_mat), "\n", custom_config$n_neighbors, " Neighbors")) +
xlab("UMAP1") +
ylab("UMAP2") +
guides(color = guide_legend(title = "Vital Status"),
fill = guide_legend(title = "Vital Status"))+
ggsci::scale_color_igv()+
ggsci::scale_fill_igv() +
lims(x = c(-2.75, 3.6), # obtained from umap below: ggplot_build(umap_mat_plot_colors)$layout$panel_scales_x[[1]]$range$range
y = c(-2.614908, 3.500000))+ # obtained from umap below: ggplot_build(umap_mat_plot_colors)$layout$panel_scales_y[[1]]$range$range
coord_equal()
umap_mat_plot + ggtitle("Figure 1C")
#### UMAP Plot 1 END ####
#### UMAP Plot 2 START ####
# Chi-square test for proteobacteria domination and covid outcome
## Proteobacteria < 0.05 (0%) = 0
## Proteobacteria >= 0.05 (5%) = 2
## Covid outcome: Alive = 0
## Covid outcome: Deceased = 1
table(covid_lookup$proteo_doms, covid_lookup$vital_status)
# Run chi-square test
chisq <- chisq.test(covid_lookup$proteo_doms, covid_lookup$vital_status, correct = FALSE)
chisq
# corrplot::corrplot((100*chisq$residuals^2/chisq$statistic), is.cor = FALSE)
umap_mat_plot_colors_df <-
umap_mat2$layout %>%
as.data.frame() %>%
mutate(patient_ID = row.names(.)) %>%
left_join(covid_lookup %>%
select(patient_ID, vital_des, proteo_pctseqs, entero_pctseqs)) %>%
select(patient_ID, V1, V2, vital_des, proteo_pctseqs, entero_pctseqs) %>%
mutate(proteo_col = case_when(proteo_pctseqs >= 0.05 ~ 0.5,
TRUE ~ 0),
entero_col = case_when(entero_pctseqs > 0.30 ~ 0.5,
TRUE ~ 0),
no_dom_col = case_when(proteo_pctseqs < 0.05 & entero_pctseqs < 0.30 ~ 1,
TRUE ~ 0)) %>%
pivot_longer(cols=c(proteo_col,entero_col,no_dom_col),names_to = "colorby",values_to = "amount") %>%
mutate(colorby = factor(colorby, levels = c("proteo_col", "entero_col", "no_dom_col"))) #%>%
umap_mat_plot_colors <-
ggplot(umap_mat_plot_colors_df)+
ggforce::geom_arc_bar(aes(
x0 = V1,
y0 = V2,
r0 = 0,
r = .15,
amount = amount,
fill = colorby,
color = colorby
), stat = "pie", n = 360, alpha = 0.4, size = 0.01)+
annotate("text", x = -0.75, y = 3.5, label = paste0("Chisq(", chisq$parameter, ")=",
round(chisq$statistic, 2), "; p=", round(chisq$p.value, 3)),
size = 4) +
stat_ellipse(data = umap_mat_plot_colors_df %>%
group_by(patient_ID) %>%
dplyr::slice_max(amount) %>%
filter(colorby == "proteo_col"), inherit.aes = F,
aes(x = V1, y = V2, color = colorby),
type = "t") +
stat_ellipse(data = umap_mat_plot_colors_df %>%
group_by(patient_ID) %>%
dplyr::slice_max(amount) %>%
filter(colorby == "proteo_col"), inherit.aes = F,
geom = "polygon",
aes(x = V1, y = V2, fill = colorby),
color = "black",
alpha = 0.35,
type = "euclid", level = 0.25, show.legend = F) +
theme_bw() +
theme(panel.grid = eb(),
axis.title = et(color = "black", size = 14),
axis.text = et(color = "black", size = 12),
legend.title = et(color = "black", size = 14),
legend.text = et(color = "black", size = 12),
plot.margin = margin(t = 0, # Top margin
r = 0, # Right margin
b = 0, # Bottom margin
l = 0)) + # Left margin) +
# ggtitle(paste0("Shotgun Taxonomy: UMAP \nCOVID-19 Microbiota Dominations \n", "n = ", nrow(umap_mat), "\n", custom_config$n_neighbors, " Neighbors")) +
xlab("UMAP1") +
ylab("UMAP2") +
scale_fill_manual(labels = c("Proteobacteria >= 5%", "Enterococcus >= 30%", "No Dominations"),
values = c("proteo_col"="red","entero_col"="#129246","no_dom_col" = "gray"))+
scale_color_manual(labels = c("Proteobacteria >= 5%", "Enterococcus >= 30%", "No Dominations"),
values = c("proteo_col"="red","entero_col"="#129246","no_dom_col" = "gray"))+
guides(fill = guide_legend(title = "Microbiota Composition"),
color = guide_legend(title = "Microbiota Composition"))+
lims(x = c(-2.75, 3.6), # obtained from umap below: ggplot_build(umap_mat_plot_colors)$layout$panel_scales_x[[1]]$range$range
y = c(-2.614908, 3.500000))+ # obtained from umap below: ggplot_build(umap_mat_plot_colors)$layout$panel_scales_y[[1]]$range$range
coord_equal()
umap_mat_plot_colors + ggtitle("Figure 1D")
#### UMAP Plot 2 END ####
#### UMAP Plot 1 + Plot 2 START ####
pdf("./Results/shotgun_umap_umap_doms_vital.pdf", height = 6, width = 14)
cowplot::plot_grid(umap_mat_plot,
umap_mat_plot_colors,
nrow = 1, rel_widths = c(1, 1.134))
dev.off()
#### UMAP Plot 1 + Plot 2 END ####
#### LEfSe START ####
# Custom function for nomenclature
genus_species <- function(x) {
sstr <- str_split(x,"\\|")[[1]]
sstr <- gsub("_+$"," sp.",sstr)
return(paste0(sstr[length(sstr)-1], "|",sstr[length(sstr)]))
}
# Relative abundance dataframe to combine on LEfSe plot
shotgun_rel_abd <-
covid_kraken %>%
left_join(proteo) %>%
arrange(Kingdom, Phylum, Class, Order, Family, Genus) %>%
mutate(Genus = factor(Genus, levels = unique(Genus))) %>%
group_by(patient_ID) %>%
arrange(Genus) %>%
mutate(cum.pct = cumsum(pctseqs),
y.text = (cum.pct + c(0, cum.pct[-length(cum.pct)]))/2) %>%
ungroup() %>%
dplyr::select(-cum.pct) %>%
mutate(pctseqs_100 = pctseqs*100,
log10_rel = log(pctseqs_100, base = 10)) %>%
mutate(phylogeny = paste(Kingdom, Phylum, Class, Order, Family, Genus, Species, sep = " | "),
phylogeny = sapply(phylogeny, genus_species),
phylogeny = gsub(pattern = "\\s+\\|\\s+$", replacement = "", x = phylogeny)) %>%
filter(phylogeny != " | ") %>%
left_join(covid_lookup %>%
select(patient_ID, vital_des)) %>%
dplyr::rename(feature = taxid)
# Build phyloseq object from shotgun data
# This is the shotgun taxonomy (otu table)
shotgun_otu <- covid_kraken %>%
mutate(Genus=paste(Kingdom,Phylum,Class,Order,Family,Genus,sep="|")) %>%
group_by(patient_ID) %>%
mutate(total=sum(numseqs)) %>%
group_by(patient_ID,total,Kingdom,Phylum,Class,Order,Family,Genus,Species,taxid) %>%
reshape2::dcast(patient_ID ~ taxid,value.var="numseqs",fill=0,fun.aggregate=sum) %>%
arrange(desc(patient_ID)) %>%
column_to_rownames(var = "patient_ID") %>%
t()
samp_covid <- sample_data(covid_lookup %>%
select(patient_ID, vital_des) %>%
column_to_rownames(var = "patient_ID")
)
# Shotgun_otu phyloseq data building
shotgun_otu_2 <- otu_table(shotgun_otu, taxa_are_rows = T)
taxmat <- matrix(sample(letters, nrow(shotgun_otu), replace = TRUE), nrow = nrow(shotgun_otu), ncol = 7)
rownames(taxmat) <- rownames(shotgun_otu)
colnames(taxmat) <- c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species")
tax_tbl <- tax_table(taxmat)
phy_shotgun_otu_2 <- phyloseq(shotgun_otu_2, tax_tbl, samp_covid)
# Build a lookup
shotgun_lefse_lookup <- tax_table(phy_shotgun_otu_2) %>%
as.matrix() %>%
as.data.frame() %>%
rownames_to_column(var = "taxid") %>%
mutate(taxid = as.integer(taxid)) %>%
select(taxid) %>%
left_join(tax_lookup) %>%
mutate(phylogeny = paste(Kingdom, Phylum, Class, Order, Family, Genus, Species, sep = " | ")) %>%
select(taxid, phylogeny)
# Run LEfSe analysis using shotgun taxonomy
set.seed(123)
phy_shotgun_otu_lefse <- run_lefse(
ps = phy_shotgun_otu_2,
group = "vital_des",
subgroup = NULL,
taxa_rank = "none",
transform = "identity",
norm = "CPM",
kw_cutoff = 0.05,
lda_cutoff = 3,
bootstrap_n = 30,
bootstrap_fraction = 2/3,
wilcoxon_cutoff = 0.05,
multigrp_strat = FALSE,
sample_min = 5,
only_same_subgrp = FALSE,
curv = FALSE)
# phy_shotgun_otu_lefse@marker_table %>% view
# Plot LEfSe results
phy_shotgun_otu_lefse_df <- phy_shotgun_otu_lefse@marker_table
gg_lefse_rel_abd <-
phy_shotgun_otu_lefse_df %>%
as.matrix() %>%
as.data.frame() %>%
mutate(
ef_lda_sign = ifelse(enrich_group == "Alive", ef_lda, as.numeric(paste0("-",ef_lda))),
ef_lda_sign = as.numeric(ef_lda_sign),
ef_lda = as.numeric(ef_lda),
pvalue = as.numeric(pvalue),
padj = as.numeric(padj)) %>%
left_join(shotgun_lefse_lookup %>%
mutate(taxid = as.character(taxid)) %>%
dplyr::rename(feature = taxid)) %>%
mutate(phylogeny = sapply(phylogeny, genus_species)) %>%
filter(phylogeny != " | ") %>%
group_by(phylogeny) %>%
slice_min(padj, n = 1, with_ties = F) %>%
mutate(phylogeny = gsub(pattern = "\\s+\\|\\s+$", replacement = "", x = phylogeny)) %>%
full_join(shotgun_rel_abd %>%
mutate(feature = as.character(feature)) %>%
select(-phylogeny),
by = "feature")
gg_lefse <- gg_lefse_rel_abd %>%
filter(enrich_group == vital_des) %>%
group_by(enrich_group, feature) %>%
dplyr::slice(1) %>%
ungroup()
tax_melt <- yingtools2::get.otu.melt(phy_shotgun_otu_lefse, filter.zero = FALSE) %>%
dplyr::mutate(taxid = as.character(otu)) %>%
filter(taxid %in% phy_shotgun_otu_lefse_df[[1]]) %>%
left_join(gg_lefse %>%
select(feature, phylogeny, ef_lda), by = c("taxid"="feature")) %>%
filter(!is.na(ef_lda))
pdf("./Results/shotgun_otu_lefse_analysis.pdf", height = 8, width = 14)
# Print Plot and save plot object to combine with other plots
gg_lefse_plot <-
gg_lefse %>%
ggplot(.,
aes(
x = reorder(phylogeny, -ef_lda_sign),
y = ef_lda_sign,
fill = enrich_group,
label = phylogeny
)) +
geom_col(color = "black", alpha = 0.8) +
geom_text(
nudge_y = ifelse(gg_lefse$ef_lda_sign > 0, 0.25, -0.25),
hjust = ifelse(gg_lefse$ef_lda_sign > 0, 0, 1),
size = 3.5
) +
theme_bw()+
theme(
panel.grid.minor = eb(),
panel.grid.major.y = eb(),
panel.border = eb(),
panel.grid.major.x = element_line(linetype="dotted", color = "grey75"),
axis.text.y = eb(),
axis.text.x = et(size = 12, color = "black"),
axis.title = et(size = 14, color = "black"),
axis.ticks.y = eb(),
legend.title = et(size = 14, color = "black"),
legend.text = et(size = 12, color = "black"),
plot.title = et(size = 16, color = "black")
)+
ggsci::scale_fill_lancet() +
scale_y_continuous(breaks = seq(-5,5,1),
limits = c(-8.5, 8.5))+
coord_flip() +
ylab("\nLDA Score (log10)") +
xlab("Phylogeny\n") +
labs(fill = "Vital Status")
gg_lefse_plot
dev.off()
gg_lefse_plot + ggtitle("Figure 1E")
#### LEfSe END ####
#### Combine Plots Together START ####
# Load legend
tax_legend <- magick::image_read_pdf("./Results/legend.v2.pdf")
gg_tax_legend <- ggdraw() + draw_image(tax_legend)
pdf("./Results/Figure_1.pdf", height = 14, width = 14)
gridExtra::grid.arrange(
proteo_grob, # 1
gg_tax_legend, # 2
gg_alpha_total, # 3
umap_mat_plot, # 4
umap_mat_plot_colors, # 5
gg_lefse_plot, # 6
layout_matrix = rbind(c(1,1,1,1, NA, 3,3,3),
c(1,1,1,1, NA, 3,3,3),
c(1,1,1,1, NA, 3,3,3),
c(1,1,1,1, 2, 3,3,3),
c(1,1,1,1, 2, 3,3,3),
c(1,1,1,1, 2, 3,3,3),
c(1,1,1,1, 2, 3,3,3),
c(1,1,1,1, NA, 3,3,3),
c(1,1,1,1, NA, 3,3,3),
c(1,1,1,1, NA, 3,3,3),
c(4,4,4,4, 5,5,5,5),
c(4,4,4,4, 5,5,5,5),
c(4,4,4,4, 5,5,5,5),
c(4,4,4,4, 5,5,5,5),
c(4,4,4,4, 5,5,5,5),
c(4,4,4,4, 5,5,5,5),
c(4,4,4,4, 5,5,5,5),
c(6,6,6,6,6,6,6,6),
c(6,6,6,6,6,6,6,6),
c(6,6,6,6,6,6,6,6),
c(6,6,6,6,6,6,6,6),
c(6,6,6,6,6,6,6,6),
c(6,6,6,6,6,6,6,6),
c(6,6,6,6,6,6,6,6),
c(6,6,6,6,6,6,6,6),
c(6,6,6,6,6,6,6,6),
c(6,6,6,6,6,6,6,6),
c(6,6,6,6,6,6,6,6),
c(6,6,6,6,6,6,6,6),
c(6,6,6,6,6,6,6,6),
c(6,6,6,6,6,6,6,6))
)
dev.off()
```
### Figure 4A: Cutpoint Analysis
```{r, score-roc, fig.dim=c(10,8), message=FALSE, warning=FALSE}
# Cutpoint dataframe
cutpoints_df <- metab_quant %>%
left_join(covid_lookup %>%
select(patient_ID, vital_status))
# Optimal cutpoints
# Create function to handle any errors during map function
safe_cutpointr <- possibly(.f = cutpointr, otherwise = "Error")
set.seed(123456)
cutpoints <-
cutpoints_df %>%
group_by(compound) %>%
group_map(~ safe_cutpointr(., mvalue, vital_status, compound,
method = maximize_metric,
metric = youden,
pos_class = 0,
neg_class = 1,
boot_runs = 2,
use_midpoints = TRUE,
na.rm = T),
.keep = TRUE)
cutpoints_unnest <- cutpoints %>%
map_df(as_tibble)
# Summary table
cutpoints_unnest_summary <-
cutpoints_unnest %>%
group_by(subgroup, pos_class) %>%
summarize(top_auc = max(AUC)) %>%
filter(top_auc == max(top_auc)) %>%
arrange(-top_auc)
##### ROC Plots START #####
# Plot ROC curves
cutpoints_unnest %>%
arrange(desc(AUC)) %>%
group_by(pos_class) %>%
ungroup() %>%
unnest(roc_curve) %>%
arrange(desc(AUC)) %>%
mutate(auc_label = paste0("AUC = ",round(AUC,5))) %>%
ggplot(aes(x = fpr, y = tpr))+
geom_line()+
geom_text(aes(label = auc_label, x = 0.6, y = 0.2))+
geom_text(aes(label = round(optimal_cutpoint, 3), y = 0.8, x = 0.2))+
facet_wrap(~subgroup+pos_class)
# Plot top results
cutpoints_unnest %>%
mutate(tvalue = as.numeric(str_extract(string = pos_class, pattern = "[0-9]\\.[0-9]+|[0-9]+")),
variable = gsub("\\s<=.*", "", pos_class)) %>%
separate(subgroup, c('group1', 'group2'), sep="__", remove = FALSE) %>%
select(-boot) %>%
mutate(group_ratio = if_else(!is.na(group2), paste(group1, group2, sep = " : "), group1)) %>%
arrange(desc(AUC)) %>%
group_by(pos_class) %>%
group_by(tvalue, variable, subgroup, group_ratio, pos_class) %>%
summarize(top_auc = max(AUC)) %>%
ungroup() %>%
arrange(desc(top_auc)) %>%
group_by(tvalue, pos_class) %>%
arrange(pos_class, tvalue, subgroup, group_ratio) %>%