forked from microbiome/OMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdada2_workflow.Rmd
208 lines (162 loc) · 5.2 KB
/
dada2_workflow.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
---
title: "DADA2 tutorial"
output: html_notebook
---
Example of DADA2 tutorial can be found from [here](https://benjjneb.github.io/dada2/tutorial.html).
This example include only the code. The pipeline ends to _TreeSummarizedExperiment_.
We recommend to use the most recent reference database. You can find it from
[here](https://benjjneb.github.io/dada2/training.html).
```{r dada2_1}
# GETTING READY
library(dada2); packageVersion("dada2")
```
```{r dada2_2}
path <- "data/MiSeq_SOP" # CHANGE ME to the directory containing the fastq files after unzipping.
list.files(path)
```
```{r dada2_3}
# Forward and reverse fastq filenames have format: SAMPLENAME_R1_001.fastq and SAMPLENAME_R2_001.fastq
fnFs <- sort(list.files(path, pattern="_R1_001.fastq", full.names = TRUE))
fnRs <- sort(list.files(path, pattern="_R2_001.fastq", full.names = TRUE))
# Extract sample names, assuming filenames have format: SAMPLENAME_XXX.fastq
sample.names <- sapply(strsplit(basename(fnFs), "_"), `[`, 1)
```
```{r dada2_4}
# INSPECT READ QUALITY PROFILES
plotQualityProfile(fnFs[1:2])
```
```{r dada2_5}
plotQualityProfile(fnRs[1:2])
```
```{r dada2_6}
# FILTER AND TRIM
# Place filtered files in filtered/ subdirectory
filtFs <- file.path(path, "filtered", paste0(sample.names, "_F_filt.fastq.gz"))
filtRs <- file.path(path, "filtered", paste0(sample.names, "_R_filt.fastq.gz"))
names(filtFs) <- sample.names
names(filtRs) <- sample.names
```
```{r dada2_7}
out <- filterAndTrim(fnFs, filtFs, fnRs, filtRs, truncLen=c(240,160),
maxN=0, maxEE=c(2,2), truncQ=2, rm.phix=TRUE,
compress=TRUE, multithread=TRUE) # On Windows set multithread=FALSE
head(out)
```
```{r dada2_8}
# LEARN THE ERROR RATES
errF <- learnErrors(filtFs, multithread=TRUE)
```
```{r dada2_9}
errR <- learnErrors(filtRs, multithread=TRUE)
```
```{r dada2_10}
plotErrors(errF, nominalQ=TRUE)
```
```{r dada2_11}
# SAMPLE INFERENCE
dadaFs <- dada(filtFs, err=errF, multithread=TRUE)
```
```{r dada2_12}
dadaRs <- dada(filtRs, err=errR, multithread=TRUE)
```
```{r dada2_13}
dadaFs[[1]]
```
```{r dada2_14}
# MERGE PAIRED READS
mergers <- mergePairs(dadaFs, filtFs, dadaRs, filtRs, verbose=TRUE)
# Inspect the merger data.frame from the first sample
head(mergers[[1]])
```
```{r dada2_15}
# CONSTRUCT SEQUENCE TABLE
seqtab <- makeSequenceTable(mergers)
dim(seqtab)
```
```{r dada2_16}
# Inspect distribution of sequence lengths
table(nchar(getSequences(seqtab)))
```
```{r dada2_17}
# REMOVE CHIMERAS
seqtab.nochim <- removeBimeraDenovo(seqtab, method="consensus", multithread=TRUE, verbose=TRUE)
dim(seqtab.nochim)
```
```{r dada2_18}
sum(seqtab.nochim)/sum(seqtab)
```
```{r dada2_19}
# TRACK READS THROUGH THE PIPELINE
getN <- function(x) sum(getUniques(x))
track <- cbind(out, sapply(dadaFs, getN), sapply(dadaRs, getN), sapply(mergers, getN), rowSums(seqtab.nochim))
# If processing a single sample, remove the sapply calls: e.g. replace sapply(dadaFs, getN) with getN(dadaFs)
colnames(track) <- c("input", "filtered", "denoisedF", "denoisedR", "merged", "nonchim")
rownames(track) <- sample.names
head(track)
```
```{r dada_20}
# ASSIGN TAXONOMY
# You can find updated reference databases from
# https://benjjneb.github.io/dada2/training.html
# Change path
taxa <- assignTaxonomy(seqtab.nochim, "data/silva_nr_v132_train_set.fa.gz", multithread=TRUE)
```
```{r dada2_21}
# Change path
taxa <- addSpecies(taxa, "data/silva_species_assignment_v132.fa.gz")
```
```{r dada2_22}
taxa.print <- taxa # Removing sequence rownames for display only
rownames(taxa.print) <- NULL
head(taxa.print)
```
```{r dada2_23}
# EVALUATE ACCURACY
unqs.mock <- seqtab.nochim["Mock",]
unqs.mock <- sort(unqs.mock[unqs.mock>0], decreasing=TRUE) # Drop ASVs absent in the Mock
cat("DADA2 inferred", length(unqs.mock), "sample sequences present in the Mock community.\n")
```
```{r dada2_24}
mock.ref <- getSequences(file.path(path, "HMP_MOCK.v35.fasta"))
match.ref <- sum(sapply(names(unqs.mock), function(x) any(grepl(x, mock.ref))))
cat("Of those,", sum(match.ref), "were exact matches to the expected reference sequences.\n")
```
## Create a TreeSE data object
```{r dada2_25}
library(mia)
library(ggplot2)
if( !require("BiocManager") ){
install.packages("BiocManager")
library("BiocManager")
}
if( !require("Biostrings") ){
BiocManager::install("Biostrings")
library("Biostrings")
}
library(Biostrings)
```
```{r dada2_26}
samples.out <- rownames(seqtab.nochim)
subject <- sapply(strsplit(samples.out, "D"), `[`, 1)
gender <- substr(subject,1,1)
subject <- substr(subject,2,999)
day <- as.integer(sapply(strsplit(samples.out, "D"), `[`, 2))
samdf <- data.frame(Subject=subject, Gender=gender, Day=day)
samdf$When <- "Early"
samdf$When[samdf$Day>100] <- "Late"
rownames(samdf) <- samples.out
```
```{r dada2_27}
tse <- TreeSummarizedExperiment(assays = SimpleList(counts = t(seqtab.nochim)),
colData = DataFrame(samdf),
rowData = DataFrame(taxa)
)
# Remove mock sample like it is also done in DADA2 pipeline tutorial
tse <- tse[ , colnames(tse) != "mock"]
```
```{r dada2_38}
dna <- Biostrings::DNAStringSet( rownames(tse) )
referenceSeq(tse) <- dna
rownames(tse) <- paste0("ASV", seq( nrow(tse) ))
tse
```