|
| 1 | +library(spacexr) |
| 2 | +library(Matrix) |
| 3 | +library(SingleCellExperiment) |
| 4 | +library(anndataR) |
| 5 | + |
| 6 | +## VIASH START |
| 7 | +par <- list( |
| 8 | + "input_spatial_normalized_counts" = "task_ist_preprocessing/resources_test/task_ist_preprocessing/mouse_brain_combined/spatial_aggregated_counts.h5ad", |
| 9 | + "input_scrnaseq_reference"= "task_ist_preprocessing/resources_test/task_ist_preprocessing/mouse_brain_combined/scrnaseq_reference.h5ad", |
| 10 | + "output" = "task_ist_preprocessing/tmp/spatial_types.h5ad" |
| 11 | +) |
| 12 | + |
| 13 | +meta <- list( |
| 14 | + 'cpus': 4, |
| 15 | +) |
| 16 | + |
| 17 | +## VIASH END |
| 18 | + |
| 19 | +# Read the input h5ad file and convert to SingleCellExperiment |
| 20 | +sce <- read_h5ad(par$input_spatial_normalized_counts, as = "SingleCellExperiment") |
| 21 | + |
| 22 | +# Extract spatial coordinates and counts matrix |
| 23 | +centroid_x <- colData(sce)$centroid_x |
| 24 | +centroid_y <- colData(sce)$centroid_y |
| 25 | +coords <- data.frame(centroid_x, centroid_y) |
| 26 | +counts <- assay(sce,"counts") |
| 27 | +rownames(coords) <- colData(sce)$cell_id |
| 28 | +puck <- SpatialRNA(coords, counts) |
| 29 | + |
| 30 | +# Read reference scrnaseq |
| 31 | +ref <- read_h5ad(par$input_scrnaseq_reference, as = "SingleCellExperiment") |
| 32 | + |
| 33 | +#filter reference cell types to those with >25 cells |
| 34 | +valid_celltypes <- names(table(colData(ref)$cell_type))[table(colData(ref)$cell_type) >= 25] |
| 35 | +filtered_ref <- ref[,colData(ref)$cell_type %in% valid_celltypes] |
| 36 | + |
| 37 | +ref_counts <- assay(filtered_ref, "counts") |
| 38 | +# factor to drop filtered cell types |
| 39 | +colData(filtered_ref)$cell_type <- factor(colData(filtered_ref)$cell_type) |
| 40 | +cell_types <- colData(filtered_ref)$cell_type |
| 41 | +names(cell_types) <- colnames(ref_counts) |
| 42 | +reference <- Reference(ref_counts, cell_types, min_UMI = 0) |
| 43 | + |
| 44 | +# check cores |
| 45 | +cores <- 1 |
| 46 | +if ("cpus" %in% names(meta) && !is.null(meta$cpus)) cores <- meta$cpus |
| 47 | +cat(sprintf("Number of cores: %s\n", cores)) |
| 48 | + |
| 49 | +# Run the algorithm |
| 50 | +myRCTD <- create.RCTD(puck, reference, max_cores = cores) |
| 51 | +myRCTD <- run.RCTD(myRCTD, doublet_mode = "doublet") |
| 52 | + |
| 53 | +# Extract results |
| 54 | +results <- myRCTD@results |
| 55 | +spatial_cell_types <- results$results_df$first_type |
| 56 | +# Include None Spatial cell type for the "reject" cells |
| 57 | +levels(spatial_cell_types) <- c(levels(spatial_cell_types), "None_sp") |
| 58 | +spatial_cell_types[results$results_df$spot_class == "reject"] <- "None_sp" |
| 59 | +names(spatial_cell_types) <- rownames(results$results_df) |
| 60 | + |
| 61 | +# |
| 62 | +colData(sce)$cell_type <- "None_sp" |
| 63 | +colData(sce)[names(spatial_cell_types),"cell_type"] <- as.character(spatial_cell_types) |
| 64 | + |
| 65 | +# Write the final object to h5ad format |
| 66 | +# set to 'w', is this ok? |
| 67 | +dir.create(dirname(par$output), showWarnings = FALSE, recursive = TRUE) |
| 68 | +write_h5ad(sce, par$output, mode = "w") |
0 commit comments