-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3a_endoComp_summary.Rmd
337 lines (262 loc) · 9.03 KB
/
3a_endoComp_summary.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
---
title: "Does endophyte composition explain decay?"
author: "Marissa Lee"
date: "12/2/2018"
output: github_document
---
Contents:
1. Summarize sequencing effort
2. Create a filtered dataset without rare taxa
3. Identify taxa by functional groups
4. Export OTU tables for analysis
5. Summarize alpha diversity
___________________
___________________
Load libraries and functions
```{r}
#chunk options
knitr::opts_chunk$set(echo=FALSE, message=FALSE, warning=FALSE)
#---------------------------#
#libraries
library(tidyverse) # ggplot2, dplyr, tidyr, readr, purrr
library(tidymodels) # broom
library(sjstats) #provides function eta_sq() to extract r2 for each predictor in a lm
library(grid) #plotting
library(gridExtra) # plotting
#library(GGally) # bivariate plots
#library(lmtest) # for coxtest()
#---------------------------#
#fxns
source('code/helper_fxns.R')
sourceDir('code')
```
Load decay data
```{r loaddata}
# samples and decay
stemSamples <- load_stemSamples() # this function is in load_microbeData.R
initial_mass <- read_in_initial_mass()
harvest_mass <- LoadHarvestFiles()
pmr <- Calc_massRemaining(initial_mass, harvest_mass) # calculate percent mass remaining (pmr)
pmr_byStem <- AvgPMR_byStem(pmr, long.form = F) # average pmr for each stem and timepoint
stem.respVars <- list("time7", "time13", "time25", "time37","time59")
decayfits <- read_csv("derived_data/decayfits.csv") # see 1_decayPatterns.Rmd to update
code.respVars <- list("w.t50","beta","alpha","w.r2")
# weibull params
# beta = scale param = higher values mean *slower* decay
# alpha = shape param = higher values mean more S-shaped
```
Load wood trait data
```{r}
traits.code <- read.csv(file = "derived_data/traits_code.csv", row.names = 1)
traits.stem <- read.csv(file = "derived_data/traits_stem.csv", row.names = 1)
# ordering and stuff
trait.order <- traitcol.order()
traitVars1 <- c(trait.order$phys.cols, trait.order$nutr.cols)
traitVars1 # slice1 (w/ size)
traitVars2 <- c(trait.order$cfract.cols)
traitVars2 # slice2 (w/o size)
```
Load residuals from 2b_woodTraits_explainDecay.Rmd
```{r}
resids_explainDecay.list <- readRDS(file = "derived_data/resids_explainDecay_list.RData")
```
Load microbial community data
```{r}
# samples and decay
stemSamples <- load_stemSamples() # this function is in load_microbeData.R
# microbes
microb.data <- load_MicrobeCollection(stemSamples)
taxAndFunguild <- microb.data$taxAndFunguild
comm.otu <- microb.data$comm.otu
seqSamples <- microb.data$seqSamples
#check for oomycetes
colnames(comm.otu)[grep("oo", colnames(comm.otu))] # 9
taxAndFunguild %>%
filter(kingdom != "Fungi") #9
```
___________________
# 1. Summarize sequencing effort
Sample-effort curves
```{r}
#this takes a while... uncomment if the data changes
#plot_sampleEffortCurves(comm.otu) # output/figures/sampleEffortCurve.pdf
```
How many total OTUs and reads?
```{r}
# how many OTUs?
dim(comm.otu)
dim(taxAndFunguild)
taxAndFunguild %>%
group_by(kingdom) %>%
summarize(n = length(OTUId))
# how many reads?
x <- grepl("oo", colnames(comm.otu))
comm.otu.oo <- comm.otu[,x]
comm.otu.fun <- comm.otu[,!x]
sum(colSums(comm.otu.oo))
sum(colSums(comm.otu.fun))
```
How many per sample?
```{r}
# mean number of reads per sample
mean(rowSums(comm.otu))
sd(rowSums(comm.otu))/sqrt(length(rowSums(comm.otu)))
# mean numbr of OTUs per sample
comm.otu.pa <- (comm.otu > 0) * 1
mean(rowSums(comm.otu.pa))
sd(rowSums(comm.otu.pa))/sqrt(length(rowSums(comm.otu.pa)))
```
___________________
# 2. Create a filtered dataset without rare taxa
Filter community matrix to include only taxa that are present in a least 20% of all the samples. This step removes taxa that may not contribute much to our understanding of the relationship between species’ multivariate abundance and environment.
```{r}
comm.otu.trimmed <- removeRareOTUs(comm.otu)
# are oomycetes in here? nope
colnames(comm.otu.trimmed)[grep("oo", colnames(comm.otu.trimmed))]
# are pathotrophs in here?
head(taxAndFunguild)
taxAndFunguild %>%
filter(OTUId %in% colnames(comm.otu.trimmed)) %>%
filter(grepl("Patho", Trophic.Mode)) -> common.paths
write.csv(common.paths, file = "output/common_pathogens.csv")
```
This drops all the oomycetes
___________________
# 3. Identify taxa by functional groups
How many OTUs matched to a genus-level taxonomic assignment?
```{r}
colnames(taxAndFunguild)
n.unc <- sum(taxAndFunguild$genus == "unclassified")
n.unc / dim(taxAndFunguild)[1]
taxAndFunguild %>%
filter(genus != "unclassified") %>%
group_by(Confidence.Ranking) %>%
summarize(n = length(OTUId)) -> tmp
conf <- 244 + 54
conf/sum(tmp$n)
```
Identify the saprotrophs
```{r}
taxAndFunguild %>%
filter(grepl("Sapro", Guild)) -> sapro.tax
comm.otu.sapro <- comm.otu[, colnames(comm.otu) %in% sapro.tax$OTUId]
dim(comm.otu.sapro) # 176 OTUs
comm.otu.sapro.trimmed <- comm.otu.trimmed[, colnames(comm.otu.trimmed) %in% sapro.tax$OTUId]
dim(comm.otu.sapro.trimmed) # only 11 OTUs
```
There are 176 saprotroph OTUs, of those 11 are present in the trimmed community
Identify the basidios
```{r}
taxAndFunguild %>%
filter(grepl("Basidio", phylum)) -> basid.tax
basid.tax
comm.otu.basid <- comm.otu[, colnames(comm.otu) %in% basid.tax$OTUId]
dim(comm.otu.basid) # 526 OTUs
comm.otu.basid.trimmed <- comm.otu.trimmed[, colnames(comm.otu.trimmed) %in% basid.tax$OTUId]
dim(comm.otu.basid.trimmed) # only 15 OTUs
```
There are 526 Basidiomycota OTUs, of those 15 are present in the trimmed community
Identify the oomycetes
```{r}
taxAndFunguild %>%
filter(grepl("Protist", kingdom)) -> oo.tax
oo.tax
comm.otu.oo <- comm.otu[, colnames(comm.otu) %in% oo.tax$OTUId]
dim(comm.otu.oo) # 9 OTUs
#remove empty samples
#comm.otu.oo <- comm.otu.oo[rowSums(comm.otu.oo) > 0,]
comm.otu.oo.trimmed <- comm.otu.trimmed[, colnames(comm.otu.trimmed) %in% oo.tax$OTUId]
dim(comm.otu.oo.trimmed) # 0 OTUs
```
There are 9 Oomycete OTUs, of those 0 are present in the trimmed community
Identify the Pathotrophs
```{r}
taxAndFunguild %>%
filter(grepl("Patho", Guild)) -> patho.tax
comm.otu.patho <- comm.otu[, colnames(comm.otu) %in% patho.tax$OTUId]
dim(comm.otu.patho) # 129 OTUs
comm.otu.patho.trimmed <- comm.otu.trimmed[, colnames(comm.otu.trimmed) %in% patho.tax$OTUId]
dim(comm.otu.patho.trimmed) # only 11 OTUs
```
There are 129 Pathotroph OTUs, of those 10 are present in the trimmed community
___________________
# 4. Export OTU tables for analysis
```{r}
#all
dim(comm.otu) # 3297 OTUs
dim(comm.otu.trimmed) # 146 OTUs
#sapros
dim(comm.otu.sapro) # 176 OTUs
dim(comm.otu.sapro.trimmed) # only 11 OTUs
#basidios
dim(comm.otu.basid) # 526 OTUs
dim(comm.otu.basid.trimmed) # only 15 OTUs
#oomycetes
dim(comm.otu.oo) # 9 OTUs in 11 samples
dim(comm.otu.oo.trimmed) # 0 OTUs
#pathos
dim(comm.otu.patho) # 129 OTUs
dim(comm.otu.patho.trimmed) # only 10 OTUs
otu.list <- list(all = comm.otu,
sapro = comm.otu.sapro,
basid = comm.otu.basid,
oo = comm.otu.oo,
patho = comm.otu.patho)
saveRDS(otu.list, file = "derived_data/otu_list.RData")
otu.list.trimmed <- list(all = comm.otu.trimmed,
sapro = comm.otu.sapro.trimmed,
basid = comm.otu.basid.trimmed,
patho = comm.otu.patho.trimmed)
saveRDS(otu.list.trimmed, file = "derived_data/otu_list_trimmed.RData")
# remember there are 0 oomycete OTUs in the trimmed dataset so these are not included in the list
```
___________________
# 5. Summarize alpha diversity
```{r}
sr.otumat <- function(otumat){
# number of asvs
n.asvs <- length(colnames(otumat))
# number of ASVs per sample
otumat.pa <- (otumat>0) * 1
df <- data.frame(sample = names(rowSums(otumat.pa)),
nASVs = rowSums(otumat.pa),
row.names = NULL, stringsAsFactors = F)
# number of samples per ASV
df.samps <- data.frame(ASV = names(colSums(otumat.pa)),
nSamps = colSums(otumat.pa),
row.names = NULL, stringsAsFactors = F)
r.list <- c(total.asvs = n.asvs,
meanSR = mean(df$nASVs, na.rm = T),
sdSR = sd(df$nASVs, na.rm = T),
n = sum(!is.na(df$nASVs)),
meanSamps = mean(df.samps$nSamps, na.rm = T),
sdSamps = sd(df.samps$nSamps, na.rm = T),
emptySamps = sum(rowSums(otumat.pa) == 0))
return(r.list)
}
# full otu tables
otu.list %>%
map(~sr.otumat(.x)) %>%
as_tibble() %>%
t() %>%
data.frame() -> otus.sr
colnames(otus.sr) <- c("total.ASVs","meanSR","sdSR","n.Samps",
"meanSamps.ASV","sdSamps.ASV","empty.Samps")
# trimmed otu tables
otu.list.trimmed %>%
map(~sr.otumat(.x)) %>%
as_tibble() %>%
t() %>%
data.frame() -> otus.sr.trimmed
colnames(otus.sr.trimmed) <- c("total.ASVs","meanSR","sdSR","n.Samps",
"meanSamps.ASV","sdSamps.ASV","empty.Samps")
# combine
otus.sr$trim <- "no"
otus.sr$community <- row.names(otus.sr)
otus.sr.trimmed$trim <- "yes"
otus.sr.trimmed$community <- row.names(otus.sr.trimmed)
otus.sr %>%
full_join(otus.sr.trimmed) %>%
arrange(trim, community) -> otu.sr.all
write.csv(otu.sr.all, file = "output/tables/otu_summary.csv")
```