
#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 <- read_excel('Exp 20250804 - Optimization of SALLE Extraction Pt2.xlsx','Plate 1 Transposed')

name_df <- read_excel('Exp 20250804 - Optimization of SALLE Extraction Pt2.xlsx','Sample Names')

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

#MERGE NAMES_DF WITH MAIN DATAFRAME
df1 <- cbind(name_df, df[!names(df) %in% names(name_df)])

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

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

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

#CALCULATE THE WATER (BLANK) CORRECTED ABSORBANCE VALUES
df3 <- inner_join(df2, df_waterControls, by=c("Wavelength")) %>%
  mutate(BC_value = value - MeanAbs) %>%
  dplyr::select(Well_ID, SampleName, Conc, TechRep, Wavelength, value, BC_value) %>%
  filter(!SampleName %in% c("Water Control"))

#EXTRACT 0uM CONTROLS AND COMPUTE THE AVERAGE OF TECHREPS
df3_Blank <- df3 %>%
  filter(SampleName == "Neg Ctrl") %>%
  group_by(Wavelength) %>%
  summarize_at(vars(BC_value), list(BC_MeanAbs = mean, BC_SdAbs = sd))

#CALCULATE THE 0uM (REAGENT BLANK) CORRECTED ABSORBANCE VALUES
df4 <- inner_join(df3, df3_Blank, 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
df5 <- df4 %>%
  group_by(SampleName, Conc, Wavelength) %>%
  summarize_at(vars(NC_value), list(MeanAbs = mean, SdAbs = sd))

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

#ISOLATE THE DATA FOR THE LMAX PEAK
df5_peak <- df5 %>%
  filter(Wavelength == 494 & !SampleName %in% c("Pos Ctrl", "Neg Ctrl"))

df5_pos <- df5 %>%
  filter(Wavelength == 494 & SampleName == "Pos Ctrl")

df5_neg <- df5 %>%
  filter(Wavelength == 494 & SampleName == "Neg Ctrl")

df6_peak <- cbind(str_split_fixed(df5_peak$SampleName, " ", 2), df5_peak[2:5])
names(df6_peak)[1:2]<-c("SampleName","ExpRep")

df6_peak$Recovery <- df6_peak$MeanAbs/df5_pos$MeanAbs

df6_peak$SampleName <- factor(df6_peak$SampleName, levels = c("Blank", "Im", "E150D", "E150D:Im"))

#PLOT DATA - 494 NM
pal <- brewer.pal(6, "Blues")
ggplot(df6_peak, aes(x=Conc, y=MeanAbs, group=SampleName)) + 
  stat_summary(fun.data = "mean_sdl", aes(color=SampleName, linetype=SampleName), size = 1, geom="line") +
  geom_point(aes(shape=SampleName, color=SampleName), size = 4) +
  #geom_errorbar(aes(ymin=(MeanAbs - SdAbs), ymax=(MeanAbs + SdAbs), color=SampleName), width = 0.05) +
  scale_y_continuous(breaks = seq(0, 0.65, by = 0.1), limits = c(-0.01,0.6)) +
  labs(x = "[NaCl] (M)",
       y = "Δ Absorbance 494 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_blank(), 
        legend.justification=c(0,1),
        legend.position=c(0.05,1), 
        legend.text=element_text(size = 14),
        legend.background = element_blank(),
        panel.border = element_rect(colour = "black", fill=NA)) +
  scale_color_manual(values = c(pal[3], pal[3], pal[5], pal[5]), labels=c("0 \U003BCM Imidazole", "50 \U003BCM Imidazole", "0 \U003BCM Imidazole + E150D (0.2%)", "50 \U003BCM Imidazole + E150D (0.2%)")) + 
  scale_shape_manual(labels=c("0 \U003BCM Imidazole", "50 \U003BCM Imidazole", "0 \U003BCM Imidazole + E150D (0.2%)", "50 \U003BCM Imidazole + E150D (0.2%)"), values = c(16,17,16,17)) +
  scale_linetype_manual(labels=c("0 \U003BCM Imidazole", "50 \U003BCM Imidazole", "0 \U003BCM Imidazole + E150D (0.2%)", "50 \U003BCM Imidazole + E150D (0.2%)"), values = c("solid", "dashed", "solid", "dashed")) +
  guides(color=guide_legend(byrow=TRUE), 
         shape=guide_legend(byrow=TRUE),
         linetype=guide_legend(byrow=TRUE))

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


