---
title: "Classify Mutants from SGD"
output: html_notebook
---

## Load mutant annotations (manually curated from SGD)

```{r}
library(ggpubr)

mutant_info.df <- readxl::read_excel("/Users/kmoyung/MacAlpine_KM/Metadata/Mutant_Classification_SGD.xlsx")
table(mutant_info.df$TF_Class)
table(mutant_info.df$Type)
# Use functional categories from Kemmeren et al.
table(mutant_info.df$Functional_Category)
mutant_info.df$NewType <- mutant_info.df$Functional_Category

# Group types that aren't shown into 'other'
main_groups <- c("TF", "Chromatin Remodeler", "Chromatin Factor", "Nucleosome Assembly", "Pol II Activity", "Kinase")
mutant_info.df$NewType <- ifelse(!(mutant_info.df$Type %in% main_groups), "Other", mutant_info.df$Type)
table(mutant_info.df$NewType)
```
## Calculate positions for piechart

```{r}
# Count total number of each type of chromatin change
count_mut.df <- mutant_info.df %>%
  group_by(NewType) %>%
  tally() %>%
  mutate(Pct = (n / sum(n)) * 100,
         ymax = Pct,
         ymin = c(0, head(ymax, n = -1)),
         labelPosition = (ymax + ymin) / 2,
         csum = rev(cumsum(rev(Pct))),
         pos = Pct / 2 + lead(csum, 1),
         pos = if_else(is.na(pos), Pct / 2, pos))
```

## Plot donut chart

Plot donut chart of mutant types using native function

```{r}
n_colors <- length(unique(count_mut.df$NewType))
# pal <- RColorBrewer::brewer.pal(n = n_colors, "Set3")
# pal <- RColorBrewer::brewer.pal(n = n_colors, "YlGnBu")
pal <- RColorBrewer::brewer.pal(n = n_colors, "Purples")

sorted_count_mut.df <- count_mut.df %>%
  arrange(desc(n))

png(file = "/Users/kmoyung/supp_figures/mutant_piechart.png", width = 9.5, height = 5.5, units = 'in', res = 800)
pie(sorted_count_mut.df$n, labels = paste0(sorted_count_mut.df$NewType, " (", sorted_count_mut.df$n, ")"), col = rev(pal), clockwise = F, init.angle = 40, border = "black", cex = 1.5)
par(new = TRUE)
# Add a smaller white pie chart on top to create a donut effect
pie(rep(1, length(values)), radius = 0.45, col = "white", labels = "", axes = F, lty = "blank", border = "black")
# Draw a black border for the white circle
plotrix::draw.circle(x = 0, y = 0, radius = 0.45, border = "black")
text(0, 0, "201 Yeast\nDeletion\nMutants", cex = 1.3)
dev.off()
```

TODO: Plot piechart of mutant TFs classified by either activator or repressor (or both/unknown)

```{r}
n_colors <- length(unique(count_mut.df$NewType))
pal <- RColorBrewer::brewer.pal(n = n_colors, "Dark2")
```

Bar chart alternative

```{r}
ggplot(mutant_info.df, aes(x = forcats::fct_infreq(NewType), fill = NewType)) + 
  geom_bar() + 
  geom_text(stat = 'count', aes(label = ..count..), vjust = 1.5)
```

## Classify mutants using Slim GO database

```{r}
library(clusterProfiler)

# Load slim GO terms
slim_go <- read.csv("/Users/kmoyung/MacAlpine_KM/Metadata/GO_Slim/go_slim_mapping.tab", header = F, sep = "\t")
colnames(slim_go) <- c("ORF", "Gene", "SGDID", "GO_Aspect", "Term", "GOID", "Feature")

# Subset for genes that are in our mutant dataset
mutant_slim_go_function.df <- subset(slim_go, Gene %in% mutants$V1 & GO_Aspect %in% c("F"))
mutant_slim_go_process.df <- subset(slim_go, Gene %in% mutants$V1 & GO_Aspect %in% c("P"))
```

