Chapter 17 Supp table 6
PERMANOVA statistical tests of different comparisons based on Bray-Curtis distances for the Lipid data. PERMANOVA results, 999 permutations, pairwise tests display holm adjusted p-values.
17.1 libraries
17.1.1 Statisitical tests
#load phylosq object
load(file = "./data/lipid_physeq_object")
load(file = "./data/lipid_physeq_1k_object")
#Extract metadata
meta <- as(sample_data(physeq_1k), "data.frame")
#Change Temp.Storage.time values
meta$Temp.Storage.time <- gsub(pattern = "\\|", replacement = "_", meta$Temp.Storage.time)
meta$Temp.Storage.time <- gsub(pattern = " day", replacement = "D", meta$Temp.Storage.time)
#Update metadata with better names and column names
#Calculate bray distance
bray.dist <- as.matrix(phyloseq::distance(physeq, method="bray"))
#Pairwise comparison function
paiwise_function <- function(dist_df, meta_df, meta_column) {
#Combination
cbn <- combn(x = unique(meta_df[,meta_column]), m = 2)
#Create empty final data frame with 4 columns
# and a number of rows equal to the the number of combinations
pairwise_permanova_df <- as.data.frame(matrix(data = NA, nrow = ncol(cbn), ncol = 4))
#Add column names
colnames(pairwise_permanova_df) <- c("X","Y","p","p.adj")
#Loop through the combinations
for(i in 1:ncol(cbn)){
#Subset metadf to contain only our samples of interest
metadf_subset <- meta[meta[,meta_column]%in% cbn[,i],]
#Subset distance matrix
samples_subset <- row.names(metadf_subset)
dist.subset <- dist_df[samples_subset,samples_subset]
#PERMANOVA/ADONIS
f <- paste0("dist.subset~",meta_column)
pairwise_adonis <- vegan::adonis2(formula = formula(f), permutations = 999,
data = metadf_subset, by = "margin")
#Add the group names and p-value to the main data frame
pairwise_permanova_df[i,1:2] <- cbn[,i]
pairwise_permanova_df[i,3] <- pairwise_adonis[1,"Pr(>F)"]
}
#Add adjusted P-values
pairwise_permanova_df$p.adj <- p.adjust(pairwise_permanova_df$p, method = "BH")
#Convert to wide table with adjusted p-values
p_adjust_long <- pairwise_permanova_df[,c(1,2,4)]
p_adjust_wide <- spread(data = p_adjust_long, Y, p.adj, fill=NA)
#Reformat
row.names(p_adjust_wide) <- p_adjust_wide[,1]
p_adjust_wide <- p_adjust_wide[,-1]
return(p_adjust_wide)
}
#PERMANOVA tests
#Patient
patient_permanova <-
vegan::adonis2(bray.dist ~ Sample.number,
data = meta, permutations=999, by = "margin")
#Change a row name
row.names(patient_permanova)[1] <- "Participant number"
#pairwise
patient_pairwise <- paiwise_function(bray.dist, meta, "Sample.number")
#Write out data
write.table("Participant number", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, col.names = FALSE, row.names = FALSE)
write.table(patient_permanova, file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = NA)
write.table("Pairwise", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
write.table(patient_pairwise, file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = NA)
write.table("", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
#Remove results
remove(patient_permanova, patient_pairwise)
#buffer
buffer_permanova <-
vegan::adonis2(bray.dist ~ Buffer.type ,
data = meta, permutations=999, by = "margin")
#Change a row name
row.names(buffer_permanova)[1] <- "Bufferused"
#pairwise
buffer_pairwise <- paiwise_function(bray.dist, meta, "Buffer.type")
#Write out data
write.table("Bufferused", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append=TRUE,col.names = FALSE, row.names = FALSE)
write.table(buffer_permanova, file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = NA)
write.table("Pairwise", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
write.table(buffer_pairwise, file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = NA)
write.table("", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
#Remove results
remove(buffer_permanova, buffer_pairwise)
#temp
temp_permanova <-
vegan::adonis2(bray.dist ~ Temp,
data = meta, permutations=999, by = "margin")
#pairwise
temp_pairwise <- paiwise_function(bray.dist, meta, "Temp")
#Write out data
write.table("Temp", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append=TRUE,col.names = FALSE, row.names = FALSE)
write.table(temp_permanova, file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = NA)
write.table("Pairwise", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
write.table(temp_pairwise, file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = NA)
write.table("", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
#Remove results
remove(temp_permanova, temp_pairwise)
#Storage condition
condition_permanova <-
vegan::adonis2(bray.dist ~ Temp.Storage.time,
data = meta, permutations=999, by = "margin")
#Change a row name
row.names(condition_permanova)[1] <- "Storageconditions"
#pairwise
condition_pairwise <- paiwise_function(bray.dist, meta, "Temp.Storage.time")
#Write out data
write.table("Storageconditions", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append=TRUE,col.names = FALSE, row.names = FALSE)
write.table(condition_permanova, file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = NA)
write.table("Pairwise", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
write.table(condition_pairwise, file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = NA)
write.table("", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
#Remove results
remove(condition_permanova, condition_pairwise)
#Buffer and participant
buffer_patient_permanova <-
vegan::adonis2(bray.dist ~ Buffer.type + Sample.number,
data = meta, permutations=999, by = "margin")
#Change row names
row.names(buffer_patient_permanova)[1:2] <- c("Bufferused","Participant number")
#pairwise
#create new metadata column
meta$Bufferused_and_Patientnumber <- paste0(meta$Buffer.type,"_",meta$Sample.number)
buffer_patient_pairwise <- paiwise_function(bray.dist, meta, "Bufferused_and_Patientnumber")
#Write out data
write.table("Bufferused and Participant number", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append=TRUE,col.names = FALSE, row.names = FALSE)
write.table(buffer_patient_permanova, file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = NA)
write.table("Pairwise", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
write.table(buffer_patient_pairwise, file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = NA)
write.table("", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
#Remove results
remove(buffer_patient_permanova, buffer_patient_pairwise)
#Buffer and temp
buffer_temp_permanova <-
vegan::adonis2(bray.dist ~ Buffer.type + Temp,
data = meta, permutations=999, by = "margin")
#Change row names
row.names(buffer_temp_permanova)[1] <- c("Bufferused")
#pairwise
#create new metadata column
meta$Bufferused_and_Temp <- paste0(meta$Buffer.type,"_",meta$Temp)
buffer_temp_pairwise <- paiwise_function(bray.dist, meta, "Bufferused_and_Temp")
#Write out data
write.table("Bufferused and Temp", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append=TRUE,col.names = FALSE, row.names = FALSE)
write.table(buffer_temp_permanova, file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = NA)
write.table("Pairwise", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
write.table(buffer_temp_pairwise, file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = NA)
write.table("", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
#Remove results
remove(buffer_temp_permanova, buffer_temp_pairwise)
#Temp and Participant
temp_participant_permanova <-
vegan::adonis2(bray.dist ~ Temp + Sample.number,
data = meta, permutations=999, by = "margin")
#Change row names
row.names(temp_participant_permanova)[2] <- c("Participant number")
#pairwise
#create new metadata column
meta$Temp_and_participant <- paste0(meta$Temp,"_",meta$Sample.number)
temp_participant_pairwise <- paiwise_function(bray.dist, meta, "Temp_and_participant")
#Write out data
write.table("Temp and Participant number", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append=TRUE,col.names = FALSE, row.names = FALSE)
write.table(temp_participant_permanova, file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = NA)
write.table("Pairwise", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
write.table(temp_participant_pairwise, file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = NA)
write.table("", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
#Remove results
remove(temp_participant_permanova, temp_participant_pairwise)
#Storage condition and participant
condition_patient_permanova <-
vegan::adonis2(bray.dist ~ Temp.Storage.time + Sample.number,
data = meta, permutations=999, by = "margin")
#Change row names
row.names(condition_patient_permanova)[1:2] <- c("Storageconditions","Participant number")
#Write out data
write.table("Storageconditions and Participant number", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append=TRUE,col.names = FALSE, row.names = FALSE)
write.table(condition_patient_permanova, file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = NA)
write.table("Pairwise PERMANOVA skipped as there would be too few samples per grouping", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
write.table("", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
#Remove results
remove(condition_patient_permanova)
#Buffer, Storage condition and participant
buffer_condition_patient_permanova <-
vegan::adonis2(bray.dist ~ Buffer.type + Temp.Storage.time + Sample.number,
data = meta, permutations=999, by = "margin")
#Change row names
row.names(buffer_condition_patient_permanova)[1:3] <-
c("Bufferused", "Storageconditions","Participant number")
#Write out data
write.table("Bufferused, Storageconditions and Participant number", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append=TRUE,col.names = FALSE, row.names = FALSE)
write.table(buffer_condition_patient_permanova, file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = NA)
write.table("Pairwise PERMANOVA skipped as there would be too few samples per grouping", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
write.table("", file = "./data/permanova_lipid.tsv",
sep = "\t", quote=FALSE, append = TRUE, col.names = FALSE, row.names = FALSE)
#Remove results
remove(buffer_condition_patient_permanova)