From 208a246070dfec7a86e2fc4d6d38b61a6a4061e8 Mon Sep 17 00:00:00 2001 From: Nirmayi Date: Wed, 20 Nov 2024 14:18:42 +0100 Subject: [PATCH] minor improvements --- src/helpers/utils.R | 12 +++--- src/metrics/ks_statistic_sc_features/script.R | 38 ++++++------------- src/metrics/ks_statistic_spatial/script.py | 6 +-- 3 files changed, 19 insertions(+), 37 deletions(-) diff --git a/src/helpers/utils.R b/src/helpers/utils.R index b8685cac..7b513d7d 100644 --- a/src/helpers/utils.R +++ b/src/helpers/utils.R @@ -185,15 +185,15 @@ generate_sim_spatialCluster <- function(real_adata, sim_adata){ metadata = sim_adata$obsm )) - sim_sce <- BayesSpace::spatialPreprocess(sim_sce, platform="ST", n.HVGs=2000, log.normalize=FALSE) + sim_sce <- BayesSpace::spatialPreprocess(sim_sce, n.PCs=7, platform="ST", n.HVGs=2000, log.normalize=FALSE) sim_sce <- BayesSpace::spatialCluster(sim_sce, q=max(unique(real_adata$obs[,c("spatial_cluster")])), - platform="ST", - d=7, - init.method="mclust", model="t", gamma=2, - nrep=1000, burn.in=100, - save.chain=TRUE + platform = "ST", + d = 7, + init.method = "mclust", model = "t", gamma = 2, + nrep = 1000, burn.in = 100, + save.chain = TRUE ) sim_cluster <- sim_sce$spatial.cluster return(sim_cluster) diff --git a/src/metrics/ks_statistic_sc_features/script.R b/src/metrics/ks_statistic_sc_features/script.R index 3ca616de..25106336 100644 --- a/src/metrics/ks_statistic_sc_features/script.R +++ b/src/metrics/ks_statistic_sc_features/script.R @@ -8,17 +8,16 @@ requireNamespace("CARD", quietly = TRUE) requireNamespace("SingleCellExperiment", quietly = TRUE) requireNamespace("SummarizedExperiment", quietly = TRUE) - ## VIASH START par <- list( - input_spatial_dataset = "resources_test/spatialsimbench_mobnew/MOBNEW.rds", - input_singlecell_dataset = "resources_test/spatialsimbench_mobnew/MOBNEW_sc.rds", + input_spatial_dataset = "resources_test/spatialsimbench_mobnew/dataset_sp.h5ad", + input_singlecell_dataset = "resources_test/spatialsimbench_mobnew/dataset_sc.h5ad", input_simulated_dataset = "resources_test/spatialsimbench_mobnew/simulated_dataset.h5ad", output = "output.h5ad" ) meta <- list( name = "ks_statistic", - resources_dir = "target/executable/metrics/ks_statistics_scFeatures" + resources_dir = "target/executable/metrics/ks_statistic_sc_features" ) ## VIASH END @@ -28,26 +27,11 @@ input_real_sp <- anndata::read_h5ad(par$input_spatial_dataset) input_sc <- anndata::read_h5ad(par$input_singlecell_dataset) input_simulated_sp <- anndata::read_h5ad(par$input_simulated_dataset) - -cat("Reading input files\n") -real_sce <- scater::logNormCounts(SingleCellExperiment::SingleCellExperiment( - list(counts = Matrix::t(input_real_sp$layers[["counts"]])), - colData = input_real_sp$obs, - metadata = input_real_sp$obsm -)) - -real_log_count <- SummarizedExperiment::assay(real_sce, "logcounts") -real_prob_matrix <- real_sce@metadata$celltype_prop +real_log_count <- t(input_real_sp$layers[["logcounts"]]) +real_prob_matrix <- input_real_sp$obsm[["celltype_proportions"]] colnames(real_prob_matrix) <- paste0("ct", seq_len(ncol(real_prob_matrix))) -rownames(real_prob_matrix) <- colnames(real_log_count) -sim_sce <- scater::logNormCounts(SingleCellExperiment::SingleCellExperiment( - list(counts = Matrix::t(input_simulated_sp$layers[["counts"]])), - colData = input_simulated_sp$obs, - metadata = input_simulated_sp$obsm -)) - -sim_log_count <- SummarizedExperiment::assay(sim_sce, "logcounts") +sim_log_count <- scuttle::normalizeCounts(t(input_simulated_sp$layers[["counts"]])) # build cell type deconvolution in simulated data sim_prob_matrix <- CARD_processing(input_real_sp, input_sc) @@ -56,19 +40,21 @@ feat_types <- c("L_stats", "celltype_interaction", "nn_correlation", "morans_I") real_scfeatures_result <- scFeatures::scFeatures(real_log_count, sample = rep("sample1", ncol(real_log_count)), - spatialCoords = list(SingleCellExperiment::colData(real_sce)$row,SingleCellExperiment::colData(real_sce)$col), + spatialCoords = input_real_sp$obs[c("row", "col")], feature_types = feat_types, type = "spatial_t", species = sc_species, - spotProbability = t(real_prob_matrix)) + spotProbability = t(real_prob_matrix) +) sim_scfeatures_result <- scFeatures::scFeatures(sim_log_count, sample = rep("sample1", ncol(sim_log_count)), - spatialCoords = list(SingleCellExperiment::colData(sim_sce)$row,SingleCellExperiment::colData(sim_sce)$col), + spatialCoords = input_simulated_sp$obs[c("row", "col")], feature_types = feat_types, type = "spatial_t", species = sc_species, - spotProbability = t(sim_prob_matrix)) + spotProbability = t(sim_prob_matrix) +) ks_statistic_L_stats <- ks::kde.test(x1 = as.numeric(real_scfeatures_result$L_stats), x2 = as.numeric(sim_scfeatures_result$L_stats)) ks_statistic_celltype_interaction <- ks::kde.test(x1 = as.numeric(real_scfeatures_result$celltype_interaction), x2 = as.numeric(sim_scfeatures_result$celltype_interaction)) diff --git a/src/metrics/ks_statistic_spatial/script.py b/src/metrics/ks_statistic_spatial/script.py index 890f4705..70693c6b 100644 --- a/src/metrics/ks_statistic_spatial/script.py +++ b/src/metrics/ks_statistic_spatial/script.py @@ -19,17 +19,13 @@ print('Reading input files', flush=True) input_spatial_dataset = ad.read_h5ad(par['input_spatial_dataset']) -# input_singlecell_dataset = ad.read_h5ad(par['input_singlecell_dataset']) input_simulated_dataset = ad.read_h5ad(par['input_simulated_dataset']) def get_spatial_network(num_sample=None, spatial=None, radius=None, coord_type="grid", n_rings=2, set_diag=False): spatial_adata = ad.AnnData(np.empty((num_sample, 1), dtype="float32")) spatial_adata.obsm["spatial"] = spatial - # sq.gr.spatial_neighbors(spatial_adata, n_rings=n_rings, coord_type=coord_type, n_neighs=n_neighs, radius=radius,set_diag =set_diag) - sq.gr.spatial_neighbors(spatial_adata, n_rings=n_rings, coord_type=coord_type, radius=radius, set_diag=set_diag, - delaunay=True) + sq.gr.spatial_neighbors(spatial_adata, n_rings=n_rings, coord_type=coord_type, radius=radius, set_diag=set_diag, delaunay=True) sn = spatial_adata.obsp["spatial_connectivities"] - return sn