R/U-Test.R

Defines functions perform_wilcox_test

Documented in perform_wilcox_test

#' Function for Wilcoxon-Mann-Whitney U-Test
#'
#' @param con A data frame or matrix containing the expression values for the control.
#' @param exp A data frame or matrix containing the expression values for the experiment.
#' @param alpha Value of significance level ranging from 0 to 1 (default = 0.05 states 5 \% significance).
#'
#' @return A data frame containing values for statistic score, p-values etc for each gene being tested.
#'
#' @export
#'
#' @importFrom stats wilcox.test
#' @importFrom stats p.adjust
#' @importFrom stats na.omit
#' @examples
#' library(DGEAR)
#' data("gene_exp_data")
#' data = read_and_preprocess_data(datafile = gene_exp_data, con1=1,con2=10,exp1=11,exp2=20)
#' perform_wilcox_test(con= data$con, exp= data$exp)
perform_wilcox_test <- function(con, exp, alpha = 0.05) {
  p.value = NULL
  statistic = NULL
  for(i in 1 : nrow(con)) {
    x = as.numeric(con[i,])
    y = as.numeric(exp[i,])
    u = wilcox.test(x, y, paired = F)
    p.value[i] = u$p.value
    statistic[i] = u$statistic
  }

  u_stat = cbind.data.frame(ID=row.names(con),p.value,statistic)
  #u_stat$fdr[u_stat$p.value<=(alpha/nrow(u_stat))*seq(length=nrow(u_stat))] <- 1
  u_stat$BH = p.adjust(u_stat$p.value, method = "BH")
  u_stat$fdr[u_stat$BH<=alpha]<- 1
  
  DEGs=u_stat$ID[u_stat$fdr == 1]
  DEGs= na.omit(DEGs)
  DEGs = as.data.frame(DEGs)
  colnames(DEGs) = "DEGs"
  print(DEGs)
  return(list(Table = u_stat, DEGs = DEGs))
}

Try the DGEAR package in your browser

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

DGEAR documentation built on June 27, 2024, 1:07 a.m.