Skip to content

Commit

Permalink
Require harmony '1.2.0'
Browse files Browse the repository at this point in the history
- Reenable temporarily broken unit test
- Give helpful error message for outdated harmony version
  • Loading branch information
const-ae committed Jun 19, 2024
1 parent 7614a49 commit a07c4f1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Imports:
MatrixGenerics,
matrixStats,
Rcpp,
harmony (>= 1.0.3),
harmony (>= 1.2.0),
limma,
BiocNeighbors
Suggests:
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ the `group_by` argument for `find_de_neighborhoods`. (thanks Katha for pushing f
* The formula parsing automatically detects global variables and adds them to the
colData. This avoids problems with the random test / training assignment.
* Duplicate column names in colData are now longer allowed.
* Require harmony version >= 1.2.0 (thanks Maija for reporting the problem)


# v1.1
Expand Down
20 changes: 3 additions & 17 deletions R/harmony_wrapper.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ harmony_init <- function(embedding, design_matrix,
sigma <- rep_len(sigma, nclust)
lambda_range = c(0.1, 10)
if(packageVersion("harmony") < "1.2.0"){
harmonyObj <- harmony_new_object()
harmonyObj$setup(
embedding, phi,
sigma, theta, lambda_vec, max.iter.cluster, epsilon.cluster,
epsilon.harmony, nclust, block.size, lambda_range, B_vec, verbose
)
stop("Your 'harmony' version is outdated: ", packageVersion("harmony"), ". Please update to version >= 1.2.0")
}else{
alpha <- 0.2
harmonyObj <- harmony::RunHarmony(embedding, mm_groups, nclust = nclust, max.iter = 0, return_object = TRUE, verbose = FALSE)
Expand All @@ -53,23 +48,14 @@ harmony_init <- function(embedding, design_matrix,
#' @keywords internal
harmony_new_object <- function(){
Y <- randn(3, 100)
harmonyObj <- if(utils::packageVersion("harmony") >= "1.0.3"){
# Harmony ignores 'verbose = FALSE'
harmony::RunHarmony(Y, rep(c("a", "b"), length.out = 100), nclust = 2, max.iter = 0, return_object = TRUE, verbose = FALSE)
}else{
harmony::HarmonyMatrix(Y, rep(c("a", "b"), length.out = 100), do_pca = FALSE, nclust = 2, max.iter.harmony = 0, return_object = TRUE)
}
harmony::RunHarmony(Y, rep(c("a", "b"), length.out = 100), nclust = 2, max.iter = 0, return_object = TRUE, verbose = FALSE)
harmonyObj
}

harmony_init_clustering <- function(harmonyObj, iter.max = 25, nstart = 10){
stopifnot(is(harmonyObj, "Rcpp_harmony"))
harmonyObj$Y <- t(stats::kmeans(t(harmonyObj$Z_cos), centers = harmonyObj$K, iter.max = iter.max, nstart = nstart)$centers)
if(utils::packageVersion("harmony") <= "1.1.0"){
harmonyObj$init_cluster_cpp(0)
}else{
harmonyObj$init_cluster_cpp()
}
harmonyObj$init_cluster_cpp()
harmonyObj
}

Expand Down
13 changes: 9 additions & 4 deletions tests/testthat/test-align.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ test_that("check that aligning points works perfectly for low number of points",


test_that("check that harmony alignment works as expected", {
skip("Harmony v1.1.0 has a regression about small input matrices")
set.seed(1)
n_genes <- 10
n_emb <- 2
Expand All @@ -127,16 +126,22 @@ test_that("check that harmony alignment works as expected", {
alignment_design = NULL, alignment_design_matrix = design_matrix,
use_assay = "foo", is_test_data = rep(FALSE, ncol(mat)))
gr <- rep(seq_len(n_points), times = 2)
fit_al2 <- align_harmony(fit, design = fit$alignment_design_matrix, nclust = n_points, ridge_penalty = 1e-3, verbose = FALSE)
suppressWarnings({
fit_al2 <- align_harmony(fit, design = fit$alignment_design_matrix, nclust = n_points, ridge_penalty = 1e-3, verbose = FALSE)
})
set.seed(1)
harm <- t(harmony::RunHarmony(mat, meta_data = df, vars_use = "tmp", nclust = n_points, lambda = 1e-8, verbose = FALSE))
suppressWarnings({
harm <- t(harmony::RunHarmony(mat, meta_data = df, vars_use = "tmp", nclust = n_points, lambda = 1e-8, verbose = FALSE))
})

expect_equal(fit_al2$embedding[,df$tmp == "a"], fit_al2$embedding[,df$tmp == "b"], tolerance = 1e-2)
expect_equal(harm[,df$tmp == "a"], harm[,df$tmp == "b"], tolerance = 1e-2)

# Reimplement harmony correction
set.seed(1)
harm_obj <- harmony_init(mat, design_matrix, nclust = n_points, lambda = 1e-8, verbose = FALSE)
suppressWarnings({
harm_obj <- harmony_init(mat, design_matrix, nclust = n_points, lambda = 1e-8, verbose = FALSE)
})
harm_obj <- harmony_max_div_clustering(harm_obj)
Z_corr <- harm_obj$Z_orig
for(k in seq_len(harm_obj$K)){
Expand Down

0 comments on commit a07c4f1

Please sign in to comment.