0

I'd like to ask how to calculate distances between groups using weighted UniFrac or Bray-Curtis method in R. I already got distances between each sample using UniFrac method.

library("phyloseq")
require(GUniFrac)
require(vegan)
require(ade4)

otu.tab <- t(otu_table(phy))
treefile <- phy_tree(phy)

# calculate the UniFracs
unifracs <- GUniFrac(otu.tab, treefile, alpha = c(0, 0.5, 1))$unifracs

# create
d5 <- unifracs[, , "d_0.5"]   # GUniFrac with alpha 0.5 

The distance result table is:

> d5
           SamA_1    SamA_2    SamB_1    SamB_2    SamC_1    SamC_2        
SamA_1  0.0000000 0.3939102 0.3972185 0.3846277 0.4052247 0.4187168  
SamA_2  0.3939102 0.0000000 0.2986860 0.3003682 0.3384096 0.3475723  
SamB_1  0.3972185 0.2986860 0.0000000 0.3549342 0.2997557 0.3457747  
SamB_2  0.3846277 0.3003682 0.3549342 0.0000000 0.3378881 0.3529470  
SamC_1  0.4052247 0.3384096 0.2997557 0.3378881 0.0000000 0.3828705 
SamC_2  0.4187168 0.3475723 0.3457747 0.3529470 0.3828705 0.0000000  

> sample_data(phy)
         Treat    Groups    
SamA_1     CON         A
SamA_2     CON         A
SamB_1   Treat         B
SamB_2   Treat         B
SamC_1   Treat         C
SamC_2   Treat         C

I'd like to calculate distances between groups A, B, and C, eventually I want a distances table such as "d5".

Which function is proper to get a calculated distances table between groups in R?

1 Answers1

0

Here is a quick way to do this for Bray-Curtis dissimilarity using the mctoolsr package. You should also be able to replace the "dm" object with your UniFrac distance object, but just make sure that when you calculate the UniFrac matrix, the samples are in the same order as your mctoolsr object (i.e. input$map_loaded). Alternatively, you could melt your distance matrix into long format (e.g use reshape2::melt() and then calculate mean and standard errors and eventually graph it, but that is outside the scope of your original question. Here is an example using the mctoolsr tutorial.

# Calculate mean Bray-Curtis dissimilarities in mctoolsr
# See tutorial here https://github.com/leffj/mctoolsr
install.packages("devtools")
devtools::install_github("leffj/mctoolsr")
library(mctoolsr)
tax_table_fp = system.file('extdata', 'fruits_veggies_taxa_table_wTax.biom', 
                           package = 'mctoolsr')
map_fp = system.file('extdata', 'fruits_veggies_metadata.txt', 
                     package = 'mctoolsr')
input = load_taxa_table(tax_table_fp, map_fp)
input_rar = single_rarefy(input, 1000)
dm = calc_dm(input_rar$data_loaded)
dm_mean <- calc_mean_dissimilarities(dm, 
                                     input_rar$map_loaded, 
                                     "Sample_type")
dm_mean