Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -238,5 +238,6 @@ Changes in version 1.21.x
+ getPERMANOVA/addPERMANOVA: method = "unifrac" uses the rowTree of a TreeSE; earlier it failed with "argument tree is missing"; unused factor levels are dropped from the covariates, as vegan::adonis2() can crash R with them, e.g. when samples are subsetted for pairwise tests, and a grouping variable with only one group gives an error instead of a warning (1.21.36, 2026-11-21)
+ Vignette: overview of the functions grouped by use case, with examples (2026-11-21)
+ exportMothur: the OTU column of taxonomy.tsv is taken from the row names; with stringr < 1.6.0 the export failed (2026-11-22)
+ getDissimilarity: method = "overlap" returns the dissimilarity 1 - overlap; it returned the overlap similarity, so identical samples had the largest dissimilarity (#787) (2026-11-22)
+ getAlpha/addAlpha: allen and rao placed the tip abundances by table row instead of node, which gave wrong values on most trees (2026-11-22)
+ transformAssay: pseudocount = TRUE (and method = "pseudocount" without a value) is deprecated; the warning gives the value used. A numeric pseudocount on an assay with only positive values is an error instead of a warning, and the error for zeroes suggests a pseudocount (#813) (2026-11-22)
12 changes: 8 additions & 4 deletions R/addDissimilarity.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@
#' calculated using relative abundances, the higher the value the higher the
#' similarity is. When using relative abundances, overlap value 1 means that
#' all the abundances of features are equal between two samples, and 0 means
#' that samples have completely different relative abundances.
#' that samples have completely different relative abundances. The
#' dissimilarity returned for \code{method = "overlap"} is 1 - overlap.
#'
#' Unifrac is calculated with a C++ implementation of the Striped Unifrac
#' algorithm (McDonald et al. 2018).
Expand Down Expand Up @@ -156,19 +157,22 @@
#' # load dataset
#' data(GlobalPatterns)
#' tse <- GlobalPatterns
#' # Overlap and JSD are calculated from relative abundances
#' tse <- transformAssay(tse, method = "relabundance")
#'
#' ### Overlap dissimilarity
#'
#' tse <- addDissimilarity(tse, method = "overlap", detection = 0.25)
#' tse <- addDissimilarity(
#' tse, method = "overlap", assay.type = "relabundance", detection = 0.25)
#' metadata(tse)[["overlap"]]
#'
#' ### JSD dissimilarity
#'
#' tse <- addDissimilarity(tse, method = "jsd")
#' tse <- addDissimilarity(tse, method = "jsd", assay.type = "relabundance")
#' metadata(tse)[["jsd"]]
#'
#' # Multi Dimensional Scaling applied to JSD dissimilarity matrix
#' tse <- addMDS(tse, method = "overlap", assay.type = "counts")
#' tse <- addMDS(tse, method = "jsd", assay.type = "relabundance")
#' reducedDim(tse, "MDS") |> head()
#'
#' ### Unifrac dissimilarity
Expand Down
4 changes: 2 additions & 2 deletions R/calculateOverlap.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
colnames(result) <- colnames(x)
rownames(result) <- colnames(x)

# Convert into distances
result <- stats::as.dist(result)
# Overlap is a similarity; 1 - overlap is the dissimilarity
result <- stats::as.dist(1 - result)
return(result)
}

Expand Down
12 changes: 8 additions & 4 deletions man/getDissimilarity.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 13 additions & 12 deletions tests/testthat/test-5calculateOverlap.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@ test_that("Overlap", {

# Test output
expect_true(class(result) == "dist")
# Test values
# Test values: dissimilarity is 1 - overlap
result <- as.matrix(result)

# Reference
reference <- matrix(c(0.0000000, 0.3799961, 0.4111838, 0.3799961, 0.0000000,
0.3634972, 0.4111838, 0.3634972, 0.0000000), nrow=3)
expect_equal(round(unname(result), 7), round(reference), 7)
overlap <- matrix(c(1, 0.3799961, 0.4111838, 0.3799961, 1,
0.3634972, 0.4111838, 0.3634972, 1), nrow=3)
expect_equal(unname(result), 1 - overlap, tolerance = 1e-6)
# Test with different detection threshold
result <- getDissimilarity(tse, method = "overlap", assay.type = "relabundance", detection = 0)
result <- as.matrix(result)

# Reference
reference <- matrix(c(0.0000000, 0.8811552, 0.9038734, 0.8811552, 0.0000000,
0.8390008, 0.9038734, 0.8390008, 0.00000000), nrow=3)
expect_equal(round(unname(result), 7), round(reference, 7))
overlap <- matrix(c(1, 0.8811552, 0.9038734, 0.8811552, 1,
0.8390008, 0.9038734, 0.8390008, 1), nrow=3)
expect_equal(unname(result), 1 - overlap, tolerance = 1e-6)
# Identical samples have dissimilarity 0
tse2 <- cbind(tse, tse[, 1])
colnames(tse2)[4] <- "copy"
result <- as.matrix(getDissimilarity(
tse2, method = "overlap", assay.type = "relabundance"))
expect_equal(result[1, "copy"], 0)
})

Loading