From 1997c3d10d823a0b0664578c530d8a247fa66fbe Mon Sep 17 00:00:00 2001 From: littlecabiria <71769896+littlecabiria@users.noreply.github.com> Date: Fri, 22 Nov 2024 15:05:05 +1100 Subject: [PATCH] update scFeartures script --- src/metrics/ks_statistic_sc_features/script.R | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/src/metrics/ks_statistic_sc_features/script.R b/src/metrics/ks_statistic_sc_features/script.R index 25106336..1747a7a8 100644 --- a/src/metrics/ks_statistic_sc_features/script.R +++ b/src/metrics/ks_statistic_sc_features/script.R @@ -8,11 +8,28 @@ requireNamespace("CARD", quietly = TRUE) requireNamespace("SingleCellExperiment", quietly = TRUE) requireNamespace("SummarizedExperiment", quietly = TRUE) +<<<<<<< Updated upstream ## VIASH START par <- list( 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", +======= +packages <- c("SingleCellExperiment", "SummarizedExperiment", "concaveman", "sp", + "Matrix", "methods", "ggplot2", "ggcorrplot", "MuSiC", "fields", + "MCMCpack", "dplyr", "sf", "RANN", "stats", "reshape2", "RColorBrewer", + "scatterpie", "grDevices", "nnls", "pbmcapply", "spatstat", "gtools", + "RcppML", "NMF") + +# Load each package +lapply(packages, library, character.only = TRUE) + +## VIASH START +par <- list( + input_spatial_dataset = "temp_pdac.positive.ks_statistic_sc_features/_viash_par/input_spatial_dataset_1/output_sp.h5ad", + input_singlecell_dataset = "temp_pdac.positive.ks_statistic_sc_features/_viash_par/input_singlecell_dataset_1/output_sc.h5ad", + input_simulated_dataset = "temp_pdac.positive.ks_statistic_sc_features/_viash_par/input_simulated_dataset_1/pdac.positive.generate_sim_spatialcluster.output_sp.h5ad", +>>>>>>> Stashed changes output = "output.h5ad" ) meta <- list( @@ -31,11 +48,26 @@ 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))) +<<<<<<< Updated upstream sim_log_count <- scuttle::normalizeCounts(t(input_simulated_sp$layers[["counts"]])) +======= +real_log_count <- real_log_count[ , colnames(real_log_count) %in% rownames(real_prob_matrix) ] -# build cell type deconvolution in simulated data + +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") +>>>>>>> Stashed changes + +# build cell type deconvolution in simulated data ## error sim_prob_matrix <- CARD_processing(input_real_sp, input_sc) +sim_log_count <- sim_log_count[ , colnames(sim_log_count) %in% rownames(sim_prob_matrix) ] + feat_types <- c("L_stats", "celltype_interaction", "nn_correlation", "morans_I") real_scfeatures_result <- scFeatures::scFeatures(real_log_count, @@ -48,6 +80,7 @@ real_scfeatures_result <- scFeatures::scFeatures(real_log_count, ) sim_scfeatures_result <- scFeatures::scFeatures(sim_log_count, +<<<<<<< Updated upstream sample = rep("sample1", ncol(sim_log_count)), spatialCoords = input_simulated_sp$obs[c("row", "col")], feature_types = feat_types, @@ -55,6 +88,15 @@ sim_scfeatures_result <- scFeatures::scFeatures(sim_log_count, species = sc_species, spotProbability = t(sim_prob_matrix) ) +======= + sample = rep("sample1", ncol(sim_log_count)), + spatialCoords = list( as.numeric( unlist( lapply( strsplit( colnames(sim_log_count) , "x"), `[`, 1) ) ) , + as.numeric( unlist( lapply( strsplit( colnames(sim_log_count) , "x"), `[`, 2) ) ) ) , + feature_types = feat_types, + type = "spatial_t", + species = sc_species, + spotProbability = t(sim_prob_matrix) ) +>>>>>>> Stashed changes 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))