Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sainirmayi committed Nov 20, 2024
1 parent 1d1ffcc commit 208a246
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 37 deletions.
12 changes: 6 additions & 6 deletions src/helpers/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 12 additions & 26 deletions src/metrics/ks_statistic_sc_features/script.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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))
Expand Down
6 changes: 1 addition & 5 deletions src/metrics/ks_statistic_spatial/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 208a246

Please sign in to comment.