Zettelkasten

source(file.path("..", "Analyses", "Differential Expression", "RNA-Seq - EdgeR.R"), verbose=TRUE)

#################################################################
# Volcano plot                                                  #
#################################################################
volcanoPlotTT <- de.results %>%
      dplyr::mutate(
        Legend = dplyr::case_when(
          FDR <= 0.05 & logFC > log2(fc.cutoff) ~ "Increased",
          FDR <= 0.05 & logFC < (log2(fc.cutoff) * -1) ~ "Decreased",
          TRUE ~ "Not.Significant"
        )
      )

dataToAnnotate <- volcanoPlotTT %>% # Determine results to annotate
  dplyr::filter(
    Legend != "Not.Significant"
  ) %>%
  head(n = 15)

y.intecept.value <- volcanoPlotTT %>%
  dplyr::filter(
    abs(logFC) > log2(fc.cutoff),
    Legend == "Not.Significant"
  ) %>%
  dplyr::pull(
    PValue
  ) %>% log10() %>% (function(x) {x*-1}) %>% max()

volcanoplot <- ggplot2::ggplot(
    volcanoPlotTT,
    ggplot2::aes(x = logFC, y = -log10(PValue))
  ) +
  ggplot2::geom_point(
    ggplot2::aes(color = Legend)
  ) +
  ggplot2::geom_hline( # Significance cutoff
    yintercept = y.intecept.value,
    colour = "#990000",
    linetype = "dashed"
  ) +
  ggplot2::geom_vline( # Foldchange cutoff (negative)
    xintercept = (log2(fc.cutoff) * -1),
    colour = "#990000",
    linetype = "dashed"
  ) +
  ggplot2::geom_vline( # Foldchange cutoff (positive)
    xintercept = log2(fc.cutoff),
    colour = "#990000",
    linetype = "dashed"
  ) +
  ggrepel::geom_text_repel( # Annotate top significant hits
    data = dataToAnnotate,
    ggplot2::aes(label = hgnc_symbol),
    size = 3,
    box.padding = grid::unit(0.35, "lines"),
    point.padding = grid::unit(0.3, "lines")
  ) +
  ggplot2::labs(
    title = "Healthy Controls  vs  COPD 3 & 4",
    x = "log2(Fold Change)",
    y = "-log10(P Value)",
    legend = "Legend"
  ) +
  ggplot2::scale_color_manual(
    values = c(
      Decreased = "blue",
      Increased = "red",
      Not.Significant = "grey"
    ),
    labels = function(label) {
      gsub(
        x = label,
        pattern = "\\.",
        replacement = " "
      )
    }
  ) +
  ggplot2::theme_bw(base_size = 12) +
  ggplot2::theme(
    legend.position = "bottom",
    panel.grid.minor = ggplot2::element_blank()
  ) +
  ggprism::theme_prism() + 
  #ggprism::scale_colour_prism() + 
  ggprism::scale_shape_prism()

ggplot2::ggsave(
  filename = file.path(out.dir, "volcanoplot.png"),
  plot = volcanoplot
)