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