## load libraries ----
library(tidyverse)
library(janitor)
library(ggpubr)

## load data ----
file <- "perturbationScreen_bootstrapped_mean_Figure6.csv"
data <- read_csv(file)

## calculate untreated mean over replicates ---- 
data %>% 
  filter(type == "untreated") %>% 
  remove_empty() %>% 
  group_by(antibody, replicate) %>% 
  summarise(mean = mean(value, na.rm = T), 
            sd = sd(value, na.rm = T), 
            cv = sd/mean) -> untreated

## calculate fold-change compared to untreated ----
data %>% 
  filter(description_1 == "Stim", 
         type == "sample") %>% 
  remove_empty("cols") %>% 
  select(-description_1, -type) %>% 
  left_join(untreated) %>% 
  mutate(fc = value/mean,
         log2fc = log2(fc)) %>% 
  group_by(antibody, EGFconc, IGFconc, time) %>% 
  summarize(meanLog2FC = mean(log2fc, na.rm = T)) %>% 
  arrange(EGFconc, IGFconc, time) %>% 
  mutate(id = seq_along(EGFconc)) %>% 
  select(IGFconc, time, EGFconc, antibody, meanLog2FC, id) -> igf.data

## plot heatmap ----
igf.data %>% 
  mutate(rescue = ifelse(EGFconc == 1 & IGFconc == 0, "low", NA),
         rescue = ifelse(EGFconc == 7.5 & IGFconc == 0, "high", rescue),
         rescue = ifelse(EGFconc == 1 & IGFconc == 100, "rescued", rescue)) %>% 
  filter(!is.na(rescue)) %>% 
  ungroup() %>% 
  ggplot(aes(x = as.factor(time), y = antibody, fill = meanLog2FC)) + geom_tile() + 
  facet_wrap(~ rescue, nrow = 1) +
  scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, limits = c(-3,3), 
                       oob = scales::squish) + 
  theme_pubr() + 
  labs(x = "Time (hours)", y = "") + 
  theme(legend.position = "right") 

## plot time-course for AKT, mTOR and S6 ----
igf.data %>% 
  mutate(rescue = ifelse(EGFconc == 1 & IGFconc == 0, "low", NA),
         rescue = ifelse(EGFconc == 7.5 & IGFconc == 0, "high", rescue),
         rescue = ifelse(EGFconc == 1 & IGFconc == 100, "rescued", rescue)) %>% 
  filter(!is.na(rescue)) %>% 
  ungroup() %>% 
  filter(antibody %in% c("AKT", "mTOR", "RPS6")) %>% 
  ggplot(aes(x = time, y = meanLog2FC, color = rescue)) + geom_line(size = 1.5) + 
  facet_wrap(~antibody) + 
  scale_color_discrete(name = "", labels = c("high EGF", "low EGF", "low EGF + IGF")) +
  theme_pubr() + 
  labs(x = "Time (hours)", y = "log2 FC") 


