-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1. decontam.R
195 lines (144 loc) · 8.67 KB
/
1. decontam.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
# This code is used to perform Blank Correction using the decontam package
# This is STEP 1 after running PEMA
# For each batch, we need: extended final tables + taxonomy assignments + a file sorting samples into "True Sample" or "Control Sample"
library(phyloseq)
BiocManager::install("decontam")
library(decontam)
library(ggplot2)
install.packages("gtable")
library(tidyr)
library(dplyr)
library(Biostrings)
#----------------COI HCMR data-------------------------
setwd("~/Datapaper/1.blankCorrection/COI")
# Do the following for each batch:
# _predecontam suffixes means that empty samples have been removed and recorded
# Reformatting of PEMA final table
ASVtable_COI <- read.table("extenedFinalTable_Sep20_COI_200924_predecontam.tsv", header = TRUE, sep = "\t")
ASVtable_COI_format <- ASVtable_COI %>%
separate(col = "ASV_number.amplicon", into = c("ASV_number", "ID"), sep = ":")
OTUtable <- select(ASVtable_COI_format, -c("ASV_number", "Classification", "TAXON.NCBI_TAX_ID"))
# add IDs as row names
rownames(OTUtable) <- OTUtable[[1]] # This sets the first column as row names
OTUtable <- OTUtable[,-1]
# Read file containing sample info (True Sample or Control Sample)
Sample <- read.csv("sample_data_Sep20_COI.csv", row.names = 1)
# OUTSIDE OF R: Reformat tax_assignments from PEMA's outputs
# add columns names, remove empty columns + confidence levels, remove tails from ASV number
# Then, read it here
Tax <- read.table("tax_assignments_Sep20_COI_200924.tsv", header = TRUE, sep = "\t")
Tax <- Tax %>%
separate(col = "ASV", into = c("ASV", "Number"), sep = "_")
Tax <- select(Tax, -c("Number"))
rownames(Tax) <- Tax[, 1]
Tax <- select(Tax, -c("ASV"))
# BUild phyloseq object
OTU <- otu_table(OTUtable, taxa_are_rows = TRUE)
sample_data <- sample_data(Sample)
Tax <- as.matrix(Tax) # Ensure Tax is a matrix
taxonomy <- tax_table(Tax)
ps <- phyloseq(OTU, sample_data, taxonomy)
# Remove samples with zero reads
ps <- prune_samples(sample_sums(ps) > 0, ps)
df <- as.data.frame(sample_data(ps)) # Put sample_data into a ggplot-friendly data.frame
df$LibrarySize <- sample_sums(ps)
df <- df[order(df$LibrarySize),]
df$Index <- seq(nrow(df))
ggplot(data=df, aes(x=Index, y=LibrarySize, color=Sample_or_Control)) + geom_point()
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)
# Optional: Filter by threshold (0.5 example shown)
contamdf.prev05 <- isContaminant(ps, method="prevalence", neg="is.neg", threshold=0.5)
table(contamdf.prev05$contaminant)
# Make phyloseq object of presence-absence in negative controls and true samples
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 (prevalence method is based on presence/absence of each sequence, and not on DNA concentration)
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)")
write.table(df.pa, "contaminantResults_Sep20_COI_200924.csv")
# Check which ASV/OTU are classified as contaminants.
# Remove them from extended final table outside of R. Save into "extenedFinalTable_..._postdecontam.tsv". Proceed to next step "2. rename_samples".
#----------------18S HCMR data-------------------------
setwd("~/Datapaper/1.blankCorrection/18S")
# Do the following for each batch:
# _predecontam means that empty samples have been removed and recorded
# Reformatting of PEMA final table
OTUtable <- read.table("extenedFinalTable_Aug23_18S_pr2_0624_predecontam.tsv", header = TRUE, sep = "\t")
rownames(OTUtable) <- OTUtable[[1]] # This sets the first column as row names
OTUtable <- OTUtable[,-1]
OTUtable <- select(OTUtable, -c("Classification", "TAXON.NCBI_TAX_ID"))
Sample <- read.csv("sample_data_Aug23_18S.csv", row.names = 1)
Tax <- read.table("tax_Aug23_18S_pr2.txt",sep = "\t", header=T, row.names=1)
# BUild phyloseq object
OTU <- otu_table(OTUtable, taxa_are_rows = TRUE)
sample_data <- sample_data(Sample)
Tax <- as.matrix(Tax) # Ensure Tax is a matrix
taxonomy <- tax_table(Tax)
ps <- phyloseq(OTU, sample_data, taxonomy)
df <- as.data.frame(sample_data(ps)) # Put sample_data into a ggplot-friendly data.frame
df$LibrarySize <- sample_sums(ps)
df <- df[order(df$LibrarySize),]
df$Index <- seq(nrow(df))
ggplot(data=df, aes(x=Index, y=LibrarySize, color=Sample_or_Control)) + geom_point()
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)
contamdf.prev05 <- isContaminant(ps, method="prevalence", neg="is.neg", threshold=0.5)
table(contamdf.prev05$contaminant)
# Make phyloseq object of presence-absence in negative controls and true samples
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)")
write.table(df.pa, "contaminantResults_Aug23_18S.csv")
# Check which ASV/OTU are classified as contaminants.
# Remove them from extended final table outside of R. Save into "extenedFinalTable_..._postdecontam.tsv". Proceed to next step "2. rename_samples".
#----------------ITS HCMR data-------------------------
setwd("~/Datapaper/1.blankCorrection/ITS")
# Do the following for each batch:
# _predecontam means that empty samples have been removed and recorded
# Reformatting of PEMA final table
OTUtable <- read.table("extenedFinalTable_Sep20_ITS_210924_predecontam.tsv", header = TRUE, sep = "\t")
rownames(OTUtable) <- OTUtable[[1]] # This sets the first column as row names
OTUtable <- select(OTUtable, -c("OTU", "Classification", "TAXON.NCBI_TAX_ID"))
# Load sample info (classification into true sample or negative control)
Sample <- read.csv("sample_data_Sep20_ITS.csv", row.names = 1)
# Load tax classification obtained from extended final table, manually reformatted to have the different levels
Tax <- read.table("tax_Sep20_ITS_210924.tsv",sep = "\t", header=T, row.names=1)
# BUild phyloseq object
OTU <- otu_table(OTUtable, taxa_are_rows = TRUE)
sample_data <- sample_data(Sample)
Tax <- as.matrix(Tax) # Ensure Tax is a matrix
taxonomy <- tax_table(Tax)
ps <- phyloseq(OTU, sample_data, taxonomy)
df <- as.data.frame(sample_data(ps)) # Put sample_data into a ggplot-friendly data.frame
df$LibrarySize <- sample_sums(ps)
df <- df[order(df$LibrarySize),]
df$Index <- seq(nrow(df))
ggplot(data=df, aes(x=Index, y=LibrarySize, color=Sample_or_Control)) + geom_point()
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)
contamdf.prev05 <- isContaminant(ps, method="prevalence", neg="is.neg", threshold=0.5)
table(contamdf.prev05$contaminant)
# Make phyloseq object of presence-absence in negative controls and true samples
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)")
write.table(df.pa, "contaminantResults_Sep20_ITS_210924.csv")
# Check which ASV/OTU are classified as contaminants.
# Remove them from extended final table outside of R. Save into "extenedFinalTable_..._postdecontam.tsv". Proceed to next step "2. rename_samples".