---
title: "Fadiga, et al, 2019"
output: html_notebook
---
```{r}
##Load libraries and data
library(tidyverse)
library(reshape2)
library(RColorBrewer)
library(magrittr)
load("Fadiga2019.RData")
```

```{r}
##Theme for graphs
th=(theme_bw() +
  theme(axis.text.y = element_text(family = "Arial", face = "bold", size = "8", color="black"),
        axis.text.x = element_text(family = "Arial", face = "bold", size = "8", color="black"),
        axis.title = element_blank(),
        plot.title = element_text(
          family = "Arial", face = "bold", size = "10", color="black", hjust=0.5, vjust = 1),
        legend.key.size = unit(0.18, "in"),
        legend.title = element_text(face = "plain", size = "8")
        ))
```

```{r}
#Timecourse graph 4x1hr heat shock (Figure 1D)
timecourse.m=melt(timecourse, id.vars = "dphs")
timecourse.m$dphs=factor(timecourse.m$dphs, levels=c("d5", "d7", "d9", "d12", "d14", "d20", "d25"), ordered = T)
```
```{r}
p=ggplot(timecourse.m, aes(x=dphs, y=value, fill=variable))
p + geom_bar(colour="black", position = "fill", stat = "identity") + 
  th +
  scale_y_continuous(labels = scales::percent) +
  scale_x_discrete(labels=c("5 dphs", "7 dphs", "9 dphs", "12 dphs", "14 dphs", "20 dphs", "25 dphs")) +
  scale_fill_brewer(palette="YlGnBu", labels=c("4", "3", "2", "1")) +
  guides(fill=guide_legend(title=NULL)) +
  labs(title = "# of unique labels per ovariole, 4x hs")
ggsave("timecourse.tiff", width = 5.3, height = 1.9, units = "in")
```

```{r}
##Comparison of clonal diversity in Reilien et al vs this study (Figure 1E)
ClonalDiversityCompare.m = melt(ClonalDiversityCompare)
ClonalDiversityCompare.m = ClonalDiversityCompare.m %>% group_by(study) %>% mutate(percent = (value/sum(value)))
ClonalDiversityCompare.m$study = factor(ClonalDiversityCompare.m$study, levels = c("Fadiga", "Reilein"))
ClonalDiversityCompare.m$variable = factor(ClonalDiversityCompare.m$variable, levels = c("one", "two", "three", "four", "five", "six"))
```

```{r}
##Comparison of clonal diversity (Figure 1F)
p = ggplot(ClonalDiversityCompare.m)
p + geom_bar(aes(x = variable, y = percent, fill = study), color = "black", position = "dodge", stat = "identity") +
  th +
  scale_y_continuous(labels = scales::percent) +
  scale_x_discrete(labels=c("1", "2", "3", "4", "5", "6")) +
  scale_fill_brewer(
    palette="Set2",
    labels=c("This study", "Reilein, et al.")
    ) +
  theme(legend.position = c(0.68,0.9),  
        legend.background = element_rect(fill="transparent"),
        legend.key.size = unit(0.1, "in")) +
  guides(fill=guide_legend(title=NULL)) +
  labs(title = "# of unique labels, 9 dphs")
ggsave("ClonalDiversity.tiff", width = 2.1, height = 1.9, dpi=300, units = "in")
```

```{r}
#Timecourse graph 1x1hr heat shock (Figure 1G)
timecourse2.m=melt(timecourse2, id.vars = "dphs")
timecourse2.m$dphs=factor(timecourse2.m$dphs, levels=c("d9", "d14"), ordered = T)
timecourse2.m$variable=factor(timecourse2.m$variable, levels=c("Gen3", "Gen2", "Gen1"), ordered = T)
```
```{r}
p=ggplot(timecourse2.m, aes(x=dphs, y=value, fill=variable))
p + geom_bar(colour="black", position = "fill",stat = "identity") + 
  th +
  scale_y_continuous(labels = scales::percent) +
  scale_x_discrete(labels=c("9 dphs", "14 dphs")) +
  scale_fill_manual(values = c("#a1dab4", "#41b6c4", "#225ea8"), labels=c("3", "2", "1")) +
  guides(fill=guide_legend(title=NULL)) +
  labs(title = "  ")
ggsave("timecourse2.tiff", width = 2.5, height = 1.9, units = "in")
```

```{r}
##Heat shock control graph (Figure 2A)
NoHScon2.m=melt(NoHScon2)
NoHScon2_mean=aggregate(NoHScon2.m$value, list(NoHScon2.m$Genotype, NoHScon2.m$variable), mean)
colnames(NoHScon2_mean)=c("variable", "CloneType", "value")
```

```{r}
p=ggplot(NoHScon2_mean)
p + 
  geom_bar(aes(fill=CloneType, y=value, x=variable), color="black", position=position_dodge(width = 0.95), stat="identity") +
  geom_point(data=NoHScon2.m, aes(x=Genotype, y=value, fill=variable), position=position_dodge(width = 0.95), show.legend = F) +
  th +
  theme(legend.position = c(0.8, 0.83), 
        # legend.key.size = unit(0.1, "in"),
        legend.background = element_rect(fill="transparent")) +
  scale_y_continuous(labels = scales::percent) +
  guides(fill=guide_legend(title=NULL,
                 keywidth=0.15,
                 keyheight=0.15,
                 default.unit="inch")) +
  scale_x_discrete(labels=c("GFP negative", "Multi-color")) +
  scale_fill_brewer(palette = "Set2", labels=c("No clones", "FSC clones", "Transient clones")) +
  labs(title = "No heat-shock control")
ggsave("NoHScon2.tiff", width = 3.7, height = 1.9, units = "in")

```

```{r}
##Color Frequencies graph 4x 1hr heat shock (Figure 3A)
ColorFreq.m=melt(ColorFreq, id.vars = "dphs")
ColorFreq.m$variable=factor(ColorFreq.m$variable, levels=c("Y", "G", "T", "B", "P", "W"), ordered = T)
ColorFreq.m$dphs=factor(ColorFreq.m$dphs, levels=c("Expected", "d5", "d7", "d9", "d14"), ordered = T)
```
```{r}
p=ggplot(ColorFreq.m, aes(x=dphs, y=value, fill=variable))
p + geom_bar(colour="black", stat = "identity") +
  th +
  scale_fill_manual(
    values=c("#FFD700", "#00FF00", "#008080", "#00BFFF", "#FF69B4", "lightgrey"), 
    labels=c("RFP & GFP", "GFP only", "GFP & LacZ", "LacZ only", "RFP & LacZ", "RFP, GFP, & LacZ")) +
  scale_y_continuous(labels = scales::percent) +
  scale_x_discrete(labels=c("Predicted", "5 dphs", "7 dphs", "9 dphs", "14 dphs")) +
  guides(fill=guide_legend(title=NULL)) +
  theme(
    axis.title = element_text(family = "Arial", face = "bold", size = "8", color="black")) +
  labs(title = "Frequency of clone type, 4x hs", x=element_blank(), y="Percent") + 
ggsave("ColorFreq.tiff", width = 5.3, height = 1.9, units = "in")
```

```{r}
##Color Frequencies graph 1x1hr heat shock (Figure 3A2)
ColorFreq2.m=melt(ColorFreq2, id.vars = "dphs")
ColorFreq2.m$variable=factor(ColorFreq2.m$variable, levels=c("Y", "G", "T", "B", "P", "W"), ordered = T)
ColorFreq.m$dphs=factor(ColorFreq.m$dphs, levels=c("d9", "d14"), ordered = T)
```
```{r}
p=ggplot(ColorFreq2.m, aes(x=dphs, y=value, fill=variable))
p + geom_bar(colour="black", stat = "identity") +
  th +
  scale_fill_manual(
    values=c("#FFD700", "#00FF00", "#008080", "#00BFFF", "#FF69B4", "lightgrey"), 
    labels=c("RFP & GFP", "GFP only", "GFP & LacZ", "LacZ only", "RFP & LacZ", "RFP, GFP, & LacZ")) +
  scale_y_continuous(labels = scales::percent) +
  scale_x_discrete(labels=c("9 dphs", "14 dphs")) +
  guides(fill=guide_legend(title=NULL)) +
  theme(
    axis.title = element_text(family = "Arial", face = "bold", size = "8", color="black")) +
  labs(title = "Frequency of clone type, 1x hs", x=element_blank(), y="Percent") + 
ggsave("ColorFreq2.tiff", width = 3.5, height = 1.9, units = "in")
```

```{r}
##Types of clones at 7dphs (Figure 3B)
CloneCategories.m=melt(CloneCategories)
CloneCategories_mean=aggregate(CloneCategories.m[,2], list(CloneCategories.m$variable), mean)
colnames(CloneCategories_mean)=c("variable", "value")
CloneCategories_mean$variable=factor(CloneCategories_mean$variable, levels=c("Normal", "Transients", "FSC.Loss", "Small", "Discontinuous"), ordered = T)
p=ggplot(CloneCategories_mean)
p + 
  geom_bar(aes(x=variable, y=value), color="black", fill="light grey", width=0.8, position="dodge", stat="identity") +
  geom_point(data=CloneCategories.m, aes(x=variable, y=value), position=position_dodge(width = 0.9), show.legend = F) +
  th +
  theme(legend.position = c(0.8, 0.9), 
        legend.key.size = unit(0.1, "in")) +
  scale_y_continuous(labels = scales::percent, limits = c(0,0.8)) +
  guides(fill=guide_legend(title=NULL)) +
  labs(title = "Frequency of clone categories") +
  scale_x_discrete(labels=c("Normal", "Transients", "FSC Loss", "Small", "Discontinuous"))
ggsave("CloneCategories.tiff", width = 5.6, height = 2.06, units = "in")
```

```{r}
##Clone Induction Frequencies (Figure 4C)
CloneInduction.m=melt(CloneInduction)
CloneInduction.m=CloneInduction.m %>% group_by(experiment) %>% mutate(percent = (value/sum(value)))
CloneInduction.m$experiment=factor(CloneInduction.m$experiment, levels=c("d730min", "d7_4x", "d14_4x"))
CloneInduction.m$variable=factor(CloneInduction.m$variable, levels=c("full", "mosaic", "none"))
p=ggplot(CloneInduction.m)
p + geom_bar(aes(x=experiment, y=percent, fill=variable), stat = "identity", color="black") +
  th +
  theme(axis.text.y = element_text(size = "6"),
        axis.text.x = element_text(size = "6")) +
  scale_y_continuous(labels = scales::percent) +
  scale_x_discrete(labels=c("7dphs GFP-,\n1x30min HS", "7dphs GFP-,\n4x60min HS", "14dphs GFP-,\n4x60min HS", "7d MARCM \n4x60min HS")) +
  scale_fill_brewer(palette="YlGnBu") +
  guides(fill=guide_legend(title=NULL)) +
  labs(title = "Clone Induction Frequencies")
ggsave("CloneInduction.tiff", width = 3.7, height = 1.9, dpi=300, units = "in")
```

```{r}
##IsoFRT 19A Clone Sizes (Figure 4D)
CloneSize.m=melt(CloneSize)
p=ggplot(CloneSize.m) 
p + geom_boxplot(aes(x = variable, y = value), color = "black", fill="light grey") +
  geom_dotplot(aes(x = variable, y = value), bins = 40, stackdir = "center", binaxis = "y") +
  th +
  theme(axis.text.y = element_text(size = "6"),
        axis.text.x = element_text(size = "6")) +
  scale_y_continuous(labels = scales::percent, limits = c(0,1)) +
  scale_x_discrete(labels = c("7dphs GFP-,\n1x30min HS", "7dphs GFP-,\n4x60min HS", "14dphs GFP-,\n4x60min HS", "7d MARCM \n4x60min HS")) +
  labs(title = "FSC clone sizes in mosaic ovarioles")
ggsave("CloneSize.tiff", width = 3.7, height = 1.9, dpi=300, units = "in")
```

```{r}
##T-tests for clone size comparisons
CloneSize_summary = CloneSize.m %>% group_by(variable) %>% summarize(average=mean(value, na.rm = T), sd=sd(value, na.rm = T))
CloneSize_ttests = data.frame(
  d7x30_vs_d7 = t.test(CloneSize$d7x30,CloneSize$d7)$p.value,
  d7_vs_d14 = t.test(CloneSize$d7,CloneSize$d14)$p.value,
  d730_vs_d14 = t.test(CloneSize$d7x30,CloneSize$d14)$p.value,
  d7x30_vs_MARCM19A = t.test(CloneSize$d7x30,CloneSize$MARCM19A)$p.value,
  d7_vs_MARCM19A = t.test(CloneSize$d7,CloneSize$MARCM19A)$p.value,
  d730_vs_MARCM19A = t.test(CloneSize$d7x30,CloneSize$MARCM19A)$p.value
)
CloneSize_ttests
```

```{r}
##Position of the anterior-most GFP cell (Figure 5D)
ClonePosition$ClonePosition=factor(ClonePosition$ClonePosition, levels = c("Ant.", "Adj.", "FasIII+"), ordered = T)
p=ggplot(ClonePosition, aes(x=ClonePosition, y=value))
p + geom_bar(stat = "identity") + th + scale_y_continuous(labels = scales::percent, limits = c(0,1)) + labs(title = "Position of most \nanterior GFP\u2013 cell")
ggsave("ClonePosition.tiff", width = 1.8, height = 1.9, units = "in")
```



