Skip to content

Commit c678f2e

Browse files
committed
fix: avoid basilisk cluster arg collisions
1 parent a5d88c7 commit c678f2e

2 files changed

Lines changed: 29 additions & 10 deletions

File tree

R/celloracle.R

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,22 @@ RunInSilicoPerturbation <- function(sce, target_gene, perturbation_value = 0.0,
4242

4343
message("Running CellOracle via basilisk...")
4444

45-
co_res <- basilisk::basiliskRun(env = sclet_celloracle_env, fun = function(counts, cl, emb, tgt, pval, grn_f, ...) {
45+
co_res <- basilisk::basiliskRun(
46+
env = sclet_celloracle_env,
47+
fun = function(counts, cluster_labels, emb, tgt, pval, grn_f, reduction_name, ...) {
4648
ad <- reticulate::import("anndata")
4749
pd <- reticulate::import("pandas")
4850
co <- reticulate::import("celloracle")
4951
sc <- reticulate::import("scanpy")
5052

51-
obs <- pd$DataFrame(list(clusters = cl), index = rownames(counts))
53+
obs <- pd$DataFrame(list(clusters = cluster_labels), index = rownames(counts))
5254
adata <- ad$AnnData(X = counts, obs = obs)
53-
adata$obsm[paste0("X_", tolower(reduction))] <- emb
55+
adata$obsm[paste0("X_", tolower(reduction_name))] <- emb
5456

5557
# Preprocessing inside Python
5658
sc$pp$normalize_total(adata, target_sum = 10000)
5759
sc$pp$log1p(adata)
58-
sc$pp$neighbors(adata, use_rep = paste0("X_", tolower(reduction)))
60+
sc$pp$neighbors(adata, use_rep = paste0("X_", tolower(reduction_name)))
5961

6062
# Load base GRN
6163
base_grn <- pd$read_csv(grn_f, index_col = 0L)
@@ -81,7 +83,15 @@ RunInSilicoPerturbation <- function(sce, target_gene, perturbation_value = 0.0,
8183
shift <- oracle$adata$obsm["delta_X"]
8284

8385
return(as.matrix(shift))
84-
}, counts = counts_mat, cl = clusters, emb = emb, tgt = target_gene, pval = perturbation_value, grn_f = base_grn_path, ...)
86+
},
87+
counts = counts_mat,
88+
cluster_labels = clusters,
89+
emb = emb,
90+
tgt = target_gene,
91+
pval = perturbation_value,
92+
grn_f = base_grn_path,
93+
reduction_name = reduction,
94+
...)
8595

8696
# Store the shift vector in reducedDim, e.g., "celloracle_shift"
8797
rownames(co_res) <- colnames(sce)

R/cellrank.R

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,24 @@ RunCellRank <- function(sce, reduction = "PCA", cluster_key = ActiveIdent(sce),
4949

5050
message("Running CellRank via basilisk...")
5151

52-
cr_res <- basilisk::basiliskRun(env = sclet_cellrank_env, fun = function(v, s, u, emb, cl, ...) {
52+
cr_res <- basilisk::basiliskRun(
53+
env = sclet_cellrank_env,
54+
fun = function(v, s, u, emb, cluster_labels, reduction_name, ...) {
5355
ad <- reticulate::import("anndata")
5456
cr <- reticulate::import("cellrank")
5557
pd <- reticulate::import("pandas")
5658

57-
obs <- pd$DataFrame(list(clusters = cl), index = rownames(v))
59+
obs <- pd$DataFrame(list(clusters = cluster_labels), index = rownames(v))
5860
adata <- ad$AnnData(X = s, obs = obs)
5961
adata$layers["spliced"] <- s
6062
adata$layers["unspliced"] <- u
6163
adata$layers["velocity"] <- v
62-
adata$obsm[paste0("X_", tolower(reduction))] <- emb
64+
adata$obsm[paste0("X_", tolower(reduction_name))] <- emb
6365

6466
# We need a neighbor graph and velocity graph.
6567
# Since we just have the matrices, we can use scvelo to compute velocity graph first.
6668
scv <- reticulate::import("scvelo")
67-
scv$pp$neighbors(adata, use_rep = paste0("X_", tolower(reduction)))
69+
scv$pp$neighbors(adata, use_rep = paste0("X_", tolower(reduction_name)))
6870
scv$tl$velocity_graph(adata)
6971

7072
# CellRank pipeline
@@ -84,7 +86,14 @@ RunCellRank <- function(sce, reduction = "PCA", cluster_key = ActiveIdent(sce),
8486
terminal_states = as.character(term_states),
8587
absorption_probs = as.matrix(abs_probs$X)
8688
))
87-
}, v = v_mat, s = s_mat, u = u_mat, emb = emb, cl = clusters, ...)
89+
},
90+
v = v_mat,
91+
s = s_mat,
92+
u = u_mat,
93+
emb = emb,
94+
cluster_labels = clusters,
95+
reduction_name = reduction,
96+
...)
8897

8998
# Store results
9099
sce$cellrank_terminal_states <- cr_res$terminal_states

0 commit comments

Comments
 (0)