Last update: 24-10-16

Last markdown compiled: 24-10-16

knitr::opts_chunk$set(warning = FALSE, message = FALSE, results = FALSE) 

# dirs
## Sebastian @Linux
dir_moa <- "/home/isar/Dropbox/scn_moa/"

# data
load(file = paste0(dir_moa, "/data/final/", "gnp.pro.RData"))
load(file = paste0(dir_moa, "/data/final/", "gnp.mrn.RData"))
load(file = paste0(dir_moa, "/data/final/", "gnp.mic.RData"))

# results
load(file = paste0(dir_moa, "/data/final/", "dex.all.RData"))
load(file = paste0(dir_moa, "/data/final/", "timeomics.singles.RData"))
load(file = paste0(dir_moa, "/data/final/", "res.wgcna.RData"))


## packages
source(paste0(dir_moa, "/scripts/", "scn_moa_fct.R"))

# load packagea
sapply(packages_general, require, character.only = TRUE)
sapply(packages_pca, require, character.only = TRUE)
sapply(packages_cluster, require, character.only = TRUE)
sapply(packages_comparisons, require, character.only = TRUE)
#sapply(packages_heatmap, require, character.only = TRUE)

library(ComplexUpset)
library(qdapTools)

DE numbers

Total number of DE features

Threshold for all omics levels: adj.P.Val < 0.05 & abs(logFC) >= 1

prep

# comps of progression
comp_progression_mrn <- c('MBtoPM', 'PMtoMC', 'MCtoMM', 'MMtoB', 'BtoS', 'StoPMN')
comp_progression_mic <- c('MBtoMC','MCtoMM', 'MMtoB', 'BtoS', 'StoPMN')
comp_progression_pro <- c('MBtoPM', 'PMtoMC', 'MCtoMM', 'MMtoB', 'BtoPMN')

mrn

# check
# dex.all$dex.mrn$dex_stage_mrn_vsn$comparison

# prep
dexNr_mrn_fc1 <- dex.all$dex.mrn$dex_stage_mrn_vsn %>%
  filter(comparison %in% comp_progression_mrn) %>%
  group_by(comparison) %>%
  dplyr::summarize(
    n_down = -sum(adj.P.Val < 0.05 & logFC <= -1),
    n_up = sum(adj.P.Val < 0.05 & logFC >= 1))  %>% 
  pivot_longer(-comparison, names_to = "direction", values_to = "n")

dexNr_mrn_fc1 %<>% mutate(comparison = recode(comparison,
                             'MBtoPM' = 'MB -> PM',
                            'PMtoMC'= 'PM -> MC',
                            'MCtoMM' = 'MC -> MM',
                            'MMtoB' = 'MM -> B',
                         "BtoS"= "B -> S",
                         "StoPMN"= "S -> PMN"))

# facet for Fc levels
dexNr_mrn_fc1$comparison <- factor(dexNr_mrn_fc1$comparison, levels = c('MB -> PM', 'PM -> MC', 'MC -> MM', 'MM -> B', "B -> S", "S -> PMN"))
#ncol(gnp.mrn$exp_mrn_cou_vsn)

plot_dex_mrn_gnp_progression <- dexNr_mrn_fc1 %>%
  ggplot(aes(x = comparison, y = n, fill = direction)) +
  geom_bar(stat="identity") +
  geom_text(aes(y = n + 100 * sign(n), label=n), size = 5) +
  # ggtitle("Total features = 8,432") + # subtitle = ''
  xlab("Development stage") +
  ylab("Differentially expressed transcripts") +
  ylim(-1600, 1600) +
  theme_light(base_size = 20) +
  theme(axis.text.x=element_text(angle=30, hjust=1),
        legend.position = "none",
        axis.title.x = element_blank()) +
  scale_fill_manual(values = colors_UpDown_n)

plot_dex_mrn_gnp_progression

ggsave(
  filename = paste0(dir_moa, "/figures/fig1/", "plot_dex_mrn_gnp_progression", ".png"),
  plot = plot_dex_mrn_gnp_progression,
  device = png,
  width = 4,
  height = 6)

ggsave(
  filename = paste0(dir_moa, "/figures/fig1/", "plot_dex_mrn_gnp_progression", ".svg"),
  plot = plot_dex_mrn_gnp_progression,
  device = svg,
  width = 4,
  height = 6)

mic

# dex.all$dex.mrn$dex_stage_mrn_vsn$comparison

# prep
dexNr_mic_fc1 <- dex.all$dex.mic$dex_stage_mic_vsn %>%
  filter(comparison %in% comp_progression_mic) %>%
  group_by(comparison) %>%
  dplyr::summarize(
    n_down = -sum(adj.P.Val < 0.05 & logFC <= -1),
    n_up = sum(adj.P.Val < 0.05 & logFC >= 1))  %>% 
  pivot_longer(-comparison, names_to = "direction", values_to = "n")

dexNr_mic_fc1 %<>% mutate(comparison = recode(comparison,
                             'MBtoMC' = 'MB -> MC',
                            'MCtoMM'= 'MC -> MM',
                            'MMtoB' ='MM -> B',
                            'BtoS' ='B -> S',
                            "StoPMN"= "S -> PMN"))

# facet for Fc levels
ncol(gnp.mic$exp_mic_vsn)

dexNr_mic_fc1$comparison <- factor(dexNr_mic_fc1$comparison,
                                   levels = c("MB -> MC", " - ","MC -> MM", "MM -> B","B -> S", "S -> PMN"))

plot_dex_mic_progression <- dexNr_mic_fc1 %>%
  ggplot(aes(x = comparison, y = n, fill = direction)) +
  geom_bar(stat="identity") +
  geom_text(aes(y = n + 15 * sign(n), label=n), size = 5) +
  #ggtitle('Total features = 283') +
  xlab("Development stage") +
  ylab("Differentially expressed transcripts") +
  ylim(-150, 150) +
  theme_light(base_size = 20) +
  theme(axis.text.x=element_text(angle=30, hjust=1),
        legend.position = "none",
        axis.title.x = element_blank())+
  scale_fill_manual(values = colors_UpDown_n) +
  scale_x_discrete("Stage", drop=FALSE)

plot_dex_mic_progression

ggsave(
  filename = paste0(dir_moa, "/figures/fig1/", "plot_dex_mic_progression", ".png"),
  plot = plot_dex_mic_progression,
  device = png,
  width = 4,
  height = 6)

ggsave(
  filename = paste0(dir_moa, "/figures/fig1/", "plot_dex_mic_progression", ".svg"),
  plot = plot_dex_mic_progression,
  device = svg,
  width = 4,
  height = 6)

pro

# dex.gnp$dex.mrn$dex_stage_mrn_vsn$comparison

# prep
dexNr_pro_fc1 <- dex.all$dex.pro$dex_stage_pro_vsn %>%
  filter(comparison %in% comp_progression_pro) %>%
  group_by(comparison) %>%
  dplyr::summarize(
    n_down = -sum(adj.P.Val < 0.05 & logFC <= -1),
    n_up = sum(adj.P.Val < 0.05 & logFC >= 1))  %>% 
  pivot_longer(-comparison, names_to = "direction", values_to = "n")

dexNr_pro_fc1 %<>% mutate(comparison = recode(comparison,
                            'MBtoPM' = 'MB -> PM',
                            'PMtoMC'= 'PM -> MC',
                            'MCtoMM' ='MC -> MM',
                            'MMtoB' ='MM -> B',
                            "BtoPMN"= "B -> PMN"))

dexNr_pro_fc1$comparison <- factor(dexNr_pro_fc1$comparison,
                                   levels = c("MB -> PM", 'PM -> MC', "MC -> MM", "MM -> B"," - ", "B -> PMN"))


ncol(gnp.pro$exp_pro_vsn)

plot_dex_pro_progression <- dexNr_pro_fc1 %>%
  ggplot(aes(x = comparison, y = n, fill = direction)) +
  geom_bar(stat="identity") +
  geom_text(aes(y = n + 50 * sign(n), label=n), size = 5) +
  #ggtitle('Total features = 3,156') +
  xlab("Development stage") +
  ylab("Differentially expressed proteins") +
  ylim(-450, 450) +
  theme_light(base_size = 20) +
  theme(axis.text.x=element_text(angle=30, hjust=1),
        legend.position = "none",
        axis.title.x = element_blank())+
  scale_fill_manual(values = colors_UpDown_n) +
  scale_x_discrete("stage", drop=FALSE)

plot_dex_pro_progression

ggsave(
  filename = paste0(dir_moa, "/figures/fig1/", "plot_dex_pro_progression", ".png"),
  plot = plot_dex_pro_progression,
  device = png,
  width = 4,
  height = 6)

ggsave(
  filename = paste0(dir_moa, "/figures/fig1/", "plot_dex_pro_progression", ".svg"),
  plot = plot_dex_pro_progression,
  device = svg,
  width = 4,
  height = 6)

Volcanos

fct

add_dex_direction <- function(dexDF) {
  dexDF %<>% mutate(
    direction = case_when(
      adj.P.Val < 0.05 & logFC <= -1 ~ 'down',
      adj.P.Val < 0.05 & logFC >= 1 ~  'up',
      adj.P.Val < 0.05 & logFC >= -1 | logFC <= -1 ~ 'ns',
      adj.P.Val > 0.05 ~ 'ns' ))
  return(dexDF)    
}


add_dex_level <- function(dexDF) {
  dexDF %<>% mutate(
    FC_level = case_when(
      adj.P.Val <= 0.05 & logFC < -1 & logFC > -2 ~ "dn_min",
      adj.P.Val <= 0.05 & logFC < -2 & logFC > -3 ~ "dn_med",
      adj.P.Val <= 0.05 & logFC < -3 ~ "dn_max",
      adj.P.Val <= 0.05 & logFC > 1 & logFC < 2 ~ "up_min",
      adj.P.Val <= 0.05 & logFC > 2 & logFC < 3 ~ "up_med",
      adj.P.Val <= 0.05 & logFC > 3 ~ "up_max",
      adj.P.Val <= 0.05 & logFC > -1 & logFC < 1 ~ "ns",
      adj.P.Val > 0.05 ~ "ns"))
  return(dexDF)    
}

# color of dots
colors_volcano <- c( "#2100be", "#5f007f", "#c321a5", "#999999", '#db5c68', '#f48849',  '#f0f921')
names(colors_volcano) <- c("dn_max", "dn_med", "dn_min",  "ns", 'up_min', 'up_med', 'up_max')


#function for reverse log-scale the y-axis
revlog_trans <- function(base = exp(1)){
  ## Define the desired transformation.
  trans <- function(x){
  -log(x, base)
                }
  ## Define the reverse of the desired transformation
  inv <- function(x){
  base^(-x)
                }
  ## Creates the transformation
    trans_new(paste("revlog-", base, sep = ""),
  trans, ## The transformation function (can be defined using anonymous functions)
  inv,  ## The reverse of the transformation
              log_breaks(base = base), ## default way to define the scale breaks
  domain = c(1e-100, Inf) ## The domain over which the transformation is valued
             )
}


plot_volcano = function(DF, comp, title, FOI=NA) {
  # get data
  df <- DF %>% filter(comparison == comp)
  maxFC_prots <- df %>% filter(FC_level != 'ns') %>% slice_max(n=3, logFC) %>% pull(SYMBOL)
  minFC_prots <- df %>% filter(FC_level != 'ns') %>% slice_min(n=3, logFC) %>% pull(SYMBOL)
  minP_prots <- df %>% filter(FC_level != 'ns') %>% slice_min(n=3, adj.P.Val) %>% pull(SYMBOL)
  fois <- unique(c(maxFC_prots, minFC_prots, minP_prots, FOI))
  df$lab <- ifelse(df$SYMBOL %in% fois, df$SYMBOL, NA)
  
  # plot
  df %>%
    ggplot(aes(x = logFC, y = adj.P.Val , color= FC_level, label = lab)) +
    geom_point(alpha=0.8,size=2) +
    scale_color_manual(values = colors_volcano) +
    geom_text_repel(color = 'black', size = 3, , max.overlaps=Inf) +
    scale_y_continuous(
      name = "adj.P.Val",
      labels = scales::scientific,
      trans = revlog_trans(base = 10)) +
    ggtitle(title) +
    #theme_classic() +
    theme(legend.position = "none", 
          axis.text=element_text(size=12),
          axis.title=element_text(size=12),
          plot.title = element_text(size = 13)) # face = "bold")
  
}

prep

dex.pro <- dex.all$dex.pro$dex_stage_pro_vsn
dex.mrn <- dex.all$dex.mrn$dex_stage_mrn_vsn
dex.mic <- dex.all$dex.mic$dex_stage_mic_vsn

# correction of a mistake made in dex (corrected in script, run again)
# dex.mic %<>% filter(comparison == 'MBtoMC') %>% 
#   distinct(fid, .keep_all = T) %>%
#   bind_rows(dex.mic %>% filter(comparison != 'MBtoMC'))

dex.pro <- add_dex_direction(dex.pro)
dex.mrn <- add_dex_direction(dex.mrn)
dex.mic <- add_dex_direction(dex.mic)

dex.pro <- add_dex_level(dex.pro)
dex.mrn <- add_dex_level(dex.mrn)
dex.mic <- add_dex_level(dex.mic)

dex.mic$SYMBOL <- dex.mic$fid


# Get numbers of significants
dex_nums.pro <- dex.pro %>% 
  group_by(comparison) %>% dplyr::count(direction)

dex_nums.mrn <- dex.mrn %>% 
  group_by(comparison) %>% dplyr::count(direction)

dex_nums.mic <- dex.mic %>% 
  group_by(comparison) %>% dplyr::count(direction)

plots

pro

# dex_nums.pro %>% filter(str_detect(comparison, 'BtoPMN'))

p.vol.MBtoPM_pro <- plot_volcano(dex.pro, 'MBtoPM', 'MB->PM pro: dn 237 / up 386')

p.vol.PMtoMC_pro <- plot_volcano(dex.pro, 'PMtoMC', 'PM->MC pro: dn 60 / up 123')

p.vol.MCtoMM_pro <- plot_volcano(dex.pro, 'MCtoMM', 'MC->MM pro: dn 64 / up 82')

p.vol.MMtoB_pro <- plot_volcano(dex.pro, 'MMtoB', 'MM->B pro: dn 3 / up 6')

p.vol.BtoPMN_pro <- plot_volcano(dex.pro, 'BtoPMN', 'B->PMN pro: dn 2 / up 0')

mrn

# dex_nums.mrn %>% filter(str_detect(comparison, 'StoPMN'))

p.vol.MBtoPM_mrn <- plot_volcano(dex.mrn, 'MBtoPM', 'MB->PM mrn: dn 376 / up 569')

p.vol.PMtoMC_mrn <- plot_volcano(dex.mrn, 'PMtoMC', 'PM->MC mrn: dn 761 / up 740')

p.vol.MCtoMM_mrn <- plot_volcano(dex.mrn, 'MCtoMM', 'MC->MM mrn: dn 1418 / up 1188')

p.vol.MMtoB_mrn <- plot_volcano(dex.mrn, 'MMtoB', 'MM->B mrn: dn 570 / up 257')

p.vol.BtoS_mrn <- plot_volcano(dex.mrn, 'BtoS', 'B->S mrn: dn 1445 / up 1191')

p.vol.StoPMN_mrn <- plot_volcano(dex.mrn, 'StoPMN', 'S->PMN mrn: dn 615 / up 699')

mic

# dex_nums.mic %>% filter(str_detect(comparison, 'StoPMN'))

p.vol.MBtoMC_mic <- plot_volcano(dex.mic, 'MBtoMC', 'MB->MC mic: dn 65 / up 49')

p.vol.MCtoMM_mic <- plot_volcano(dex.mic, 'MCtoMM', 'MC->MM mic: dn 14 / up 22')

p.vol.MMtoB_mic <- plot_volcano(dex.mic, 'MMtoB', 'MM->B mic: dn 0 / up 0')

p.vol.BtoS_mic <- plot_volcano(dex.mic, 'BtoS', 'B->S mic: dn 6 / up 6')

p.vol.StoPMN_mic <- plot_volcano(dex.mic, 'StoPMN', 'S->PMN mic: dn 2 / up 5')

figs

# first 3
fig_gnp_vol_1_pre <- ggarrange(
    p.vol.MBtoPM_mrn + theme_noX,
  p.vol.MBtoPM_pro + theme_nada,
  p.vol.MBtoMC_mic + theme_nada,
  
  p.vol.PMtoMC_mrn + theme_noX,
  p.vol.PMtoMC_pro + theme_nada,
  NULL,
  p.vol.MCtoMM_mrn,
  p.vol.MCtoMM_pro + theme_noY,
  p.vol.MCtoMM_mic + theme_noY,
  common.legend = F, legend= NULL)
# fig_gnp_vol_1_pre

fig_gnp_vol_1 <- annotate_figure(fig_gnp_vol_1_pre,
               top = text_grob("GNP: MB -> PM -> MC -> MM", 
                 color = "black", face = "bold", size = 20))
fig_gnp_vol_1

# next
fig_gnp_vol_2_pre <- ggarrange(
  p.vol.MMtoB_mrn + theme_noX,
  p.vol.MMtoB_pro + theme_nada,
  p.vol.MMtoB_mic + theme_nada,
  p.vol.BtoS_mrn + theme_noX,
  NULL,
  p.vol.BtoS_mic  + theme_nada,
  p.vol.StoPMN_mrn,
  p.vol.BtoPMN_pro  + theme_noY,
  p.vol.StoPMN_mic  + theme_noY,
  common.legend = F, legend= NULL)
fig_gnp_vol_2_pre

fig_gnp_vol_2 <- annotate_figure(fig_gnp_vol_2_pre,
               top = text_grob("GNP: MM -> B -> S -> PMN", 
                 color = "black", face = "bold", size = 20))
fig_gnp_vol_2

Overlap analysis

mrn

# collect data
dex.MBtoPM.mrn.up <- dex.all$dex.mrn$dex_stage_mrn_vsn %>% 
                          filter(comparison == 'MBtoPM') %>%
                          filter( adj.P.Val < 0.05 & logFC >= 1)
dex.MBtoPM.mrn.dn <- dex.all$dex.mrn$dex_stage_mrn_vsn %>% 
                          filter(comparison == 'MBtoPM') %>%
                          filter( adj.P.Val < 0.05 & logFC <= -1)

dex.PMtoMC.mrn.up <- dex.all$dex.mrn$dex_stage_mrn_vsn %>% 
                          filter(comparison == 'PMtoMC') %>%
                          filter( adj.P.Val < 0.05 & logFC >= 1)
dex.PMtoMC.mrn.dn <- dex.all$dex.mrn$dex_stage_mrn_vsn %>% 
                          filter(comparison == 'PMtoMC') %>%
                          filter( adj.P.Val < 0.05 & logFC <= -1)

dex.MCtoMM.mrn.up <- dex.all$dex.mrn$dex_stage_mrn_vsn %>% 
                          filter(comparison == 'MCtoMM') %>%
                          filter( adj.P.Val < 0.05 & logFC >= 1)
dex.MCtoMM.mrn.dn <- dex.all$dex.mrn$dex_stage_mrn_vsn %>% 
                          filter(comparison == 'MCtoMM') %>%
                          filter( adj.P.Val < 0.05 & logFC <= -1)

dex.MMtoB.mrn.up <- dex.all$dex.mrn$dex_stage_mrn_vsn %>% 
                          filter(comparison == 'MMtoB') %>%
                          filter( adj.P.Val < 0.05 & logFC >= 1)
dex.MMtoB.mrn.dn <- dex.all$dex.mrn$dex_stage_mrn_vsn %>% 
                          filter(comparison == 'MMtoB') %>%
                          filter( adj.P.Val < 0.05 & logFC <= -1)

dex.BtoS.mrn.up <- dex.all$dex.mrn$dex_stage_mrn_vsn %>% 
                          filter(comparison == 'BtoS') %>%
                          filter( adj.P.Val < 0.05 & logFC >= 1)
dex.BtoS.mrn.dn <- dex.all$dex.mrn$dex_stage_mrn_vsn %>% 
                          filter(comparison == 'BtoS') %>%
                          filter( adj.P.Val < 0.05 & logFC <= -1)

dex.StoPMN.mrn.up <- dex.all$dex.mrn$dex_stage_mrn_vsn %>% 
                          filter(comparison == 'StoPMN') %>%
                          filter( adj.P.Val < 0.05 & logFC >= 1)
dex.StoPMN.mrn.dn <- dex.all$dex.mrn$dex_stage_mrn_vsn %>% 
                          filter(comparison == 'StoPMN') %>%
                         filter( adj.P.Val < 0.05 & logFC <= -1)

# create TRUE/FALSE table
dex_4usp_mrn.pre <- data.frame(SYMBOL = dex.all$dex.mrn$dex_stage_mrn_vsn %>% pull(SYMBOL) %>% unique())

dex_4usp_mrn.pre$MBtoPM.up <- ifelse(dex_4usp_mrn.pre$SYMBOL %in% dex.MBtoPM.mrn.up$SYMBOL, T, F)
dex_4usp_mrn.pre$MBtoPM.dn <- ifelse(dex_4usp_mrn.pre$SYMBOL %in% dex.MBtoPM.mrn.dn$SYMBOL, T, F)
dex_4usp_mrn.pre$PMtoMC.up <- ifelse(dex_4usp_mrn.pre$SYMBOL %in% dex.PMtoMC.mrn.up$SYMBOL, T, F)
dex_4usp_mrn.pre$PMtoMC.dn <- ifelse(dex_4usp_mrn.pre$SYMBOL %in% dex.PMtoMC.mrn.dn$SYMBOL, T, F)
dex_4usp_mrn.pre$MCtoMM.up <- ifelse(dex_4usp_mrn.pre$SYMBOL %in% dex.MCtoMM.mrn.up$SYMBOL, T, F)
dex_4usp_mrn.pre$MCtoMM.dn <- ifelse(dex_4usp_mrn.pre$SYMBOL %in% dex.MCtoMM.mrn.dn$SYMBOL, T, F)
dex_4usp_mrn.pre$MMtoB.up <- ifelse(dex_4usp_mrn.pre$SYMBOL %in% dex.MMtoB.mrn.up$SYMBOL, T, F)
dex_4usp_mrn.pre$MMtoB.dn <- ifelse(dex_4usp_mrn.pre$SYMBOL %in% dex.MMtoB.mrn.dn$SYMBOL, T, F)
dex_4usp_mrn.pre$BtoS.up <- ifelse(dex_4usp_mrn.pre$SYMBOL %in% dex.BtoS.mrn.up$SYMBOL, T, F)
dex_4usp_mrn.pre$BtoS.dn <- ifelse(dex_4usp_mrn.pre$SYMBOL %in% dex.BtoS.mrn.dn$SYMBOL, T, F)
dex_4usp_mrn.pre$StoPMN.up <- ifelse(dex_4usp_mrn.pre$SYMBOL %in% dex.StoPMN.mrn.up$SYMBOL, T, F)
dex_4usp_mrn.pre$StoPMN.dn <- ifelse(dex_4usp_mrn.pre$SYMBOL %in% dex.StoPMN.mrn.dn$SYMBOL, T, F)

prog_stages_mrn <- colnames(dex_4usp_mrn.pre)[-1]


# 2 overlap
upset(dex_4usp_mrn.pre, prog_stages_mrn, name='Step', width_ratio=0.1, min_size= 50)

# min 3 overlap
upset(dex_4usp_mrn.pre, prog_stages_mrn, name='Step', width_ratio=0.1, 
      min_degree=3,
      min_size= 10)

pro

# collect data
dex.MBtoPM.pro.up <- dex.all$dex.pro$dex_stage_pro_vsn %>% 
                          filter(comparison == 'MBtoPM') %>%
                          filter( adj.P.Val < 0.05 & logFC >= 1)
dex.MBtoPM.pro.dn <- dex.all$dex.pro$dex_stage_pro_vsn %>% 
                          filter(comparison == 'MBtoPM') %>%
                          filter( adj.P.Val < 0.05 & logFC <= -1)

dex.PMtoMC.pro.up <- dex.all$dex.pro$dex_stage_pro_vsn %>% 
                          filter(comparison == 'PMtoMC') %>%
                          filter( adj.P.Val < 0.05 & logFC >= 1)
dex.PMtoMC.pro.dn <- dex.all$dex.pro$dex_stage_pro_vsn %>% 
                          filter(comparison == 'PMtoMC') %>%
                          filter( adj.P.Val < 0.05 & logFC <= -1)

dex.MCtoMM.pro.up <- dex.all$dex.pro$dex_stage_pro_vsn %>% 
                          filter(comparison == 'MCtoMM') %>%
                          filter( adj.P.Val < 0.05 & logFC >= 1)
dex.MCtoMM.pro.dn <- dex.all$dex.pro$dex_stage_pro_vsn %>% 
                          filter(comparison == 'MCtoMM') %>%
                          filter( adj.P.Val < 0.05 & logFC <= -1)

dex.MMtoB.pro.up <- dex.all$dex.pro$dex_stage_pro_vsn %>% 
                          filter(comparison == 'MMtoB') %>%
                          filter( adj.P.Val < 0.05 & logFC >= 1)
dex.MMtoB.pro.dn <- dex.all$dex.pro$dex_stage_pro_vsn %>% 
                          filter(comparison == 'MMtoB') %>%
                          filter( adj.P.Val < 0.05 & logFC <= -1)

dex.BtoPMN.pro.up <- dex.all$dex.pro$dex_stage_pro_vsn %>% 
                          filter(comparison == 'BtoPMN') %>%
                          filter( adj.P.Val < 0.05 & logFC >= 1)
dex.BtoPMN.pro.dn <- dex.all$dex.pro$dex_stage_pro_vsn %>% 
                          filter(comparison == 'BtoPMN') %>%
                         filter( adj.P.Val < 0.05 & logFC <= -1)

# create TRUE/FALSE table
dex_4usp_pro.pre <- data.frame(SYMBOL = dex.all$dex.pro$dex_stage_pro_vsn %>% pull(SYMBOL) %>% unique())

dex_4usp_pro.pre$MBtoPM.up <- ifelse(dex_4usp_pro.pre$SYMBOL %in% dex.MBtoPM.pro.up$SYMBOL, T, F)
dex_4usp_pro.pre$MBtoPM.dn <- ifelse(dex_4usp_pro.pre$SYMBOL %in% dex.MBtoPM.pro.dn$SYMBOL, T, F)
dex_4usp_pro.pre$PMtoMC.up <- ifelse(dex_4usp_pro.pre$SYMBOL %in% dex.PMtoMC.pro.up$SYMBOL, T, F)
dex_4usp_pro.pre$PMtoMC.dn <- ifelse(dex_4usp_pro.pre$SYMBOL %in% dex.PMtoMC.pro.dn$SYMBOL, T, F)
dex_4usp_pro.pre$MCtoMM.up <- ifelse(dex_4usp_pro.pre$SYMBOL %in% dex.MCtoMM.pro.up$SYMBOL, T, F)
dex_4usp_pro.pre$MCtoMM.dn <- ifelse(dex_4usp_pro.pre$SYMBOL %in% dex.MCtoMM.pro.dn$SYMBOL, T, F)
dex_4usp_pro.pre$MMtoB.up <- ifelse(dex_4usp_pro.pre$SYMBOL %in% dex.MMtoB.pro.up$SYMBOL, T, F)
dex_4usp_pro.pre$MMtoB.dn <- ifelse(dex_4usp_pro.pre$SYMBOL %in% dex.MMtoB.pro.dn$SYMBOL, T, F)
dex_4usp_pro.pre$BtoPMN.up <- ifelse(dex_4usp_pro.pre$SYMBOL %in% dex.BtoPMN.pro.up$SYMBOL, T, F)
dex_4usp_pro.pre$BtoPMN.dn <- ifelse(dex_4usp_pro.pre$SYMBOL %in% dex.BtoPMN.pro.dn$SYMBOL, T, F)

prog_stages_pro <- colnames(dex_4usp_pro.pre)[-1]


# min 10
upset(dex_4usp_pro.pre, prog_stages_pro, name='Step', width_ratio=0.1, min_size= 10)

upset(dex_4usp_pro.pre, prog_stages_pro, name='Step', width_ratio=0.1)

# min 3 overlap
upset(dex_4usp_pro.pre, prog_stages_pro, name='Step', width_ratio=0.1, 
      min_degree=3)

mic

# collect data
dex.MBtoMC.mic.up <- dex.all$dex.mic$dex_stage_mic_vsn %>% 
                          filter(comparison == 'MBtoMC') %>%
                          filter( adj.P.Val < 0.05 & logFC >= 1)
dex.MBtoMC.mic.dn <- dex.all$dex.mic$dex_stage_mic_vsn %>% 
                          filter(comparison == 'MBtoMC') %>%
                          filter( adj.P.Val < 0.05 & logFC <= -1)

dex.MCtoMM.mic.up <- dex.all$dex.mic$dex_stage_mic_vsn %>% 
                          filter(comparison == 'MCtoMM') %>%
                          filter( adj.P.Val < 0.05 & logFC >= 1)
dex.MCtoMM.mic.dn <- dex.all$dex.mic$dex_stage_mic_vsn %>% 
                          filter(comparison == 'MCtoMM') %>%
                          filter( adj.P.Val < 0.05 & logFC <= -1)

dex.MMtoB.mic.up <- dex.all$dex.mic$dex_stage_mic_vsn %>% 
                          filter(comparison == 'MMtoB') %>%
                          filter( adj.P.Val < 0.05 & logFC >= 1)
dex.MMtoB.mic.dn <- dex.all$dex.mic$dex_stage_mic_vsn %>% 
                          filter(comparison == 'MMtoB') %>%
                          filter( adj.P.Val < 0.05 & logFC <= -1)

dex.BtoS.mic.up <- dex.all$dex.mic$dex_stage_mic_vsn %>% 
                          filter(comparison == 'BtoS') %>%
                          filter( adj.P.Val < 0.05 & logFC >= 1)
dex.BtoS.mic.dn <- dex.all$dex.mic$dex_stage_mic_vsn %>% 
                          filter(comparison == 'BtoS') %>%
                          filter( adj.P.Val < 0.05 & logFC <= -1)

dex.StoPMN.mic.up <- dex.all$dex.mic$dex_stage_mic_vsn %>% 
                          filter(comparison == 'StoPMN') %>%
                          filter( adj.P.Val < 0.05 & logFC >= 1)
dex.StoPMN.mic.dn <- dex.all$dex.mic$dex_stage_mic_vsn %>% 
                          filter(comparison == 'StoPMN') %>%
                         filter( adj.P.Val < 0.05 & logFC <= -1)

# create TRUE/FALSE table
dex_4usp_mic.pre <- data.frame(fid = dex.all$dex.mic$dex_stage_mic_vsn %>% pull(fid) %>% unique())

dex_4usp_mic.pre$MBtoMC.up <- ifelse(dex_4usp_mic.pre$fid %in% dex.MBtoMC.mic.up$fid, T, F)
dex_4usp_mic.pre$MBtoMC.dn <- ifelse(dex_4usp_mic.pre$fid %in% dex.MBtoMC.mic.dn$fid, T, F)
dex_4usp_mic.pre$MCtoMM.up <- ifelse(dex_4usp_mic.pre$fid %in% dex.MCtoMM.mic.up$fid, T, F)
dex_4usp_mic.pre$MCtoMM.dn <- ifelse(dex_4usp_mic.pre$fid %in% dex.MCtoMM.mic.dn$fid, T, F)
dex_4usp_mic.pre$MMtoB.up <- ifelse(dex_4usp_mic.pre$fid %in% dex.MMtoB.mic.up$fid, T, F)
dex_4usp_mic.pre$MMtoB.dn <- ifelse(dex_4usp_mic.pre$fid %in% dex.MMtoB.mic.dn$fid, T, F)
dex_4usp_mic.pre$BtoS.up <- ifelse(dex_4usp_mic.pre$fid %in% dex.BtoS.mic.up$fid, T, F)
dex_4usp_mic.pre$BtoS.dn <- ifelse(dex_4usp_mic.pre$fid %in% dex.BtoS.mic.dn$fid, T, F)
dex_4usp_mic.pre$StoPMN.up <- ifelse(dex_4usp_mic.pre$fid %in% dex.StoPMN.mic.up$fid, T, F)
dex_4usp_mic.pre$StoPMN.dn <- ifelse(dex_4usp_mic.pre$fid %in% dex.StoPMN.mic.dn$fid, T, F)

prog_stages_mic <- colnames(dex_4usp_mic.pre)[-1]


# 2 overlap
upset(dex_4usp_mic.pre, prog_stages_mic, name='Step', width_ratio=0.1)

# min 3 overlap
upset(dex_4usp_mic.pre, prog_stages_mic, name='Step', width_ratio=0.1, 
      min_degree=3)