
#Load libraries
setwd(dirname(rstudioapi::getSourceEditorContext()$path)) #path is where this code is saved - data must be in the same folder
library(readxl)
library(dplyr)
library(reshape)
library(ggplot2)
library(RColorBrewer)


#OPEN FILES
df_d1 <- read_excel('Exp 20250404 - AMTA with Imidazole Factor Screen Day1.xlsx','Plate 1 Transposed')
df_d2 <- read_excel('Exp 20250407 - AMTA with Imidazole Factor Screen Day2.xlsx','Plate 1 Transposed')

name_df_d1 <- read_excel('Exp 20250404 - AMTA with Imidazole Factor Screen Day1.xlsx','Sample Names Abs')
name_df_d2 <- read_excel('Exp 20250407 - AMTA with Imidazole Factor Screen Day2.xlsx','Sample Names Abs')

#RENAME FIRST COLUMN
names(df_d1)[1]<-"Well_ID"
names(df_d2)[1]<-"Well_ID"

#MERGE NAMES_DF WITH MAIN DATAFRAME
df1_d1 <- cbind(name_df_d1, df_d1[!names(df_d1) %in% names(name_df_d1)])
df1_d2 <- cbind(name_df_d2, df_d2[!names(df_d2) %in% names(name_df_d2)])

#SUBSET THE DATA TO 427 NM & ADD A COLUMN FOR DATE OF EXPERIMENT
df1_d1_427 <- cbind(df1_d1[1:8],df1_d1["427"], Day=rep("1", nrow(df_d1))) 
df1_d2_427 <- cbind(df1_d2[1:8],df1_d2["427"], Day=rep("2", nrow(df_d2)))

#COMBINE DATASETS
df2_427 <- rbind(df1_d1_427, df1_d2_427)
colnames(df2_427)[9] <- "AbsValue"

#EXTRACT WATER CONTROLS AND COMPUTE THE AVERAGE OF TECHREPS
df_waterControls <- df2_427 %>%
  filter(SampleName == "Water Control") %>%
  group_by(Day) %>%
  summarize_at(vars("AbsValue"), list(MeanAbs = mean, SdAbs = sd))

#CALCULATE THE WATER (BLANK) CORRECTED ABSORBANCE VALUES
df3_427 <- inner_join(df2_427, df_waterControls, by="Day") %>%
  mutate(BC_value = AbsValue - MeanAbs) %>%
  dplyr::select(Well_ID, SampleName, Rxn, Batch, AMTA, NO2, Acid, TechRep, Day, AbsValue, BC_value) %>%
  filter(SampleName != "Water Control")

df3_427$AMTA <- gsub(" mM", "\\1", df3_427$AMTA)

#COMPUTE THE AVERAGE OF TECHREPS
df4_427 <- df3_427 %>%
  group_by(SampleName, Rxn, AMTA, NO2, Acid, Batch, Day) %>%
  summarize_at(vars(BC_value), list(MeanAbs = mean, SdAbs = sd))


#PLOT DATA - ACID VS AMTA
pal <- brewer.pal(7, "Blues")
ggplot(df3_427, aes(x=AMTA, y=AbsValue, group=as.factor(Acid))) + 
  #geom_point(aes(color=Acid), size = 3, shape=1) +
  stat_summary(fun.data = "mean_sdl", aes(color=Acid), size = 1, width=0.2, geom="errorbar") +
  stat_summary(fun.data = "mean_sdl", aes(color=Acid), size = 1, geom="line") +
  stat_summary(fun.data = "mean_sdl", aes(color=Acid), size = 4, geom="point") +
  labs(x = "AMTA (mM)",
       y = "Absorbance 427 nm (a.u.)") +
  scale_y_continuous(breaks = seq(0, 0.4, by = 0.05), limits = c(-0.02,0.28)) +
  theme_classic() +
  theme(aspect.ratio=1,
        axis.title=element_text(size=18), 
        axis.text=element_text(colour = 1, size = 14), 
        legend.title=element_blank(), 
        legend.justification=c(0,1),
        legend.position=c(0.05,0.97), 
        legend.text=element_text(size = 18),
        panel.border = element_rect(colour = "black", fill=NA)) +
  scale_color_manual(values = c(pal[4],pal[7]), labels=c(expression("HBF"[4]), "HCl")) +
  guides(color=guide_legend(byrow=TRUE, nrow = 1))

#PRINT/SAVE IMAGES, CHANGE FILE NAMES AS APPROPRIATE
ggsave("FactorScreen_AMTAvsAcid.pdf", height=13, width=13, units="cm")

#PLOT DATA - ACID VS NO2
ggplot(df3_427, aes(x=as.numeric(NO2), y=AbsValue, group=as.factor(Acid))) + 
  #geom_point(aes(color=Acid), size = 2, alpha=0.3) +
  stat_summary(fun.data = "mean_sdl", aes(color=Acid), size = 1, width=0.002, geom="errorbar") +
  stat_summary(fun.data = "mean_sdl", aes(color=Acid), size = 1, geom="line") +
  stat_summary(fun.data = "mean_sdl", aes(color=Acid), size = 4, geom="point") +
  labs(x = expression("NaNO"[2] ~ "(%)"),
       y = "Absorbance 427 nm (a.u.)") +
  scale_y_continuous(breaks = seq(0, 0.4, by = 0.05), limits = c(-0.02,0.28)) +
  scale_x_continuous(labels = scales::percent, limits = c(0,0.06)) +
  theme_classic() +
  theme(aspect.ratio=1,
        axis.title=element_text(size=18), 
        axis.text=element_text(colour = 1, size = 14), 
        legend.title=element_blank(), 
        legend.justification=c(0,1),
        legend.position=c(0.05,0.97), 
        legend.text=element_text(size = 18),
        panel.border = element_rect(colour = "black", fill=NA)) +
  scale_color_manual(values = c(pal[4],pal[7]), labels=c(expression("HBF"[4]), "HCl")) +
  guides(color=guide_legend(byrow=TRUE, nrow = 1))

#PRINT/SAVE IMAGES, CHANGE FILE NAMES AS APPROPRIATE
ggsave("FactorScreen_NO2vsAcid.pdf", height=13, width=13, units="cm")

#PLOT DATA - AMTA VS NO2
ggplot(df3_427, aes(x=as.numeric(NO2), y=AbsValue, group=as.factor(AMTA))) + 
  #geom_point(aes(color=AMTA), size = 2, alpha=0.3) +
  stat_summary(fun.data = "mean_sdl", aes(color=AMTA), size = 1, width=0.002, geom="errorbar") +
  stat_summary(fun.data = "mean_sdl", aes(color=AMTA), size = 1, geom="line") +
  stat_summary(fun.data = "mean_sdl", aes(color=AMTA), size = 4, geom="point") +
  labs(x = expression("NaNO"[2] ~ "(%)"),
       y = "Absorbance 427 nm (a.u.)") +
  scale_y_continuous(breaks = seq(0, 0.4, by = 0.05), limits = c(-0.02,0.28)) +
  scale_x_continuous(labels = scales::percent, limits = c(0,0.06)) +
  theme_classic() +
  theme(aspect.ratio=1,
        axis.title=element_text(size=18), 
        axis.text=element_text(colour = 1, size = 14), 
        legend.title=element_blank(), 
        legend.justification=c(0,1),
        legend.position=c(0.05,0.97), 
        legend.text=element_text(size = 18),
        panel.border = element_rect(colour = "black", fill=NA)) +
  scale_color_manual(values = c(pal[4],pal[7]), labels=c("0.1 mM AMTA", "1 mM AMTA")) +
  guides(color=guide_legend(byrow=TRUE, nrow = 1))

#PRINT/SAVE IMAGES, CHANGE FILE NAMES AS APPROPRIATE
ggsave("FactorScreen_NO2vsAMTA.pdf", height=13, width=13, units="cm")

#PLOT DATA - REACTION CONDITIONS
ggplot(df4_427, aes(x=Rxn, y=MeanAbs, group=as.factor(Rxn))) + 
  #geom_boxplot(linewidth=1, outliers = FALSE) +
  stat_summary(fun.data = "mean_sdl", aes(color=Rxn), size = 0.5, width=0.5, geom="errorbar", show.legend=FALSE) +
  stat_summary(fun.data = "mean_sdl", aes(color=Rxn), size = 4, geom="point", show.legend=FALSE) +
  geom_point(aes(color=Rxn), size = 3, shape=1, show.legend=FALSE) +
  labs(x = "Factor Experiment Condition",
       y = "Absorbance 427 nm (a.u.)") +
  scale_y_continuous(breaks = seq(-0.1, 0.4, by = 0.05), limits = c(-0.1,0.28)) +
  theme_classic() +
  theme(aspect.ratio=0.8,
        axis.title=element_text(size=18), 
        axis.text=element_text(colour = 1, size = 14), 
        legend.title=element_blank(), 
        legend.justification=c(0,1),
        legend.position=c(0.05,0.97), 
        legend.text=element_text(size = 18),
        panel.border = element_rect(colour = "black", fill=NA)) +
  scale_color_brewer(palette="Dark2")

#PRINT/SAVE IMAGES, CHANGE FILE NAMES AS APPROPRIATE
ggsave("FactorScreen_Conditions.pdf", height=13, width=16.25, units="cm")


