forked from microbiome/course_2022_oulu
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path05-alpha_diversity_demo.Rmd
136 lines (87 loc) · 3.09 KB
/
05-alpha_diversity_demo.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
# Alpha diversity demo
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE, message=FALSE, warning=FALSE)
```
## Alpha diversity estimation
First let`s load the required packages and data set
```{r load}
library(mia)
library(miaViz)
library(tidyverse)
# library(vegan)
tse <- read_rds("data/Tengeler2020/tse.rds")
tse
```
Then let's estimate multiple diversity indices.
```{r estimate diversity}
?estimateDiversity
tse <- estimateDiversity(tse,
index = c("shannon","gini_simpson","faith"),
name = c("shannon","gini_simpson","faith"))
head(colData(tse))
```
We can see that the variables are included in the data.
Similarly, let's calculate richness indices.
```{r estimate richness}
tse <- estimateRichness(tse,
index = c("chao1","observed"))
head(colData(tse))
```
## Visualizing alpha diversity
We can plot the distributions of individual indices:
```{r distributions}
#individual plot
p <- as_tibble(colData(tse)) %>%
ggplot(aes(shannon)) +
geom_histogram()
print(p)
#multiple plots
p <- as_tibble(colData(tse)) %>%
pivot_longer(cols = c("shannon","gini_simpson","faith","chao1","observed"), names_to = "index", values_to = "alpha") %>%
ggplot(aes(alpha)) +
geom_histogram() +
facet_wrap(vars(index), scales = "free")
print(p)
```
and the correlation between indices:
```{r scatterlots, fig.width=13,fig.height=12}
p <- as_tibble(colData(tse)) %>%
pivot_longer(cols = c("shannon","gini_simpson","faith","chao1","observed"), names_to = "index", values_to = "alpha") %>%
full_join(.,., by = "sample_name") %>%
ggplot( aes(x = alpha.x, y = alpha.y)) +
geom_point() +
geom_smooth() +
facet_wrap(index.x ~ index.y, scales = "free")
print(p)
```
## Comparing alpha diversity
It is often interesting to look for any group differences:
```{r boxplots}
p <- as_tibble(colData(tse)) %>%
pivot_longer(cols = c("shannon","gini_simpson","faith","chao1","observed"), names_to = "index", values_to = "alpha") %>%
ggplot( aes(x = patient_status, y = alpha)) +
geom_boxplot(outlier.shape = NA) +
geom_jitter(alpha =0.5) +
facet_wrap(vars(index), scales = "free")
print(p)
```
Moreover, we can test the group differences by parametric or non-parametric tests:
```{r comparison}
df1 <- as_tibble(colData(tse)) %>%
pivot_longer(cols = c("faith","chao1","observed"), names_to = "index", values_to = "alpha") %>%
group_by(index) %>%
nest() %>%
mutate(test_pval = map_dbl(data, ~ t.test(alpha ~ patient_status, data = .x)$p.value)) %>%
mutate(test = "ttest" )
df2 <- as_tibble(colData(tse)) %>%
pivot_longer(cols = c("shannon","gini_simpson"), names_to = "index", values_to = "alpha") %>%
group_by(index) %>%
nest() %>%
mutate(test_pval = map_dbl(data, ~ wilcox.test(alpha ~ patient_status, data = .x)$p.value))%>%
mutate(test = "wilcoxon" )
df <- rbind(df1,df2) %>% select(-data) %>% arrange(test_pval) %>% ungroup()
df
```
End of the demo.
## Exercises
Do "Alpha diversity basics" from the [exercises](https://microbiome.github.io/OMA/exercises.html).