-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxenium_cluster.py
More file actions
502 lines (376 loc) · 20.6 KB
/
xenium_cluster.py
File metadata and controls
502 lines (376 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import scanpy as sc
import scipy.cluster.hierarchy as sch
import anndata as ad
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os
import subprocess
import torch
from utils.embeddings import *
from matplotlib.colors import ListedColormap
from typing import List
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
from mclustpy import mclustpy
class XeniumCluster:
# TO DO
# add option to only include high variable genes
SPOT_SIZE = 100
THIRD_DIM = False
def __init__(self, data: pd.DataFrame, dataset_name: str, spot_size: int = 100) -> None:
self.raw_xenium_data = data
self.xenium_spot_data = None
self.dataset_name = dataset_name
self.SPOT_SIZE = spot_size
self.spot_data_location = f"data/spot_data/{dataset_name}"
def set_spot_size(self, new_spot_size):
if not isinstance(new_spot_size, (int, float)):
raise TypeError("The spot size must be numeric.")
if new_spot_size <= 0:
raise ValueError("Spot size must be positive.")
self.SPOT_SIZE = new_spot_size
# update this to be a re-init procedure
def set_data(self, data):
self.raw_xenium_data = data
def convert_pd_to_ad(self, data):
obs_df = data.index.to_frame(index=False).astype("category")
return sc.AnnData(X=data.values, obs=obs_df, var=pd.DataFrame(index=data.columns))
def normalize_counts(self, data):
data.layers['raw']=data.X
# Why does the demo do this????
# sc.pp.normalize_total(data, target_sum=1e4)
sc.pp.log1p(data)
def create_spot_data(self, third_dim=False, save_data=True):
x_min, x_max = min(self.raw_xenium_data["x_location"]), max(self.raw_xenium_data["x_location"])
y_min, y_max = min(self.raw_xenium_data["y_location"]), max(self.raw_xenium_data["y_location"])
MIN_PAD = 1e-8
x_values = np.arange(x_min - MIN_PAD, x_max + self.SPOT_SIZE, self.SPOT_SIZE)
y_values = np.arange(y_min - MIN_PAD, y_max + self.SPOT_SIZE, self.SPOT_SIZE)
self.xenium_spot_data = self.raw_xenium_data.copy()
if third_dim:
z_min, z_max = min(self.raw_xenium_data["z_location"]), max(self.raw_xenium_data["z_location"])
z_values = np.arange(z_min - MIN_PAD, z_max + self.SPOT_SIZE, self.SPOT_SIZE)
self.xenium_spot_data["col"] = np.searchsorted(x_values, self.xenium_spot_data["x_location"]) - 1
self.xenium_spot_data["row"] = np.searchsorted(y_values, self.xenium_spot_data["y_location"]) - 1
self.xenium_spot_data["z-index"] = np.searchsorted(z_values, self.xenium_spot_data["z_location"]) - 1
self.xenium_spot_data["spot_number"] = (self.xenium_spot_data["col"] * len(y_values) * len(z_values)) + (self.xenium_spot_data["row"] * len(z_values)) + self.xenium_spot_data["z-index"]
else:
self.xenium_spot_data["col"] = np.searchsorted(x_values, self.xenium_spot_data["x_location"]) - 1
self.xenium_spot_data["row"] = np.searchsorted(y_values, self.xenium_spot_data["y_location"]) - 1
self.xenium_spot_data["spot_number"] = self.xenium_spot_data["col"] * len(y_values) + self.xenium_spot_data["row"]
counts = self.xenium_spot_data.groupby(['spot_number', 'feature_name']).size().reset_index(name='count')
counts_pivot = counts.pivot_table(index='spot_number',
columns='feature_name',
values='count',
fill_value=0)
location_means = self.xenium_spot_data.groupby('spot_number').agg({
'row': 'mean',
'col': 'mean',
'x_location': 'mean',
'y_location': 'mean',
'z_location': 'mean'
}).reset_index()
self.xenium_spot_data = location_means.join(counts_pivot, on='spot_number')
if save_data:
self.xenium_spot_data.to_csv(f"{self.spot_data_location}/{self.dataset_name}_SPOTSIZE={self.SPOT_SIZE}um_z={third_dim}.csv")
self.xenium_spot_data.set_index(["spot_number", "x_location", "y_location", "z_location", "row", "col"], inplace=True)
self.xenium_spot_data = self.convert_pd_to_ad(self.xenium_spot_data)
def generate_neighborhood_graph(self, data: ad.AnnData, n_neighbors=15, n_pcs=20, plot_pcas=True):
# generate the neigborhood graph based on pca
sc.pp.pca(data, svd_solver='arpack')
if plot_pcas:
sc.pl.pca_variance_ratio(data, log=True)
sc.pp.neighbors(data, n_neighbors=n_neighbors, n_pcs=n_pcs)
def filter_only_high_variable_genes(self, data: ad.AnnData, min_mean: float=0.3, max_mean: float=7, min_disp: float=-0.5, flavor="seurat", plot_highly_variable_genes: bool=False, n_top_genes: int=None):
sc.pp.highly_variable_genes(data, min_mean=min_mean, max_mean=max_mean, min_disp=min_disp, n_top_genes=n_top_genes, flavor=flavor)
if plot_highly_variable_genes:
sc.pl.highly_variable_genes(data)
def pca(self, data: ad.AnnData, num_pcs: int):
sc.pp.pca(data, num_pcs)
def Leiden(
self,
data: ad.AnnData,
resolutions: List[float],
embedding: str = "umap",
**kwargs
):
for resolution in resolutions:
key_added = f'leiden_{resolution}'
# Running the clustering algorithm
sc.tl.leiden(data, resolution=resolution, key_added=key_added)
# Calculate and plot embedding
get_embedding(data, embedding, **kwargs)
# plot embedding
_ = plot_embedding(data, key_added, embedding, **kwargs)
target_dir = f"results/{self.dataset_name}/Leiden/{resolution}/clusters/{self.SPOT_SIZE}"
os.makedirs(target_dir, exist_ok=True)
# Extracting row, col, and cluster values from the dataframe
rows = torch.tensor(data.obs["row"].astype(int))
cols = torch.tensor(data.obs["col"].astype(int))
clusters = torch.tensor(data.obs[f'leiden_{resolution}'].astype(int))
num_clusters = clusters.unique().size(0)
num_rows = int(max(rows) - min(rows) + 1)
num_cols = int(max(cols) - min(cols) + 1)
cluster_grid = torch.zeros((num_rows, num_cols), dtype=torch.int)
cluster_grid[rows, cols] = torch.tensor(clusters, dtype=torch.int) + 1
if self.dataset_name == "SYNTHETIC":
colormap = plt.cm.get_cmap('viridis', num_clusters)
else:
colors = plt.cm.get_cmap('viridis', num_clusters + 1)
colormap_colors = np.vstack(([[1, 1, 1, 1]], colors(np.linspace(0, 1, num_clusters))))
colormap = ListedColormap(colormap_colors)
plt.figure(figsize=(6, 6))
plt.imshow(cluster_grid.cpu(), cmap=colormap, interpolation='nearest', origin='lower')
plt.colorbar(ticks=range(num_clusters + 1), label='Cluster Values')
plt.title(f'Cluster Assignment with Leiden ($\gamma$ = {resolution})')
plt.savefig(
f"{target_dir}/clusters_RES={resolution}.png"
)
return {resolution: data.obs[f'leiden_{resolution}'].values.astype(int) for resolution in resolutions}
def Louvain(
self,
data: ad.AnnData,
resolutions: List[float],
embedding: str = "umap",
**kwargs
):
for resolution in resolutions:
key_added = f'louvain_{resolution}'
sc.tl.louvain(data, resolution=resolution, key_added=key_added)
# calculate embedding
get_embedding(data, embedding, **kwargs)
# plot embedding
_ = plot_embedding(data, key_added, embedding, **kwargs)
target_dir = f"results/{self.dataset_name}/Louvain/{resolution}/clusters/{self.SPOT_SIZE}"
os.makedirs(target_dir, exist_ok=True)
# Extracting row, col, and cluster values from the dataframe
rows = torch.tensor(data.obs["row"].astype(int))
cols = torch.tensor(data.obs["col"].astype(int))
clusters = torch.tensor(data.obs[f'louvain_{resolution}'].astype(int))
num_clusters = clusters.unique().size(0)
num_rows = int(max(rows) - min(rows) + 1)
num_cols = int(max(cols) - min(cols) + 1)
cluster_grid = torch.zeros((num_rows, num_cols), dtype=torch.int)
cluster_grid[rows, cols] = torch.tensor(clusters, dtype=torch.int) + 1
if self.dataset_name == "SYNTHETIC":
colormap = plt.cm.get_cmap('viridis', num_clusters)
else:
colors = plt.cm.get_cmap('viridis', num_clusters + 1)
colormap_colors = np.vstack(([[1, 1, 1, 1]], colors(np.linspace(0, 1, num_clusters))))
colormap = ListedColormap(colormap_colors)
plt.figure(figsize=(6, 6))
plt.imshow(cluster_grid.cpu(), cmap=colormap, interpolation='nearest', origin='lower')
plt.colorbar(ticks=range(num_clusters + 1), label='Cluster Values')
plt.title(f'Cluster Assignment with Louvain ($\gamma$ = {resolution})')
plt.savefig(
f"{target_dir}/clusters_RES={resolution}.png"
)
return {resolution: data.obs[f'louvain_{resolution}'].values.astype(int) for resolution in resolutions}
def Hierarchical(
self,
data: ad.AnnData,
num_clusters: int = 3,
groupby: List[str] = ["spot_number"],
embedding: str = "umap",
include_spatial = True,
**kwargs
):
key_added = f'dendrogram_{groupby}'
# calculate cluster assignment
if include_spatial:
# Normalize spatial coordinates to have a similar scale to the gene expression data
norm_row = (data.obs['row'].astype(int) - np.min(data.obs['row'].astype(int))) / np.ptp(data.obs['row'].astype(int))
norm_col = (data.obs['col'].astype(int) - np.min(data.obs['col'].astype(int))) / np.ptp(data.obs['col'].astype(int))
# Create a temporary copy of X and append normalized spatial coordinates
temp_X = np.concatenate([data.X, np.array(norm_row)[:, np.newaxis], np.array(norm_col)[:, np.newaxis]], axis=1)
# Now perform the clustering with the temporary X
var=data.var.copy()
var = pd.concat((var, pd.DataFrame(index=['norm_row', 'norm_col'])), axis=1)
temp_data = sc.AnnData(X=temp_X, obs=data.obs.copy(), var=var)
# Calculate dendrogram
sc.tl.dendrogram(temp_data, groupby=groupby, key_added=key_added)
linkage_matrix = temp_data.uns[key_added]['linkage']
else:
sc.tl.dendrogram(data, groupby=groupby, key_added=key_added)
linkage_matrix = data.uns[key_added]['linkage']
# Form clusters from the dendrogram
cluster_labels = sch.fcluster(linkage_matrix, t=num_clusters, criterion='maxclust')
# Assign cluster labels to observations
data.obs[key_added] = cluster_labels
# plot dendrogram
# sc.pl.dendrogram(data, groupby=groupby)
# calculate embedding
get_embedding(data, embedding, **kwargs)
# plot embedding
_ = plot_embedding(data, key_added, embedding, **kwargs)
target_dir = f"results/{self.dataset_name}/Hierarchical/{num_clusters}/clusters/{self.SPOT_SIZE}"
os.makedirs(target_dir, exist_ok=True)
# Extracting row, col, and cluster values from the dataframe
rows = torch.tensor(data.obs["row"].astype(int))
cols = torch.tensor(data.obs["col"].astype(int))
clusters = torch.tensor(data.obs[key_added].astype(int))
num_clusters = clusters.unique().size(0)
num_rows = int(max(rows) - min(rows) + 1)
num_cols = int(max(cols) - min(cols) + 1)
cluster_grid = torch.zeros((num_rows, num_cols), dtype=torch.int)
cluster_grid[rows, cols] = torch.tensor(clusters, dtype=torch.int)
if self.dataset_name == "SYNTHETIC":
colormap = plt.cm.get_cmap('viridis', num_clusters + 1)
else:
colors = plt.cm.get_cmap('viridis', num_clusters + 1)
colormap_colors = np.vstack(([[1, 1, 1, 1]], colors(np.linspace(0, 1, num_clusters))))
colormap = ListedColormap(colormap_colors)
plt.figure(figsize=(6, 6))
plt.imshow(cluster_grid.cpu(), cmap=colormap, interpolation='nearest', origin='lower')
plt.colorbar(ticks=range(num_clusters + 1), label='Cluster Values')
plt.title(f'Cluster Assignment with Hierarchical')
plt.savefig(
f"{target_dir}/clusters_K={num_clusters}.png"
)
return data.obs[key_added].values.astype(int)
def KMeans(
self,
data: ad.AnnData,
K: int = 3,
include_spatial=True,
normalize=True,
save_plot=True,
):
spatial_init_data = data.X
if include_spatial:
spatial_locations = data.obs[["row", "col"]]
spatial_init_data = np.concatenate((spatial_locations, data.X), axis=1)
if normalize:
spatial_init_data = StandardScaler().fit_transform(spatial_init_data)
kmeans = KMeans(n_clusters=K).fit(spatial_init_data)
cluster_assignments = kmeans.predict(spatial_init_data)
data.obs["cluster"] = cluster_assignments
target_dir = f"results/{self.dataset_name}/K-Means/{K}/clusters/{self.SPOT_SIZE}"
os.makedirs(target_dir, exist_ok=True)
# Extracting row, col, and cluster values from the dataframe
rows = torch.tensor(data.obs["row"].astype(int))
cols = torch.tensor(data.obs["col"].astype(int))
clusters = torch.tensor(data.obs["cluster"].astype(int))
num_clusters = clusters.unique().size(0)
num_rows = int(max(rows) - min(rows) + 1)
num_cols = int(max(cols) - min(cols) + 1)
cluster_grid = torch.zeros((num_rows, num_cols), dtype=torch.int)
cluster_grid[rows, cols] = torch.tensor(clusters, dtype=torch.int) + 1
if self.dataset_name == "SYNTHETIC":
colormap = plt.cm.get_cmap('viridis', num_clusters)
else:
colors = plt.cm.get_cmap('viridis', num_clusters + 1)
colormap_colors = np.vstack(([[1, 1, 1, 1]], colors(np.linspace(0, 1, num_clusters))))
colormap = ListedColormap(colormap_colors)
plt.figure(figsize=(6, 6))
plt.imshow(cluster_grid.cpu(), cmap=colormap, interpolation='nearest', origin='lower')
plt.colorbar(ticks=range(num_clusters + 1), label='Cluster Values')
plt.title(f'Cluster Assignment with K-Means')
plt.savefig(
f"{target_dir}/clusters_K={K}.png"
)
return cluster_assignments
def BayesSpace(
self,
data: ad.AnnData,
init_method: str = "mclust",
num_pcs: int = 15,
K: int = 3,
grid_search: bool = True,
):
def run_r_script(script_path: str, *args):
"""
Function to run an R script with optional arguments.
Parameters:
script_path (str): Path to the R script.
*args: Additional arguments to pass to the R script.
"""
command = ["Rscript", script_path] + list(args)
subprocess.run(command, check=True, capture_output=True)
run_r_script("xenium_BayesSpace.R", self.dataset_name, f"{self.SPOT_SIZE}", f"{init_method}", f"{num_pcs}", f"{K}", f"{grid_search}")
target_dir = f"results/{self.dataset_name}/BayesSpace/{num_pcs}/{K}/clusters/{init_method}/{self.SPOT_SIZE}"
os.makedirs(target_dir, exist_ok=True)
gammas = np.linspace(1, 3, 9) if grid_search else [2]
for gamma in gammas:
new_target_dir = os.path.join(target_dir, f"{gamma:.2f}")
os.makedirs(new_target_dir, exist_ok=True)
BayesSpace_clusters = pd.read_csv(f"{target_dir}/{gamma:.2f}/clusters_K={K}.csv", index_col=0)
data.obs["cluster"] = np.array(BayesSpace_clusters["BayesSpace cluster"])
# Extracting row, col, and cluster values from the dataframe
rows = torch.tensor(data.obs["row"].astype(int))
cols = torch.tensor(data.obs["col"].astype(int))
clusters = torch.tensor(data.obs["cluster"].astype(int))
num_clusters = clusters.unique().size(0)
num_rows = int(max(rows) - min(rows) + 1)
num_cols = int(max(cols) - min(cols) + 1)
cluster_grid = torch.zeros((num_rows, num_cols), dtype=torch.int)
if self.dataset_name == "SYNTHETIC":
cluster_grid[rows, cols] = torch.tensor(clusters, dtype=torch.int)
colormap = plt.cm.get_cmap('viridis', num_clusters)
else:
cluster_grid[rows, cols] = torch.tensor(clusters, dtype=torch.int) + 1
colors = plt.cm.get_cmap('viridis', num_clusters + 1)
colormap_colors = np.vstack(([[1, 1, 1, 1]], colors(np.linspace(0, 1, num_clusters))))
colormap = ListedColormap(colormap_colors)
plt.figure(figsize=(6, 6))
plt.imshow(cluster_grid, cmap=colormap, interpolation='nearest', origin='lower')
plt.colorbar(ticks=range(num_clusters + 1), label='Cluster Values')
plt.title(f'Cluster Assignment with BayesSpace ($\gamma$ = {gamma})')
plt.savefig(
os.path.join(new_target_dir, f"clusters_K={K}.png")
)
return data.obs["cluster"].values.astype(int)
def mclust(
self,
data: ad.AnnData,
G: int = 17,
model_name: str = "EEE",
temp_dir: str = "temporary_pca_file.csv"
):
"""
G: if int, will check for all clusters 1:G. If list, will only check values in list.
"""
def run_r_script(script_path: str, *args):
"""
Function to run an R script with optional arguments and capture its output.
Parameters:
script_path (str): Path to the R script.
*args: Additional arguments to pass to the R script.
Returns:
str: The standard output from the R script.
"""
command = ["Rscript", script_path] + list(args)
result = subprocess.run(command, check=True, capture_output=True, text=True)
return result.stdout
np.savetxt(temp_dir, data.obsm["X_pca"], delimiter=",")
num_output_clusters = run_r_script("mclust.R", temp_dir, f"{G}", f"{data.obsm['X_pca'].shape[1]}", f"{self.SPOT_SIZE}", self.dataset_name)
target_dir = f"results/{self.dataset_name}/mclust/{data.obsm['X_pca'].shape[1]}/{G}/clusters/{self.SPOT_SIZE}"
mclust_clusters = pd.read_csv(f"{target_dir}/clusters_K={G}.csv", index_col=0)
data.obs["cluster"] = np.array(mclust_clusters["mclust cluster"])
# Extracting row, col, and cluster values from the dataframe
rows = torch.tensor(data.obs["row"].astype(int))
cols = torch.tensor(data.obs["col"].astype(int))
clusters = torch.tensor(data.obs["cluster"].astype(int))
num_clusters = clusters.unique().size(0)
num_rows = int(max(rows) - min(rows) + 1)
num_cols = int(max(cols) - min(cols) + 1)
cluster_grid = torch.zeros((num_rows, num_cols), dtype=torch.int)
cluster_grid[rows, cols] = torch.tensor(clusters, dtype=torch.int) + 1
if self.dataset_name == "SYNTHETIC":
cluster_grid[rows, cols] = torch.tensor(clusters, dtype=torch.int)
colormap = plt.cm.get_cmap('viridis', num_clusters)
else:
cluster_grid[rows, cols] = torch.tensor(clusters, dtype=torch.int) + 1
colors = plt.cm.get_cmap('viridis', num_clusters + 1)
colormap_colors = np.vstack(([[1, 1, 1, 1]], colors(np.linspace(0, 1, num_clusters))))
colormap = ListedColormap(colormap_colors)
plt.figure(figsize=(6, 6))
plt.imshow(cluster_grid.cpu(), cmap=colormap, interpolation='nearest', origin='lower')
plt.colorbar(ticks=range(num_clusters + 1), label='Cluster Values')
plt.title(f'Cluster Assignment with mclust')
plt.savefig(
f"{target_dir}/clusters_K={G}.png"
)
return data.obs["cluster"].values