R/ReadRRA.R

Defines functions ReadRRA

Documented in ReadRRA

#' Read gene summary file in MAGeCK-RRA results
#'
#' @docType methods
#' @name ReadRRA
#' @rdname ReadRRA
#' @aliases readrra
#'
#' @param gene_summary A data frame or a file path to gene summary file generated by MAGeCK-RRA.
#' @param score "lfc" (default) or "rra", specifying the score type.
#'
#' @return A data frame including three columns, including "id", "LFC" and "FDR".
#' @details If the score type is equal to lfc, then LFC will be returned. If the score type is rra,
#' the log10 transformed RRA score will be returned.
#'
#' @author Wubing Zhang
#'
#'
#' @examples
#' file1 = file.path(system.file("extdata", package = "MAGeCKFlute"),
#' "testdata/rra.gene_summary.txt")
#' gdata = ReadRRA(file1)
#' head(gdata)
#'
#' @export
#'

ReadRRA <- function(gene_summary, score = c("lfc", "rra")[1]){
  if(is.null(dim(gene_summary))){
    gene_summary = read.table(file = gene_summary, sep = "\t", header = TRUE, quote = "",
                    comment.char = "", check.names = FALSE, stringsAsFactors = FALSE)
  }
  if(all(c("id", "Score", "FDR")%in%colnames(gene_summary))){
    dd = as.data.frame(gene_summary[,c("id", "Score", "FDR")], stringsAsFactors = FALSE)
    dd$id = as.character(dd$id)
    return(dd)
  }
  gene_summary = gene_summary[, c(1, 3, 9, 8, 14, 5, 11)]
  colnames(gene_summary) = c("id", "negscore", "poscore", "neglfc", "poslfc", "negfdr", "posfdr")
  dd = gene_summary
  if("lfc" %in% tolower(score)){
    dd$LFC = dd$poslfc
    dd$FDR = dd$posfdr
    dd$LFC[abs(dd$neglfc)>dd$poslfc] = dd$neglfc[abs(dd$neglfc)>dd$poslfc]
    dd$FDR[abs(dd$neglfc)>dd$poslfc] = dd$negfdr[abs(dd$neglfc)>dd$poslfc]
    dd = dd[, c("id", "LFC", "FDR")]
  }else if("rra" %in% tolower(score)){
    idx_neg = dd$negscore<dd$poscore
    dd$LFC = apply(-log10(dd[, 2:3]), 1, max);
    dd$LFC[idx_neg] = -dd$LFC[idx_neg]
    dd$FDR = dd$posfdr; dd$FDR[idx_neg] = dd$negfdr[idx_neg]
    dd = dd[, c("id", "LFC", "FDR")]
  }
  colnames(dd) = c("id", "Score", "FDR")
  dd$id = as.character(dd$id)
  return(dd)
}

Try the MAGeCKFlute package in your browser

Any scripts or data that you put into this service are public.

MAGeCKFlute documentation built on Nov. 8, 2020, 5:40 p.m.