-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript_covid_preprocessing.R
172 lines (133 loc) · 6.15 KB
/
script_covid_preprocessing.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
######################################
#############PRE-PROCESSING###########
######################################
library(dada2)
library(phyloseq)
path <- "/path/to/reads"
fns <- list.files(path)
fns
fastqs <- fns[grepl(".fq$", fns)]
fastqs <- sort(fastqs) # Sort ensures forward/reverse reads are in same order
#### make sure that R1 is for forward read and R2 for reverse!!!!
fnFs <- fastqs[grepl(".1.fq", fastqs)] # Just the forward read files
fnRs <- fastqs[grepl(".2.fq", fastqs)] # Just the reverse read files
# Get sample names from the first part of the forward read filenames
sample.names <- sapply(strsplit(fnFs, ".1.fq"), `[`, 1) ## check if is 1 or 2!
# Fully specify the path for the fnFs and fnRs
fnFs <- file.path(path, fnFs)
fnRs <- file.path(path, fnRs)
###############
pdf("plotQualityProfile.pdf", onefile=T)
plotQualityProfile(fnFs[1:10]) ## remove 20 plus 10
plotQualityProfile(fnRs[1:10]) ## remove 20 plus 10
dev.off()
#### remove primers
filt_path <- file.path(path, "filtered") # Place filtered files in filtered/ subdirectory
filtFs <- file.path(filt_path, paste0(sample.names, "_F_filt.fastq.gz"))
filtRs <- file.path(filt_path, paste0(sample.names, "_R_filt.fastq.gz"))
# Filter #### important remove primers!!! and remove low quality regions
out <- filterAndTrim(fnFs, filtFs, fnRs, filtRs, truncLen=c(130,200), ##150
trimLeft=c(30, 30),
maxN=0, maxEE=c(2,2), truncQ=11, rm.phix=TRUE,
compress=TRUE, multithread=TRUE) #
head(out)
###############
pdf("plotQualityProfile.filt.pdf", onefile=T)
plotQualityProfile(filtFs[1:15])
plotQualityProfile(filtRs[1:15])
dev.off()
set.seed(12345)
# Learn forward error rates
errF <- learnErrors(filtFs, nread=1e6, multithread=TRUE)
# Learn reverse error rates
errR <- learnErrors(filtRs, nread=1e6, multithread=TRUE)
# Sample inference and merger of paired-end reads
mergers <- vector("list", length(sample.names))
names(mergers) <- sample.names
pdf("plotErrors_F.pdf", onefile=T)
plotErrors(errF, nominalQ=TRUE)
dev.off()
pdf("plotErrors_R.pdf", onefile=T)
plotErrors(errR, nominalQ=TRUE)
dev.off()
derepRs <- derepFastq(filtRs, verbose=TRUE)
derepFs <- derepFastq(filtFs, verbose=TRUE)
# Name the derep-class objects by the sample names
names(derepFs) <- sample.names
names(derepRs) <- sample.names
dadaFs <- dada(derepFs, err=errF, multithread=TRUE)
dadaRs <- dada(derepRs, err=errR, multithread=TRUE)
dadaFs[[1]]
mergers <- mergePairs(dadaFs, derepFs, dadaRs, derepRs, verbose=TRUE)
# Inspect the merger data.frame from the first sample
head(mergers[[1]])
# Construct sequence table and remove chimeras
seqtab <- makeSequenceTable(mergers)
dim(seqtab)
# Inspect distribution of sequence lengths
table(nchar(getSequences(seqtab)))
seqtab.nochim <- removeBimeraDenovo(seqtab, method="consensus", multithread=TRUE, verbose=TRUE)
dim(seqtab.nochim)
sum(seqtab.nochim)/sum(seqtab)
getN <- function(x) sum(getUniques(x))
track <- cbind(out, sapply(dadaFs, getN), sapply(mergers, getN), rowSums(seqtab), 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", "denoised", "merged", "tabled", "nonchim")
rownames(track) <- sample.names
head(track)
# Assign taxonomy
taxHS <- assignTaxonomy(seqtab.nochim, "rdp_train_set_18.fa.gz", multithread=TRUE)
taxHS <- addSpecies(taxHS, "rdp_species_assignment_18.fa.gz")
colnames(taxHS) <- c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus","Species")
unname(head(taxHS))
unname(tail(taxHS))
#ASV_names <- paste0("SVs",1:ncol(seqtab.nochim))
#colnames(seqtab.nochim) <- ASV_names
#rownames(taxHS) <- ASV_names
# Write to disk
write.table(track, file = "APR_track.tsv", quote=FALSE)
write.table(seqtab.nochim, file = "APR_sequence_table_SV.tsv", quote=FALSE)
write.table(taxHS, file = "APR_taxa_SV.tsv", quote=FALSE)
number_individuals <- as.data.frame(rowSums(seqtab.nochim))
second_min_value <- sort(number_individuals[,1])[2]
Samples_IDs <- rownames(seqtab.nochim)
Sample_data_table <- as.data.frame(read.table("Metadata_table.txt", sep="\t", header=TRUE))
rownames(Sample_data_table) <- Samples_IDs
ps <- phyloseq(otu_table(seqtab.nochim, taxa_are_rows=FALSE),
sample_data(Sample_data_table),
tax_table(taxHS))
######################################
############DECONTAMINATION###########
######################################
#remove contamination
sample_data(ps)$is.neg <- sample_data(ps)$Sample_or_Control == "Control Sample"
contamdf.prev <- isContaminant(ps, method="prevalence", neg="is.neg")
table(contamdf.prev$contaminant)
head(which(contamdf.prev$contaminant))
ps.pa <- transform_sample_counts(ps, function(abund) 1*(abund>0))
ps.pa.neg <- prune_samples(sample_data(ps.pa)$Sample_or_Control == "Control Sample", ps.pa)
ps.pa.pos <- prune_samples(sample_data(ps.pa)$Sample_or_Control == "True Sample", ps.pa)
# Make data.frame of prevalence in positive and negative samples
df.pa <- data.frame(pa.pos=taxa_sums(ps.pa.pos), pa.neg=taxa_sums(ps.pa.neg),
contaminant=contamdf.prev$contaminant)
ggplot(data=df.pa, aes(x=pa.neg, y=pa.pos, color=contaminant)) + geom_point() +
xlab("Prevalence (Negative Controls)") + ylab("Prevalence (True Samples)")
#remove contamination from phyloseq object
contamination <- as.integer(as.logical(contamdf.prev$contaminant))
contaminants <- c("1")
replacements <- c("3")
contamination <- replace(contamination, contamination %in% contaminants, replacements)
noncontaminants <- c("0")
replacements <- c("1")
contamination <- replace(contamination, contamination %in% noncontaminants, replacements)
contaminants <- c("3")
replacements <- c("0")
contamination <- replace(contamination, contamination %in% contaminants, replacements)
contamination <- as.logical(as.integer(contamination))
ps.nocontam = prune_taxa(contamination, ps)
Metadata <- as.data.frame(read.table("Metadata_table.txt", sep="\t", header=TRUE))
rownames(Metadata) <- Samples_IDs
Nonblanks<-as.character(Metadata$Logic)
Nonblanks <- as.logical(as.integer(Nonblanks))
ps.nocontam.noblanks = prune_samples(Nonblanks, ps.nocontam)
saveRDS(ps.nocontam.noblanks, "data/COVID_contamremoved.rdata")