
#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)
#library(scales)


#OPEN FILES
df_p1 <- read_excel('Exp 20250409 - AMTA with Imidazole Pt4.xlsx','Plate 1 Transposed')
df_p2 <- read_excel('Exp 20250411 - AMTA with Imidazole Pt5.xlsx','Plate 1 Transposed')
df_p3 <- read_excel('Exp 20250502 - AMTA with Imidazole Pt7.xlsx','Plate 1 Transposed')

name_df_p1 <- read_excel('Exp 20250409 - AMTA with Imidazole Pt4.xlsx','Sample Names')
name_df_p2 <- read_excel('Exp 20250411 - AMTA with Imidazole Pt5.xlsx','Sample Names')
name_df_p3 <- read_excel('Exp 20250502 - AMTA with Imidazole Pt7.xlsx','Sample Names')


#RENAME FIRST COLUMN
names(df_p1)[1]<-"Well_ID"
names(df_p2)[1]<-"Well_ID"
names(df_p3)[1]<-"Well_ID"

#MERGE NAMES_DF WITH MAIN DATAFRAME
df1_p1 <- cbind(name_df_p1, df_p1[!names(df_p1) %in% names(name_df_p1)])
df1_p2 <- cbind(name_df_p2, df_p2[!names(df_p2) %in% names(name_df_p2)])
df1_p3 <- cbind(name_df_p3, df_p3[!names(df_p3) %in% names(name_df_p3)])

#SUBSET THE DATA TO 427 NM AND 494 NM & ADD A COLUMN FOR DATE OF EXPERIMENT
df1_p1_sub <- cbind(df1_p1[1:5],
                    df1_p1["427"], 
                    df1_p1["494"], 
                    Time=rep("10 sec", nrow(df_p1)),
                    Plate=rep("1", nrow(df1_p1))) 
df1_p2_sub <- cbind(df1_p2[1:3],
                    AMTA=rep("10", nrow(df1_p2)),
                    df1_p2[5],
                    df1_p2["427"],
                    df1_p2["494"],
                    Time=rep("10 min", nrow(df1_p2)),
                    Plate=rep("2", nrow(df1_p2)))
df1_p3_sub <- cbind(df1_p3[1:5],
                    df1_p3["427"],
                    df1_p3["494"],
                    Time=rep("10 min", nrow(df1_p3)),
                    Plate=rep("3", nrow(df1_p3)))
df1_p3_sub$SampleType <- gsub("mM", "\\1", df1_p3_sub$SampleType)
colnames(df1_p3_sub)[4] <- "AMTA"

#COMBINE DATASETS
df2_sub <- rbind(df1_p1_sub, df1_p2_sub, df1_p3_sub)
colnames(df2_sub)[6] <- "Abs427"
colnames(df2_sub)[7] <- "Abs494"

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

#CALCULATE THE WATER (BLANK) CORRECTED ABSORBANCE VALUES
df3_sub <- inner_join(df2_sub, df_waterControls, by="Plate") %>%
  mutate(BC_value427 = Abs427 - Abs427_MeanAbs) %>%
  mutate(BC_value494 = Abs494 - Abs494_MeanAbs) %>%
  dplyr::select(Well_ID, SampleName, Conc, AMTA, Time, TechRep, Abs427, Abs494, BC_value427, BC_value494) %>%
  filter(SampleName == "Im")

#COMPUTE THE AVERAGE OF TECHREPS
df4_sub <- df3_sub %>%
  group_by(SampleName, Conc, AMTA, Time) %>%
  summarize_at(vars(BC_value427, BC_value494), list(MeanAbs = mean, SdAbs = sd))


#PLOT DATA - 494 NM
pal <- brewer.pal(7, "Blues")
ggplot(df4_sub, aes(x=Conc, y=BC_value494_MeanAbs, color=as.factor(AMTA), group=interaction(AMTA, Time))) + 
  geom_point(aes(shape=Time), size = 4) +
  geom_errorbar(aes(color=AMTA, ymin=(BC_value494_MeanAbs - BC_value494_SdAbs), ymax=(BC_value494_MeanAbs + BC_value494_SdAbs)), width=0.2,) +
  geom_line(aes(linetype=Time)) +
  labs(x = "[Imidazole] (mM)",
       y = "Absorbance 494 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(color = 1, size = 14), 
        legend.title=element_text(color=1, size=14, face="bold"), 
        legend.justification=c(0,1),
        legend.position=c(0.05,0.97), 
        legend.text=element_text(size = 14),
        panel.border = element_rect(colour = "black", fill=NA)) +
  scale_color_manual(values = c(pal[2], pal[4], pal[7]), labels=c("1 mM", "10 mM", "50 mM"), name=c("[AMTA]")) + 
  scale_shape_discrete(name = c("Reaction Time")) +
  scale_linetype_discrete(name = c("Reaction Time")) +
  guides(color=guide_legend(bycol=TRUE, nrow = 1), 
         shape=guide_legend(bycol=TRUE, reverse=TRUE, nrow = 1),
         linetype=guide_legend(bycol=TRUE, reverse=TRUE, nrow = 1))

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

#PLOT DATA - 427 NM
ggplot(df4_sub, aes(x=Conc, y=BC_value427_MeanAbs, color=as.factor(AMTA), group=interaction(AMTA, Time))) + 
  geom_point(aes(shape=Time), size = 4) +
  geom_errorbar(aes(color=AMTA, ymin=(BC_value427_MeanAbs - BC_value427_SdAbs), ymax=(BC_value427_MeanAbs + BC_value427_SdAbs)), width=0.2,) +
  geom_line(aes(linetype=Time)) +
  labs(x = "[Imidazole] (mM)",
       y = "Absorbance 427 nm (a.u.)") +
  theme_classic() +
  theme(aspect.ratio=1,
        axis.title=element_text(size=18), 
        axis.text=element_text(color = 1, size = 14), 
        legend.title=element_text(color=1, size=14, face="bold", hjust = 0.5), 
        legend.justification="center",
        legend.position="bottom", 
        legend.text=element_text(size = 14),
        panel.border = element_rect(colour = "black", fill=NA)) +
  scale_color_brewer(palette="Blues", labels=c("1 mM", "10 mM", "50 mM"), name=c("[AMTA]")) +
  scale_shape_discrete(name = c("Reaction Time")) +
  scale_linetype_discrete(name = c("Reaction Time")) +
  guides(color=guide_legend(bycol=TRUE, nrow = 1, title.position="top"), 
         shape=guide_legend(bycol=TRUE, reverse=TRUE, nrow = 1, title.position="top"),
         linetype=guide_legend(bycol=TRUE, reverse=TRUE, nrow = 1, title.position="top"))

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


