#Figure 5 plot code

#5A - NPC example images

#5B - Experiment Illustration

#5C - Experiment Metadata

#5D - OCR vs ECAR
library(ggplot2)
library(tidyverse)
library(ggpubr)
library(rstatix)
#load data (compiled from Wave software export into single .csv file)
ocr_data <- read.csv("~/Desktop/Purcell 2023 dataset/Fig. 5/all_ocr_ecar.csv")
ocr_data$Genotype <- factor(ocr_data$Genotype, levels = c("Control", "del3q29"), labels = c("Ctrl", "3q29"))
ocr_data$Medium <- factor(ocr_data$Medium, levels = c("Glucose", "Galactose"), labels = c("GLU", "GAL"))
ocr_data$Experiment <- factor(ocr_data$Experiment, levels = c("1", "2", "3"))
a <- ocr_data %>%
  filter(Time < 2) %>%
  group_by(Medium, Genotype) %>%
  summarise(
    OCR_mean = mean(OCR),
    OCR_sd = sd(OCR),
    n = n(),
    OCR_se = OCR_sd / n,
    ECAR_mean = mean(ECAR),
    ECAR_sd = sd(ECAR),
    ECAR_se = ECAR_sd / n
  )
p <- ggplot(a, aes(x=OCR_mean, y=ECAR_mean)) +
  geom_errorbarh(aes(xmin=OCR_mean-OCR_se,
                     xmax=OCR_mean+OCR_se, color = interaction(Medium, Genotype)),
                 height=0.1, size = 0.3)+
  geom_errorbar(aes(ymin=ECAR_mean-ECAR_se,
                    ymax=ECAR_mean+ECAR_se, color = interaction(Medium, Genotype)),
                width=0.3, size = 0.3) +
  geom_point(aes(color = interaction(Medium, Genotype)), size = 3.5, shape = 21, fill = "white") +    
  theme_classic() + 
  scale_x_continuous(limits = c(0,20)) +
  scale_y_continuous(limits = c(0,4)) +
  scale_color_manual(values = c("#47b88b", "#B847AD", "#4774b8", "#FC6666")) +
  labs(x="OCR (pmol/min/ug)", y="ECAR (mpH/min/ug)", title = "") + 
  annotate(geom="text", x=3, y=0, label="Quiescent",
           color="grey40", size = 5) +
  annotate(geom="text", x=3, y=4, label="Glycolytic",
           color="grey40", size = 5) +
  annotate(geom="text", x=17, y=4, label="Energetic",
           color="grey40", size = 5) +
  annotate(geom="text", x=17, y=0, label="Aerobic",
           color="grey40", size = 5) +
  theme(text = element_text(size = 20)) +
  theme(legend.position = "none") +
  theme(panel.border = element_rect(colour = "black", fill=NA, linewidth = 1)) +
  theme(aspect.ratio = 3/3)
p

#5E - Control OCR
#calculate OCR summary statistics
o <- ocr_data %>%
  group_by(Genotype, Medium, Time) %>%
  summarise(
    OCR_mean = mean(OCR),
    OCR_sd = sd(OCR),
    n = n(),
    OCR_se = OCR_sd / sqrt(n)
  )

ocr_ctrl <- ggplot(subset(o, o$Genotype == "Ctrl"), aes(x=Time, y=OCR_mean)) + 
  geom_line(aes(color = Medium)) +
  geom_errorbar(aes(ymin=OCR_mean-OCR_se, ymax=OCR_mean+OCR_se, color = Medium), width=2, size = 0.4) +
  geom_point(aes(color = Medium), shape=21, fill="white", size=4) +
  scale_color_manual(values = c("#47b88b", "#B847AD")) +
  labs(x="Time (min.)", y="OCR (pmol/min/ug)", title="") +
  theme_classic() +
  theme(legend.position = "none") +
  theme(text = element_text(size = 20)) +
  ylim(0, 15) +
  theme(aspect.ratio = 3/2)
ocr_ctrl

#5F - 3q29Del OCR
ocr_3q29 <- ggplot(subset(o, o$Genotype == "3q29"), aes(x=Time, y=OCR_mean)) + 
  geom_line(aes(color = Medium)) +
  geom_errorbar(aes(ymin=OCR_mean-OCR_se, ymax=OCR_mean+OCR_se, color = Medium), width=2, size = 0.4) +
  geom_point(aes(color = Medium), shape=21, fill="white", size=4) +
  scale_color_manual(values = c("#4774b8", "#FC6666")) +
  labs(x="Time (min.)", y="OCR (pmol/min/ug)", title="") +
  theme_classic() +
  theme(legend.position = "none") +
  theme(text = element_text(size = 20)) +
  ylim(0, 15) +
  theme(aspect.ratio = 3/2)
ocr_3q29

#5G - Baseline OCR mean - Glucose
#Load Seahorse measures data
meas <- read.csv("~/Desktop/Purcell 2023 dataset/Fig. 5/all_measures.csv")
meas$Genotype <- factor(meas$Genotype, levels = c("Control", "del3q29"), labels = c("Ctrl", "3q29"))
meas$Medium <- factor(meas$Medium, levels = c("Glucose", "Galactose"), labels = c("GLU", "GAL"))
meas$Experiment <- factor(meas$Experiment, levels = c("1", "2", "3"))
b <- ocr_data %>%
  filter(Time < 16) %>%
  group_by(Experiment, Medium, Genotype, N) %>%
  summarise(
    baseline_OCR_mean = mean(OCR),
    baseline_OCR_sd = sd(OCR),
    baseline_OCR_se = baseline_OCR_sd / 2
  )

base_bxplt_glu <- ggplot(subset(b, b$Medium == "GLU"), aes(x=Genotype, y=baseline_OCR_mean)) + 
  geom_boxplot(aes(fill=Genotype), outlier.shape = NA, size = 0.3, width = 0.6, position = position_dodge(0.9), alpha=1, weight=0.5) +
  scale_fill_manual(values = c("#47b88b", "#4774b8")) + 
  geom_point(aes(color = Genotype, shape = Experiment), 
             position = position_jitterdodge(jitter.width = 0.01, dodge.width = 0.09), 
             size = 3, alpha=0.8, fill="white") +
  scale_color_manual(values = c("#47b88b", "#4774b8")) +
  scale_shape_manual(values = c(21, 22, 23)) +
  labs(x="", y="Baseline OCR Mean", title="") +
  theme_classic() +
  theme(text = element_text(size = 20)) +
  theme(axis.text.x = element_text(color = c("#47b88b", "#4774b8"))) +
  theme(legend.position = "none") +
  ylim(0, 25) +
  ggpubr::rotate_x_text(angle = 0) +
  theme(aspect.ratio = 4/2)
base_bxplt_glu

#5H - Baseline OCR mean - galactose
base_bxplt_gal <- ggplot(subset(b, b$Medium == "GAL"), aes(x=Genotype, y=baseline_OCR_mean)) + 
  geom_boxplot(aes(fill=Genotype), outlier.shape = NA, size = 0.3, width = 0.6, position = position_dodge(0.9), alpha=1, weight=0.5) +
  scale_fill_manual(values = c("#B847AD", "#FC6666")) + 
  geom_point(aes(color = Genotype, shape = Experiment), 
             position = position_jitterdodge(jitter.width = 0.01, dodge.width = 0.09), 
             size = 3, alpha=0.8, fill="white") +
  scale_color_manual(values = c("#B847AD", "#FC6666")) +
  scale_shape_manual(values = c(21, 22, 23)) +
  labs(x="", y="Baseline OCR Mean", title="") +
  theme_classic() +
  theme(legend.position = "none") +
  theme(text = element_text(size = 20)) +
  theme(axis.text.x = element_text(color = c("#B847AD", "#FC6666"))) +
  ylim(0, 25) +
  ggpubr::rotate_x_text(angle = 0) +
  theme(aspect.ratio = 4/2)
base_bxplt_gal

#5I - Galactose response
gal_response <- ggplot(subset(meas, meas$Medium == "GAL"), aes(x=Genotype, y=Galactose_Response)) + 
  geom_boxplot(aes(fill=Genotype), outlier.shape = NA, size = 0.3, width = 0.6, position = position_dodge(0.9), alpha=1, weight=0.5) +
  scale_fill_manual(values = c("#B847AD", "#FC6666")) + 
  geom_point(aes(color = Genotype, shape = Experiment), 
             position = position_jitterdodge(jitter.width = 0.01, dodge.width = 0.09), 
             size = 3, alpha=0.8, fill="white") +
  scale_color_manual(values = c("#B847AD", "#FC6666")) +
  scale_shape_manual(values = c(21, 22, 23)) +
  labs(x="", y="Galactose Response", title="") +
  theme_classic() +
  theme(text = element_text(size = 20)) +
  theme(legend.position = "none") +
  theme(axis.text.x = element_text(color = c("#B847AD", "#FC6666"))) +
  ylim(0, 5) +
  ggpubr::rotate_x_text(angle = 0) +
  theme(aspect.ratio = 4/2)
gal_response

#5J - Maximal respiration - Glucose
max_glu <- ggplot(subset(meas, meas$Medium == "GLU"), aes(x=Genotype, y=Maximal_Respiration)) + 
  geom_boxplot(aes(fill=Genotype), outlier.shape = NA, size = 0.3, width = 0.6, position = position_dodge(0.9), alpha=1, weight=0.5) +
  scale_fill_manual(values = c("#47b88b", "#4774b8")) + 
  geom_point(aes(color = Genotype, shape = Experiment), 
             position = position_jitterdodge(jitter.width = 0.01, dodge.width = 0.09), 
             size = 3, alpha=0.8, fill="white") +
  scale_color_manual(values = c("#47b88b", "#4774b8")) +
  scale_shape_manual(values = c(21, 22, 23)) +
  labs(x="", y="Maximal Respiration", title="") +
  theme_classic() +
  theme(text = element_text(size = 20)) +
  theme(axis.text.x = element_text(color = c("#47b88b", "#4774b8"))) +
  theme(legend.position = "none") +
  ylim(0, 12) +
  ggpubr::rotate_x_text(angle = 0) +
  theme(aspect.ratio = 4/2)
max_glu

#5K - Maximal respiration - Galactose
max_gal <- ggplot(subset(meas, meas$Medium == "GAL"), aes(x=Genotype, y=Maximal_Respiration)) + 
  geom_boxplot(aes(fill=Genotype), outlier.shape = NA, size = 0.3, width = 0.6, position = position_dodge(0.9), alpha=1, weight=0.5) +
  scale_fill_manual(values = c("#B847AD", "#FC6666")) + 
  geom_point(aes(color = Genotype, shape = Experiment), 
             position = position_jitterdodge(jitter.width = 0.01, dodge.width = 0.09), 
             size = 3, alpha=0.8, fill="white") +
  scale_color_manual(values = c("#B847AD", "#FC6666")) +
  scale_shape_manual(values = c(21, 22, 23)) +
  labs(x="", y="Maximal Respiration", title="") +
  theme_classic() +
  theme(text = element_text(size = 20)) +
  theme(axis.text.x = element_text(color = c("#B847AD", "#FC6666"))) +
  theme(legend.position = "none") +
  ylim(0, 12) +
  ggpubr::rotate_x_text(angle = 0) +
  theme(aspect.ratio = 4/2)
max_gal

#5L - Maximal respiration ratio
max_ratio <- ggplot(meas, aes(x=Medium, y=Maximal_Respiration_Ratio)) + 
  geom_hline(yintercept = 1, color = "grey") +
  geom_boxplot(aes(fill=Medium), outlier.shape = NA, size = 0.3, width = 0.6, position = position_dodge(0.9), alpha=1, weight=0.5) +
  scale_fill_manual(values = c("#4774b8", "#FC6666")) + 
  geom_point(aes(color = Medium, shape = Experiment),
             size = 3, alpha=0.8, fill="white") +
  scale_color_manual(values = c("#4774b8", "#FC6666")) +
  scale_shape_manual(values = c(21, 22, 23)) +
  labs(x="", y="Max Respiration\n 3q29Del/Control", title="") +
  theme_classic() +
  theme(text = element_text(size = 20)) +
  theme(legend.position = "none") +
  ylim(0, 1.5) +
  ggpubr::rotate_x_text(angle = 0) +
  theme(aspect.ratio = 4/2)
max_ratio

#5M - Spare respiratory capacity
spare_bxplt <- ggplot(meas, aes(x=Medium, y=Spare_Respiratory_Capacity_pct, group = interaction(Medium, Genotype))) + 
  geom_boxplot(aes(fill=interaction(Medium, Genotype)), outlier.shape = NA, size = 0.3, width = 0.6, position = position_dodge(0.9), alpha=1, weight=0.5) +
  scale_fill_manual(values = c("#47b88b", "#B847AD", "#4774b8", "#FC6666")) + 
  geom_point(aes(color =interaction(Medium, Genotype), shape = Experiment), 
             position = position_jitterdodge(jitter.width = 0.1, dodge.width = 0.9), 
             size = 3, alpha=0.8, fill="white") +
  scale_color_manual(values = c("#47b88b", "#B847AD", "#4774b8", "#FC6666")) +
  scale_shape_manual(values = c(21, 22, 23)) +
  labs(x="", y="Spare Capacity\n(fold over glucose)", title="") +
  theme_classic() +
  theme(text = element_text(size = 20)) +
  theme(legend.position = "none") +
  ylim(0, 3) +
  theme(aspect.ratio = 3/2)
spare_bxplt






