unifrac
calculates the unweighted UniFrac distance between
communities.
unifrac(x, phy)
a community matrix, i.e. an object of class matrix or Matrix, or an object of class phyloseq.
a phylogenetic tree (object of class phylo).
a dist object.
Lozupone C, Knight R. (2005) UniFrac: a new phylogenetic method for comparing microbial communities. Appl Environ Microbiol. 71 (12):8228--35. BMC Bioinformatics 7:371.
tree <- ape::read.tree(text ="((t1:1,t2:1)N2:1,(t3:1,t4:1)N3:1)N1;")
com <- Matrix::sparseMatrix(c(1,3,4,1,4,5,1,2,3,4,5,6,3,4,6),
c(1,1,1,2,2,2,3,3,3,3,3,3,4,4,4),x=1,
dimnames = list(paste0("g", 1:6), tree$tip.label))
unifrac(com, tree)
#> g1 g2 g3 g4 g5
#> g2 0.6000000
#> g3 0.3333333 0.6000000
#> g4 0.1666667 0.6666667 0.1666667
#> g5 0.2000000 0.5000000 0.5000000 0.3333333
#> g6 0.6666667 0.3333333 0.4000000 0.5000000 0.6000000