library(reshape2)
library(tidyverse)
library(ggplot2)
library(RColorBrewer)

CoMPP

compp <- read.csv("Nb13373_CoMPP.csv", stringsAsFactors = F)
compp.m <- melt(compp)
colnames(compp.m) <- c("mAb", "sample", "value")
compp.m$treatment <- c(rep("CDTA", 88),rep("NaOH", 88))
compp.m$mAb <- factor(compp.m$mAb, levels=unique(compp.m$mAb))
compp.m$treatment <- factor(compp.m$treatment, levels=unique(compp.m$treatment))
compp.m$sample <- rep(c(rep("Control.1", 22), rep("Control.2", 22), rep("Nb13373.1",22), rep("Nb13373.2",22)),2)
ggplot(data = compp.m, aes(x=mAb, y=sample, fill=value)) +
  geom_tile(color="white") + 
  scale_fill_gradient(low = "white", high = "red") + 
  geom_text(aes(label = value), size=3)+
  coord_flip()+
  theme_minimal()+
  facet_wrap(~treatment)+
  theme(axis.text.y = element_text(size=9, color = "black"),
        axis.text.x = element_text(size=9, angle = 35, hjust = .9, color = "black"),
        axis.title.y = element_blank(),
        aspect.ratio = 3,
        legend.position = "none")

ggsave("Fig_Nb13373_CoMPP.png", dpi = 300)
## Saving 7 x 5 in image

ELISA

elisa <- read.csv("ELISA_gsrOE.csv",stringsAsFactors = F)
elisa$mAb <- paste(elisa$mAb, ": ", elisa$epitope)
elisa <- elisa[,-2]
elisa.m <- melt(elisa)
colnames(elisa.m) <- c("mAb", "sample", "value") 
elisa.m$treatment <- c(rep("ammonium oxalate", 46), rep("sodium carbonate", 46), rep("1M KOH", 46), rep("4M KOH", 46))
elisa.m$mAb <- factor(elisa.m$mAb, levels=unique(elisa.m$mAb))
elisa.m$treatment <- factor(elisa.m$treatment, levels=unique(elisa.m$treatment))
elisa.m$sample <- rep(c(rep("Col-0", 23), rep("35S:GSR-YFP", 23)), 4)
ggplot(data = elisa.m, aes(x=mAb, y=sample, fill=value)) +
  geom_tile(color="white") + 
  scale_fill_gradient(low = "white", high = "red") + 
  geom_text(aes(label = value), size=2)+
  coord_flip()+
  theme_minimal()+
  facet_wrap(~treatment, nrow = 1)+
  theme(text = element_text(size=8),
        axis.text.y = element_text(color = "black"),
        axis.text.x = element_text(angle = 35, hjust = .9, color = "black"),
        axis.title = element_blank(),
        aspect.ratio = 4,
        legend.position = "none")

ggsave("Fig_DUF23OE_ELISA.png", dpi = 300)
## Saving 7 x 5 in image

#HPAEC

hpaec <- read.csv("180424_DUF23_HPAEC.csv", stringsAsFactors = F)
#Calculate mol ratios
hpaec$sum <- rowSums(hpaec[,3:12])
hpaec$Fucose.sum <- hpaec$Fucose/hpaec$sum
hpaec$Rhamnose.sum <- hpaec$Rhamnose/hpaec$sum
hpaec$Arabinose.sum <- hpaec$Arabinose/hpaec$sum
hpaec$Glucosamine.sum <- hpaec$Glucosamine/hpaec$sum
hpaec$Galactose.sum <- hpaec$Galactose/hpaec$sum
hpaec$Glucose.sum <- hpaec$Glucose/hpaec$sum
hpaec$Xylose.sum <- hpaec$Xylose/hpaec$sum
hpaec$Mannose.sum <- hpaec$Mannose/hpaec$sum
hpaec$GalA.sum <- hpaec$GalA/hpaec$sum
hpaec$GlcA.sum <- hpaec$GlcA/hpaec$sum

hpaec.split <- split.data.frame(hpaec, f = hpaec$genotype)
#calculate averages
hpaec.avg <- data.frame(matrix(NA, nrow = 9, ncol = 11))
colnames(hpaec.avg) <- c("genotype", as.character(colnames(hpaec)[3:12]))
for(i in 1:length(hpaec.split)){
hpaec.avg[i,] <- c(hpaec.split[[i]][1,1], colMeans(hpaec.split[[i]][,14:23]))
}
#calculate standard deviation
hpaec.sd <- data.frame(matrix(NA, nrow = 9, ncol = 11))
colnames(hpaec.sd) <- c("genotype", as.character(colnames(hpaec)[3:12]))
for(i in 1:length(hpaec.split)){
hpaec.sd[i,] <- c(hpaec.split[[i]][1,1], apply(hpaec.split[[i]][,14:23], 2, sd))
}
#combine avg and sd into single dataframe
hpaec.avg <- melt(hpaec.avg, id.vars = "genotype")
hpaec.sd <- melt(hpaec.sd, id.vars = "genotype")

colnames(hpaec.avg) <- c("genotype", "sugar", "avg")
colnames(hpaec.sd) <- c("genotype", "sugar", "sd")

hpaec.avg$id <- 1:nrow(hpaec.avg)
hpaec.sd$id <- 1:nrow(hpaec.sd)

hpaec.avg.sd <- merge.data.frame(hpaec.avg, hpaec.sd[,c("id", "sd")], by.x = "id", by.y = "id")

hpaec.avg.sd$avg <- as.numeric(hpaec.avg.sd$avg)
hpaec.avg.sd$sd <- as.numeric(hpaec.avg.sd$sd)

Plot by genotype

pal <- c("#A6CEE3", "#E31A1C", "#1F78B4", "#FF7F00","#B2DF8A","#6A3D9A","#FDBF6F","#33A02C","#FB9A99")

ggplot(hpaec.avg.sd, mapping = aes(x=sugar, y=avg, fill=genotype))+
  geom_col(position = "dodge")+
  scale_fill_manual(values=pal)+
  geom_errorbar(aes(ymin=avg-sd, ymax=avg+sd), width=.2,
                 position=position_dodge(.9))+
  theme_minimal() +
  theme(panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank())+
  ylab("mol%")+
  theme(axis.text.x = element_text(angle=45, hjust = 1.1))+
  coord_cartesian(expand = F)

ggsave("180424_DUF23_HPAEC_byGT.png", width = 7, height = 6)

Plot by sugar

hpaec.avg.sd$sugar <- factor(hpaec.avg.sd$sugar, levels = c("GalA","GlcA","Fucose","Rhamnose","Arabinose","Glucosamine","Galactose","Glucose","Xylose","Mannose"))

ggplot(hpaec.avg.sd, mapping = aes(x=genotype, y=avg, fill=sugar))+
  geom_col()+
  theme_minimal()+
  ylab("mol%")+
  theme(axis.text.x = element_text(angle=30, hjust = 1))+
  scale_fill_brewer(palette = "Set3")

ggsave("180424_DUF23_HPAEC_bySugar.png", width = 7, height = 6)