R/plot_coess.R

Defines functions plot_coess

Documented in plot_coess

#' @title Plot coessential genes
#' 
#' @description Generates a ranked scatter plot of cossential genes from `annotate` outputs
#' 
#' @param result_df data frame, A data frame output from `annotate_df()`, Default: NULL
#' @param inflection_df data frame, A data frame output from `get_inflection_points()`, Default: NULL
#' @param label_genes logical, TRUE to trigger gene name labeling, Default: FALSE
#' @param label_n integer, Number of genes from either end to label, Default: 1
#'
#' @return A plot generated by ggplot2, additional ggplot layers can be applied directly using `+`
#' @md
#' 
#' @examples 
#' gretta_data_dir <- './GRETTA_example/'
#' gretta_output_dir <- './GRETTA_example_output/'
#' 
#' if(!dir.exists(gretta_data_dir)){
#'   download_example_data(".")
#' }
#' 
#' load(paste0(
#' gretta_data_dir,'/sample_22Q2_ARID1A_coessential_result.rda'), 
#' envir = environment())
#' load(paste0(
#' gretta_data_dir,'/sample_22Q2_ARID1A_coessential_inflection.rda'), 
#' envir = environment())
#' 
#' plot_coess(
#' result_df = coess_df, 
#' inflection_df = coess_inflection_df, 
#' label_genes = FALSE)
#' 
#' @rdname plot_coess
#' @export 
#' @importFrom dplyr mutate filter arrange case_when
#' @importFrom ggplot2 ggplot aes geom_hline geom_point scale_colour_identity scale_x_reverse scale_y_continuous theme theme_light
#' @importFrom stringr str_split_fixed
#' @importFrom ggrepel geom_label_repel

plot_coess <- function(result_df = NULL, inflection_df = NULL,
                       label_genes = FALSE, label_n = NULL) {
  # Check data is provided
  if (is.null(result_df)) {
    stop("No result data frame provided")
  }
  if (is.null(inflection_df)) {
    stop("No inflection data frame provided")
  }
  
  # Extract gene_names only
  plot_df <- result_df %>%
    dplyr::mutate(GeneName_B = stringr::str_split_fixed(.data$GeneNameID_B,
                                                        "_", 2)[, 1]) %>%
    dplyr::arrange(.data$Rank)
  
  # Extract genes to label if indicated
  if (label_genes == TRUE) {
    if (is.null(label_n)) {
      label_n <- 1
    }
    
    pos_gene <- plot_df %>%
      dplyr::arrange(.data$Rank) %>%
      dplyr::slice(seq_len(label_n)) %>%
      dplyr::pull(.data$GeneName_B)
    neg_gene <- plot_df %>%
      dplyr::arrange(-.data$Rank) %>%
      dplyr::slice(seq_len(label_n)) %>%
      dplyr::pull(.data$GeneName_B)
    Label_on_plot <- c(pos_gene, neg_gene)
  } else {
    Label_on_plot <- c("")
  }
  
  # get cor. coef of threshold
  pos_inflection_cor <- plot_df %>%
    dplyr::filter(.data$Rank <= inflection_df$Inflection_point_pos_byRank) %>%
    dplyr::pull(.data$estimate) %>%
    min
  neg_inflection_cor <- plot_df %>%
    dplyr::filter(.data$Rank >= inflection_df$Inflection_point_neg_byRank) %>%
    dplyr::pull(.data$estimate) %>%
    max
  
  plot_df <- plot_df %>%
    dplyr::mutate(Label_on_plot = ifelse(.data$GeneName_B %in%
                                           Label_on_plot, TRUE, FALSE), Candidate_gene = ifelse(.data$Padj_BH <
                                                                                                  0.05 & (.data$estimate > pos_inflection_cor |
                                                                                                            .data$estimate < neg_inflection_cor), TRUE,
                                                                                                FALSE), point_colour = ifelse(.data$Candidate_gene ==
                                                                                                                                TRUE, "red", "grey"))
  
  plot <- ggplot2::ggplot(plot_df, ggplot2::aes(x = .data$Rank,
                                                y = .data$estimate, colour = .data$point_colour,
                                                label = ifelse(.data$Label_on_plot, .data$GeneName_B,
                                                               ""))) + ggplot2::geom_hline(yintercept = c(pos_inflection_cor,
                                                                                                          neg_inflection_cor), linetype = "dashed", colour = c("dark grey")) +
    ggplot2::geom_point() + ggrepel::geom_text_repel(seed = 4,
                                                     colour = "black", min.segment.length = 0, box.padding = 0.25,
                                                     nudge_x = -1000, max.overlaps = Inf) + ggplot2::scale_colour_identity() +
    ggplot2::scale_y_continuous(limits = c(-1,
                                           1), breaks = seq(-1, 1, by = 0.25)) + ggplot2::theme_light() +
    ggplot2::scale_x_reverse() + ggplot2::theme(text = ggplot2::element_text(size = 12)) +
    ggplot2::ylab("Pearson correlation coefficient") +
    ggplot2::xlab("")
  return(plot)
}
ytakemon/GINIR documentation built on Oct. 11, 2024, 6:06 a.m.