a b/R/PrepDeseq2.R
1
#' Prepare DESeq2 data for plotting
2
#'
3
#' This function reads a DESeq2 DEG data frame from an RDS file, filters it,
4
#' adjusts the log2FoldChange to absolute values, adds a pseudo-count to pvalues,
5
#' and transforms pvalues for plotting. The final data frame is returned and
6
#' optionally saved to a new RDS file.
7
#'
8
#' @param input_path Path to the RDS file containing the DESeq2 DEG data frame.
9
#' @param output_name Name for the processed data frame, also used as the RDS file name.
10
#' @return A data frame with processed DESeq2 DEG data.
11
#' @export
12
#' @examples
13
#' deseq2_file <- system.file("extdata",
14
#'                            "DEG_deseq2_test.rds",
15
#'                            package = "TransProR")
16
#' deseq2 <- prep_deseq2(deseq2_file)
17
#'
18
prep_deseq2 <- function(input_path, output_name = NULL) {
19
  # Read the DESeq2 DEG data frame from an RDS file
20
  DEG_deseq2 <- readRDS(input_path)
21
22
  # Filter DEG data using the deg_filter function from the same package
23
  DESeq2 <- deg_filter(DEG_deseq2)
24
  DEG_deseq2 <- DEG_deseq2[rownames(DEG_deseq2) %in% DESeq2, ]
25
26
  # Extract the Gene column as a regular column
27
  DEG_deseq2$Gene <- rownames(DEG_deseq2)
28
29
  # Select columns of interest
30
  DEG_deseq2 <- DEG_deseq2[, c('log2FoldChange', 'pvalue', "change", 'Gene')]
31
32
  # Adjust log2FoldChange values to absolute values
33
  DEG_deseq2$log2FoldChange <- abs(DEG_deseq2$log2FoldChange)
34
35
  # Add a small pseudo-count to pvalue to avoid log of zero
36
  DEG_deseq2$pvalue <- DEG_deseq2$pvalue + .Machine$double.eps
37
38
  # Transform p-value for plotting
39
  DEG_deseq2$pvalue <- -log10(DEG_deseq2$pvalue)
40
41
  # Rename columns
42
  names(DEG_deseq2) <- c('logFC', 'Pvalue', "change", 'Gene')
43
44
  # Optionally save the processed data frame as an RDS file
45
  if (!is.null(output_name)) {
46
    saveRDS(DEG_deseq2, paste0(output_name, ".Rdata"))
47
  }
48
49
  # Return the processed data frame
50
  return(DEG_deseq2)
51
}
52
53
54