library("limma")
library("gplots")
library("marray")
library("vegan")
library("sciplot")
source("file2Exprs.R")
source("tern_e.R")
source("http://faculty.ucr.edu/~tgirke/Documents/R_BioCond/My_R_Scripts/vennDia.R")
#############################################################
#############################################################
# Figure 4 (natural site experiment)
#############################################################
#############################################################
# Figure 4a (Cluster dendrogram)
#############################################################
dat_rare <- read.delim("PyroTagger_counts_rarefied_A.txt", header=T, row.names=1, sep="\t")
# application of threshold for lower presence limit of relative abundance
threshold <- 5    # 0.5% presence: use integer 5 considering x1000 normalization
dat_rare_thres <- dat_rare[ apply(dat_rare,1,max) > threshold, ]
# log2-transformation
dat_rare_thres_log <- log2(dat_rare_thres + 1)
dim(dat_rare_thres_log)
# calculate Bray-Curtis dissimilarity
dat_dist <- vegdist(t(dat_rare_thres_log), method="bray")
# clustering
dat_dist_clu <- hclust(dat_dist, "average")
# pdf("Figure_4a.pdf", width=15, height=5)
plot(dat_dist_clu, main = "Figure 4a / Bray Curtis", lwd=1.5, cex=.5)
# dev.off()
#---///---///---///---///---///---///---///---///---///---///
# Dependencies of Figure 2 and Supplementary Figure 11
#############################################################
# Data upload and rearrangements
#############################################################
### uploading of "Design_Matrix.txt"
design <- read.delim("Design_matrix.txt", sep = "\t", header=TRUE )
rownames(design) <- design[,1]
dim(design)
design
### uploading of data file "PyroTagger_counts.txt"
dat_raw <- read.table("PyroTagger_counts.txt", row.names=1, sep="\t", header=T, blank.lines.skip = FALSE)
dat_raw[is.na(dat_raw)] = 0   # set NA to zero
dim(dat_raw)
colnames(dat_raw)
sort(colSums(dat_raw), dec=T)
### sample rearrangenemts required due to depletion of Chloroflexi
## (see Supplementary Information, Bulgarelli et al.)
# pooling of samples
R_AtSha19K20K <- dat_raw[,"R_AtSha19K"] + dat_raw[,"R_AtSha20K"]
R_AtLer17G18G <- dat_raw[,"R_AtLer17G"] + dat_raw[,"R_AtLer18G"]
Bulk35G36G <- dat_raw[,"Bulk35G"] + dat_raw[,"Bulk36G"]
dat_raw_2 <- cbind(dat_raw, R_AtSha19K20K, R_AtLer17G18G, Bulk35G36G)
# removing samples with less than 1000 counts after depletion of Chloroflexi
samples_to_remove <- c("R_AtLer20K", "R_AtSha3K", "R_AtSha19K", "R_AtSha20K", "R_AtLer15K",
"R_AtSha13K", "R_AtSha14K", "Bulk1K", "R_AtLer18G", "R_AtLer17G",
"R_AtSha34G", "Bulk7G", "Bulk36G", "Bulk35G", "AtWild3")
finalsamples <- rownames(design)[!rownames(design) %in% samples_to_remove]
## adjusting design matrix and PyroTagger counts to contain  only the final samples
design <- design[finalsamples, ]
dim(design)
dat_raw_2 <- dat_raw_2[ ,finalsamples]
dim(dat_raw_2)
### uploading of SILVA taxonommy
SILVAtax <- read.delim( "OTU_classification_SILVA.txt", row.names=1, sep="\t", header=T)
### SUBSETTING the pyrotagger count data matrix and DEPLETION of Chloroflexi-OTUs
# preparation of subsets from design matrix (does not contain the 2 technial replicate samples "Bulk26K" & "Bulk27K")
# note, "Bulk25K" is the third technical replicate
# this subsetting is required, as the pyrotagger counts contains these 2 additional samples of the technical replicates
subset_cologne <- rownames(design)[which(design$Site == "Cologne")]
subset_golm <- rownames(design)[which(design$Site == "Golm")]
subset_naturalsite <- rownames(design)[which(design$Site == "naturalsite")]
# preparation of list with non-Chloroflexi OTUs
noChloro <- rownames(SILVAtax)[which(SILVAtax$Phylum != "Chloroflexi")]
length(noChloro)
noChloro <- noChloro[noChloro %in% rownames(dat_raw_2)]
length(noChloro)
table(SILVAtax[noChloro,2])
## SUBSETTING and DEPLETION
dat_raw_noC <- dat_raw_2[noChloro, c(subset_cologne, subset_golm, subset_naturalsite)]
dim(dat_raw_noC)
dat_raw_noC[1:10,1:5]
sort(colSums(dat_raw_noC), dec=T) # all samples have at least 1000 counts
#############################################################
# Data transformation and thresholding
#############################################################
# normalization by sample size and transformation in "/1000"
dat_norm <- t(t(dat_raw_noC)/colSums(dat_raw_noC)) * 1000
# application of threshold for lower presence limit of relative abundance
threshold <- 5    # 0.5% presence: use integer 5 considering x1000 normalization
dat_thres <- dat_norm[ apply(dat_norm, 1, max) > threshold, ]
# log2-transformation
dat_log <- log2(dat_thres + 1)
dim(dat_log)
subsettedOTUs <- rownames(dat_log)
# preparation of eset format required for limma based analysis
eset <- new("ExpressionSet")
exprs(eset) <- dat_log
pData(eset) <- design
#############################################################
# Figure 2 (based on Cologne soil samples)
#############################################################
### subsetting of design matrix and eset for Cologne soil samples
design_sc <- design[subset_cologne,]    # "_sc" for subset Cologne
eset_sc <- eset[,subset_cologne]
eset_sc = eset_sc[rowSums((2^exprs(eset_sc)) >= threshold + 1) >= 1,]
### limma based statistics at the level of COMPARTMENTS
sample_sc <- as.vector(design_sc$Compartment)
model_sc <- model.matrix(~0 + sample_sc)
colnames(model_sc) <- make.names(colnames(model_sc))
fit_sc <- lmFit(eset_sc, model_sc)
### Figure 2a (ternary plot)
#############################################################
# contrasts: root against soil and root against rhizo and Rhizo -soil
cont.matrix_1A <- makeContrasts(sample_scRoot - sample_scSoil, sample_scRoot - sample_scRhizosphere, sample_scRhizosphere - sample_scSoil, levels = model_sc)
fit2_1A <- contrasts.fit(fit_sc, cont.matrix_1A)
fit2_1A <- eBayes(fit2_1A)
tab_1A <- topTable(fit2_1A, n=dim(exprs(eset_sc))[1], adjust="fdr")
de_tab_1A <- decideTests(fit2_1A)
# defining vector with root-enriched OTUs (over Soil and Rhizosphere) in Cologne Soil
RootOTUs_sc <- rownames(de_tab_1A)[de_tab_1A[,1]==1 & de_tab_1A[,2]==1]
### Figure 2b (ternary plot)
#############################################################
# contrasts: root - wood, wood - soil and wood -rhizo
cont.matrix_1B <- makeContrasts(sample_scRoot - sample_scWood, sample_scWood - sample_scSoil, sample_scWood - sample_scRhizosphere, levels=model_sc)
fit2_1B <- contrasts.fit(fit_sc, cont.matrix_1B)
fit2_1B <- eBayes(fit2_1B)
de_tab_1B <- decideTests(fit2_1B)
# defining vector with root-specific OTUs that are wood-corrected in Cologne soil
rOTUs_sc <- rownames(de_tab_1B)[de_tab_1B[,1]==1][rownames(de_tab_1B)[de_tab_1B[,1]==1] %in% RootOTUs_sc]
### Supplementary Figure 11 (based on Golm soil samples)
#############################################################
#############################################################
### subsetting of design matrix and eset for Golm soil samples
design_sg <- design[subset_golm,]   # "_sg" for subset Golm
eset_sg <- eset[,subset_golm]
eset_sg = eset_sg[rowSums((2^exprs(eset_sg)) >= threshold + 1) >= 1,]
### limma based statistics at the level of COMPARTMENTS
sample_sg <- as.vector(design_sg$Compartment)
model_sg <- model.matrix(~0 + sample_sg)
colnames(model_sg) <- make.names(colnames(model_sg))
fit_sg <- lmFit(eset_sg, model_sg)
## Supplementary Figure 11a (ternary plot)
#############################################################
# contrasts: root against soil and root against rhizo and rhizo - soil
cont.matrix_1Ag <- makeContrasts(sample_sgRoot - sample_sgSoil, sample_sgRoot - sample_sgRhizosphere, sample_sgRhizosphere - sample_sgSoil, levels = model_sg)
fit2_1Ag <- contrasts.fit(fit_sg, cont.matrix_1Ag)
fit2_1Ag <- eBayes(fit2_1Ag)
tab_1Ag <- topTable(fit2_1Ag, n=dim(exprs(eset_sg))[1], adjust="fdr")
de_tab_1Ag <- decideTests(fit2_1Ag)
# defining vector with root-enriched OTUs (over Soil and Rhizosphere) in Golm Soil
RootOTUs_sg <- rownames(de_tab_1Ag)[de_tab_1Ag[,1]==1 & de_tab_1Ag[,2]==1]
length(RootOTUs_sg)
## Supplementary Figure 11b (ternary plot)
#############################################################
# contrasts: root - wood, wood - soil and wood -rhizo
cont.matrix_1Bg <- makeContrasts(sample_sgRoot - sample_sgWood, sample_sgWood - sample_sgSoil, sample_sgWood - sample_sgRhizosphere, levels=model_sg)
fit2_1Bg <- contrasts.fit(fit_sg, cont.matrix_1Bg)
fit2_1Bg <- eBayes(fit2_1Bg)
de_tab_1Bg <- decideTests(fit2_1Bg)
# defining vector with root-specific OTUs that are wood corrected in Golm Soil
rOTUs_sg <- rownames(de_tab_1Bg)[de_tab_1Bg[,1]==1][rownames(de_tab_1Bg)[de_tab_1Bg[,1]==1] %in% RootOTUs_sg]
length(rOTUs_sg)
### Figure 2e (Venn diagramm)
#############################################################
VennList = venndiagram(x = RootOTUs_sc, y = RootOTUs_sg, z = rOTUs_sc, w = rOTUs_sg, unique = T, title="Figure 2e / Cologne vs. Golm OTUs", labels=c("RootOTUs_sc", "RootOTUs_sg", "rOTUs_sc", "rOTUs_sg"), lines=c(2,3,4,1), lcol=c(2,3,4,1), tcol = 1, lwd=3, cex=1, printsub=T, type ="4")
lightblue_Cologne_only = VennList$q10                 # required for Figure 4b
lightB1_overlap = VennList$q4
lightB2_overlap = VennList$q5
lightB3_overlap = VennList$q6
lightblue_BOTH <- c(lightB1_overlap, lightB2_overlap ,lightB3_overlap)  # required for Figure 4b
lightblue_Golm_only = VennList$q11                    # required for Figure 4b
lOTUs_sc <- c(lightblue_Cologne_only,lightblue_BOTH)  # to plot Cologne and Golm soil samples
lOTUs_sg <- c(lightblue_Golm_only,lightblue_BOTH)     # to plot Cologne and Golm soil samples
darkblue_Cologne_only = VennList$q8                   # required for Figure 4b
darkblue_BOTH = VennList$q1                           # required for Figure 4b
darkblue_Golm_only = VennList$q9                      # required for Figure 4b
rOTUs_sc <- c(darkblue_Cologne_only,darkblue_BOTH)    # to plot Cologne and Golm soil samples
rOTUs_sg <- c(darkblue_Golm_only, darkblue_BOTH)      # to plot Cologne and Golm soil samples
#---///---///---///---///---///---///---///---///---///---///
# Figure 4b (Venn diagram)
# (detection of "Figure 2e OTUs" in natural site samples)
#############################################################
# generation of data subset for natural site samples
dat_raw_ns <- dat_raw_noC[,subset_naturalsite]      # analysis with raw count data
dat_raw_ns <- dat_raw_ns[rowSums(dat_raw_ns) > 0,]  # remove 0-count OTUs
dim(dat_raw_ns)
### probing rOTUs and lOTUs of both soils in natural site data subset
lightblue_sc_inP <- rownames(dat_raw_ns)[rownames(dat_raw_ns) %in% lightblue_Cologne_only]
100 / length(lightblue_Cologne_only) * length(lightblue_sc_inP)
lightblue_sg_inP <- rownames(dat_raw_ns)[rownames(dat_raw_ns) %in% lightblue_Golm_only]
100 / length(lightblue_Golm_only) * length(lightblue_sg_inP)
lightblue_scg_inP <- rownames(dat_raw_ns)[rownames(dat_raw_ns) %in% lightblue_BOTH]
100 / length(lightblue_BOTH) * length(lightblue_scg_inP)
darkblue_sc_inP <- rownames(dat_raw_ns)[rownames(dat_raw_ns) %in% darkblue_Cologne_only]
100 / length(darkblue_Cologne_only) * length(darkblue_sc_inP)
darkblue_sg_inP <- rownames(dat_raw_ns)[rownames(dat_raw_ns) %in% darkblue_Golm_only]
100 / length(darkblue_Golm_only) * length(darkblue_sg_inP)
darkblue_scg_inP <- rownames(dat_raw_ns)[rownames(dat_raw_ns) %in% darkblue_BOTH]
100 / length(darkblue_BOTH) * length(darkblue_scg_inP)
# Figure 4c (ternary plot)
#############################################################
# subsetting for natural site samples
eset_sns <- eset[,subset_naturalsite]
eset_sns = eset_sns[rowSums((2^exprs(eset_sns)) >= threshold + 1) >= 1,]
dim(eset_sns)
### LIMMA stats for Rhizosphere effect
design_sns <- design[subset_naturalsite,]
sample_sns <- as.vector(design_sns$Compartment)
model_sns <- model.matrix(~0 + sample_sns)
colnames(model_sns) <- make.names(colnames(model_sns))
fit_sns <- lmFit(eset_sns, model_sns)
# contrasts: root against soil, root against rhizo and rhizo against soil
cont.matrix_sns <- makeContrasts(sample_snsRoot - sample_snsSoil, sample_snsRoot - sample_snsRhizosphere, sample_snsRhizosphere - sample_snsSoil, levels=model_sns)
fit2_sns <- contrasts.fit(fit_sns, cont.matrix_sns)
fit2_sns <- eBayes(fit2_sns)
tab_sns <- topTable(fit2_sns, n=dim(exprs(eset_sns))[1], adjust="fdr")
de_tab_sns <- decideTests(fit2_sns)
rhizoOTUs <- rownames(de_tab_sns)[de_tab_sns[,2]==-1 & de_tab_sns[,3]==1]
### Ternary plot, mean values for all compartments
means_sns_rhizo <- fit_sns$coef[,1]
means_sns_root <- fit_sns$coef[,2]
means_sns_soil <- fit_sns$coef[,3]
temat_sns <- cbind(means_sns_root, means_sns_rhizo, means_sns_soil)
colnames(temat_sns) <- c("Root","Rhizosphere","Soil")
dim(temat_sns)
### plotting Figure 4c
# define color coding for ternary plot
fig4c_colors <- ifelse(rownames(temat_sns) %in% rhizoOTUs, "black","darkgrey")
names(fig4c_colors) <- rownames(temat_sns)
fig4c_colors[lightblue_BOTH] <- "lightblue"
fig4c_colors[darkblue_BOTH] <- "blue"
# pdf("Figure_4c.pdf", width=8, height=8)
tern_e(temat_sns, prop=T, col= fig4c_colors, main = "Figure 4c / lOTUs and rOTUs")
# dev.off()
### Figure 4d (bargraph per compartment)
#############################################################
Actinocorallia <- "OTU36"
# pdf("Fig4D_Actionocorallia_barCI.pdf")
bargraph.CI(drop.levels(pData(eset_sns)$Compartment), legend = T, exprs(eset_sns)[Actinocorallia,], main = "Figure 4d / Actinocorallia", ylab = "relative abundance (log2)", ylim=c(0,4), lc=FALSE, col="black")
# dev.off()
