# WARNING - Generated by {fusen} from /dev/flat_full.Rmd: do not edit by hand



#' Get Labels for Expected Differential Expression
#'
#' This function assigns labels to genes based on whether their actual effect estimates
#' indicate differential expression according to a given threshold and alternative hypothesis.
#'
#' @param comparison_df A data frame containing comparison results with actual effect estimates.
#' @param coeff_threshold The threshold value for determining differential expression.
#' @param alt_hypothesis The alternative hypothesis for comparison. Possible values are "greater",
#'                      "less", and "greaterAbs".
#' @return A modified data frame with an additional column indicating if the gene is differentially expressed.
#'
#' @examples
#' # Generate a sample comparison data frame
#' comparison_data <- data.frame(
#'   geneID = c("gene1", "gene2", "gene3"),
#'   actual = c(0.5, -0.3, 0.8)
#' )
#'
#' # Get labels for expected differential expression
#' labeled_data <- getLabelExpected(comparison_data, coeff_threshold = 0.2, alt_hypothesis = "greater")
#'
#' @export
getLabelExpected <- function(comparison_df, coeff_threshold, alt_hypothesis) {
  if (alt_hypothesis == "greater") {
    idx_DE <- comparison_df$actual > coeff_threshold
    comparison_df$isDE <- idx_DE
  } else if (alt_hypothesis == "less") {
    idx_DE <- comparison_df$actual < coeff_threshold
    comparison_df$isDE <- idx_DE
  } else if (alt_hypothesis == "greaterAbs") {
    idx_DE <- abs(comparison_df$actual) > coeff_threshold
    comparison_df$isDE <- idx_DE
  }
  return(comparison_df)
}


#' Generate ROC Curve Plot
#'
#' This function generates an ROC curve plot based on the comparison dataframe.
#'
#' @param comparison_df A dataframe containing comparison results.
#' @param ... additional params to pass ggplot2::aes
#' @return A ggplot object representing the ROC curve plot.
#' @importFrom plotROC geom_roc
#' @importFrom ggplot2 ggtitle theme_bw aes sym xlab ylab
#'
#' @examples
#' comparison_data <- data.frame(
#'   geneID = c("gene1", "gene2", "gene3"),
#'   isDE = c(TRUE, FALSE, TRUE),
#'   p.adj = c(0.05, 0.2, 0.01)
#' )
#' roc_plot(comparison_data)
#'
#' @export
roc_plot <- function(comparison_df, ...) {
  
  checkLabelValidityForROC <- function(labels) {
    if (all(labels == TRUE)) 
      message("WARNING : No FALSE label in 'isDE' column, ROC curve cannot be computed")
    if (all(labels == FALSE)) 
      message("WARNING : No TRUE label in 'isDE' column, ROC curve cannot be computed")
  }
  
  checkLabelValidityForROC(comparison_df$isDE)
  
  args <- lapply(list(...), function(x) if (!is.null(x)) ggplot2::sym(x))

  #comparison_df$isDE <- factor(comparison_df$isDE, levels= c(TRUE, FALSE))
  p <- ggplot2::ggplot(comparison_df, ggplot2::aes(d = !isDE , m = p.adj, !!!args )) +
        plotROC::geom_roc(n.cuts = 0, labels = FALSE) + 
        ggplot2::theme_bw() +
        ggplot2::ggtitle("ROC curve") +
        ggplot2::xlab("False positive rate") +
        ggplot2::ylab("True positive rate")
  
  ## -- annotation AUC
  df_AUC <- subset(plotROC::calc_auc(p) , select = -c(PANEL, group))
  df_AUC$AUC <- round(df_AUC$AUC, digits = 3)
  if (nrow(df_AUC) == 1) annotations <- paste("AUC", df_AUC$AUC, sep = " : ")
  else annotations <- do.call(paste, c(df_AUC, sep = " - AUC: "))
  annotations <- paste(annotations, collapse  = "\n")
  p <- p + ggplot2::annotate("text", x = .75, y = .25, label = annotations)
  return(p)
}