R/plot_screen.R

Defines functions plot_screen

Documented in plot_screen

#' @title Plot genetic screen results
#' 
#' @description Generates a ranked scatter plot of genetic interaction scores from `GINI_screen` outputs
#' 
#' @param result_df data frame, A data frame output from `GINI_screen()`, Default: NULL
#' @param label_genes logical, TRUE to trigger gene name labeling, Default: FALSE
#' @param gene_list characters, A vector of Hugo Symbols to plot. labels_genes must be TRUE to plot them, Default: NULL
#' @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_KO_screen.rda"), envir = environment())
#' 
#' plot_screen(screen_results, label_genes = TRUE, "ARID1B")
#' 
#' @rdname plot_screen
#' @export 
#' @importFrom dplyr mutate filter arrange case_when
#' @importFrom ggplot2 ggplot aes scale_color_identity scale_size_identity scale_x_reverse theme_light ylab
#' @importFrom ggrepel geom_label_repel


plot_screen <- function(result_df = NULL, label_genes = FALSE, gene_list = NULL, label_n = NULL) {
  # Check data is provided
  if (is.null(result_df)) {
    stop("No result data frame provided")
  }
  
  # Arrange and rank
  plot_df <- result_df %>%
    dplyr::arrange(-.data$Interaction_score) %>%
    dplyr::filter(!is.na(.data$Interaction_score)) %>%
    dplyr::mutate(
      Rank = seq_len(length(.data$Interaction_score)),
      color = dplyr::case_when(
        .data$Pval >= 0.05 ~ "darkgray", .data$Pval < 0.05 & .data$log2FC_by_median >
          0 ~ "#882255", .data$Pval < 0.05 & .data$log2FC_by_median < 0 ~
          "#882255"
      )
    )
  
  # Plot according to rank
  if (label_genes == TRUE & !is.null(gene_list)) {
    plot_df <- plot_df %>%
      dplyr::arrange(.data$Rank) %>%
      dplyr::mutate(
        label = dplyr::case_when(
          .data$GeneNames %in% gene_list ~ TRUE, 
          TRUE ~ FALSE
        )
      )
    
    plot <- ggplot2::ggplot(plot_df, ggplot2::aes(x = .data$Rank, y = .data$Interaction_score)) +
      ggplot2::geom_point(aes(color = .data$color, size = ifelse(.data$Pval < 0.05, 2, 1))) +
      ggplot2::scale_color_identity() + ggplot2::scale_size_identity() + ggplot2::scale_x_reverse() +
      ggrepel::geom_label_repel(
        ggplot2::aes(label = ifelse(.data$label, .data$GeneNames, "")),
        max.overlaps = Inf, force = 5, box.padding = 1, direction = "both",
        min.segment.length = ggplot2::unit(0, "lines"),
        segment.color = "grey50", color = "black"
      ) +
      ggplot2::theme_light() + ggplot2::ylab("Genetic interaction score")
    
  } else if(label_genes == TRUE) {
    if (is.null(label_n)) {
      label_n <- 1
    }
    
    last <- nrow(plot_df)
    plot_df <- plot_df %>%
      dplyr::arrange(.data$Rank) %>%
      dplyr::mutate(
        label = dplyr::case_when(
          .data$Rank %in% seq_len(label_n) ~
            TRUE, .data$Rank %in% c((last - (label_n - 1)):last) ~
            TRUE, TRUE ~ FALSE
        )
      )
    
    plot <- ggplot2::ggplot(plot_df, ggplot2::aes(x = .data$Rank, y = .data$Interaction_score)) +
      ggplot2::geom_point(aes(color = .data$color, size = ifelse(.data$Pval < 0.05, 2, 1))) +
      ggplot2::scale_color_identity() + ggplot2::scale_size_identity() + ggplot2::scale_x_reverse() +
      ggrepel::geom_label_repel(
        ggplot2::aes(label = ifelse(.data$label, .data$GeneNames, "")),
        max.overlaps = Inf, force = 5, box.padding = 1, direction = "both",
        min.segment.length = ggplot2::unit(0, "lines"),
        segment.color = "grey50", color = "black"
      ) +
      ggplot2::theme_light() + ggplot2::ylab("Genetic interaction score")
  } else {
    plot <- ggplot2::ggplot(plot_df, ggplot2::aes(x = .data$Rank, y = .data$Interaction_score)) +
      ggplot2::geom_point(aes(color = .data$color, size = ifelse(.data$Pval < 0.05, 2, 1))) +
      ggplot2::scale_color_identity() + ggplot2::scale_size_identity() + ggplot2::scale_x_reverse() +
      ggplot2::theme_light() + ggplot2::ylab("Genetic interaction score")
  }
  return(plot)
}
ytakemon/GINIR documentation built on Feb. 27, 2024, 1:33 p.m.