comparison spatialGE_clust.R @ 0:555ca19d07e6 draft default tip

planemo upload for repository https://github.com/goeckslab/tools-st/tree/main/tools/spatialge commit 482b2e0e6ca7aaa789ba07b8cd689da9a01532ef
author goeckslab
date Wed, 13 Aug 2025 19:32:19 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:555ca19d07e6
1 # ------------------------------
2 # Spatially-Informed Clustering
3 # ------------------------------
4
5 # Purpose:
6 # Group cells into tissue domains using hierarchical clustering on a weighted similarity matrix
7
8 library(spatialGE)
9 library(optparse)
10 library(ggplot2)
11 library(tools)
12 library(dplyr)
13
14 ### Command Line Options
15
16
17 option_list <- list(
18 make_option(c("-f", "--file"), action = "store", default = NA, type = "character",
19 help = "STlist .rds file path from preprocessing"),
20 make_option(c("--visium"), action = "store_true", type = "logical", default = FALSE,
21 help = "Flag for Visium data"),
22 make_option(c("--raw"), action = "store_true", type = "logical", default = FALSE,
23 help = "Flag for raw data"),
24 make_option(c("--cosmx"), action = "store_true", type = "logical", default = FALSE,
25 help = "Flag for CosMX data"),
26 make_option(c("-s", "--samples"), action = "store", default = NULL, type = "character",
27 help = "Sample subset to perform clustering on"),
28 make_option(c("-w", "--weight"), action = "store", default = 0.025, type = "numeric",
29 help = "Weight to be applied to spatial distances between 0-1"),
30 make_option(c("-d", "--dist"), action = "store", default = "euclidean", type = "character",
31 help = "Distance metric to be used, all methods found in wordspace::dist.matrix"),
32 make_option(c("--dslogical"), action = "store_true", type = "logical", default = FALSE,
33 help = "Deepsplit logical flag"),
34 make_option(c("--logical"), action = "store", default = TRUE, type = "logical",
35 help = "Control cluster resolution, true will produce more clusters"),
36 make_option(c("--dsnumeric"), action = "store_true", type = "logical", default = FALSE,
37 help = "Deepsplit numeric flag"),
38 make_option(c("--numeric"), action = "store", default = 0, type = "numeric",
39 help = "control cluster resolution between 0-4, higher number will produce more clusters"),
40 make_option(c("-p", "--ptsize"), action = "store", default = 2.75, type = "numeric",
41 help = "Size of points on cluster plot")
42 )
43
44 ### Main
45
46 # parse args
47 opt <- parse_args(OptionParser(option_list = option_list))
48
49 # read in ST data from spatialGE preprocessing
50 STdata <- readRDS(opt$file)
51
52 message("Rds object successfully loaded")
53
54 # if deepSplit flag is included, choose between logical or numeric
55 if (opt$logical) {
56 deepsplit <- opt$dslogical
57 } else if (opt$numeric) {
58 deepsplit <- opt$dsnumeric
59 }
60
61 if (!is.null(opt$samples)) {
62 opt$samples <- strsplit(opt$samples, ",")[[1]]
63 } else if (is.null(opt$samples)) {
64 opt$samples <- NULL
65 }
66
67 # perform data clustering on transformed data
68 clusters <- STclust(x = STdata, samples = opt$samples, ws = opt$weight, dist_metric = opt$dist, deepSplit = deepsplit)
69
70 message("Unsupervised spatially-informed clustering has been performed")
71
72 # transform S4 to list for easier slot access
73 S4toList <- function(obj) {
74 slot_names <- slotNames(obj)
75 structure(lapply(slot_names, slot, object = obj), names = slot_names)
76 }
77
78 STdata <- S4toList(STdata)
79
80 message("Transformed to S4 for slot name access")
81
82 # depending on data input type, pull correct sample column
83 if (opt$visium) {
84 sample_col <- "sample_id"
85 } else if (opt$raw) {
86 sample_col <- "sampleID"
87 } else if (opt$cosmx) {
88 sample_col <- "sample_name"
89 }
90
91
92 if (!is.null(opt$samples)) {
93 samples <- STdata$sample_meta %>% pull(.data[[sample_col]]) %>% intersect(opt$samples)
94 } else {
95 samples <- STdata$sample_meta %>% pull(.data[[sample_col]])
96 }
97
98
99 message("Sample names identified")
100
101 # create cluster plot directory
102 if (!dir.exists("cluster_plots")) dir.create("cluster_plots")
103
104 # iterate through each sample, generate a plot, and save
105 for (s in samples) {
106
107 plot <- STplot(x = clusters, ws = opt$weight, ptsize = opt$ptsize, deepSplit = deepsplit, samples = s)
108
109 message("Cluster plots generated")
110
111
112 #create unique plot file names based on sample name
113
114 filename <- paste0("clustered_", s, ".png")
115
116 #save plot to subdir
117 ggsave(
118 path = "./cluster_plots",
119 filename = filename,
120 bg = "white",
121 width = 12
122 )
123 }
124
125 message("Cluster plots saved")