bindetect_results <- read_tsv("input-data/TFBS_TOBIAS/bindetect_results.txt", col_types = cols())

if(!file.exists("data/TFBS_differential_binding/downloads/41467_2017_BFncomms15089_MOESM3454_ESM.xlsx")){
  dir.create("data/TFBS_differential_binding/downloads",recursive = T)
  # download supplementary table from https://www.nature.com/articles/ncomms15089 
  download.file("https://static-content.springer.com/esm/art%3A10.1038%2Fncomms15089/MediaObjects/41467_2017_BFncomms15089_MOESM3454_ESM.xlsx",
                "data/TFBS_differential_binding/downloads/41467_2017_BFncomms15089_MOESM3454_ESM.xlsx")
}
ttmTF <- read_xlsx("data/TFBS_differential_binding/downloads/41467_2017_BFncomms15089_MOESM3454_ESM.xlsx",sheet = "ttmTF summary",cell_limits(c(3, 1), c(NA, 21)))
tbl <- 
  ttmTF %>% 
  gather(key = "tissue",value = "name") %>%
  drop_na() %>% 
  mutate( name = toupper(name)) %>% 
  # filter(tissue %in% c("Liver","Brain"))
  filter(tissue %in% c("Liver"))

library(ggrepel)
bindetect_results %>%
  mutate( name = toupper(name)) %>% 
  left_join( tbl, by="name" ) %>% 
  ggplot( aes(x=Fresh_Liver_Fresh_Brain_change,y=-log10(Fresh_Liver_Fresh_Brain_pvalue), color=tissue) ) +
  geom_point( data= . %>% filter(is.na(tissue))) + # draw NA points first
  geom_point( data= . %>% filter(!is.na(tissue))) +
  scale_color_manual(values = c(Brain="blue",Liver="red"),na.value="grey",name="tissue type\nmaintenance\nTF in mouse") +
  coord_cartesian(xlim=c(-0.6,0.6),ylim=c(0,250)) +
  geom_text_repel(data = . %>% filter(!is.na(tissue)),
                  aes(label=name), size=3)  +
  xlab("Brain <---> Liver") +
  ylab("-log10(p-value)") + 
  # ggtitle("Differential TF binding in Atlantic salmon brain vs liver") +
  theme_bw()