R/eBay.R

Defines functions eBay

Documented in eBay

#' An method for differential abundance detection
#'
#'
#' @param otu.data  an OTU table with n rows (samples) and m columns (taxa)
#' @param group a n-vector of group indicators
#' @param cutf level of significance
#' @param test.method t-test or Wilcoxon rank sum test
#' @param adj.m the adjustment methods for p-values
#' @return final.p    the adjusted p values
#' @return dif.otus   the set of differentially abundant OTUs
#' @examples
#' ####generate data####
#' library(eBay)
#' set.seed(1)
#' rand_pi <- runif(20)
#' control_pi = case_pi = rand_pi/sum(rand_pi)
#' control_theta = case_theta = 0.1
#' group <- rep(c(0,1),each =20)
#' ntree_table <- simulation_dm(p=20,seed=1, N=20,control_pi, case_pi,
#' control_theta,case_theta)
#' #####differential abundance testing###
#' ebay.res <- eBay(otu.data=ntree_table, group=group, test.method="t",
#' cutf=0.05,adj.m="BH")
#' @export
#' @importFrom MGLM  MGLMfit
#' @import stats
eBay=function(otu.data,group,cutf,test.methods,adj.m){
  sample.s<- nrow(otu.data)
  otu.n <- ncol(otu.data)
  case.s <- length(which(group == 0))
  con.s <- length(which(group == 1))
  case <- which(group == 0)
  con <- which(group == 1)

  ntree_table<- otu.data
  taxa.p <- ncol(ntree_table)
  rownames(ntree_table) <- as.character(1:sample.s)

  fit_glm <- MGLMfit(ntree_table, dist = "DM")  ####estimate the parameter alpha
  ntree_para <- fit_glm@estimate

  ntree_para_mat <- matrix(rep(ntree_para, sample.s),byrow = TRUE,ncol = taxa.p)

  exp_norm <- matrix(NA,ncol=taxa.p,nrow=sample.s)

  for (n in 1:sample.s) {
    exp_norm[n, ] = unlist((ntree_table[n, ] + ntree_para_mat[n,]) / (sum(ntree_table[n, ]) +sum(ntree_para_mat[n,])))
  }

  colnames(exp_norm)= colnames(ntree_table)

  exp_clr <- apply(exp_norm, 1, function(x){log2(x) - mean(log2(x))})
 if(test.methods=="t"){
  exp_test_t <- apply(exp_clr, 1, function(input) {
    t.test(input[case], input[con])$p.value
  })
  final.p.t <- p.adjust(exp_test_t,adj.m)
  dif.otus.t <- which( final.p.t< cutf)
  return(list(final.p=final.p.t, dif.otus=dif.otus.t))

 }
  if(test.methods=="wilcoxon"){
  exp_test_wil <- apply(exp_norm, 2, function(input) {
    wilcox.test(input[case], input[con])$p.value
  })
  final.p.wil <- p.adjust(exp_test_wil,adj.m)
  dif.otus.wil <-which(final.p.wil < cutf)
  return(list(final.p=final.p.wil,dif.otus=dif.otus.wil))
}
}
liudoubletian/eBay documentation built on April 3, 2020, 7:45 p.m.