-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMP_ASVConstruction.R
262 lines (175 loc) · 7.74 KB
/
SMP_ASVConstruction.R
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
# ASV Construction #############################################################
### Author: [email protected] University of Fribourg 2023
### dada2 tutorial: https://benjjneb.github.io/dada2/tutorial.html
library("dada2")
library("ShortRead")
library("DECIPHER")
library("ggplot2")
theme_set(theme_bw(base_size = 15) +
theme(rect = element_rect(fill = "transparent")))
library("gridExtra")
rm(list = ls())
gc()
setwd("/scratch/pSMP/Korn/SMP_18S_reseq/")
ncore <- 6
################################################################################
list.files(pattern = "fastq.gz")
rF <- sort(list.files(pattern = "_R1.fastq.gz", full.names = TRUE))
rR <- sort(list.files(pattern = "_R2.fastq.gz", full.names = TRUE))
## Extract sample names
sample.names <- sapply(strsplit(basename(rF), "_"), `[`, 1)
## Filter and trim. Place filtered files in filtered/ subdirectory
rF.f <- file.path("filtered", paste0(sample.names, "_R1_filt.fastq.gz"))
rR.f <- file.path("filtered", paste0(sample.names, "_R2_filt.fastq.gz"))
names(rF.f) <- sample.names
names(rR.f) <- sample.names
### Check for primers
## Remove Ns from reads
rF.fN <- file.path("filtN", basename(rF))
rR.fN <- file.path("filtN", basename(rR))
filterAndTrim(rF, rF.fN, rR, rR.fN, maxN = 0, multithread = ncore)
## Forward and reverse primer (choose either 16S or 18S)
# FWD <- "GTGYCAGCMGCCGCGGTAA" # 515FB
# REV <- "GGACTACNVGGGTWTCTAAT" # 806RB
FWD <- "CGGTAAYTCCAGCTCYV" # 574*_f
REV <- "CCGTCAATTHCTTYAART" # 1132r
## Compile all orientations of the primers
allOrients <- function(primer) {
dna <- DNAString(primer)
orients <- c(Forward = dna, Complement = complement(dna),
Reverse = reverse(dna), RevComp = reverseComplement(dna))
return(sapply(orients, toString)) # Convert back to character vector
}
FWD.orients <- allOrients(FWD)
REV.orients <- allOrients(REV)
## Count occurence of all primer orientations in the reads
primerHits <- function(primer, fn) {
nhits <- vcountPattern(primer, sread(readFastq(fn)), fixed = FALSE)
return(sum(nhits > 0))
}
## Cut the primers
cutadapt <- "/usr/bin/cutadapt"
# system2(cutadapt, args = "--version")
path.cut <- file.path(".", "cutPrimers")
if(!dir.exists(path.cut)) dir.create(path.cut)
rF.cut <- file.path(path.cut, basename(rF.fN))
rR.cut <- file.path(path.cut, basename(rR.fN))
## Trim FWD and the reverse-complement of REV off of R1 & REV and the
## reverse-complement of FWD off of R2
FWD.RC <- dada2:::rc(FWD)
REV.RC <- dada2:::rc(REV)
R1.flags <- paste("-g", FWD, "-a", REV.RC)
R2.flags <- paste("-G", REV, "-A", FWD.RC)
## Run Cutadapt
for(i in seq_along(rF)) {
system2(cutadapt, args = c(R1.flags, R2.flags, "-n", 2,
"-o", rF.cut[i], "-p", rR.cut[i], # output
rF.fN[i], rR.fN[i])) # input
}
## Compare reads before and after trimming
# (reads <- ceiling(runif(1, 1, length(rF.cut))))
# rbind(FWD.ForwardReads = sapply(FWD.orients, primerHits, fn = rF.fN[[reads]]),
# FWD.ReverseReads = sapply(FWD.orients, primerHits, fn = rR.fN[[reads]]),
# REV.ForwardReads = sapply(REV.orients, primerHits, fn = rF.fN[[reads]]),
# REV.ReverseReads = sapply(REV.orients, primerHits, fn = rR.fN[[reads]]))
# rbind(FWD.ForwardReads = sapply(FWD.orients, primerHits, fn = rF.cut[[reads]]),
# FWD.ReverseReads = sapply(FWD.orients, primerHits, fn = rR.cut[[reads]]),
# REV.ForwardReads = sapply(REV.orients, primerHits, fn = rF.cut[[reads]]),
# REV.ReverseReads = sapply(REV.orients, primerHits, fn = rR.cut[[reads]]))
### Filter and trim. Place filtered files in filtered/ subdirectory
## 16S
# rF.cut.f <- file.path("filtered", paste0(sample.names, "_16S_R1_filt.fastq.gz"))
# rR.cut.f <- file.path("filtered", paste0(sample.names, "_16S_R2_filt.fastq.gz"))
## 18S
rF.cut.f <- file.path("filtered", paste0(sample.names, "_18S_R1_filt.fastq.gz"))
rR.cut.f <- file.path("filtered", paste0(sample.names, "_18S_R2_filt.fastq.gz"))
names(rF.cut.f) <- sample.names
names(rR.cut.f) <- sample.names
### Quality filtering
out <- filterAndTrim(rF.fN, rF.cut.f, rR.fN, rR.cut.f,
maxN = 0, maxEE = c(2, 2), minLen = 150,
truncQ = 10, rm.phix = TRUE,
compress = TRUE, multithread = ncore, verbose = TRUE)
### Plot quality profiles exemplarily
set.seed(74398)
startPlot <- ceiling(runif(1, 0, length(rF) - 3))
plot.rF <- plotQualityProfile(rF.fN[startPlot:(startPlot + 2)])
plot.rF.f <- plotQualityProfile(rF.cut.f[startPlot:(startPlot + 2)])
plot.rR <- plotQualityProfile(rR.fN[startPlot:(startPlot + 2)])
plot.rR.f <- plotQualityProfile(rR.cut.f[startPlot:(startPlot + 2)])
grid.arrange(plot.rF, plot.rR, plot.rF.f, plot.rR.f, ncol = 2)
### Estimate and plot the error rates
errF <- learnErrors(rF.cut.f, multithread = ncore, verbose = TRUE)
errR <- learnErrors(rR.cut.f, multithread = ncore, verbose = TRUE)
plotErrors(errF, nominalQ = TRUE)
plotErrors(errR, nominalQ = TRUE)
### Core sample inference algorithm
dadaF <- dada(rF.cut.f, err = errF, multithread = ncore)
dadaR <- dada(rR.cut.f, err = errR, multithread = ncore)
# dadaFs[[1]]
# saveRDS(dadaF, "dadaF_SMP16S.rds")
# saveRDS(dadaR, "dadaR_SMP16S.rds")
# dadaF <- readRDS("dadaF_SMP16S.rds")
# dadaR <- readRDS("dadaR_SMP16S.rds")
saveRDS(dadaF, "dadaF_SMP18S.rds")
saveRDS(dadaR, "dadaR_SMP18S.rds")
# dadaF <- readRDS("dadaF_SMP18S.rds")
# dadaR <- readRDS("dadaR_SMP18S.rds")
### Merge paired reads
# contigs <- mergePairs(dadaF, rF.cut.f, dadaR, rR.cut.f, verbose = TRUE)
contigs <- mergePairs(dadaF, rF.cut.f, dadaR, rR.cut.f, verbose = TRUE,
justConcatenate = TRUE)
head(contigs[[1]])
# saveRDS(contigs, "contigs_SMP16S.rds")
# contigs <- readRDS("contigs_SMP16S.rds")
saveRDS(contigs, "contigs_SMP18S.rds")
# contigs <- readRDS("contigs_SMP18S.rds")
### Construct amplicon sequence variant table (ASV) table
seqtab <- makeSequenceTable(contigs)
dim(seqtab)
### Inspect distribution of sequence lengths
table(nchar(getSequences(seqtab)))
### Chimera detection
seqtab.nochim <- removeBimeraDenovo(seqtab, method = "consensus",
multithread = ncore, verbose = TRUE)
dim(seqtab.nochim)
sum(seqtab.nochim) / sum(seqtab)
# saveRDS(seqtab.nochim, "seqtab.nochim_SMP16S.rds")
# seqtab.nochim <- readRDS("seqtab.nochim_SMP16S.rds")
saveRDS(seqtab.nochim, "seqtab.nochim_SMP18S.rds")
# seqtab.nochim <- readRDS("seqtab.nochim_SMP18S.rds")
### Track reads through the pipeline
# getN <- function(x) sum(getUniques(x))
#
# track <- cbind(out, sapply(dadaF, getN), sapply(dadaR, getN),
# sapply(contigs, getN), rowSums(seqtab.nochim))
#
# colnames(track) <- c("input", "filtered", "denoisedF", "denoisedR", "merged",
# "nonchim")
# rownames(track) <- sample.names
# head(track)
### Assign taxonomy with RDP classifier
taxa <- assignTaxonomy(seqtab.nochim,
"~/Desktop/SMP_unsynced/silva/silva_nr_v138_train_set.fa.gz",
# "../silva_nr_v138_train_set.fa.gz",
multithread = TRUE, verbose = TRUE)
# saveRDS(taxa, "taxa_SMP16S.rds")
saveRDS(taxa, "taxa_SMP18S.rds")
### Assign taxonomy with IdTaxa
dna <- DNAStringSet(getSequences(seqtab.nochim))
## Load training data
load("~/Desktop/SMP_unsynced/silva/SILVA_SSU_r138_2019.RData")
# load("~/Seafile/dada2/SILVA_SSU_r138_2019.RData")
ids <- IdTaxa(dna, trainingSet, strand = "top", processors = ncore,
verbose = TRUE)
ranks <- c("domain", "phylum", "class", "order", "Family", "genus", "species")
## Convert output object of class "Taxa"
taxid <- t(sapply(ids, function(x) {
m <- match(ranks, x$rank)
taxa <- x$taxon[m]
taxa
}))
colnames(taxid) <- ranks
rownames(taxid) <- getSequences(seqtab.nochim)
# saveRDS(taxid, "taxid_SMP16S.rds")
saveRDS(taxid, "taxid_SMP18S.rds")