library(ggplot2)
library(dplyr)
library(readODS)
library(tidyr)
library(tidyverse)
library(svglite)
library(extrafont)

source("~/Labo/PD_Malhotra_CRG/R_Common.R")

#######################
#####Dot Blot Data#####
#######################
FullData <- read_ods("Quantifications.ods", sheet = 1)
FullData <- as_tibble(FullData)
FullData <- FullData %>%
    mutate(ExpDate = case_when(ExpDate == "10/22/21" ~ "First",
                               ExpDate == "11/04/21" ~ "Second",
                               ExpDate == "11/11/21" ~ "Third",
                               TRUE ~ "ERROR"),
           Replicate = as.factor(Replicate),
           Replicate = as.factor(Replicate),
           Genotype  = factor(Genotype,
                              levels = c("WT_MUC5AC.GFP", "Stx2_KO_clone1", "Stx2_KO_clone11")),
           Secretion = factor(Secretion, levels = c("Basal", "ATP")))


GenotypeLabels = c("WT_MUC5AC.GFP" = "WT",
                   "Stx2_KO_clone1" = "STX2 KO",
                   "Stx2_KO_clone11" = "STX2 KO\nclone 11")

GenotypeLabels_b = c("WT_MUC5AC.GFP" = "WT",
                   "Stx2_KO_clone1" = "STX2 KO",
                   "Stx2_KO_clone11" = "STX2 KO (clone 11)")

######################
####MUC5AC Secreted ##
DB_data <- FullData %>% filter(Fraction == "Secreted" & ExpDate != "First", Genotype != "Stx2_KO_clone11") %>%
    select(!antiTubulin)

##Mean Mucin intensities of basal secreted
SecretedATP_MucinMeans <- DB_data %>% filter(Genotype == 'WT_MUC5AC.GFP', Secretion == "ATP") %>%
    group_by(ExpDate) %>% summarise(meanATP_Mucin = mean(antiGFP)) %>% ungroup()

##Relative MUC5AC calculation
DB_data <- DB_data %>%
    mutate(ATP_MeanMucin = ifelse(ExpDate == as.character(SecretedATP_MucinMeans[1,'ExpDate']),
                                  pull(SecretedATP_MucinMeans[1,'meanATP_Mucin']),
                           ifelse(ExpDate == as.character(SecretedATP_MucinMeans[2,'ExpDate']),
                                  pull(SecretedATP_MucinMeans[2,'meanATP_Mucin']),
                                  pull(SecretedATP_MucinMeans[3,'meanATP_Mucin'])))) %>%
    mutate(rMUC5AC = antiGFP/ATP_MeanMucin)

DB_data %>%
    ggplot(aes(x = Genotype, y = rMUC5AC, color = ExpDate)) +
    geom_jitter(width = 0.1, alpha = 0.8) +
    stat_summary(fun.data = data_summary, color = "red", alpha = 1) +
    facet_wrap(~Secretion) +
    ylab("Mucin-5AC GFP") +
    scale_x_discrete(labels = GenotypeLabels) +
    scale_y_continuous(n.breaks = 8) +
    scale_color_manual(values = BiologialReplicatesColors) +
    theme_bw() +
    CustomTheme

ggsave(filename = "SecretionAssay_Stx2KOs_smGFP.svg", width = 50, height = 50, units = "mm")


##ANOVA test
Secreted.aov <- aov(rMUC5AC ~ (Genotype*Secretion)/ExpDate, DB_data)
anova(Secreted.aov)

##ATP
SecretedATP <- DB_data  %>% filter(Secretion == "ATP")
SecretedATP.aov <- aov(rMUC5AC ~ Genotype/ExpDate, SecretedATP)
anova(SecretedATP.aov)

##Basal
SecretedBasal <- DB_data %>% filter(Secretion == "Basal")
SecretedBasal.aov <- aov(rMUC5AC ~ Genotype/ExpDate, SecretedBasal)
anova(SecretedBasal.aov)


DB_data %>% group_by(Secretion, Genotype) %>%
    summarise(means = mean(rMUC5AC))

####MUC5AC LYSATES##
##Mean Mucin intensities of basal lysates
BasalMucinMeans <- FullData %>% filter(Genotype == 'WT_MUC5AC.GFP', Secretion == "Basal", Fraction == "Lysate") %>%
    group_by(ExpDate) %>% summarise(meanBasalMucin = mean(antiGFP)) %>% ungroup()

##Relative MUC5AC calculation
DB_LysateData <-     FullData %>%
    mutate(BasalMeanMucin = ifelse(ExpDate == as.character(BasalMucinMeans[1,'ExpDate']), pull(BasalMucinMeans[1,'meanBasalMucin']),
                            ifelse(ExpDate == as.character(BasalMucinMeans[2,'ExpDate']), pull(BasalMucinMeans[2,'meanBasalMucin']),
                                   pull(BasalMucinMeans[3,'meanBasalMucin'])))) %>%
    mutate(rMUC5AC = antiGFP/BasalMeanMucin)

###################
##Lysate Mucin##Basal Conditions Only##
DB_LysateData %>% filter(Fraction == "Lysate" & Secretion == "Basal") %>%
    ggplot(aes(x = Genotype, y = rMUC5AC, color = ExpDate)) +
    geom_jitter(width = 0.1, alpha = 0.6) +
    stat_summary(fun.data = data_summary, color = "red", alpha = 0.8) +
    ylab("MUC5AC GFP (relative to WT)") +
    #scale_x_discrete(labels = GenotypeLabels) +
    #facet_wrap(~Fraction) +
    theme_bw() +
    ylim(0, 2.3)+
    #scale_color_manual(values = BiologialReplicatesColors) +
    labs(color = "Biological Replicates",
         caption = "mean +/- sd") +
    CustomTheme

ggsave(filename = "Lysates_SecretionAssay_rMUC5AC.pdf", width = 5, height = 4)

##ANOVA test
LysateBasal <- DB_LysateData %>% filter(Fraction == "Lysate", Secretion == "Basal")
LysateBasal.aov <- aov(rMUC5AC ~ Genotype/ExpDate, LysateBasal)
anova(LysateBasal.aov)
##Tukey
TukeyHSD(LysateBasal.aov)


###################
##### WB Data #####

##Mean Tubulin intensities of basal lysates
WB_data <- FullData %>% filter(Fraction == "Lysate" & ExpDate != "First",
                              Genotype != "Stx2_KO_clone11") %>%
    select(!antiGFP)


BasalMeansWB <- WB_data %>%
    filter(Genotype == 'WT_MUC5AC.GFP', Secretion == "Basal", Fraction == "Lysate") %>%
    group_by(ExpDate) %>% summarise(meanBasalTubulin = mean(antiTubulin))
                                    
##TUBULIN##
##Calculation of relative to Basal WT Tubulin##
WB_data <-  WB_data %>%
    mutate(BasalMeanTubulin = ifelse(ExpDate == as.character(BasalMeansWB[1,'ExpDate']),
                                     pull(BasalMeansWB[1,'meanBasalTubulin']),
                              ifelse(ExpDate == as.character(BasalMeansWB[2,'ExpDate']),
                                     pull(BasalMeansWB[2,'meanBasalTubulin']),
                                     pull(BasalMeansWB[3,'meanBasalTubulin'])))) %>%
    mutate(rTubulin = antiTubulin/BasalMeanTubulin)


##Loading Controls - anti beta Tubulin##
WB_data %>% 
    ggplot(aes(x = Genotype, y = rTubulin, color = Secretion)) +
    geom_jitter(width = 0.1, alpha = 0.6) +
    stat_summary(fun.data = data_summary, color = "red", alpha = 0.8) +
    scale_x_discrete(labels = GenotypeLabels_b) +
    ylab("Beta tubulin") +
    theme_bw() +
    ylim(0, 2) +
    scale_color_manual(values = SecretionColors) +
    CustomTheme

ggsave(filename = "Lysate_Stx2KO_SecretionAssay_Tubulin.svg", width = 40, height = 55, units = "mm")


##ANOVA test
LysateTubulin.aov <- aov(rTubulin ~ (Genotype*Secretion)/ExpDate, WB_data)
anova(LysateTubulin.aov)
