Functions/mi4limma.R

source("Functions/eBayes_mod.R")

mi4limma <- function (qData, sTab, VarRubin, comp.type = "OnevsOne", robust = F) 
{
  switch(comp.type, OnevsOne = contrast <- 1, OnevsAll = contrast <- 2)
  rownames(sTab) <- sTab$Sample.name
  colnames(qData) <-sTab$Sample.name
  sTab.old <- sTab
  conds <- factor(sTab$Condition, levels = unique(sTab$Condition))
  sTab <- sTab[unlist(lapply(split(sTab, conds), function(x) {
    x["Sample.name"]
  })), ]
  qData <- qData[, unlist(lapply(split(sTab.old, conds), function(x) {
    x["Sample.name"]
  }))]
  conds <- conds[order(conds)]
  res.l <- NULL
  design.matrix <- DAPAR::make.design(sTab)
  if (!is.null(design.matrix)) {
    contra <- DAPAR::make.contrast(design.matrix, condition = conds, 
                            contrast)
    cmtx <- limma::makeContrasts(contrasts = contra, levels = make.names(colnames(design.matrix)))
    fit <- eBayes.mod(limma::contrasts.fit(limma::lmFit(qData, 
                                                        design.matrix), cmtx), VarRubin, robust=robust)
    res.l <- DAPAR:::formatLimmaResult(fit, conds, contrast)
  }
  return(res.l)
}
mariechion/mi4p documentation built on Oct. 3, 2024, 4:50 a.m.