Last update: 24-10-16

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

# dirs
## Sebastian @Linux
dir_moa <- "/home/isar/Dropbox/scn_moa/"
## Sebastian @MAC
#dir_moa <- "/Users/home_sh/Library/CloudStorage/Dropbox/scn_moa"


# data, revised 2024.01.09
# exp
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"))
#load(file = paste0(dir_moa, "/data/final/", "scn.pro.RData")) # discarded prom project
# results
load(file = paste0(dir_moa, "/data/final/", "dex.all.RData"))
load(file = paste0(dir_moa, "/data/final/preps/timeomics/", "spca.timeOmics.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"))
#source(paste0(dir_moa, "/scripts/FINAL/plotter/", "feature_plots.R"))

# load packagea
sapply(packages_general, require, character.only = TRUE)
sapply(packages_enrichment, require, character.only = TRUE)

library(timeOmics)
library("viridis")
library(gt)

mrn

prep

# copy time omics data
meds.mrn <- spca.mrn

# timeOmics plot and extract the data
plot_dat0.mrn <- plotLong(meds.mrn)

# change time to stage for traj plots
plot_dat0.mrn %<>% mutate(time = recode(time,
                    '1' = 'MB',
                    '2' = 'PM',
                    '3' = 'MC',
                    '4' = 'MM',
                    '5' = 'B',
                    '6' = 'S',
                    '7' = 'PMN'))

# Calculate mean expression values
plot_dat.mrn <-
  plot_dat0.mrn %>%
  group_by(time, comp, contribution) %>% # could I use the cluster instead? probably yes
  mutate(mean_exp = mean(value))  %>%
  ungroup()

## calculate the centrality
# make wide
plot_dat_wide.mrn <- plot_dat0.mrn %>% 
  #mutate(time = paste0("Time", time)) %>%
  pivot_wider(names_from = time, values_from = value)

# collect the PCs and contributions
combo.mrn <- expand.grid(
  unique(plot_dat.mrn$comp %>% as.character()) %>% sort,
  unique(plot_dat.mrn$contribution %>% as.character())
)

# get centrality
out_centrality.mrn <- vector("list", nrow(combo.mrn))
for(i in 1:nrow(combo.mrn)){
  comp_ind <- combo.mrn[i, 1]
  contr_ind <- combo.mrn[i, 2]
  dat <-
    plot_dat_wide.mrn %>%
    as.data.frame() %>%
    dplyr::filter(comp == comp_ind & contribution == contr_ind) %>%
    `rownames<-`(.$molecule) %>% 
    dplyr::select(MB:PMN) %>%
    as.matrix() 
  dat <- dat %>%
    as.data.frame() %>%
    mutate(centrality  = 1 - pchisq(rowSums(scale(dat)^2), df = 6))

  out_centrality.mrn[[i]] <- dat
}

# collect the plots
outPlots.mrn <- vector("list", nrow(combo.mrn))

for(i in 1:nrow(combo.mrn)){
  comp_ind <- combo.mrn[i, 1]
  contr_ind <- combo.mrn[i, 2]
  centrality <- out_centrality.mrn[[i]]$centrality
  cols <- rep(centrality, 7)
  outPlots.mrn[[i]] <-
    plot_dat.mrn %>%
    mutate(time = factor(time, levels = levels_mrn)) %>%
    dplyr::filter(comp == comp_ind & contribution == contr_ind) %>%
    mutate(cols = cols) %>%
    arrange(cols) %>%
    ggplot() +
    geom_line(aes(x = time, y = value, group = cols, colour = cols)) +
    scale_colour_viridis_c(option = "D") +
    theme_light(base_size = 18)+
    geom_line(aes(x = time, y = mean_exp, group = cols), colour = "red", linewidth = 1.5) +
    theme(axis.text.x=element_text(angle=30, hjust=1))
}

ggarrange(plotlist = outPlots.mrn)

# save plots
plotCollection.mrn <- outPlots.mrn

plots

# mrn down
p.cat.mrn_down <- plotCollection.mrn[[1]]
p.cat.mrn_down <- p.cat.mrn_down + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank())
p.cat.mrn_down

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.mrn_down", ".png"),
  plot = p.cat.mrn_down,
  device = png,
  width = 3,
  height = 3)

# mrn up
p.cat.mrn_up <- plotCollection.mrn[[5]]
p.cat.mrn_up <- p.cat.mrn_up + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank())
p.cat.mrn_up

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.mrn_up", ".png"),
  plot = p.cat.mrn_up,
  device = png,
  width = 3,
  height = 3)


# mrn up down
p.cat.mrn_upDn <- plotCollection.mrn[[6]]
p.cat.mrn_upDn <- p.cat.mrn_upDn + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x=element_blank())
p.cat.mrn_upDn

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.mrn_upDn", ".png"),
  plot = p.cat.mrn_upDn,
  device = png,
  width = 3,
  height = 3)


# mrn up down
p.cat.mrn_dnUp <- plotCollection.mrn[[2]]
p.cat.mrn_dnUp <- p.cat.mrn_dnUp + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank())
p.cat.mrn_dnUp

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.mrn_dnUp", ".png"),
  plot = p.cat.mrn_dnUp,
  device = png,
  width = 3,
  height = 3)

# mrn up down up
p.cat.mrn_upDnUp <- plotCollection.mrn[[7]]
p.cat.mrn_upDnUp <- p.cat.mrn_upDnUp + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x=element_blank())
p.cat.mrn_upDnUp

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.mrn_upDnUp", ".png"),
  plot = p.cat.mrn_upDnUp,
  device = png,
  width = 3,
  height = 3)


# mrn up down
p.cat.mrn_dnUpDn <- plotCollection.mrn[[3]]
p.cat.mrn_dnUpDn <- p.cat.mrn_dnUpDn + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank())
p.cat.mrn_dnUpDn

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.mrn_dnUpDn", ".png"),
  plot = p.cat.mrn_dnUpDn,
  device = png,
  width = 3,
  height = 3)

pro

Last 3 stages combined ## prep

# copy of the original time omics data
meds.pro.cFin <- spca.pro

# plots and extraction of the data
plot_dat0.pro.cFin <- plotLong(meds.pro.cFin)

# change time to stage for plotting
plot_dat0.pro.cFin %<>% mutate(time = recode(time,
                    '1' = 'MB',
                    '2' = 'PM',
                    '3' = 'MC',
                    '4' = 'Mature'))

# Calculate mean expression
plot_dat.pro.cFin <-
  plot_dat0.pro.cFin %>%
  group_by(time, comp, contribution) %>%
  mutate(mean_exp = mean(value))  %>%
  ungroup()

## calculate the centrality
# make wide
plot_dat_wide.pro.cFin <- plot_dat0.pro.cFin %>% 
  #mutate(time = paste0("Time", time)) %>%
  pivot_wider(names_from = time, values_from = value)

# combine
combo.pro.cFin <- expand.grid(
  unique(plot_dat.pro.cFin$comp %>% as.character()) %>% sort,
  unique(plot_dat.pro.cFin$contribution %>% as.character())
)

# get centrality
out_centrality.pro.cFin <- vector("list", nrow(combo.pro.cFin))
for(i in 1:nrow(combo.pro.cFin)){
  comp_ind <- combo.pro.cFin[i, 1]
  contr_ind <- combo.pro.cFin[i, 2]
  dat <-
    plot_dat_wide.pro.cFin %>%
    as.data.frame() %>%
    dplyr::filter(comp == comp_ind & contribution == contr_ind) %>%
    `rownames<-`(.$molecule) %>% 
    dplyr::select(MB:Mature) %>%
    as.matrix()
  dat <- dat %>%
    as.data.frame() %>%
    mutate(centrality  = 1 - pchisq(rowSums(scale(dat)^2), df = 3)) # degrees of freedom: n stages - 1
  out_centrality.pro.cFin[[i]] <- dat
}

# collect the plots
outPlots.pro.cFin <- vector("list", nrow(combo.pro.cFin))

for(i in 1:nrow(combo.pro.cFin)){
  comp_ind <- combo.pro.cFin[i, 1]
  contr_ind <- combo.pro.cFin[i, 2]
  centrality <- out_centrality.pro.cFin[[i]]$centrality
  cols <- rep(centrality, 4) # correct to change this number to the number of cols???
  outPlots.pro.cFin[[i]] <-
    plot_dat.pro.cFin %>%
    mutate(time = factor(time, levels = c('MB', 'PM', 'MC', 'Mature'))) %>%
    dplyr::filter(comp == comp_ind & contribution == contr_ind) %>%
    mutate(cols = cols) %>%
    arrange(cols) %>%
    ggplot() +
    geom_line(aes(x = time, y = value, group = cols, colour = cols)) +
    scale_colour_viridis_c(option = "D") +
    theme_light(base_size = 18)+
    geom_line(aes(x = time, y = mean_exp, group = cols), colour = "red", linewidth = 1.5) +
    theme(axis.text.x=element_text(angle=30, hjust=1))
}

ggarrange(plotlist = outPlots.pro.cFin)

# save plots
plotCollection.pro.cFin <- outPlots.pro.cFin

plots

# up
p.cat.pro_up <- plotCollection.pro.cFin[[1]]
p.cat.pro_up <- p.cat.pro_up + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank())
p.cat.pro_up

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.pro_up", ".png"),
  plot = p.cat.pro_up,
  device = png,
  width = 3,
  height = 3)

# up down
p.cat.pro_upDn <- plotCollection.pro.cFin[[2]]
p.cat.pro_upDn <- p.cat.pro_upDn + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x=element_blank())
p.cat.pro_upDn

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.pro_upDn", ".png"),
  plot = p.cat.pro_upDn,
  device = png,
  width = 3,
  height = 2.8)


# up down up
p.cat.pro_upDnUp <- plotCollection.pro.cFin[[3]]
p.cat.pro_upDnUp <- p.cat.pro_upDnUp + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x=element_blank())
p.cat.pro_upDnUp

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.pro_upDnUp", ".png"),
  plot = p.cat.pro_upDnUp,
  device = png,
  width = 3,
  height = 3)


# down
p.cat.pro_down <- plotCollection.pro.cFin[[4]]
p.cat.pro_down <- p.cat.pro_down + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank())
p.cat.pro_down

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.pro_down", ".png"),
  plot = p.cat.pro_down,
  device = png,
  width = 3,
  height = 3)



# down up
p.cat.pro_dnUp <- plotCollection.pro.cFin[[5]]
p.cat.pro_dnUp <- p.cat.pro_dnUp + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank())
p.cat.pro_dnUp

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.pro_dnUp", ".png"),
  plot = p.cat.pro_dnUp,
  device = png,
  width = 3,
  height = 3)


# down up down
p.cat.pro_dnUpDn <- plotCollection.pro.cFin[[6]]
p.cat.pro_dnUpDn <- p.cat.pro_dnUpDn + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x=element_blank())
p.cat.pro_dnUpDn

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.pro_dnUpDn", ".png"),
  plot = p.cat.pro_dnUpDn,
  device = png,
  width = 3,
  height = 3)

mic

prep

# copy time omics data
meds.mic <- spca.mic

# timeOmics plot and extract the data
plot_dat0.mic <- plotLong(meds.mic)

table(plot_dat0.mic$time)

# change time to stage for traj plots
plot_dat0.mic %<>% mutate(time = recode(time,
                    '1' = 'MB',
                    '2' = 'MC',
                    '3' = 'MM',
                    '4' = 'B',
                    '5' = 'S',
                    '6' = 'PMN'))

# Calculate mean expression values
plot_dat.mic <-
  plot_dat0.mic %>%
  group_by(time, comp, contribution) %>% # could I use the cluster instead? probably yes
  mutate(mean_exp = mean(value))  %>%
  ungroup()

## calculate the centrality
# make wide
plot_dat_wide.mic <- plot_dat0.mic %>% 
  #mutate(time = paste0("Time", time)) %>%
  pivot_wider(names_from = time, values_from = value)

# collect the PCs and contributions
combo.mic <- expand.grid(
  unique(plot_dat.mic$comp %>% as.character()) %>% sort,
  unique(plot_dat.mic$contribution %>% as.character())
)

# get centrality
out_centrality.mic <- vector("list", nrow(combo.mic))
for(i in 1:nrow(combo.mic)){
  comp_ind <- combo.mic[i, 1]
  contr_ind <- combo.mic[i, 2]
  dat <-
    plot_dat_wide.mic %>%
    as.data.frame() %>%
    dplyr::filter(comp == comp_ind & contribution == contr_ind) %>%
    `rownames<-`(.$molecule) %>% 
    dplyr::select(MB:PMN) %>%
    as.matrix() 
  dat <- dat %>%
    as.data.frame() %>%
    mutate(centrality  = 1 - pchisq(rowSums(scale(dat)^2), df = 5))

  out_centrality.mic[[i]] <- dat
}

# collect the plots
outPlots.mic <- vector("list", nrow(combo.mic))

for(i in 1:nrow(combo.mic)){
  comp_ind <- combo.mic[i, 1]
  contr_ind <- combo.mic[i, 2]
  centrality <- out_centrality.mic[[i]]$centrality
  cols <- rep(centrality, 6) # adapt to stage n
  outPlots.mic[[i]] <-
    plot_dat.mic %>%
    mutate(time = factor(time, levels = levels_mic)) %>%
    dplyr::filter(comp == comp_ind & contribution == contr_ind) %>%
    mutate(cols = cols) %>%
    arrange(cols) %>%
    ggplot() +
    geom_line(aes(x = time, y = value, group = cols, colour = cols)) +
    scale_colour_viridis_c(option = "D") +
    theme_light(base_size = 18)+
    geom_line(aes(x = time, y = mean_exp, group = cols), colour = "red", linewidth = 1.5) +
    theme(axis.text.x=element_text(angle=30, hjust=1))
}

ggarrange(plotlist = outPlots.mic)

# save plots
plotCollection.mic <- outPlots.mic

plots

# mic down
p.cat.mic_down <- plotCollection.mic[[1]]
p.cat.mic_down <- p.cat.mic_down + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank())
p.cat.mic_down

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.mic_down", ".png"),
  plot = p.cat.mic_down,
  device = png,
  width = 3,
  height = 3)


# mic down up
p.cat.mic_dnUp <- plotCollection.mic[[2]]
p.cat.mic_dnUp <- p.cat.mic_dnUp + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank())
p.cat.mic_dnUp

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.mic_dnUp", ".png"),
  plot = p.cat.mic_dnUp,
  device = png,
  width = 3,
  height = 3)


# mic up
p.cat.mic_up <- plotCollection.mic[[4]]
p.cat.mic_up <- p.cat.mic_up + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank())
p.cat.mic_up

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.mic_up", ".png"),
  plot = p.cat.mic_up,
  device = png,
  width = 3,
  height = 3)


# mic up down
p.cat.mic_upDn <- plotCollection.mic[[5]]
p.cat.mic_upDn <- p.cat.mic_upDn + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x=element_blank())
p.cat.mic_upDn

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.mic_upDn", ".png"),
  plot = p.cat.mic_upDn,
  device = png,
  width = 3,
  height = 2.8)


# mic up down
p.cat.mic_upDn.late <- plotCollection.mic[[6]]
p.cat.mic_upDn.late <- p.cat.mic_upDn.late + theme(legend.position="none", axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x=element_blank())
p.cat.mic_upDn.late

ggsave(
  filename = paste0(dir_moa, "/figures/fig2/", "p.cat.mic_upDn.late", ".png"),
  plot = p.cat.mic_upDn.late,
  device = png,
  width = 3,
  height = 3)

extract features & numbers

mrn

# work copy
dat.mrn4feat <- plot_dat.mrn

# rename clusters: from -1 via 1 to 4
plotLong(meds.mrn)

table(dat.mrn4feat$cluster)

dat.mrn4feat %<>%
  mutate(traj = recode(cluster,
                    '1' = 'up',
                    '-1' = 'dn',
                    '-2' = 'dnUp',
                    '2' = 'upDn',
                    '-3' = 'dnUpDn',
                    '3' = 'upDnUp',
                    '-4' = 'dnUpDnUp',
                    '4' = 'upDnUpDn'))

# create numers table
numbers.mrn.raw <- dat.mrn4feat %>% distinct(molecule, .keep_all = T) %>%
  pull(traj) %>% table() %>% as.data.frame()
names(numbers.mrn.raw) <- c('traj', 'n.raw')

## now try to limit the features per cluster to the ones with high centrality (= 55) 
centrality.mrn <- bind_rows(out_centrality.mrn, .id = "cluster")
centrality.mrn$ENSG <- rownames(centrality.mrn)
table(centrality.mrn$cluster) # these are 1:8

ggarrange(plotlist = outPlots.mrn)

centrality.mrn %<>%
  mutate(traj = recode(cluster,
                    '1' = 'dn',
                    '2' = 'dnUp',
                    '3' = 'dnUpDn',
                    '4' = 'dnUpDnUp',
                    '5' = 'up',
                    '6' = 'upDn',
                    '7' = 'upDnup',
                    '8' = 'upDnUpDn'))

centrality.mrn.90 <- centrality.mrn %>% select(ENSG, traj, centrality) %>% 
  group_by(traj) %>% 
  arrange(traj, desc(centrality)) %>% 
  filter(centrality > quantile(centrality, .10))

numbers.mrn.90 <- table(centrality.mrn.90$traj) %>% as.data.frame()
names(numbers.mrn.90) <- c('traj', 'n.90')

numbers.mrn <- left_join(numbers.mrn.raw, numbers.mrn.90, by = c('traj'))

add.percentage <- function(df, num.col, baseNum, newColName){
  df[[newColName]] <- round(df[[num.col]] / baseNum * 100, 0)
  df[[newColName]] <- paste0("(", df[[newColName]], "%)")
  return(df)
}

tot.mrn <- ncol(gnp.mrn$exp_mrn_tpm_vsn)
numbers.mrn <- add.percentage(numbers.mrn, 'n.raw', tot.mrn, 'prc.raw') # its debatable of we should use the raw number or the input?
numbers.mrn <- add.percentage(numbers.mrn, 'n.90', tot.mrn, 'prc.90')

numbers.mrn %>% select(traj, n.raw, prc.raw, n.90, prc.90) %>% gt()

tab.mrn <- numbers.mrn %>% select(traj, n= n.90, prc = prc.90) %>% gt() 

tab.mrn %>% gtsave(filename = paste0(dir_moa, "/figures/fig2/", "tab.mrn", '.png'))

361+313

pro

# work copy
dat.pro4feat <- plot_dat.pro.cFin

# rename clusters: from -1 via 1 to 4
plotLong(meds.pro.cFin)

table(dat.pro4feat$cluster)

dat.pro4feat %<>%
  mutate(traj = recode(cluster,
                    '1' = 'up',
                    '-1' = 'dn',
                    '-2' = 'dnUp',
                    '2' = 'upDn',
                    '-3' = 'dnUpDn',
                    '3' = 'upDnUp'))

# create numers table
numbers.pro.raw <- dat.pro4feat %>% distinct(molecule, .keep_all = T) %>%
  pull(traj) %>% table() %>% as.data.frame()
names(numbers.pro.raw) <- c('traj', 'n.raw')

## now try to limit the features per cluster to the ones with high centrality (= 55) 
centrality.pro <- bind_rows(out_centrality.pro.cFin, .id = "cluster")
centrality.pro$ENSG <- rownames(centrality.pro)
table(centrality.pro$cluster) # these are 1:8

ggarrange(plotlist = outPlots.pro.cFin)

centrality.pro %<>%  
  mutate(traj = recode(cluster,
                    '1' = 'up',
                    '2' = 'upDn',
                    '3' = 'upDnUp',
                    '4' = 'dn',
                    '5' = 'dnUp',
                    '6' = 'dnUpDn'))

centrality.pro.90 <- centrality.pro %>% select(ENSG, traj, centrality) %>% 
  group_by(traj) %>% 
  arrange(traj, desc(centrality)) %>% 
  filter(centrality > quantile(centrality, .10))

numbers.pro.90 <- table(centrality.pro.90$traj) %>% as.data.frame()
names(numbers.pro.90) <- c('traj', 'n.90')

numbers.pro <- left_join(numbers.pro.raw, numbers.pro.90, by = c('traj'))

add.percentage <- function(df, num.col, baseNum, newColName){
  df[[newColName]] <- round(df[[num.col]] / baseNum * 100, 0)
  df[[newColName]] <- paste0("(", df[[newColName]], "%)")
  return(df)
}

tot.pro <- ncol(gnp.pro$exp_pro_vsn)
numbers.pro <- add.percentage(numbers.pro, 'n.raw', tot.pro, 'prc.raw') # its debatable of we should use the raw number or the input?
numbers.pro <- add.percentage(numbers.pro, 'n.90', tot.pro, 'prc.90')

numbers.pro %>% select(traj, n.raw, prc.raw, n.90, prc.90) %>% gt()

tab.pro <- numbers.pro %>% select(traj, n= n.90, prc = prc.90) %>% gt() 

tab.pro %>% gtsave(filename = paste0(dir_moa, "/figures/fig2/", "tab.pro", '.png'))

329+261

mic

# work copy
dat.mic4feat <- plot_dat.mic

# rename clusters: from -1 via 1 to 4
plotLong(meds.mic)

table(dat.mic4feat$cluster)

dat.mic4feat %<>%
  mutate(traj = recode(cluster,
                    '1' = 'up',
                    '-1' = 'dn',
                    '-2' = 'dnUp',
                    '2' = 'upDn',
                    '-3' = 'NA',
                    '3' = 'upDn late'))

# create numers table
numbers.mic.raw <- dat.mic4feat %>% distinct(molecule, .keep_all = T) %>%
  pull(traj) %>% table() %>% as.data.frame()
names(numbers.mic.raw) <- c('traj', 'n.raw')

## now try to limit the features per cluster to the ones with high centrality (= 55) 
centrality.mic <- bind_rows(out_centrality.mic, .id = "cluster")
centrality.mic$ENSG <- rownames(centrality.mic)
table(centrality.mic$cluster) # these are 1:8

ggarrange(plotlist = outPlots.mic)

centrality.mic %<>%
  mutate(traj = recode(cluster,
                    '1' = 'dn',
                    '2' = 'dnUp',
                    '3' = 'NA',
                    '4' = 'up',
                    '5' = 'upDn',
                    '6' = 'upDn late'))

centrality.mic.90 <- centrality.mic %>% select(ENSG, traj, centrality) %>% 
  group_by(traj) %>% 
  arrange(traj, desc(centrality)) %>% 
  filter(centrality > quantile(centrality, .10))

numbers.mic.90 <- table(centrality.mic.90$traj) %>% as.data.frame()
names(numbers.mic.90) <- c('traj', 'n.90')

numbers.mic <- left_join(numbers.mic.raw, numbers.mic.90, by = c('traj'))

add.percentage <- function(df, num.col, baseNum, newColName){
  df[[newColName]] <- round(df[[num.col]] / baseNum * 100, 0)
  df[[newColName]] <- paste0("(", df[[newColName]], "%)")
  return(df)
}

tot.mic <- ncol(gnp.mic$exp_mic_vsn)
numbers.mic <- add.percentage(numbers.mic, 'n.raw', tot.mic, 'prc.raw') # its debatable of we should use the raw number or the input?
numbers.mic <- add.percentage(numbers.mic, 'n.90', tot.mic, 'prc.90')

numbers.mic %>% select(traj, n.raw, prc.raw, n.90, prc.90) %>% gt()

tab.mic <- numbers.mic %>% select(traj, n= n.90, prc = prc.90) %>% gt() 

tab.mic %>% gtsave(filename = paste0(dir_moa, "/figures/fig2/", "tab.mic", '.png'))

21+12

export

# export
save(
  centrality.pro,
  centrality.mic,
  centrality.mrn,
  file = paste0(dir_moa, "/data/final/preps/timeomics/", "centralities.RData"))