#Plot the fluorescence spectra for triplicate wells using water-blank corrected fluorescence values

#Df input required: First column is list of wells, subsequent columns are fluorescence intensities at specific wavelength (which are the column headings)
#Sample Name input requires: Columns are Well_ID,	SampleName, SampleType, SampleType, TechRep


################################################################

#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(tidyverse)
library(ggplot2)
library(RColorBrewer)


#OPEN FILES
df <- read_excel('Exp 20250702 - Fluorescence of Caramel Pt2.xlsx','Plate 1 Ex492 Transposed')

name_df <- read_excel('Exp 20250702 - Fluorescence of Caramel 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", "SampleType", "TechRep"))
names(df2)[names(df2) == "variable"] <- "Wavelength"

# Convert fluorescence values to numeric, force NaN on the 'OVER' readings
df2$value <- as.numeric(as.character(df2$value))

# FOR NON-WATER BLANK CORRECTED DATA
df4 <- df2 %>%
  group_by(SampleName, SampleType, Wavelength) %>%
  summarize_at(vars(value), list(MeanAbs = mean, SdAbs = sd))

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

#SEPARATE OUT WATER CONTROL AND CARAMEL SAMPLES
df5.e150d <- df4 %>%
  filter(SampleName %in% c("E150D", "Neg Ctrl") & SampleType != "AMTA SAR")

df5.e150a <- df4 %>%
  filter(SampleName %in% c("E150A", "Neg Ctrl") & SampleType != "AMTA SAR")

df5 <- df4 %>%
  filter(SampleName != "Water Control" & SampleType != "AMTA SAR") %>%
  unite(Condition, c("SampleName", "SampleType"))

#PLOT DATA - E150D
pal <- brewer.pal(8, "Dark2")
ggplot(df5, aes(x=Wavelength, y=MeanAbs, group=Condition)) + 
  geom_line(aes(color=Condition, linetype = Condition)) + 
  #geom_ribbon(aes(ymin=MeanAbs-SdAbs, ymax=MeanAbs+SdAbs, fill=as.factor(SampleType)), alpha=0.3, show.legend=FALSE) + 
  labs(x = "Wavelength (nm)",
       y = expression(paste("Fluorescence Intensity (×10"^3," i.u.)"))) +
  scale_x_continuous(breaks = seq(550, 800, by = 50), limits = c(550, 800)) +
  scale_y_continuous(breaks = seq(0, 20000, by = 4000), limits = c(-0.02,20000), labels=function(x) x / 1000) +
  theme_classic() +
  theme(aspect.ratio=1,
        title =element_text(size=14),
        axis.title=element_text(size=14), 
        axis.text=element_text(colour = 1, size = 14), 
        legend.title=element_blank(), 
        legend.justification=c(0,1),
        legend.position.inside=c(1.05,1), 
        legend.text=element_text(size = 14), 
        panel.border = element_rect(colour = "black", fill=NA)) +
  scale_color_manual(values = c(pal[1], pal[1], pal[2], pal[2], pal[3]), labels = c("E150A AMTA Deriv.", "E150A Control", "E150D AMTA Deriv.", "E150D Control", "Control AMTA Deriv.")) +
  scale_linetype_manual(values = c("dashed", "solid", "dashed", "solid", "solid"), labels = c("E150A AMTA Deriv.", "E150A Control", "E150D AMTA Deriv.", "E150D Control", "Control AMTA Deriv.")) +
  guides(color=guide_legend(byrow=TRUE), linetype=guide_legend(byrow=TRUE))

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