Skip to content

Commit

Permalink
add spatial clustering process in simulated dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
littlecabiria committed Aug 26, 2024
1 parent 90bcfbe commit 97f1e78
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/metrics/downstream/script.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ crosscor_mantel <- generate_mantel(real_moransI, sim_moransI)

cat("spatial clustering evaluation\n")
# TODO
# 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_sce <- scater::logNormCounts(SingleCellExperiment::SingleCellExperiment(
list(counts = Matrix::t(input_simulated_sp$layers[["counts"]])),
colData = input_simulated_sp$obs,
metadata = input_simulated_sp$obsm
))

# generate the simulated clustering result first by BayersSpace
# sim_sce <- BayesSpace::spatialPreprocess(sim_sce, platform=par$plat,
Expand Down
3 changes: 1 addition & 2 deletions src/metrics/ks_statistic_spatial/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ def get_trans(adata=None, ct=None):

# transition matrix
real = np.array(input_spatial_dataset.obs['spatial_cluster'].values.tolist())
# sim = np.array(input_simulated_dataset.obs['spatial_cluster'].values.tolist())
sim <- generate_sim_spatialCluster(input_spatial_dataset, input_simulated_dataset)
sim = np.array(input_simulated_dataset.obs['spatial_cluster'].values.tolist())


transition_matrix_real = get_trans(adata=input_spatial_dataset, ct=real)
Expand Down
55 changes: 55 additions & 0 deletions src/process_datasets/generate_sim_spatialcluster/config.vsh.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: generate_sim_sparialcluster
namespace: process_datasets
description: generate spatial cluster of simulated dataset before run metrics folder
argument_groups:
- name: Inputs
arguments:
- type: file
name: --input_sp
description: Raw spatial dataset
example: resources_test/datasets/MOBNEW/dataset_sp.h5ad
required: true
- type: file
name: --input_sp_sim
description: similated spatial dataset
example: resources_test/datasets/MOBNEW/simulated_dataset.h5ad
required: true

- name: Outputs
arguments:
- type: file
name: --output_sp
description: Processed simulated spatial dataset
example: simulated_dataset.h5ad
direction: output
required: true

resources:
- type: r_script
path: script.R
- path: /src/helpers/utils.R

test_resources:
- path: /resources_test/datasets/MOBNEW
dest: resources_test/datasets/MOBNEW
- type: python_script
path: /common/component_tests/run_and_check_output.py

engines:
- type: docker
image: openproblems/base_r:1.0.0
setup:
- type: r
bioc:
- scater
- BayesSpace
- SingleCellExperiment
cran:
- aricode
- anndata
- reshape2


runners:
- type: executable
- type: nextflow
37 changes: 37 additions & 0 deletions src/process_datasets/generate_sim_spatialcluster/script.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@


## VIASH START
par <- list(
# inputs
input_spatial_dataset = "resources_test/datasets/MOBNEW/dataset_sp.h5ad",
input_simulated_dataset = "resources_test/datasets/MOBNEW/simulated_dataset.h5ad",
# outputs
output_sp = "resources_test/datasets/MOBNEW/simulated_dataset.h5ad"
)
meta <- list(
resources_dir = "target/executable/process_datasets/generate_sim_sparialCluster"
)
## VIASH END
source(file.path(meta$resources_dir, "utils.R"))

cat("Read input files\n")
input_real_sp <- anndata::read_h5ad(par$input_spatial_dataset)
input_simulated_sp <- anndata::read_h5ad(par$input_simulated_dataset)

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
))
cat("add spatial cluster in simulated dataset:\n")
sim_cluster <- generate_sim_spatialCluster(input_real_sp, input_simulated_sp)

# need reclassify again
real_cluster <- input_real_sp$obs[,c("spatial_cluster")]
location <- colnames(counts(sim_sce))
sim_new_cluster <- reclassify_simsce(location, real_cluster, sim_cluster)

input_simulated_sp$obs$spatial_cluster <- sim_new_cluster

cat("Writing output to file\n")
input_simulated_sp$write_h5ad(par$output_sp, compression = "gzip")

0 comments on commit 97f1e78

Please sign in to comment.