Scalable discovery of spatial multicellular patterns via neighborhood-to-sequence transformation.
The 1 match
- [1] § Methods › Spatial annotation of hepatic cell types and vascular structures in the MERFISH Liver Atlas ↔ code/simu_utils.R, lines 302–329 · score 0.54 · cell populations, spatial transcriptomic, inferred, gene, tissue
Paper
Loaded from Europe PMC by your browser, not stored by OSCR: doi.org · Europe PMC
The paper is loaded when this pane is shown.
The authors' code
R · 677 lines · 21 KB · no license · 1 match
- # Author: Zheng Li
- # Date: 2021-05-19
- # Purpose:
- # This script includes all functions to conduct the simulation study for BASS
- suppressPackageStartupMessages({
- library(BASS)
- library(Giotto)
- library(BayesSpace)
- library(Seurat)
- library(SC3)
- library(GapClust)
- library(mclust)
- library(tidyverse)
- library(scater)
- library(gtools)
- library(splatter)
- library(reticulate)
- library(CVXR)
- })
- #' Concatenate two strings
- "%&%" <- function(x, y) paste0(x, y)
- #' Cluster cells with Seurat
- seu_cluster <- function(sim_dat, C, resolutions = seq(0.1, 4, by = 0.1))
- {
- cnts <- do.call(cbind, sim_dat[[1]])
- colnames(cnts) <- "Cell" %&% 1:ncol(cnts)
- seu <- CreateSeuratObject(counts = cnts, min.cells = 1, min.features = 1)
- seu <- NormalizeData(seu, verbose = F)
- seu <- ScaleData(seu, features = rownames(seu), verbose = F)
- seu <- RunPCA(seu, features = rownames(seu), verbose = F)
- seu <- FindNeighbors(seu, dims = 1:20, verbose = F)
- seu <- FindClusters(seu, resolution = resolutions, verbose = F)
- kest <- sapply("RNA_snn_res." %&% resolutions, function(res){
- length(unique(seu[[res, drop = T]]))
- })
- out_res <- names(which.min(abs(kest - C)))
- c_est <- as.numeric(as.character(seu[[out_res, drop = T]])) + 1
- # evaluation
- ctrue <- unlist(sim_dat[[2]])
- ari <- eval_ARI(c_est, ctrue)
- F1 <- eval_F1(c_est, ctrue)
- MCC <- eval_MCC(c_est, ctrue)
- C_est <- length(unique(c_est))
- clust_props <- calc_clust_prop(c_est, ctrue)
- list(metric = c(ari, F1, MCC), C_est = C_est, clust_props = clust_props)
- }
- #' Cluster cells with SC3
- sc3_cluster <- function(sim_dat, C, seed = 0)
- {
- set.seed(seed)
- cnts <- do.call(cbind, sim_dat[[1]])
- x <- scater::normalizeCounts(cnts, log = TRUE)
- sce <- SingleCellExperiment(assays = list(counts = cnts, logcounts = x))
- rowData(sce)$feature_symbol <- rownames(sce)
- sce <- tryCatch({
- sc3(sce, ks = C, n_cores = 1)},
- error = function(e){
- print(e)
- return("error")}
- )
- if(is.character(sce)){
- out <- list(metric = rep("error", 9), C_est = "error",
- clust_props = "error")
- } else{
- if(ncol(x) > 5000) sce <- sc3_run_svm(sce, ks = C)
- c_est <- colData(sce)[, "sc3_" %&% C %&% "_clusters"]
- ctrue <- unlist(sim_dat[[2]])
- ari <- eval_ARI(c_est, ctrue)
- F1 <- eval_F1(c_est, ctrue)
- MCC <- eval_MCC(c_est, ctrue)
- C_est <- length(unique(c_est))
- clust_props <- calc_clust_prop(c_est, ctrue)
- out <- list(metric = c(ari, F1, MCC), C_est = C_est,
- clust_props = clust_props)
- }
- return(out)
- }
- #' Cluster cells with FICT
- #' FICT input files:
- #' 1.expression, NxJ
- #' 2.coordinates,
- fict_cluster <- function(sim_dat, xy, C, case, rep)
- {
- # The following environmental variables need to be set
- # to run FICT successfully
- PYTHONPATH <- paste(
- "/net/mulan/home/zlisph/BASS_pjt/9_FICT/FICT-SAMPLE/FICT/",
- "/net/mulan/home/zlisph/BASS_pjt/9_FICT/FICT-SAMPLE/GECT/", sep = ":")
- Sys.setenv(PYTHONPATH = PYTHONPATH)
- Sys.setenv(MPLBACKEND = 'Agg')
- fict <- "~/softwares/miniconda3/bin/fict"
- tmp_fder <- "~/BASS_pjt/2_simu/fict_tmp/case" %&% case %&% "/rep" %&% rep
- if(!file.exists(tmp_fder)) dir.create(tmp_fder, recursive = T)
- # prepare input
- # 1.coordinates
- write.table(xy, file = tmp_fder %&% "/simu.coordinates",
- col.names = F, quote = F)
- # 2.expression
- cnts <- do.call(cbind, sim_dat[[1]])
- ge <- scater::normalizeCounts(cnts, log = TRUE)
- ge <- t(ge) # ncell x ngene
- write.table(ge, file = tmp_fder %&% "/simu.expression",
- col.names = F, quote = F)
- # run FICT
- # Note:
- # by default hidden = 20, size of denoise auto-encoder
- cmd <- paste(fict, "-p", tmp_fder %&% "/simu", "-o",
- tmp_fder %&% "/out/", "--n_type", C)
- system(cmd, ignore.stdout = T, ignore.stderr = T)
- # evaluate
- # occasionally have error: numpy.linalg.LinAlgError: Internal Error.
- c_est <- tryCatch({
- read.table(tmp_fder %&% "/out/cluster_result.csv")
- }, error = function(e) e)
- if(!is.data.frame(c_est)){
- out <- list(metric = rep("error", 9), C_est = "error",
- clust_props = "error")
- } else{
- c_est <- c_est$V1+1
- ctrue <- unlist(sim_dat[[2]])
- ari <- eval_ARI(c_est, ctrue)
- F1 <- eval_F1(c_est, ctrue)
- MCC <- eval_MCC(c_est, ctrue)
- C_est <- length(unique(c_est))
- clust_props <- calc_clust_prop(c_est, ctrue)
- out <- list(metric = c(ari, F1, MCC), C_est = C_est,
- clust_props = clust_props)
- }
- # remove tmp files
- unlink(tmp_fder, recursive = T)
- return(out)
- }
- #' Match spatial domain labels and cell type labels
- #' in the estimated proportion matrix (pi_est) with
- #' labels in the true proportion matrix
- match_pi <- function(pi_est, pi_true, z_est, z_true)
- {
- C <- nrow(pi_true)
- R <- ncol(pi_true)
- # 1. Find spatial domain label correspondence
- perms_R <- permutations(n = R, r = R)
- accur <- rep(NA, nrow(perms_R))
- for(i in 1:nrow(perms_R))
- {
- perm <- perms_R[i, ]
- z_true_perm <- case_when(
- z_true == 1 ~ perm[1],
- z_true == 2 ~ perm[2],
- z_true == 3 ~ perm[3],
- z_true == 4 ~ perm[4])
- accur[i] <- mean(z_est == z_true_perm)
- }
- perm_final_R <- perms_R[which.max(accur), ]
- # 2. Match spatial domain label in pi_est
- pi_est_match <- pi_est[, perm_final_R]
- # 3. Match cell type label
- perms_C <- permutations(n = C, r = C)
- sse <- rep(NA, nrow(perms_C))
- for(i in 1:nrow(perms_C))
- {
- perm <- perms_C[i, ]
- sse[i] <- sum((pi_est_match[perm, ] - pi_true)^2)
- }
- perm_final_C <- perms_C[which.min(sse), ]
- pi_est_match <- pi_est_match[perm_final_C, ]
- out <- list(
- pi = pi_est_match,
- perm_K = perm_final_R,
- perm_C = perm_final_C)
- }
- #' Map spatial domain label to cell types in that domain
- #' 1 -> (1, 2, 3), 2 -> (2, 3, 4)
- #' 3 -> (3, 4, 1), 4 -> (4, 1, 2)
- map_z2c <- function(z)
- {
- case_when(
- z == 1 ~ c(1, 2, 3),
- z == 2 ~ c(2, 3, 4),
- z == 3 ~ c(3, 4, 1),
- z == 4 ~ c(4, 1, 2)
- )
- }
- #' Produce the confusion matrix
- #' One difficulty for constructing the confusion matrix is that
- #' the labeling from the clustering and truth may not match.
- #' We get around this difficulty by identifying the confusion matrix
- #' such that the number of true positives (sum of diagonal values)
- #' is maximized.
- #' Note the number of clusters in est_labels needs to be smaller than
- #' or equal to the number of clusters in true_labels
- confusion <- function(est_labels, true_labels)
- {
- C <- length(unique(true_labels))
- est_labels <- factor(est_labels, 1:C)
- A <- matrix(table(est_labels, true_labels), C, C)
- P <- Variable(C, C, boolean = T) # row permutation matrix
- Y <- Variable(C, C)
- problem <- Problem(Maximize(matrix_trace(Y)), list(Y == P %*% A,
- sum_entries(P, axis = 1) == 1, sum_entries(P, axis = 2) == 1))
- result <- solve(problem)
- P <- result$getValue(P)
- confusion_mtx <- P %*% A
- }
- #' Evaluate the F1-score
- #' Refer to paper Chicco and Jurman, BMC Genomics, 2020
- eval_F1 <- function(est_labels, true_labels)
- {
- C <- length(unique(true_labels))
- confusion_mtx <- suppressMessages(confusion(est_labels, true_labels))
- TP <- diag(confusion_mtx)
- FP <- apply(confusion_mtx, 1, sum) - diag(confusion_mtx)
- FN <- apply(confusion_mtx, 2, sum) - diag(confusion_mtx)
- F1 <- 2 * TP / (2 * TP + FP + FN)
- c(all_F1 = mean(F1), major_F1 = mean(F1[1:4]), rare_F1 = mean(F1[5:C]))
- }
- #' Evaluate Matthew correlation coefficient (MCC) score
- #' Refer to paper Chicco and Jurman, BMC Genomics, 2020
- eval_MCC <- function(est_labels, true_labels)
- {
- C <- length(unique(true_labels))
- confusion_mtx <- suppressMessages(confusion(est_labels, true_labels))
- TP <- diag(confusion_mtx)
- FP <- apply(confusion_mtx, 1, sum) - diag(confusion_mtx)
- FN <- apply(confusion_mtx, 2, sum) - diag(confusion_mtx)
- TN <- sum(confusion_mtx) - TP - FP - FN
- MCC <- (TP * TN - FP * FN) /
- sqrt((TP + FP) * (TP + FN) * (TN + FP) * (TN + FN))
- MCC[is.na(MCC)] <- 0
- c(all_MCC = mean(MCC), major_MCC = mean(MCC[1:4]), rare_MCC = mean(MCC[5:C]))
- }
- #' Evaluate the adjusted Random Index(ARI)
- eval_ARI <- function(est_labels, true_labels)
- {
- major_cells <- true_labels %in% 1:4
- rare_cells <- !major_cells
- all_ari <- adjustedRandIndex(est_labels, true_labels)
- major_ari <- adjustedRandIndex(
- est_labels[major_cells], true_labels[major_cells])
- rare_ari <- adjustedRandIndex(
- est_labels[rare_cells], true_labels[rare_cells])
- c(all_ari = all_ari, major_ari = major_ari, rare_ari = rare_ari)
- }
- #' Reorder the labeling of cell type clusters/spatial domains to
- #' cell types/spatial domains 1, 2, 3, 4 and then order the remaining
- #' redundant clusters/domains based on their cluster/domain size from
- #' large to small. Finally, calculate the proportion. Note this quantity
- #' is specifically used for evaluation in the case of a mis-specified
- #' number of cell types/spatial domains.
- calc_clust_prop <- function(est_labels, true_labels)
- {
- C <- length(unique(true_labels))
- C_est <- length(unique(est_labels))
- C_min <- min(C, C_est)
- C_max <- max(C, C_est)
- mtx <- matrix(table(est_labels, true_labels), C_est, C)
- P <- Variable(C_est, C_est, boolean = T) # row permutation matrix
- Y <- Variable(C_est, C)
- problem <- suppressMessages(Problem(
- Maximize(matrix_trace(Y[1:C_min, 1:C_min])),
- list(Y == P %*% mtx, sum_entries(P, axis = 1) == 1,
- sum_entries(P, axis = 2) == 1)))
- result <- suppressMessages(solve(problem))
- P <- result$getValue(P)
- mtx <- P %*% mtx
- order_idx <- order(apply(mtx, 1, sum)[-c(1:C_min)], decreasing = T)
- mtx <- mtx[c(1:C_min, order_idx+C_min), , drop = F]
- props <- apply(mtx, 1, sum) / sum(mtx)
- names(props) <- 1:C_est
- paste(props, collapse = ",")
- }
- #' Generate simulated spatial transcriptomic data with splatter package
- #'
- #' Spatial domain labels are based on STARmap (20180417_BZ5_control) data
- #' and annotated based on the marker genes. Spatial domain labels for
- #' section 1 to section 9 are generated based on the annotated labels.
- #'
- #' @param starmap STARmap data for inferring simulation parameters and for
- #' providing realistic cell coordinates and spatial domains.
- #' @param scenario Simulation scenario (refer to the manuscript for details)
- #' @param rare_dist Rare cell types either randomly ditributed across the entire
- #' tissue section or located in certain spatial domians.
- #' @param C Number of cell types that consist of 4 major cell types and (C-4)
- #' rare cell types. Rare cell types together consist of 30% of the total cell
- #' population and randomly distribued across the entire tissue region.
- #' Each rare cell type consist of 30%/(C-4) of the total cell population.
- #' @param J Number of genes.
- #' @param I Number of randomly selected genes among the J genes to remain in
- #' the final dataset.
- #' @param L Number of tissue sections.
- #' @param batch_facLoc Batch factor location (refer to the splatter package).
- #' Zero if there is no batch effect.
- #' @param de_prop Probability that a gene will be selected to be differentially
- #' expressed for each cell type (refer to splatter package).
- #' @param de_facLoc DE factor location (refer to splatter package).
- #' @param de_facScale DE factor scale (refer to splatter package).
- #' @param sim_seed Random seed.
- #' @param debug Output DE genes for each cell type.
- simu <- function(
- starmap,
- scenario,
- rare_dist = c("random", "spatial"),
- C,
- J,
- I = NULL,
- L,
- batch_facLoc,
- de_prop,
- de_facLoc,
- de_facScale,
- sim_seed,
- debug = FALSE
- )
- {
- cnts <- starmap$cnts
- info <- starmap$info
- N <- nrow(info)
- init_params <- splatEstimate(cnts[, which(info$c == "eL2/3")])
- # reproduce randomness in cell type assignment and
- # count data selection.
- set.seed(sim_seed)
- # 1.simulate count data
- noBatch <- ifelse(batch_facLoc == 0, TRUE, FALSE)
- group_prob <- if(C == 4){
- rep(0.25, 4)
- } else if(C > 4){
- c(rep(0.7 / 4, 4), rep(1 / (C - 4), C - 4) * 0.3)
- }
- params <- setParams(
- init_params,
- batchCells = rep(3 * N, L), # 3N here represents a large number such that
- # we have sufficient cells of each type to be
- # allocated to the spatial transcriptomics data
- batch.rmEffect = noBatch,
- batch.facLoc = batch_facLoc,
- nGenes = J,
- group.prob = group_prob,
- out.prob = 0,
- de.prob = de_prop,
- de.facLoc = de_facLoc,
- de.facScale = de_facScale,
- seed = sim_seed)
- sim_groups <- splatSimulate(
- params = params,
- method = "groups",
- verbose = FALSE)
- if(!is.null(I)){
- # down-sample J genes to I genes
- keep_idx <- sort(sample(1:J, I, replace = F))
- sim_groups <- sim_groups[keep_idx, ]
- } else{
- I <- J
- }
- # remove cells having no expressed genes
- idx_zerosize <- apply(counts(sim_groups), MARGIN = 2, sum) == 0
- sim_groups <- sim_groups[, !idx_zerosize]
- # 2.parse proportion of cells types in each spatial domain
- if(scenario == 2){
- prop <- c(0.8, 0.1, 0.1)
- } else if(scenario == 3){
- prop <- c(0.5, 0.25, 0.25)
- } else if(scenario == 4){
- prop <- rep(1, 3) / 3
- }
- c_simu <- list()
- sim_cnt <- list()
- for(l in 1:L)
- {
- # 3.set spatial domain labels
- if(l == 1){
- ztrue <- info$z
- } else{
- ztrue <- info[["slice" %&% (l-1)]]
- }
- # 4.generate cell types
- c_simu[[l]] <- rep(NA, length(ztrue))
- if(scenario == 1){
- c_simu[[l]] <- ztrue
- } else if(scenario %in% c(2, 3, 4)){
- for(z in unique(info$z))
- {
- zi_idx <- ztrue == z
- c_simu[[l]][zi_idx] <- sample(map_z2c(z), sum(zi_idx), prob = prop,
- replace = T)
- }
- } else if(scenario == 5){ # more challenging scenario with rare cell types
- # assign rare cell types
- if(rare_dist[1] == "spatial"){
- rare_idx <- rep(NA, length(ztrue))
- nrare_each <- round(0.3 / (C - 4) * length(ztrue))
- for(c in 5:C)
- {
- repeat{
- zi_idx <- sample(unique(info$z), 1) == ztrue
- zi_noAlloc_idx <- zi_idx & is.na(rare_idx)
- if(sum(zi_noAlloc_idx) >= nrare_each) break
- }
- ci_idx <- sample(which(zi_noAlloc_idx), nrare_each, replace = F)
- c_simu[[l]][ci_idx] <- c
- rare_idx[ci_idx] <- TRUE
- }
- rare_idx[is.na(rare_idx)] <- FALSE
- } else if(rare_dist[1] == "random"){
- rare_idx <- sample(c(TRUE, FALSE), length(ztrue), prob = c(0.3, 0.7),
- replace = T)
- c_simu[[l]][rare_idx] <- sample(5:C, sum(rare_idx), replace = T)
- }
- major_idx <- !rare_idx
- # assign major cell types using proportion of
- # cell types in scenario 3
- for(z in unique(info$z))
- {
- zi_idx <- ztrue == z
- zi_major_idx <- zi_idx & major_idx
- c_simu[[l]][zi_major_idx] <- sample(map_z2c(z), sum(zi_major_idx),
- prob = c(0.5, 0.25, 0.25), replace = T)
- }
- }
- # 5.assign count data
- groups <- as.data.frame(colData(sim_groups)) %>%
- filter(Batch == "Batch" %&% l)
- sim_cnt[[l]] <- array(NA, c(I, N))
- for(c in 1:C)
- {
- c_size <- sum(c_simu[[l]] == c)
- c_cells <- groups$Cell[grepl("Group" %&% c %&% "$", groups$Group)]
- cells_select <- sample(as.character(c_cells), c_size, replace = F)
- sim_cnt[[l]][, c_simu[[l]] == c] <-
- as.matrix(counts(sim_groups)[, cells_select])
- }
- colnames(sim_cnt[[l]]) <- "Cell" %&% 1:N
- rownames(sim_cnt[[l]]) <- "Gene" %&% 1:I
- }
- # return DE genes for each group
- if(debug){
- de_genes <- list()
- for(c in 1:C)
- {
- de_genes[[c]] <- rownames(rowData(sim_groups))[
- rowData(sim_groups)[, "DEFacGroup" %&% c] != 1]
- }
- return(list(sim_cnt, c_simu, sim_seed, de_genes))
- }
- return(list(sim_cnt, c_simu, sim_seed))
- }
- #' Run BASS algorithm
- run_BASS <- function(sim_dat, xy, beta_method, beta, C, R,
- init_method)
- {
- L <- length(sim_dat[[1]])
- xys <- lapply(1:L, function(x) xy)
- sim_cnt <- sim_dat[[1]]
- for(l in 1:L)
- {
- colnames(sim_cnt[[l]]) <- rownames(xys[[l]])
- }
- # run algorithms
- BASS <- createBASSObject(sim_cnt, xys, C = C, R = R,
- init_method = init_method, beta_method = "SW",
- beta = beta, tol = 1e-4, burnin = 2000, nsample = 2000)
- BASS <- BASS.preprocess(BASS)
- BASS <- BASS.run(BASS)
- BASS <- BASS.postprocess(BASS)
- ctrue <- unlist(sim_dat[[2]])
- ztrue <- unlist(info[, 4:(L+3)])
- pi_true <- table(ctrue, ztrue)
- pi_true <- pi_true %*% diag(1 / apply(pi_true, 2, sum))
- c_est <- unlist(BASS@results$c)
- z_est <- unlist(BASS@results$z)
- pi_est <- BASS@results$pi
- # evaluation
- c_ari <- eval_ARI(c_est, ctrue)
- F1 <- eval_F1(c_est, ctrue)
- MCC <- eval_MCC(c_est, ctrue)
- z_ari <- adjustedRandIndex(ztrue, z_est)
- if(C != 4 | R != 4){ # only evaluate in the main simulation
- pi_est <- NA
- mse_pi <- NA
- } else{
- pi_est <- match_pi(pi_est, pi_true, z_est, ztrue)$pi
- mse_pi <- sqrt(sum((pi_est - pi_true)^2))
- }
- C_est <- length(unique(c_est))
- R_est <- length(unique(z_est))
- c_clust_prop <- calc_clust_prop(c_est, ctrue)
- z_clust_prop <- calc_clust_prop(z_est, ztrue)
- output <- list(
- c_ari = c_ari,
- c_F1 = F1,
- c_MCC = MCC,
- C_est = C_est,
- c_clust_prop = c_clust_prop,
- z_ari = z_ari,
- R_est = R_est,
- z_clust_prop = z_clust_prop,
- pi_est = pi_est,
- mse_pi = mse_pi,
- beta = BASS@results$beta)
- return(output)
- }
- #' Run HMRF algorithm
- run_HMRF <- function(sim_dat, xy, ztrue, R, case, rep,
- usePCs = F, SEgenes = NULL, dosearchSEgenes = T)
- {
- sim_cnt <- sim_dat[[1]][[1]]
- J <- nrow(sim_cnt)
- # 0.prepare
- my_python_path <- "/net/mulan/home/zlisph//softwares/miniconda3/bin/python"
- results_folder <- "/net/mulan/home/zlisph/BASS_pjt/2_simu/HMRF_tmp/case" %&%
- case %&% "/rep" %&% rep
- if(!file.exists(results_folder)) dir.create(results_folder, recursive = T)
- instrs <- createGiottoInstructions(python_path = my_python_path)
- # 1.run HMRF
- starmap <- createGiottoObject(
- raw_exprs = as.data.frame(sim_cnt),
- spatial_locs = xy,
- instructions = instrs)
- starmap <- filterGiotto(
- gobject = starmap,
- expression_threshold = 0.5,
- gene_det_in_min_cells = 20,
- min_det_genes_per_cell = 0)
- starmap <- normalizeGiotto(starmap)
- starmap <- addStatistics(starmap)
- starmap <- createSpatialNetwork(starmap, minimum_k = 2)
- # select SE genes using
- if(dosearchSEgenes){
- genes <- binSpect(starmap, bin_method = 'kmeans')$genes
- genes <- if(length(genes) < 100) genes else genes[1:100]
- } else{
- genes <- rownames((starmap@norm_scaled_expr))
- }
- if(usePCs){
- starmap <- Giotto::runPCA(
- gobject = starmap,
- genes_to_use = genes,
- ncp = 20)
- betas <- c(0, 1, 21)
- output_folder <- results_folder %&% "/result_k" %&% R %&% "_pca"
- HMRF_out <- doHMRF(
- gobject = starmap,
- dim_reduction_to_use = "pca",
- dimensions_to_use = 1:20,
- k = R,
- betas = betas,
- output_folder = output_folder,
- overwrite_output = T)
- } else{
- betas <- c(0, 2, 26)
- output_folder <- results_folder %&% "/result_k" %&% R
- HMRF_out <- doHMRF(
- gobject = starmap,
- expression_values = "scaled",
- spatial_genes = genes,
- k = R,
- betas = betas,
- output_folder = output_folder,
- overwrite_output = T)
- }
- # 2.evaluate results
- res_dir <- output_folder %&% "/result.spatial.zscore/k_" %&% R
- allbetas <- seq(betas[1], by = betas[2], length = betas[3])
- ari <- list()
- R_est <- list()
- z_clust_prop <- list()
- for(beta in allbetas)
- {
- beta <- beta %&% ".0"
- probs <- tryCatch(
- read.table(res_dir %&% "/ftest.beta." %&% beta %&% ".unnormprob.txt",
- row.names = 1),
- error = function(e) "error",
- warning = function(w) "warning")
- if(!is.data.frame(probs)){
- out <- list(ari = rep("error", length(allbetas)),
- R_est = rep("error", length(allbetas)),
- z_clust_prop = rep("error", length(allbetas)))
- return(out)
- } else{
- z_est <- apply(probs, MARGIN = 1, which.max)
- ari[[beta]] <- adjustedRandIndex(ztrue, z_est)
- R_est[[beta]] <- length(unique(z_est))
- z_clust_prop[[beta]] <- calc_clust_prop(z_est, ztrue)
- }
- }
- unlink(results_folder, recursive = T)
- out <- list(ari = ari, R_est = R_est, z_clust_prop = z_clust_prop)
- return(out)
- }
- #' Run BayesSpace
- run_BayesSpace <- function(sim_dat, xy, ztrue, R, seed = 0)
- {
- set.seed(seed)
- sim_cnt <- sim_dat[[1]][[1]]
- colnames(xy) <- c("row", "col")
- info <- data.frame(xy)
- sce <- SingleCellExperiment(assays = list(counts = sim_cnt), colData = info)
- sce <- spatialPreprocess(sce, n.PCs = 15, n.HVGs = nrow(sce), log.normalize = T)
- sce <- spatialCluster(sce, q = R, d = 15,
- init.method = "mclust", model = "t",
- nrep = 10000, burn.in = 1000)
- z_est <- colData(sce)$spatial.cluster
- ari <- adjustedRandIndex(ztrue, z_est)
- R_est <- length(unique(z_est))
- z_clust_prop <- calc_clust_prop(z_est, ztrue)
- out <- list(ari = ari, R_est = R_est, z_clust_prop = z_clust_prop)
- return(out)
- }
- #' run_SpGCN
- run_SpaGCN <- function(sim_dat, xy, ztrue, R, p = 0.5)
- {
- sim_cnt <- sim_dat[[1]][[1]]
- xy <- data.frame(xy)
- z_est <- run_SpaGCN_py(t(sim_cnt), xy, R, p)$refined_pred
- ari <- adjustedRandIndex(ztrue, z_est)
- R_est <- length(unique(z_est))
- z_clust_prop <- calc_clust_prop(z_est, ztrue)
- out <- list(ari = ari, R_est = R_est, z_clust_prop = z_clust_prop)
- return(out)
- }
simu_utils.R at commit 81dfe73, no license · at the source
Overview
- Institute of Computing Technology, Chinese Academy of Sciences, Beijing, China
- University of Chinese Academy of Sciences, Beijing, China
- Institute of Science and Technology for Brain-Inspired Intelligence; MOE Key Laboratory of Computational Neuroscience and Brain-Inspired Intelligence; MOE Frontiers Center for Brain Science, Fudan University, Shanghai, China
- Center for Medical Research and Innovation, Shanghai Pudong Hospital, Fudan University Pudong Medical Center, Fudan University, Shanghai, China
Abstract
The abstract is not reproduced here: the paper's license (CC BY-NC-ND) does not allow it. Read it in the paper, at the publisher or on Europe PMC.
Repositories
Its files are read in the Code ↔ Paper reader above, with 1 match between paragraphs and lines of code.
zhengli09/BASS-Analysis
81dfe7353093823eed68f403d9c4033031b73de4, 2 April 2023Availability: 1 check, the latest on 28 September 2026: the link answers
- 28 September 2026: the link answers
43 files
- analysis/
DLPFC.Rmd , R, 109 lines - analysis/
MERFISH.Rmd , R, 193 lines - analysis/
STARmap.Rmd , R, 183 lines - analysis/
about.Rmd , R, 147 lines - analysis/
index.Rmd , R, 58 lines - analysis/
license.Rmd , R, 11 lines - analysis/
simu.Rmd , R, 310 lines - code/
add_simu/ , R, 319 linesnTypes_nDomains/ plot.R - code/
add_simu/ , R, 84 linesnTypes_nDomains/ simu_C.R - code/
add_simu/ , R, 82 linesnTypes_nDomains/ simu_R.R - code/
add_simu/ , R, 68 linesnTypes_nDomains/ simu_R_spaGCN.R - code/
add_simu/ , R, 92 linesrand_exclusion_genes/ plot.R - code/
add_simu/ , R, 86 linesrand_exclusion_genes/ simu_exclude_genes.R - code/
add_simu/ , R, 80 linesrand_exclusion_genes/ simu_exclude_genes_spaGC N.R - code/
add_simu/ , R, 152 linesrare_cell_type/ plot.R - code/
add_simu/ , R, 85 linesrare_cell_type/ simu_rare.R - code/
add_simu/ , R, 70 linesrare_cell_type/ simu_rare_spaGCN.R - code/
run_SpaGCN.py , Python, 55 lines - code/
simu.R , R, 83 lines - code/
simu_SpaGCN.R , R, 66 lines - code/
simu_mult.R , R, 75 lines - code/
simu_utils.R , R, 677 lines, 1 match - code/
viz.R , R, 315 lines - docs/
site_libs/ , JavaScript, 2,363 linesbootstrap-3.3.5/ js/ bootstrap.js - docs/
site_libs/ , JavaScript, 7 linesbootstrap-3.3.5/ js/ bootstrap.min.js - docs/
site_libs/ , JavaScript, 13 linesbootstrap-3.3.5/ js/ npm.js - docs/
site_libs/ , JavaScript, 7 linesbootstrap-3.3.5/ shim/ html5shiv.min.js - docs/
site_libs/ , JavaScript, 8 linesbootstrap-3.3.5/ shim/ respond.min.js - docs/
site_libs/ , JavaScript, 12 linesheader-attrs-2.11/ header-attrs.js - docs/
site_libs/ , JavaScript, 12 linesheader-attrs-2.12.1/ header-attrs.js - docs/
site_libs/ , JavaScript, 12 linesheader-attrs-2.20/ header-attrs.js - docs/
site_libs/ , JavaScript, 12 linesheader-attrs-2.3/ header-attrs.js - docs/
site_libs/ , JavaScript, 2 lineshighlightjs-9.12.0/ highlight.js - docs/
site_libs/ , JavaScript, 5 linesjquery-1.11.3/ jquery.min.js - docs/
site_libs/ , JavaScript, 7,407 linesjquery-3.6.0/ jquery-3.6.0.js - docs/
site_libs/ , JavaScript, 2 linesjquery-3.6.0/ jquery-3.6.0.min.js - docs/
site_libs/ , JavaScript, 6,957 linesjqueryui-1.11.4/ jquery-ui.js - docs/
site_libs/ , JavaScript, 12 linesjqueryui-1.11.4/ jquery-ui.min.js - docs/
site_libs/ , JavaScript, 76 linesnavigation-1.1/ codefolding.js - docs/
site_libs/ , JavaScript, 12 linesnavigation-1.1/ sourceembed.js - docs/
site_libs/ , JavaScript, 141 linesnavigation-1.1/ tabsets.js - docs/
site_libs/ , JavaScript, 1,002 linestocify-1.9.1/ jquery.tocify.js - README.md, Text, 6 lines
zhaofangyuan98/FDPMining
e2cccc564d3acc82c0aeffeb45b8886122dafddd, 7 March 2026Availability: 1 check, the latest on 28 September 2026: the link answers
- 28 September 2026: the link answers
10 files
- DataSets/
read.ipynb , Jupyter, 25 lines - FDPs-mapping/
fdpmapping.py , Python, 116 lines - FDPs-mapping/
utils_fdpmapping.py , Python, 195 lines - Step1_build_transaction/
build_transaction_C1.py , Python, 254 lines - Step1_build_transaction/
build_transaction_C2.py , Python, 254 lines - Step2_Mining/
mining_fpgrowth.py , Python, 70 lines - Step3_visualizaion_patte
rn/ , Python, 108 linesutils_vis.py - Step3_visualizaion_patte
rn/ , Python, 60 linesvisiualization_pattern.p y - tutorial.ipynb, Jupyter, 286 lines
- readme.md, Text, 141 lines
Zenodo 18902032
Availability: 1 check, the latest on 28 September 2026: the link answers (HTTP 200)
- 28 September 2026: the link answers (HTTP 200)
10 files
- DataSets/
read.ipynb , Jupyter, 25 lines - FDPs-mapping/
fdpmapping.py , Python, 116 lines - FDPs-mapping/
utils_fdpmapping.py , Python, 195 lines - Step1_build_transaction/
build_transaction_C1.py , Python, 254 lines - Step1_build_transaction/
build_transaction_C2.py , Python, 254 lines - Step2_Mining/
mining_fpgrowth.py , Python, 70 lines - Step3_visualizaion_patte
rn/ , Python, 108 linesutils_vis.py - Step3_visualizaion_patte
rn/ , Python, 60 linesvisiualization_pattern.p y - tutorial.ipynb, Jupyter, 286 lines
- readme.md, Text, 141 lines
Code availability statement
The paper has a code availability statement. Its license (CC BY-NC-ND) does not allow reproducing it here; in short, from what the harvester recognized in it:
- it points to the authors' code: zhaofangyuan98/
FDPMining , zhengli09/BASS-Analysis , Zenodo 18902032 - it says that the code is available on request
Read it in the paper: doi.org/10.1038/s42003-026-09923-1.
Tracing map
Proposed by the machine: these links were found in the paper and verified at the source, without human review. The map will receive a Zenodo DOI once one of the paper's authors has validated it with their ORCID.
What the map holds:
- 3 repositories of the authors' code, each at its verified commit, with its license and how the link was found in the paper;
- 60 scripts, each with its path and the digest of its content;
- 1 match between paragraphs of the paper and lines of the code (method lexical-v1);
- neither the text of the paper nor the code itself.
Its JSON (tracing-map.json) is deposited on Zenodo with its DOI once the map is validated.
Data
Datasets cited
- doi:10.17632/
mpjzbtfgfr.1 , at the source; found in “Data availability” - spacetx.github.io/
data.html , at spacetx.github.io; found in “Data availability” - zenodo:7332091, at Zenodo; found in “Data availability”
Code and data availability statement
The paper has a code and data availability statement. Its license (CC BY-NC-ND) does not allow reproducing it here; in short, from what the harvester recognized in it:
- it points to 3 datasets: DOI 10.17632/
mpjzbtfgfr.1 , spacetx.github.io/data.html , Zenodo 7332091 - it points to the authors' code: zhaofangyuan98/
FDPMining , zhengli09/BASS-Analysis , Zenodo 18902032 - it says that the data are available on request
- it says that the code is available on request
Read it in the paper: doi.org/10.1038/s42003-026-09923-1.
Versions
The history of this record: each version stored by the harvester or made by a correction of its authors or of the maintainers of its code, and what changed in its facts. The texts of the paper (its abstract, its availability statements) are not part of it; versions that changed only those are not listed.
Version 1, 28 September 2026: the first record
Recorded: type, language, journal, volume, issue, pages, dates, 6 authors, 2 keywords, 5 MeSH terms, 1 funder, 91 references.
Cite
This paper
Zhao, F., Wang, S., Wang, Z., Cui, Y., Zhao, Y., & Yuan, Z. (2026). Scalable discovery of spatial multicellular patterns via neighborhood-to-sequence
BibTeX
@article{zhao2026scalabl
author = {Zhao, Fangyuan and Wang, Sheng and Wang, Zhikang and Cui, Yan and Zhao, Yi and Yuan, Zhiyuan},
title = {{Scalable discovery of spatial multicellular patterns via neighborhood-to-sequence
journal = {Communications biology},
year = {2026},
month = apr,
volume = {9},
number = {1},
pages = {725},
publisher = {Nature Publishing Group},
issn = {2399-3642},
doi = {10.1038/
url = {https://
pmid = {41922721},
pmcid = {PMC13216563}
}
RIS
TY - JOUR
AU - Zhao, Fangyuan
AU - Wang, Sheng
AU - Wang, Zhikang
AU - Cui, Yan
AU - Zhao, Yi
AU - Yuan, Zhiyuan
TI - Scalable discovery of spatial multicellular patterns via neighborhood-to-sequence
T2 - Communications biology
J2 - Commun Biol
PY - 2026
DA - 2026/
VL - 9
IS - 1
SP - 725
SN - 2399-3642
PB - Nature Publishing Group
DO - 10.1038/
UR - https://
LA - en
ER -
CSL-JSON
{
"id": "10.1038/
"type": "article-journal",
"title": "Scalable discovery of spatial multicellular patterns via neighborhood-to-sequence
"container-title": "Communications biology",
"author": [
{
"family": "Zhao",
"given": "Fangyuan"
},
{
"family": "Wang",
"given": "Sheng"
},
{
"family": "Wang",
"given": "Zhikang"
},
{
"family": "Cui",
"given": "Yan"
},
{
"family": "Zhao",
"given": "Yi"
},
{
"family": "Yuan",
"given": "Zhiyuan"
}
],
"container-title-short":
"volume": "9",
"issue": "1",
"page": "725",
"DOI": "10.1038/
"PMID": "41922721",
"PMCID": "PMC13216563",
"ISSN": "2399-3642",
"publisher": "Nature Publishing Group",
"URL": "https://
"language": "en",
"issued": {
"date-parts": [
[
2026,
4,
1
]
]
}
}
The tracing map gets a citation of its own once an author has validated it and it has a DOI.
Similar papers
The papers with a page that share the most with this one: the tools found in their code, their categories, datasets, cited references and authors, the rarest counting most.
- [1] doi:10.1038/s41592-026-03194-8 [code]
- Beyond benchmarking: an expert-guided consensus approach to spatially aware clustering.Journal: Nature methodsIn common: anndata, Scanpy, Seurat, 8 other tools, 8 references
- [2] doi:10.21203/rs.3.rs-9676637/v1 [code]
- A Comprehensive Benchmarking of Spatial Deconvolution and Domain Detection Methods across Diverse Tissues and Spatial Transcriptomic TechnologiesJournal: Research Square (preprint)In common: reticulate, anndata, Scanpy, 9 other tools, 5 references
- [3] doi:10.1186/s13073-026-01704-z [code]
- Gene expression profiling enables refined parcellation of cortical layers in the heterogeneous human cerebral cortex.Journal: Genome medicineIn common: anndata, Scanpy, Seurat, 8 other tools, 4 references
- [4] doi:10.1038/s41467-026-71759-4 [code]
- CellNiche represents cellular microenvironments in atlas-scale spatial omics data with contrastive learning.Journal: Nature communicationsIn common: anndata, Scanpy, PyTorch, 4 other tools, 7 references
- [5] doi:10.1038/s41514-026-00391-9 [code]
- Region-specific transcriptional signatures of brain aging in the absence of neuropathology at the single-cell level.Journal: npj agingIn common: anndata, Scanpy, Seurat, 8 other tools, 4 references
- [6] doi:10.1038/s41467-026-71803-3 [code]
- Charting the transition from in vitro gliogenesis to the in vivo maturation of human glial progenitor cells transplanted into the hypomyelinated mouse brain.Journal: Nature communicationsIn common: reticulate, anndata, Scanpy, 9 other tools, 2 references
- [7] doi:10.1016/j.celrep.2026.117073 [code]
- Single-cell epigenomics uncovers heterochromatin instability and transcription factor dysfunction during mouse brain aging.Journal: Cell reportsIn common: reticulate, anndata, Scanpy, 7 other tools, 2 references
- [8] doi:10.1038/s41467-026-68596-w [code]
- Spatial cartography of human thymus enables the geopositioning of lineage transcription factors in rare mimetic thymic epithelial cells.Journal: Nature communicationsIn common: anndata, Scanpy, Seurat, 8 other tools, 2 references
- [9] doi:10.1038/s41586-026-10214-2 [code]
- Multidimensional profiling of heterogeneity in supratentorial ependymomas.Journal: NatureIn common: reticulate, anndata, Scanpy, 8 other tools, 1 reference
- [10] doi:10.1002/imt2.70163 [code]
- Spatial multi-omics unveils sphingolipid metabolic reprogramming within the retinal pathological niche.Journal: iMetaIn common: anndata, Scanpy, Seurat, 7 other tools, 3 references
Contribute
The authors of this paper can claim it, correct its record and validate its tracing map, and the maintainers of its code (its owner, or a public member of its organization) correct what it says of their repository; anyone signed in can ask for its removal. Every request goes to OSCR's own machine, which answers it; your account page follows them.
Sign in with ORCID to claim this paper as one of its authors, correct its record or validate its tracing map: when the paper's metadata lists your ORCID iD, you are recognized at once. Maintainers of its code: sign in with GitHub, then claim the repository on your account page.
Claim this paper
Correct its record
Say what each link of this record is, remove the ones that are not the paper's, add the ones that are missing. The correction becomes a new version of the record, in its Versions section.
Validate its tracing map
You validate the map as this page shows it: 3 repositories of the authors' code, each at its verified commit and with its license, 60 scripts, and 1 match between paragraphs and code (see the Code and Map sections). It then receives a DOI on Zenodo, with you (your ORCID iD) and OSCR as its creators; the code itself is not deposited.
The map's fingerprint: sha256:8d6f0b38498a2840…
Add the badge to its README
The badge links the code to this page. Copy one of these into the README of the paper's code: only you decide where it goes, and nothing is changed for you.
Markdown
[, paste the snippet at the top, then “Commit changes…” and, to review it first, “Create a new branch and start a pull request”. You open the pull request; OSCR asks for no permission.
Request its removal
To ask OSCR to remove this record, the copies of its authors' scripts or its tracing map, use the removal request page: signed in, you say who you are, what to remove and why, then review and confirm the request. Published rules decide every request (how).
Discussion, reproductions, activity
Discussion: questions and error reports about this paper and its code, from signed-in readers and its authors. It opens with sign-in.
Reproductions: reports from readers who ran the authors' code: what they reproduced, with which environment, commit and data. It opens with sign-in.
Activity: what happens around this paper: new versions of its record, its map's validation, discussions and reproductions. It opens with sign-in.
