Differential abundance functions
Function to calculate differential abundance table
get_diff_table <- function(auc_matrix, control.sample_list, treatment.sample_list, log2_transformed = FALSE){
# Get the AUC values per each sample and calculate the means per feature
temp.df_control <- auc_matrix %>%
select(all_of(control.sample_list))
control_means <- rowMeans(temp.df_control, na.rm = TRUE)
temp.df_treatment <- auc_matrix %>%
select(all_of(treatment.sample_list))
treatment_means <- rowMeans(temp.df_treatment, na.rm = TRUE)
diff_table <- as.data.frame(cbind(control_means, treatment_means))
if(log2_transformed == TRUE){
diff_table <- diff_table %>%
mutate(log2FC = treatment_means - control_means)
} else {
diff_table <- diff_table %>%
mutate(ratio = treatment_means/control_means) %>% # get the control/treatment ratio
mutate(log2FC = log2(ratio)) # calculate log2FC
}
rownames(diff_table) <- rownames(auc_matrix)
# Initialize pvalues matrix
pvalues <- data.frame(row.names = rownames(auc_matrix), pval = rep(0, length(rownames(auc_matrix))))
#Calculate pvalue per each of the features
for(i in 1:nrow(pvalues)){
t.test <- t.test(as.numeric(temp.df_control[i,]), as.numeric(temp.df_treatment[i,]), paired = FALSE)
pvalues$pval[i] <- t.test$p.value
}
pvalues <- pvalues %>%
rownames_to_column(var = 'FeatureID')
diff_table <- diff_table %>%
rownames_to_column(var = 'FeatureID') %>%
left_join(pvalues, by = 'FeatureID')
diff_table$pval.adj <- p.adjust(diff_table$pval, method = 'fdr')
return(diff_table)
}