From 300492ad241d1d05a3627e2c736afb4b1d5a65ec Mon Sep 17 00:00:00 2001 From: Muluh <127390183+0xMuluh@users.noreply.github.com> Date: Sun, 22 Nov 2026 13:46:55 +0200 Subject: [PATCH] getDissimilarity: overlap returns 1 - overlap (#787) --- NEWS | 1 + R/addDissimilarity.R | 12 ++++++++---- R/calculateOverlap.R | 4 ++-- man/getDissimilarity.Rd | 12 ++++++++---- tests/testthat/test-5calculateOverlap.R | 25 +++++++++++++------------ 5 files changed, 32 insertions(+), 22 deletions(-) diff --git a/NEWS b/NEWS index df4e1c942..8d018a925 100644 --- a/NEWS +++ b/NEWS @@ -238,3 +238,4 @@ 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) diff --git a/R/addDissimilarity.R b/R/addDissimilarity.R index 860a4041e..7f3ec1e77 100644 --- a/R/addDissimilarity.R +++ b/R/addDissimilarity.R @@ -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). @@ -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 diff --git a/R/calculateOverlap.R b/R/calculateOverlap.R index 1d264708e..7e08945aa 100644 --- a/R/calculateOverlap.R +++ b/R/calculateOverlap.R @@ -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) } diff --git a/man/getDissimilarity.Rd b/man/getDissimilarity.Rd index 83f9ed32c..13f9f01ba 100644 --- a/man/getDissimilarity.Rd +++ b/man/getDissimilarity.Rd @@ -141,7 +141,8 @@ Overlap reflects similarity between sample-pairs. When overlap is 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). @@ -169,19 +170,22 @@ library(scater) # 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 diff --git a/tests/testthat/test-5calculateOverlap.R b/tests/testthat/test-5calculateOverlap.R index c200567ac..c906b7a87 100644 --- a/tests/testthat/test-5calculateOverlap.R +++ b/tests/testthat/test-5calculateOverlap.R @@ -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) }) -