Skip to content

Commit 6866e01

Browse files
Merge pull request immunogenomics#156 from mojaveazure/master
Updates to RunHarmony for compatibility with Seurat v5
2 parents ea48a6c + 0c24ddc commit 6866e01

File tree

4 files changed

+56
-57
lines changed

4 files changed

+56
-57
lines changed

DESCRIPTION

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ Package: harmony
22
Title: Fast, Sensitive, and Accurate Integration of Single Cell Data
33
Version: 0.1.0
44
Authors@R: c(
5-
person("Ilya", "Korsunsky", email = "[email protected]",
5+
person("Ilya", "Korsunsky", email = "[email protected]",
66
role = c("cre", "aut"), comment = c(ORCID = "0000-0003-4848-3948")),
7-
person("Nghia", "Millard", email = "[email protected]",
7+
person("Nghia", "Millard", email = "[email protected]",
88
role = "aut", comment = c(ORCID = "0000-0002-0518-7674")),
99
person("Jean", "Fan", email = "[email protected]",
1010
role = c("aut", "ctb"), comment = c(ORCID = "0000-0002-0212-5451")),
1111
person("Kamil", "Slowikowski", email = "[email protected]",
1212
role = c("aut", "ctb"), comment = c(ORCID = "0000-0002-2843-6370")),
13-
person("Miles", "Smith",
13+
person("Miles", "Smith",
1414
role = c("ctb")),
15-
person("Soumya", "Raychaudhuri",
15+
person("Soumya", "Raychaudhuri",
1616
role = c("aut"), comment = c(ORCID = "0000-0002-1901-8265"))
1717
)
1818
Description: Implementation of the Harmony algorithm for single cell integration, described in Korsunsky et al <doi.org/10.1101/461954>. Package includes a standalone Harmony function and interfaces to external frameworks.
@@ -24,18 +24,18 @@ Depends: R(>= 3.4.0), Rcpp
2424
LazyData: true
2525
LinkingTo: Rcpp, RcppArmadillo, RcppProgress
2626
Imports:
27-
dplyr,
27+
dplyr,
2828
cowplot,
2929
tidyr,
30-
ggplot2,
30+
ggplot2,
3131
irlba,
3232
Matrix,
3333
methods,
3434
tibble,
35-
rlang,
36-
SingleCellExperiment
35+
rlang
3736
Suggests:
38-
Seurat,
37+
SingleCellExperiment,
38+
Seurat (>= 4.1.1),
3939
testthat,
4040
knitr,
4141
rmarkdown

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ importFrom(methods,as)
1414
importFrom(methods,is)
1515
importFrom(methods,new)
1616
importFrom(rlang,.data)
17+
importFrom(rlang,`%||%`)
1718
useDynLib(harmony)

R/RunHarmony.R

+36-37
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#' @param reduction.save Keyword to save Harmony reduction. Useful if you want
4040
#' to try Harmony with multiple parameters and save them as e.g.
4141
#' 'harmony_theta0', 'harmony_theta1', 'harmony_theta2'
42-
#' @param assay.use (Seurat V3 only) Which assay to Harmonize with (Default Assay as default).
42+
#' @param assay.use (Seurat V3 only) Which assay to run PCA on if no PCA present?
4343
#' @param ... other parameters
4444
#'
4545
#'
@@ -81,32 +81,30 @@ RunHarmony.Seurat <- function(
8181
project.dim = TRUE,
8282
...
8383
) {
84-
assay.use <- assay.use %||% DefaultAssay(object)
85-
if (reduction == "pca") {
86-
tryCatch(
87-
embedding <- Seurat::Embeddings(object, reduction = "pca"),
88-
error = function(e) {
89-
if (verbose) {
90-
message("Harmony needs PCA. Trying to run PCA now.")
91-
}
92-
tryCatch(
93-
object <- Seurat::RunPCA(
94-
object,
95-
assay = assay.use, verbose = verbose
96-
),
97-
error = function(e) {
98-
stop("Harmony needs PCA. Tried to run PCA and failed.")
99-
}
100-
)
84+
if (!requireNamespace('Seurat', quietly = TRUE)) {
85+
stop("Running Harmony on a Seurat object requires Seurat")
86+
}
87+
assay.use <- assay.use %||% Seurat::DefaultAssay(object)
88+
if (reduction == "pca" && !reduction %in% Seurat::Reductions(object = object)) {
89+
if (isTRUE(x = verbose)) {
90+
message("Harmony needs PCA. Trying to run PCA now.")
91+
}
92+
object <- tryCatch(
93+
expr = Seurat::RunPCA(
94+
object = object,
95+
assay = assay.use,
96+
verbose = verbose,
97+
reduction.name = reduction
98+
),
99+
error = function(...) {
100+
stop("Harmony needs PCA. Tried to run PCA and failed.")
101101
}
102102
)
103-
} else {
104-
available.dimreduc <- names(methods::slot(object = object, name = "reductions"))
105-
if (!(reduction %in% available.dimreduc)) {
106-
stop("Requested dimension reduction is not present in the Seurat object")
107-
}
108-
embedding <- Seurat::Embeddings(object, reduction = reduction)
109103
}
104+
if (!reduction %in% Seurat::Reductions(object = object)) {
105+
stop("Requested dimension reduction is not present in the Seurat object")
106+
}
107+
embedding <- Seurat::Embeddings(object, reduction = reduction)
110108
if (is.null(dims.use)) {
111109
dims.use <- seq_len(ncol(embedding))
112110
}
@@ -118,7 +116,11 @@ RunHarmony.Seurat <- function(
118116
if (length(dims.use) == 1) {
119117
stop("only specified one dimension in dims.use")
120118
}
121-
metavars_df <- Seurat::FetchData(object, group.by.vars)
119+
metavars_df <- Seurat::FetchData(
120+
object,
121+
group.by.vars,
122+
cells = Seurat::Cells(x = object[[reduction]])
123+
)
122124

123125
harmonyEmbed <- HarmonyMatrix(
124126
embedding,
@@ -142,19 +144,16 @@ RunHarmony.Seurat <- function(
142144
reference_values
143145
)
144146

145-
rownames(harmonyEmbed) <- row.names(embedding)
146-
colnames(harmonyEmbed) <- paste0(reduction.save, "_", seq_len(ncol(harmonyEmbed)))
147+
reduction.key <- Seurat::Key(reduction.save, quiet = TRUE)
148+
rownames(harmonyEmbed) <- rownames(embedding)
149+
colnames(harmonyEmbed) <- paste0(reduction.key, seq_len(ncol(harmonyEmbed)))
147150

148-
suppressWarnings({
149-
harmonydata <- Seurat::CreateDimReducObject(
150-
embeddings = harmonyEmbed,
151-
stdev = as.numeric(apply(harmonyEmbed, 2, stats::sd)),
152-
assay = assay.use,
153-
key = reduction.save
154-
)
155-
})
156-
157-
object[[reduction.save]] <- harmonydata
151+
object[[reduction.save]] <- Seurat::CreateDimReducObject(
152+
embeddings = harmonyEmbed,
153+
stdev = as.numeric(apply(harmonyEmbed, 2, stats::sd)),
154+
assay = Seurat::DefaultAssay(object = object[[reduction]]),
155+
key = reduction.key
156+
)
158157
if (project.dim) {
159158
object <- Seurat::ProjectDim(
160159
object,

man/RunHarmony.Rd

+10-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)