R/save_results.R

Defines functions save_results

Documented in save_results

#########################
# Function save_results #
#########################

#' This function will save the results of a Differential Expression analysis
#'
#' This function takes as input the output of the function "results()" of DEseq2.
#' And will save 3 tables:
#' - A table with all genes
#' - A table including only the over-expressed genes
#' - A table including only the under-expressed genes
#'
#' @param df A dataframe with the results of a Differential Expression analysis.
#' @param name The name to be used to save the tables, without file extension.
#' @param l2fc The cut-off of Log2(Fold Change) for the over- and under-expressed tables. Default = 0.
#' @export

save_results <- function(df, name, l2fc = 0){
  
  names(df)[names(df) == "padj"] <- "FDR"
  
  # Saving all genes:
  write.xlsx(df, colNames = T, rowNames = F, append = F,
             file = paste0(name, "_full.xlsx"), overwrite = T)

  #Saving over-expressed genes:
  df.sig.fold_over <- subset(df, ((FDR < cutoff_alpha) & !is.na(FDR)) &
                               log2FoldChange >= l2fc)
  write.xlsx(df.sig.fold_over, colNames = T, rowNames = F, append = F,
             file = paste0(name, "_Overexp.xlsx"), overwrite = T)

  #Saving under-expressed genes:
  df.sig.fold_under <- subset(df, ((FDR < cutoff_alpha) & !is.na(FDR)) &
                                log2FoldChange <= -l2fc)
  write.xlsx(df.sig.fold_under, colNames = T, rowNames = F, append = F,
             file = paste0(name, "_Underexp.xlsx"), overwrite = T)
}
davidrequena/drfun documentation built on May 15, 2024, 10:33 a.m.