DA.zzz: User-defined function

View source: R/DA.zzz.R

DA.zzzR Documentation

User-defined function

Description

Apply a user-defined function to all features of a count table. For implemetation in testDA and allDA

Usage

DA.zzz(
  data,
  predictor,
  paired = NULL,
  covars = NULL,
  p.adj = "fdr",
  FUN = NULL
)

Arguments

data

Either a matrix with counts/abundances, OR a phyloseq object. If a matrix/data.frame is provided rows should be taxa/genes/proteins and columns samples

predictor

The predictor of interest. Factor or Numeric, OR if data is a phyloseq object the name of the variable in sample_data(data) in quotation

paired

For paired/blocked experimental designs. Either a Factor with Subject/Block ID for running paired/blocked analysis, OR if data is a phyloseq object the name of the variable in sample_data(data) in quotation

covars

Either a named list with covariables, OR if data is a phyloseq object a character vector with names of the variables in sample_data(data)

p.adj

Character. P-value adjustment. Default "fdr". See p.adjust for details

FUN

Function to apply to data. Should take input in the following order: count_table (data.frame, samples are columns), predictor (vector), paired (factor), covars (named list with vector). Output should be a dataframe with at least the following columns: Feature, pval and Method.

Value

A data.frame with results from the user-defined method

Examples

# Creating random count_table and predictor
set.seed(4)
mat <- matrix(rnbinom(1000, size = 0.1, mu = 500), nrow = 100, ncol = 10)
rownames(mat) <- 1:100
pred <- c(rep("Control", 5), rep("Treatment", 5))

# Define function for t-test
myfun <- function(count_table, predictor, paired, covars){ 
 
# Relative abundance
rel <- apply(count_table, 2, function(x) x/sum(x))

# t-test function
# Wrapping this function in tryCatch(..., error = function(e){NA}) 
# ensures that our main function won't fail if t.test fails on some features
tfun <- function(x){
    tryCatch(t.test(x ~ predictor)$p.value, error = function(e){NA}) 
}

# P-values for each feature
pvals <- apply(rel, 1, tfun)

# Collect and return data
df <- data.frame(Feature = rownames(count_table),
                 pval = pvals)
df$pval.adj <- p.adjust(df$pval, method = "fdr")
df$Method <- "My own t-test"
return(df)
}

# Running the test
res <- DA.zzz(data = mat, predictor = pred, FUN = myfun)

Russel88/DAtest documentation built on March 24, 2022, 3:50 p.m.