
#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)])

#ADD A COLUMN FOR DATE OF EXPERIMENT
df1_p1_sub <- cbind(Time=rep("10 sec", nrow(df_p1)),
                    Plate=rep("1", nrow(df1_p1)),
                    df1_p1[1:5],
                    df1_p1[106:length(df1_p1)])

df1_p2_sub <- cbind(Time=rep("10 min", nrow(df1_p2)),
                    Plate=rep("2", nrow(df1_p2)),
                    df1_p2[1:3],
                    AMTA=rep("10", nrow(df1_p2)),
                    df1_p2[5],
                    df1_p2[106:length(df1_p2)])

df1_p3_sub <- cbind(Time=rep("10 min", nrow(df1_p3)),
                    Plate=rep("3", nrow(df1_p3)),
                    df1_p3)
df1_p3_sub$SampleType <- gsub("mM", "\\1", df1_p3_sub$SampleType)
colnames(df1_p3_sub)[6] <- "AMTA"

#COMBINE DATASETS
df2 <- rbind(df1_p1_sub, df1_p2_sub, df1_p3_sub)

#WHERE THE INDIVIDUAL SPECTRA ARE IN COLUMNS, STACK THE DATA
df3 <- melt(df2, id=c("Well_ID", "SampleName", "Conc", "AMTA", "Time","TechRep", "Plate"))
names(df3)[names(df3) == "variable"] <- "Wavelength"

df3$value = as.numeric(df3$value)

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

#CALCULATE THE WATER (BLANK) CORRECTED ABSORBANCE VALUES
df4 <- inner_join(df3, df_waterControls, by=c("Plate", "Wavelength")) %>%
  mutate(BC_value = value - MeanAbs) %>%
  dplyr::select(Well_ID, SampleName, Conc, AMTA, Time, TechRep, Wavelength, value, BC_value) %>%
  filter(SampleName == "Im")

#COMPUTE THE AVERAGE OF TECHREPS
df5 <- df4 %>%
  group_by(SampleName, Conc, AMTA, Time, Wavelength) %>%
  summarize_at(vars(BC_value), list(MeanAbs = mean, SdAbs = sd)) %>%
  filter(Conc == 100)

df5$Wavelength <- as.numeric(as.character(df5$Wavelength))

#PLOT DATA - 100 uM Im
pal <- brewer.pal(7, "Blues")
ggplot(df5, aes(x=Wavelength, y=MeanAbs, color=as.factor(AMTA), group=interaction(AMTA, Time))) +
  geom_line(aes(linetype=Time)) +
  labs(x = "Wavelength (nm)",
       y = "Absorbance (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=13, face="bold", hjust = 0.5), 
        legend.justification="center",
        legend.position="bottom", 
        legend.text=element_text(size = 12),
        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_linetype_discrete(name = c("Reaction Time")) +
  guides(color=guide_legend(bycol=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_spectra.pdf", height=13, width=13, units="cm")

#COMPUTE THE AVERAGE OF TECHREPS
df6 <- df4 %>%
  group_by(SampleName, Conc, AMTA, Time, Wavelength) %>%
  summarize_at(vars(BC_value), list(MeanAbs = mean, SdAbs = sd)) %>%
  filter(Conc %in% c(0,100) & Time == "10 min")

df6$Wavelength <- as.numeric(as.character(df6$Wavelength))

#PLOT DATA - 0 and 100 uM Im
ggplot(df6, aes(x=Wavelength, y=MeanAbs, color=as.factor(AMTA), group=interaction(AMTA, Conc))) +
  geom_line(aes(linetype=Conc)) +
  labs(x = "Wavelength (nm)",
       y = "Absorbance (a.u.)") +
  scale_y_continuous(limits = c(-0.02,3)) +
  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=13, face="bold", hjust = 0.5), 
        legend.justification="center",
        legend.position="bottom", 
        legend.text=element_text(size = 13),
        panel.border = element_rect(colour = "black", fill=NA)) +
  scale_color_manual(values = c(pal[4],pal[7]), labels=c("10 mM", "50 mM"), name=c("[AMTA]")) +
  scale_linetype_manual(values = c(2,1), labels=c("0 \U003BCM", "100 \U003BCM"), name = c("[Imidazole]")) +
  guides(color=guide_legend(bycol=TRUE, nrow = 1, title.position="top"), 
         linetype=guide_legend(bycol=TRUE, nrow = 1, title.position="top"))

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

####

# PREP DATA FROM INITIAL CONDITIONS TO SHOW WHERE 427nm CAME FROM

#ADD A COLUMN FOR DATE OF EXPERIMENT
df1_p1_sub2 <- cbind(Time=rep("10 sec", nrow(df_p1)),
                    Plate=rep("1", nrow(df1_p1)),
                    df1_p1[1:5],
                    df1_p1)

#WHERE THE INDIVIDUAL SPECTRA ARE IN COLUMNS, STACK THE DATA
df3_sub <- melt(df1_p1_sub2, id=c("Well_ID", "SampleName", "Conc", "AMTA", "Time","TechRep", "Plate"))
names(df3_sub)[names(df3_sub) == "variable"] <- "Wavelength"

df3_sub$value = as.numeric(df3_sub$value)

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

#CALCULATE THE WATER (BLANK) CORRECTED ABSORBANCE VALUES
df4_sub <- inner_join(df3_sub, df3_sub_waterControls, by=c("Wavelength")) %>%
  mutate(BC_value = value - MeanAbs) %>%
  dplyr::select(Well_ID, SampleName, Conc, AMTA, Time, TechRep, Wavelength, value, BC_value) %>%
  filter(AMTA == 1)

df7 <- df4_sub %>%
  group_by(SampleName, Conc, AMTA, Time, Wavelength) %>%
  summarize_at(vars(BC_value), list(MeanAbs = mean, SdAbs = sd))

df7$Wavelength <- as.numeric(as.character(df7$Wavelength))

#PLOT DATA - 0-100 uM Im
ggplot(df7, aes(x=Wavelength, y=MeanAbs, color=as.factor(Conc))) + 
  geom_line() +
  labs(x = "Wavelength (nm)",
       y = "Absorbance (a.u.)") +
  #scale_y_continuous(limits = c(-0.02,3)) +
  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=13, face="bold", hjust = 0.5), 
        legend.justification="center",
        legend.position="right", 
        legend.text=element_text(size = 13),
        panel.border = element_rect(colour = "black", fill=NA)) +
  scale_color_manual(values = pal[2:7], name=c("Imidazole")) +
  guides(color=guide_legend(bycol=TRUE))

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

#EXTRACT 0uM CONTROLS AND COMPUTE THE AVERAGE OF TECHREPS
df_Blank_Im <- df4_sub %>%
  filter(Conc == 0) %>%
  group_by(Wavelength) %>%
  summarize_at(vars(BC_value), list(BC_MeanAbs = mean, BC_SdAbs = sd))

#CALCULATE THE 0uM (REAGENT BLANK) CORRECTED ABSORBANCE VALUES
df5_sub <- inner_join(df4_sub, df_Blank_Im, by=c("Wavelength")) %>%
  mutate(NC_value = BC_value - BC_MeanAbs) %>%
  dplyr::select(Well_ID, SampleName, Conc, TechRep, Wavelength, value, BC_value, NC_value)

#COMPUTE THE AVERAGE OF TECHREPS
df8 <- df5_sub %>%
  group_by(SampleName, Conc, Wavelength) %>%
  summarize_at(vars(NC_value), list(MeanAbs = mean, SdAbs = sd))

df8$Wavelength <- as.numeric(as.character(df8$Wavelength))

#PLOT DATA - Im
ggplot(df8, aes(x=Wavelength, y=MeanAbs, color=as.factor(Conc))) + 
  geom_line(aes(color=as.factor(Conc))) +
  labs(x = "Wavelength (nm)",
       y = "Δ Absorbance (a.u.)") +
  scale_y_continuous(breaks = seq(0, 0.15, by = 0.05), limits = c(-0.05,0.15)) +
  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=c(0,1),
        legend.position=c(0.7,0.97), 
        legend.text=element_text(size = 14),
        panel.border = element_rect(colour = "black", fill=NA)) +
  scale_color_manual(values = pal[2:7], name = c("Imidazole"), labels = c("0 \U003BCM", "0.1 \U003BCM", "1.0 \U003BCM", "10 \U003BCM", "100 \U003BCM", "1000 \U003BCM")) + 
  #annotate("text", x = 470, y = 0.75, label = expression(lambda["max"]~"494 nm")) +
  geom_vline(xintercept=427, linetype="dashed") +
  guides(color=guide_legend(byrow=TRUE, reverse = TRUE))

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