Chapter 4 Calculating differential abundance (DA) table

Differential abundance of the metabolite data will also be done using custom functions.

4.1 Getting vectors with samples per each treatment group

Samples from time T0 will be used as control to compare the samples from all other time points of the two treatments: Water pulse (WP) and control (CTR).

Sample information will be loaded from a .csv table.

metadata <- read_csv('data/metadata.csv')

4.2 Generating table with differential abundances per feature

Using the function get_diff_table() to calculate differential abundance between the different sample groups. This function will calculate the log2FoldChange (log2FC) for each feature as well as a p-value and adjusted p-value.

Because multiple comparisons are needed, nested for() loops will be used

# Samples at T0
control_samples <- metadata %>% 
  filter(time == 'T0') %>% 
  pull(SampleID)

# Initializing a df to store the results
diff_table <- as.data.frame(matrix(nrow = 0, ncol = 7))

for(treat in c('WP', 'CTR')){
  for(t in c('T1', 'T2', 'T3', 'T4')){
    treatment_samples <- metadata %>% 
      filter(treatment == treat,
             time == t) %>% 
      pull(SampleID)
    
    temp_diff_table <- get_diff_table(norm_abundances,
                                      control.sample_list = control_samples,
                                      treatment.sample_list = treatment_samples,
                                      log2_transformed = TRUE) %>% 
      mutate(comparison = paste0('T0_vs_', treat, '_', t))
    
    diff_table <- rbind(diff_table, temp_diff_table)
    
  }
}