R/visualization.R

Defines functions vis_formula_gsea_chord vis_formula_RRS_chord vis_bar_double_enrich vis_bell_term vis_tern_all vis_tern_one vis_pw_stat_bar vis_pw_wt_radar vis_pw_cor_ph

Documented in vis_bar_double_enrich vis_bell_term vis_formula_gsea_chord vis_formula_RRS_chord vis_pw_cor_ph vis_pw_stat_bar vis_pw_wt_radar vis_tern_all vis_tern_one

#' pheatmap of selected GOBP pathways' correlation matrix
#'
#' @param pwIDs selected GOBP pathways' ID
#'
#' @return pheatmap(ggplot object)
#' @export
#'
#' @examples
#' data("find_disease_pw_result")
#' pwIDs <- unique(find_disease_pw_result$term_df$Term)
#' vis_pw_cor_ph(pwIDs)
vis_pw_cor_ph <- function(pwIDs) {
  goSims_top10 <- GOSemSim::mgoSim(pwIDs, pwIDs,
    semData = hsGO_BP, measure = "Jiang", combine = NULL
  )
  p <- pheatmap::pheatmap(goSims_top10,
    treeheight_row = 0,
    treeheight_col = 10
  ) %>%
    ggplotify::as.ggplot()
  return(p)
}



#' ggradar plot: importance weight of disease related pathways
#'
#' @param find_disease_pw_result  result of find_disease_pw()
#'
#' @return ggradar plot
#' @export
#'
#' @examples
#' data("find_disease_pw_result")
#' vis_pw_wt_radar(find_disease_pw_result)
vis_pw_wt_radar <- function(find_disease_pw_result) {
  wt_pw <- find_disease_pw_result$term_df %>%
    dplyr::select(Term, weight) %>%
    dplyr::distinct() %>%
    tibble::column_to_rownames("Term") %>%
    t()
  p <- ggradar::ggradar(wt_pw,
    centre.y = 0,
    grid.min = 0, grid.mid = 1.5, grid.max = 3,
    group.point.size = 1.5, group.line.width = 0.5,
    values.radar = c(0, 1.5, 3), label.centre.y = T,
    legend.position = "bottom", axis.label.size = 4
  )
  return(p)
}






#' barplot of go term total size and affected size
#'
#' @param merge_pw_deg_result result of merge_pw_deg()
#'
#' @return barplot(ggplot object)
#' @export
#'
#' @examples
#' data("merge_pw_deg_result")
#' vis_pw_stat_bar(merge_pw_deg_result)
vis_pw_stat_bar <- function(merge_pw_deg_result) {
  gene_stat <- merge_pw_deg_result %>%
    dplyr::select(Term, size, size_disease) %>%
    dplyr::distinct() %>%
    reshape2::melt(id = "Term") %>%
    dplyr::rename(Nums = value, Type = variable)

  p <- ggplot2::ggplot(
    gene_stat,
    ggplot2::aes(x = Term, y = Nums, fill = Type, label = Nums)
  ) +
    ggplot2::geom_bar(stat = "identity", position = "dodge") +
    ggplot2::scale_fill_manual(values = c("#377eb8", "#e41a1c")) +
    ggplot2::geom_text(position = ggplot2::position_dodge(width = 0.9), vjust = -0.25) +
    ggstatsplot::theme_ggstatsplot() +
    ggplot2::theme(
      axis.text.x = ggplot2::element_text(angle = 30, vjust = 0.8, hjust = 0.8),
      legend.title = ggplot2::element_blank()
    ) +
    ggplot2::xlab("Pathway IDs") +
    ggplot2::ylab("Gene numbers")
  return(p)
}


#' ggtern plot : one molecule, one pathway
#'
#' @param score_RRS result of score_RRS()
#' @param sle_pw  selected pathway
#' @param sle_mol selected molecules
#'
#' @return ggtern plot
#' @export
#'
#' @examples
#' data("score_RRS_result")
#' sle_pw <- "GO:0001501"
#' sle_mol <- "CGP-20712"
#' vis_tern_one(
#'   score_RRS = score_RRS_result,
#'   sle_pw = sle_pw,
#'   sle_mol = sle_mol
#' )
vis_tern_one <- function(score_RRS, sle_pw, sle_mol) {
  # sle_pw = "GO:0001501"
  # sle_mol = "CGP_20712"
  tern_dat <- score_RRS %>%
    dplyr::filter(Term %in% sle_pw) %>%
    dplyr::select(
      Term, molecule, size_disease,
      size_trans, size_cis, size_zero
    ) %>%
    dplyr::distinct(Term, molecule, .keep_all = T)
  line <- tern_dat %>%
    dplyr::filter(molecule %in% sle_mol)
  label <- paste0(sle_pw, "(", unique(tern_dat$size_disease), ")")
  p <- ggtern::ggtern(tern_dat, ggplot2::aes(x = size_cis, y = size_zero, z = size_trans)) +
    ggplot2::geom_point(size = 0.5) +
    ggtern::theme_rgbw() +
    ggtern::geom_crosshair_tern(
      data = line, ggplot2::aes(x = size_cis, y = size_zero, z = size_trans), color = "#f0027f"
    ) +
    ggtern::annotate(
      geom = "text",
      x = line$size_cis, y = line$size_zero, z = line$size_trans,
      angle = 0, vjust = -0.5, hjust = 0,
      label = line$molecule,
      color = "#f0027f"
    ) +
    ggplot2::labs(title = label) +
    ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5))
  return(p)
}



#' ggtern plot : one molecule, all pathways
#'
#' @param score_RRS result of score_RRS()
#' @param sle_mol selected molecules
#'
#' @return ggtern plot
#' @export
#'
#' @examples
#' data("score_RRS_result")
#' sle_mol <- "CGP-20712"
#' vis_tern_all(
#'   score_RRS = score_RRS_result,
#'   sle_mol = sle_mol
#' )
vis_tern_all <- function(score_RRS, sle_mol) {
  tern_dat <- score_RRS %>%
    dplyr::select(
      Term, molecule, size_disease,
      size_trans, size_cis, size_zero
    ) %>%
    dplyr::mutate(nice_Term = paste0(Term, "(", size_disease, ")")) %>%
    dplyr::distinct()
  line <- tern_dat %>%
    dplyr::filter(molecule %in% sle_mol)
  label <- paste0(sle_mol, " to All disease related pathways")

  p <- ggtern::ggtern(tern_dat, ggplot2::aes(x = size_cis, y = size_zero, z = size_trans)) +
    ggplot2::geom_point(size = 0.5) +
    ggtern::theme_rgbw() +
    ggtern::geom_crosshair_tern(
      data = line, ggplot2::aes(x = size_cis, y = size_zero, z = size_trans), color = "#f0027f"
    ) +
    ggplot2::facet_wrap(~nice_Term, nrow = 2, scales = "free") +
    ggplot2::ggtitle(label = label)
  return(p)
}





#' bell plot: tran gene; one molecule, all pathways
#'
#' @param score_RRS result of score_RRS()
#' @param sle_pw  selected pathway
#' @param sle_mol selected molecules
#'
#' @return ggplot object
#' @export
#'
#' @examples
#' data("score_RRS_result")
#' sle_pw <- "GO:0001501"
#' sle_mol <- "CGP-20712"
#' vis_bell_term(
#'   score_RRS = score_RRS_result,
#'   sle_pw = sle_pw,
#'   sle_mol = sle_mol
#' )
vis_bell_term <- function(score_RRS, sle_pw, sle_mol) {
  # sle_pw = "GO:0001501"
  # sle_mol = "CGP.20712"
  bell_dat <- score_RRS %>%
    dplyr::filter(
      Term %in% sle_pw,
      molecule %in% sle_mol,
      sign == -1
    ) %>%
    dplyr::select(Term, Gene, molecule, sign, disease_logFC, mol_logFC) %>%
    dplyr::mutate(Gene = forcats::fct_reorder(Gene, disease_logFC, .desc = T)) %>%
    reshape2::melt(id = c("Term", "Gene", "molecule", "sign")) %>%
    dplyr::rename(Type = variable, logFC = value) %>%
    dplyr::arrange(Type, desc(logFC))
  label <- paste0(
    "Reverse DEG between ",
    unique(bell_dat$molecule), " and ",
    unique(bell_dat$Term)
  )
  p <- ggplot2::ggplot(bell_dat, ggplot2::aes(x = Gene, y = logFC, color = Type)) +
    ggplot2::geom_point() +
    ggplot2::geom_segment(ggplot2::aes(
      x = Gene, xend = Gene,
      y = 0, yend = logFC
    )) +
    ggplot2::theme_bw() +
    ggsci::scale_color_aaas() +
    ggplot2::theme(
      axis.text.x = ggplot2::element_text(angle = 90, vjust = 0.5, hjust = 1),
      legend.position = "bottom"
    ) +
    ggplot2::ggtitle(label = label) +
    ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5))
  return(p)
}



#' barplot: up/down disease geneset single/both enrich reusult
#'
#' @param gsea_df result of gsea_disease_set()
#' @param sle_mols result of get_Formula()
#'
#' @return ggplot object
#' @export
#'
#' @examples
#' data("gsea_disease_set_result")
#' data("get_Formula_result")
#' gsea_df <- gsea_disease_set_result$gsea_sig
#' sle_mols <- get_Formula_result$formula1$molecule[1:10]
#' vis_bar_double_enrich(gsea_df = gsea_df, sle_mols = sle_mols)
vis_bar_double_enrich <- function(gsea_df, sle_mols) {
  # sle_mols = formula_list$formula1$molecule[1:10]
  gsea_res <- gsea_df %>%
    dplyr::select(molecule, ID, sign, NES, pvalue) %>%
    dplyr::filter(
      molecule %in% sle_mols,
      sign == -1
    ) %>%
    dplyr::mutate(sig = ifelse(pvalue < 0.001, "***",
      ifelse(pvalue < 0.01, "**",
        ifelse(pvalue < 0.05, "*", "")
      )
    ))
  gsea_res_up <- subset(gsea_res, NES > 0)
  gsea_res_down <- subset(gsea_res, NES < 0)
  p <- ggplot2::ggplot() +
    ggplot2::geom_bar(
      data = gsea_res_up, ggplot2::aes(x = molecule, y = NES, fill = ID),
      stat = "identity", position = "dodge"
    ) +
    ggplot2::geom_text(
      data = gsea_res_up, ggplot2::aes(x = molecule, y = NES, label = sig),
      vjust = -0.2
    ) +
    ggplot2::geom_bar(
      data = gsea_res_down, ggplot2::aes(x = molecule, y = NES, fill = ID),
      stat = "identity", position = "dodge"
    ) +
    ggplot2::geom_text(
      data = gsea_res_down, ggplot2::aes(x = molecule, y = NES, label = sig),
      vjust = 1.25
    ) +
    ggsci::scale_fill_nejm() +
    ggplot2::theme_bw() +
    ggplot2::xlab(label = "") +
    ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, vjust = 0.5, hjust = 1))
  return(p)
}






#' chordDiagram: selected molecules of formula2 aganinst pathways
#'
#' @param score_RRS  result of score_RRS()
#' @param sle_mols selected molecules of formula2
#'
#' @return chordDiagram
#' @export
#'
#' @examples
#' data("get_Formula_result")
#' sle_mols <- unique(get_Formula_result$formula2$molecule)
#' data("score_RRS_result")
#' score_RRS <- score_RRS_result
#' vis_formula_RRS_chord(score_RRS = score_RRS, sle_mols = sle_mols)
vis_formula_RRS_chord <- function(score_RRS, sle_mols) {
  chord_dat <- score_RRS %>%
    dplyr::filter(RRS > 0, molecule %in% sle_mols) %>%
    dplyr::select(molecule, Term)
  cols_term <- rep("grey", length(unique(chord_dat$Term)))
  names(cols_term) <- unique(chord_dat$Term)
  cols_mol <- RColorBrewer::brewer.pal(length(unique(chord_dat$molecule)), "Dark2")
  names(cols_mol) <- unique(chord_dat$molecule)
  circlize::chordDiagram(chord_dat,
    transparency = 0.2,
    grid.col = c(cols_term, cols_mol),
    annotationTrack = "grid",
    preAllocateTracks = 1
  )
  circlize::circos.track(track.index = 1, panel.fun = function(x, y) {
    circlize::circos.text(circlize::CELL_META$xcenter, circlize::CELL_META$ylim[1], circlize::CELL_META$sector.index,
      facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5), cex = 1
    )
  }, bg.border = NA)
  title("Formula molecules to Disease related pathways", cex = 0.8, )
}





#' chordDiagram: selected molecules of formula2 aganinst disease up/down geneset
#'
#' @param gsea_df  result of gsea_disease_set()
#' @param sle_mols selected molecules of formula2
#'
#' @return chordDiagram
#' @export
#'
#' @examples
#' data("gsea_disease_set_result")
#' gsea_df <- gsea_disease_set_result$gsea_sig
#' data("get_Formula_result")
#' sle_mols <- unique(get_Formula_result$formula2$molecule)
#' vis_formula_gsea_chord(gsea_df = gsea_df, sle_mols = sle_mols)
vis_formula_gsea_chord <- function(gsea_df, sle_mols) {
  chord_dat <- gsea_df %>%
    dplyr::filter(
      molecule %in% sle_mols,
      pvalue < 0.05, sign < 0
    ) %>%
    dplyr::select(molecule, ID)
  cols_ID <- c("#2b83ba", "#d7191c")
  names(cols_ID) <- unique(chord_dat$ID)
  cols_mol <- RColorBrewer::brewer.pal(length(unique(chord_dat$mole)), "Dark2")
  names(cols_mol) <- unique(chord_dat$molecule)
  circlize::chordDiagram(chord_dat,
    transparency = 0.2,
    grid.col = c(cols_ID, cols_mol),
    annotationTrack = "grid",
    preAllocateTracks = 1
  )
  circlize::circos.track(track.index = 1, panel.fun = function(x, y) {
    circlize::circos.text(circlize::CELL_META$xcenter, circlize::CELL_META$ylim[1], circlize::CELL_META$sector.index,
      facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5), cex = 1
    )
  }, bg.border = NA)
  title("Formula molecules to Disease UP/DOWN genesets", cex = 0.8)
}
lishensuo/M2D documentation built on Jan. 4, 2022, 9:44 a.m.