-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclustering.py
1118 lines (943 loc) · 45.7 KB
/
clustering.py
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
from sklearn.cluster import KMeans
import matplotlib.colors as mcolors
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.graph_objects as go
import matplotlib.patheffects as path_effects
import re
import scanpy as sc
################################################################################################
# clustering functions
################################################################################################
CLUSTERING_DICT = {
0:'ml', # max_likelihood_clustering(..., mode='standard') ** default
1:'reference', # reference_clustering(..., reference_index=...)
2:'ml-emission', # max_likelihood_clustering(..., mode='emission')
3:'ml-soft', # max_likelihood_clustering(..., mode='soft')
# 4:'kmeans-reference', # k_means_reference(..., reference_index=..., k=...)
}
def max_likelihood_clustering(Qs, mode='standard'):
'''
Args
Qs : list of length N.
each element of Qs is a
np.ndarray, of shape (n_t, r_t), for slice t in {0, ..., N-1}
mode : str, 'standard', 'emission', 'soft', default='standard'
Out
clustering_list : list of length N.
each element of clustering_list is a
np.ndarray, of shape (n_t,), with r_t labels in {0, ..., r_t-1}
Description
Assigns each spot in slice 1 to the cluster (column index) with the highest probability
Assigns each spot in slice 2 to the cluster (column index) with the highest probability
Assumes distinct sets of labels for each slice
in 'standard' mode, Q and R are used as is, where each is assumed to be a joint distribution
between spots and cell types of a given slice. Entries are joint probabilities.
in 'emission' mode, Q and R are normalized by the inner marginals to be column-stochastic.
Entries are now conditional probabilities of spots given cell types,
which we call emission probabilities, as in HMMs.
in 'soft' mode, Q and R are normalized by the outer marginals to be row-stochastic.
Entries are now conditional probabilities of cell types given spots,
constituting soft assignments / clusterings / transition matrices
'''
N = len(Qs)
if mode == 'standard':
Ms = Qs
elif mode == 'emission':
Ms = [None]*N
for t in range(N):
g_t = np.sum(Qs[t], axis=0)
Ms[t] = Qs[t] @ np.diag(1/g_t)
elif mode == 'soft':
Ms = [None]*N
for t in range(N):
a_t = np.sum(Qs[t], axis=1)
Ms[t] = np.diag(1/a_t) @ Qs[t]
else:
raise ValueError('Invalid mode')
clustering_list = [np.argmax(M, axis=1) for M in Ms]
return clustering_list
def reference_clustering(Qs, Ts, reference_index, full_P=True):
'''
Args
Qs : list of length N.
each element of Qs is a
np.ndarray, of shape (n_t, r_t), for slice t in {0, ..., N-1}
Ts : list of length N-1.
each element of Ts is a
np.ndarray, of shape (r_t, r_{t+1}), for transition t in {0, ..., N-2}
reference_index : int s in {0, ..., N-1}, index of the slice to use as reference
denote as s, with r_s cell types
full_P : bool, whether to compute using full matrix P, default=True
set to False if the full matrix P can't be stored
Out
clustering_list : list of length N.
each element of clustering_list is a
np.ndarray, of shape (n_t,), with r_s labels in {0, ..., r_s-1}
there are now the same number of labels for each slice
Description
Assigns each spot in reference slice to the cluster (column index) with the highest probability
This is identical to ml clusteirng on the reference slice.
Let s := reference_index,
labels_s = max_likelihood_clustering(Q_s)
P^(s, s+1) := Q_s @ diag(1/g_s) @ T^(s, s+1) @ diag(1/g_{s+1}) @ Q_{s+1}^T
P^(s, s+2) := Q_s @ diag(1/g_s) @ T^(s, s+1) @ diag(1/g_{s+1}) @ T^(s+1, s+2) @ diag(1/g_{s+2}) @ Q_{s+2}^T
...
P^(s, N-1) := Q_s @ diag(1/g_s) @ T^(s, s+1) @ diag(1/g_{s+1}) @ ... @ diag(1/g_{N-2}) @ T^{N-2, N-1} @ diag(1/g_{N-1}) @ Q_{N-1}^T
and analogously,
P^(s-1, s) := Q_{s-1} @ diag(1/g_{s-1}) @ T^{s-1, s} @ diag(1/g_s) @ Q_s^T
P^(s-2, s) := Q_{s-2} @ diag(1/g_{s-2}) @ T^{s-2, s-1} @ diag(1/g_{s-1}) @ T^{s-1, s} @ diag(1/g_s) @ Q_s^T
...
P^(0, s) := Q_0 @ diag(1/g_0) @ T^{0, 1} @ diag(1/g_1) @ ... @ diag(1/g_{s-1}) @ T^{s-1, s} @ diag(1/g_s) @ Q_s^T
for slices t > s,
imaxs_t = argmax(P^(s, t), axis=0)
labels_t = labels_s[imaxs_t]
for slices t < s,
imaxs_t = argmax(P^(t, s), axis=1)
labels_t = labels_s[imaxs_t]
'''
N = len(Qs)
s = reference_index
Qs_past = Qs[: s]
Q_s = Qs[s]
g_s = np.sum(Q_s, axis=0)
Qs_future = Qs[s+1:]
labels_s = np.argmax(Q_s, axis=1)
Ts_past = Ts[:s-1]
T_sm1 = Ts[s-1]
if s == N-1:
T_s = None
else:
T_s = Ts[s]
if s == N-1:
Ts_future = None
else:
Ts_future = Ts[s+1:]
# make list of suffix factors
if reference_index == N-1:
suffixes = []
else:
suffixes = [ np.diag(1 / g_s) @ T_s ] # initialize earliest timepoint suffix
for T in Ts_future:
g = np.sum(T, axis=1)
new_suffix_end = np.diag(1/g) @ T
suffixes.append(suffixes[-1] @ new_suffix_end)
# make list of prefix factors
if reference_index == 0:
prefixes = []
else:
prefixes = [ T_sm1 @ np.diag(1 / g_s) ] # initialize latest timepoint prefix
for T in Ts_past[::-1]: # iterate backwards in time
g = np.sum(T, axis=0)
new_prefix_start = T @ np.diag(1/g)
prefixes.insert(0, new_prefix_start @ prefixes[0]) # insert at beginning of prefixes, as we move backward, so that prefixes is oriented forwards in time
# in either case of full_P, we construct the cluster lists separately for future and past timepoints
clustering_list_future = []
clustering_list_past =[]
if full_P and reference_index < N-1 and reference_index > 0:
# make full transport plans between reference s and future timepoints t
for Q_t, suffix in zip(Qs_future, suffixes):
g_t = np.sum(Q_t, axis=0)
P_st = Q_s @ suffix @ np.diag(1 / g_t) @ Q_t.T
i_maxs_t = np.argmax(P_st, axis=0) # map t -> s, axis=0 because this arrow is backwards in time
labels_t = labels_s[i_maxs_t]
clustering_list_future.append(labels_t)
# make full transport plans between past timepoints t and reference s
for Q_t, prefix in zip(Qs_past, prefixes):
g_t = np.sum(Q_t, axis=0)
P_ts = Q_t @ np.diag(1 / g_t) @ prefix @ Q_s.T
i_maxs_t = np.argmax(P_ts, axis=1) # map t -> s, axis=1 because this arrow is forwards in time
labels_t = labels_s[i_maxs_t]
clustering_list_past.append(labels_t)
elif full_P and reference_index == 0:
# make full transport plans between reference s and future timepoints t
for Q_t, suffix in zip(Qs_future, suffixes):
g_t = np.sum(Q_t, axis=0)
P_st = Q_s @ suffix @ np.diag(1 / g_t) @ Q_t.T
i_maxs_t = np.argmax(P_st, axis=0)
labels_t = labels_s[i_maxs_t]
clustering_list_future.append(labels_t)
elif full_P and reference_index == N-1:
# make full transport plans between past timepoints t and reference s
for Q_t, prefix in zip(Qs_past, prefixes):
g_t = np.sum(Q_t, axis=0)
P_ts = Q_t @ np.diag(1 / g_t) @ prefix @ Q_s.T
i_maxs_t = np.argmax(P_ts, axis=1)
labels_t = labels_s[i_maxs_t]
clustering_list_past.append(labels_t)
elif not full_P and reference_index < N-1 and reference_index > 0:
# If the full matrices P_st, P_ts can't be stored, we can still slowly compute labels using a loop
# make labels for future timepoints t from those at reference s
for Q_t, suffix in zip(Qs_future, suffixes):
g_t = np.sum(Q_t, axis=0)
labels_t = np.zeros(Q_t.shape[0], dtype=int)
for j in range(Q_t.shape[0]):
if j % 10000 == 0:
print(f'Progress: {j}/{Q_t.shape[0]}')
#P_j = Q_tilde @ (np.diag(1/gR) @ R.T[:,j])
P_st_j = Q_s @ suffix @ np.diag(1 / g_t) @ Q_t.T[:,j]
# i_maxs_t = phi(j) : t -> s
i_maxs_t = np.argmax(P_st_j)
labels_t[j] = labels_s[i_maxs_t]
clustering_list_future.append(labels_t)
# make labels for past timepoints t from those at reference s
for Q_t, prefix in zip(Qs_past, prefixes):
g_t = np.sum(Q_t, axis=0)
labels_t = np.zeros(Q_t.shape[0], dtype=int)
for j in range(Q_t.shape[0]):
if j % 10000 == 0:
print(f'Progress: {j}/{Q_t.shape[0]}')
#P_j = Q_tilde @ (np.diag(1/gR) @ R.T[:,j])
P_ts_j = Q_t @ np.diag(1 / g_t) @ prefix @ Q_s.T[:,j]
# i_maxs_t = phi(j) : t -> s
i_maxs_t = np.argmax(P_ts_j)
labels_t[j] = labels_s[i_maxs_t]
clustering_list_past.append(labels_t)
elif not full_P and reference_index == 0:
# make labels for future timepoints t from those at reference s
for Q_t, suffix in zip(Qs_future, suffixes):
g_t = np.sum(Q_t, axis=0)
labels_t = np.zeros(Q_t.shape[0], dtype=int)
for j in range(Q_t.shape[0]):
if j % 10000 == 0:
print(f'Progress: {j}/{Q_t.shape[0]}')
#P_j = Q_tilde @ (np.diag(1/gR) @ R.T[:,j])
P_st_j = Q_s @ suffix @ np.diag(1 / g_t) @ Q_t.T[:,j]
# i_maxs_t = phi(j) : t -> s
i_maxs_t = np.argmax(P_st_j)
labels_t[j] = labels_s[i_maxs_t]
clustering_list_future.append(labels_t)
elif not full_P and reference_index == N-1:
# make labels for past timepoints t from those at reference s
for Q_t, prefix in zip(Qs_past, prefixes):
g_t = np.sum(Q_t, axis=0)
labels_t = np.zeros(Q_t.shape[0], dtype=int)
for j in range(Q_t.shape[0]):
if j % 10000 == 0:
print(f'Progress: {j}/{Q_t.shape[0]}')
#P_j = Q_tilde @ (np.diag(1/gR) @ R.T[:,j])
P_ts_j = Q_t @ np.diag(1 / g_t) @ prefix @ Q_s.T[:,j]
# i_maxs_t = phi(j) : t -> s
i_maxs_t = np.argmax(P_ts_j)
labels_t[j] = labels_s[i_maxs_t]
clustering_list_past.append(labels_t)
clustering_list = clustering_list_past + [labels_s] + clustering_list_future
return clustering_list
"""
def k_means_reference(Q, R, T, k, ...):
'''
Input
Q : np.ndarray, of shape (n, r_1), slice 1
R : np.ndarray, of shape (m, r_2), slice 2
T : np.ndarray, of shape (r_1, r_2), cell type coupling between slice 1 and slice 2
k : int, number of clusters to use for k-means
Output
labels_Q : np.ndarray, of shape (n,), with k labels in {0, ..., k-1}
labels_R : np.ndarray, of shape (m,), with k labels in {0, ..., k-1}
Description
Q_tilde = Q @ (np.diag(1/gQ) @ T) represents slice 1 using slice 2 cell types,
and has shape (n, r_2).
The two representations Q_tilde, R are stacked with shape (n+m, r_2).
k-means is applied to the stack, and the cluster labels are assigned to the two slices.
*** Setting argument descendant=False switches the roles of slice 1 and slice 2 ***
'''
if descendant==False:
T = T.T
Q, R = R, Q
else:
pass
Q_length = len(Q)
# compute inner marginals
gQ = np.sum(Q, axis=0)
gR = np.sum(R, axis=0)
# represent slice 1 using slice 2 cell types
Q_tilde = Q @ (np.diag(1/gQ) @ T)
# stack the two representations, which use the same set of cell types
QR_stack = np.vstack((Q_tilde, R))
# initialize k-means, fit to the stack
kmeans = KMeans(n_clusters=k, n_init=10)
QR_km = kmeans.fit(QR_stack)
QR_clusters = QR_km.labels_
# assign cluster labels to the two slices from k-means
labels_Q = QR_clusters[:Q_length]
labels_R = QR_clusters[Q_length:]
if descendant==False:
labels_Q, labels_R = labels_R, labels_Q
else:
pass
return labels_Q, labels_R
"""
################################################################################################
# plotting helper functions
################################################################################################
def get_color_dict(labels_list,
cmap='tab'):
'''
Input
clustering_list : list of np.ndarray, labels for the spots across the slices
cmap : str, color map for the clusters, default='tab', other options: 'rainbow'
Output
color_dict : dict, dictionary with cluster labels as keys and colors as values
'''
unique_values = np.unique(np.concatenate(labels_list))
if cmap== 'tab':
cmap_tab20 = plt.get_cmap('tab20')
cmap_tab20b = plt.get_cmap('tab20b')
cmap_tab20c = plt.get_cmap('tab20c')
colors_tab20 = [cmap_tab20(i) for i in range(cmap_tab20.N)]
colors_tab20b = [cmap_tab20b(i) for i in range(cmap_tab20b.N)]
colors_tab20c = [cmap_tab20c(i) for i in range(cmap_tab20c.N)]
combined_colors = colors_tab20 + colors_tab20b + colors_tab20c
print(f'total number of tab colors that can be displayed: {len(combined_colors)}')
print(f'total number of unique values: {len(unique_values)}')
colors = [combined_colors[i % len(combined_colors)] for i in range(len(unique_values))]
else:
colors = [mcolors.hsv_to_rgb((i / len(unique_values), 1, 1)) for i in range(len(unique_values))]
# Make color_dict for either color map
color_dict = dict(zip(unique_values, colors))
return color_dict
def hex_to_rgba(hex_color, alpha=1.0):
'''Convert hex color (e.g., '#1f77b4') to rgba tuple.'''
rgb = mcolors.hex2color(hex_color) # Convert hex to RGB
return (*rgb, alpha) # Append alpha and return RGBA tuple
def get_scanpy_color_dict(labels_list, alpha=1.0):
'''
Input
labels_list : list of np.ndarray, labels for the spots across the slices
alpha : float, alpha (transparency) value for the colors
Output
color_dict : dict, dictionary with cluster labels as keys and RGBA colors as values
'''
unique_values = np.unique(np.concatenate(labels_list))
# Use Scanpy's predefined palette (contains 102 colors)
scanpy_colors = sc.pl.palettes.default_102
# Convert hex colors to RGBA format and repeat colors if needed
rgba_colors = [hex_to_rgba(scanpy_colors[i % len(scanpy_colors)], alpha) for i in range(len(unique_values))]
# Create a color dictionary mapping unique values to RGBA colors
color_dict = dict(zip(unique_values, rgba_colors))
return color_dict
def get_diffmap_inputs(clustering_list,
clustering_type,
cmap='tab'):
'''
Input
clustering_list : list of np.ndarray, labels for the spots across the slices
clustering_type : str, 'ml' or 'reference'
cmap : str, color map for the clusters, default='tab',
NOTE: anything other than 'tab' results in 'rainbow', currently
Output
population_list : list of lists, number of spots in each cluster for each slice
label_list : list of lists, unique cluster labels for each slice
color_dict : dict, dictionary with cluster labels as keys and colors as
'''
# make population_list
population_list = []
for clustering in clustering_list:
populations = [len(np.where(clustering == label)[0]) for label in set(clustering)]
population_list.append(populations)
# make label_list
labels_list = []
cs = 0 # count shift or cumulative sum, to make unique labels across slices in ml case.
if clustering_type == 'ml':
for i in range(len(clustering_list)):
labels_list.append(list(set(clustering_list[i] + cs)))
cs += len(set(clustering_list[i]))
elif clustering_type == 'reference':
labels_list = [ list(set(clustering)) for clustering in clustering_list]
else:
pass
# make color_dict
color_dict = get_scanpy_color_dict(labels_list)
return population_list, labels_list, color_dict
def get_reference_transition_matrices(Qs, Ts, reference_index):
clustering_list = reference_clustering(Qs, Ts, reference_index)
################################################################################################
# plotting: core functions
################################################################################################
def plot_clustering_list(spatial_list,
clustering_list,
clustering_type='ml',
cell_type_labels=None,
cmap='tab',
title=None,
save_name=None,
dotsize=1,
flip=False):
'''
Input
spatial_list : list of np.ndarray, spatial coordinates for the slices
clustering_list : list of np.ndarray, labels for the spots across the slices
clustering_type : str, 'ml' or 'reference', default='ml'
cmap : str, color map for the clusters, default='tab', other options: 'rainbow'
title : str, title for the plot, default=None
save_name : str, file name to save the plot, default=None
flip : bool, whether to flip the spatial coordinates, default=False
Output
'''
N_slices = len(spatial_list)
if cell_type_labels is None:
cell_type_labels = [None]*N_slices
sns.set_style("white")
sns.set_context("notebook", font_scale=1.5)
fig, axes = plt.subplots(1, N_slices, figsize=(20 * N_slices, 20), facecolor='white')
# Center the spatial coordinates
slices = [S - np.mean(S, axis=0) for S in spatial_list]
# Make color_dict
_, _, color_dict = get_diffmap_inputs(clustering_list, clustering_type, cmap)
# Determine the combined limits of the axes
all_spatial = np.vstack(slices)
x_min, x_max = np.min(all_spatial[:, 0]), np.max(all_spatial[:, 0])
y_min, y_max = np.min(all_spatial[:, 1]), np.max(all_spatial[:, 1])
if clustering_type == 'ml':
cs = 0 # plays same role as in get_diffmap_inputs
for i, (S, value_vec) in enumerate(zip(slices, clustering_list)):
ax = axes[i]
ax.set_facecolor('black')
if clustering_type == 'ml':
value_vec_prime = value_vec + cs
else:
value_vec_prime = value_vec
spatial = S if not flip else S @ np.array([[-1, 0], [0, 1]])
df = pd.DataFrame({'x': spatial[:, 0], 'y': spatial[:, 1], 'value': value_vec_prime})
if clustering_type == 'ml':
cs += len(set(value_vec))
sns.scatterplot(
x='x', y='y', hue='value', palette=color_dict, data=df, ax=ax, s=dotsize, legend=True
)
ax.set_xlim(x_min, x_max)
ax.set_ylim(y_min, y_max)
if flip:
ax.invert_yaxis()
ax.axis('off')
ax.set_title(f'Slice {i+1}\n', color='black')
ax.set_aspect('equal', adjustable='box')
if cell_type_labels[i] is not None:
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles=handles, labels=cell_type_labels[i], title='')
plt.tight_layout()
plt.subplots_adjust(wspace=.4) # Adjust the horizontal spacing between subplots
if title:
plt.suptitle(title, fontsize=36, color='black')
if save_name is not None:
plt.savefig(save_name, dpi=300, transparent=True, bbox_inches="tight", facecolor='black')
plt.show()
return None
def plot_labeled_differentiation(population_list,
transition_list,
label_list,
color_dict,
cell_type_labels=None, # New parameter for node labels
clustering_type='ml',
reference_index=None,
dotsize_factor=1,
linethick_factor=10,
save_name=None,
title=None,
stretch=1,
outline=3):
'''
Args
population_list : list of lists, number of spots in each cluster for each slice
transition_list : list of np.ndarrays, cell type coupling matrices between consecutive slices
label_list : list of lists, unique cluster labels (ints) for each slice
color_dict : dict, dictionary with cluster labels as keys and colors as values
cell_type_labels : list of lists of str, default=None
clustering_type : str, 'ml' or 'reference', default='ml'
reference_index : int, index of the slice to use as reference, default=None
NOTE: reference_index is required if clustering_type='reference'
dotsize_factor : int, factor to scale the size of the dots, default=1
linethick_factor : int, factor to scale the thickness of the lines, default=10
save_name : str, file name to save the plot, default=None
Output
Description
'''
sns.set(style="white") # Set the Seaborn style
dsf = dotsize_factor
ltf = linethick_factor
N_slices = len(population_list)
# form x_positions, y_positions, and colors for each slice
x_positions = []
y_positions = []
for i, population in enumerate(population_list):
y_positions.append(np.arange(len(population)))
x_positions.append(np.ones(len(population)) * (i))
plt.figure(figsize=(stretch*5 * (N_slices - 1), 10)) # Adjust the figure size
for pair_ind, T in enumerate(transition_list):
plt.scatter(x_positions[pair_ind],
y_positions[pair_ind],
c=[color_dict[label] for label in label_list[pair_ind]],
s=dsf*np.array(population_list[pair_ind]),
edgecolor='b',
linewidth=1,
zorder=1)
plt.scatter(x_positions[pair_ind+1],
y_positions[pair_ind+1],
c=[color_dict[label] for label in label_list[pair_ind+1]],
s=dsf*np.array(population_list[pair_ind+1]),
edgecolor='b',
linewidth=1,
zorder=1)
r1 = T.shape[0]
r2 = T.shape[1]
if clustering_type == 'ml':
for i in range(r1):
for j in range(r2):
if T[i, j] > 0: # Plot line only if T[i, j] is greater than 0
# print(len(x_positions), len(x_positions[pair_ind]), len(x_positions[pair_ind+1]))
plt.plot([x_positions[pair_ind][i], x_positions[pair_ind+1][j]],
[y_positions[pair_ind][i], y_positions[pair_ind+1][j]],
'k-', lw=T[i, j] * ltf, zorder=0)
else:
pass
# Add node labels
if cell_type_labels is not None:
for i in range(N_slices):
if cell_type_labels[i] is not None:
for j in range(len(population_list[i])):
txt = plt.text(
x_positions[i][j], y_positions[i][j], cell_type_labels[i][j],
fontsize=12, ha='right', va='bottom'
)
# Add path effect for a white outline
txt.set_path_effects([
path_effects.Stroke(linewidth=outline, foreground='white'), # White outline
path_effects.Normal() # Normal text on top
])
# Add titles and labels
if title:
plt.suptitle(title, fontsize=36, color='black')
else:
plt.title('Differentiation Map')
plt.yticks([])
plt.xticks([])
plt.axis('off')
# Remove the top and right spines
sns.despine()
if save_name is not None:
plt.savefig(save_name, dpi=300, transparent=True, bbox_inches="tight", facecolor='black')
plt.show()
return None
################################################################################################
# plotting: more directly from from output Qs, Ts
################################################################################################
def diffmap_from_QT(Qs,
Ts,
cell_type_labels=None,
clustering_type='ml',
reference_index=None,
title=None,
save_name=None,
dsf=1,
stretch=1,
outline=2):
'''
Args:
Qs : list of (N) np.ndarrays, of shape (n_t, r_t), for each slice
Ts : list of (N-1) np.ndarray, of shape (r_t, r_{t+1}), for each transition
clustering_type : str, 'ml' or 'reference', default='ml'
cell_type_labels : list of (N) lists of str, default=None
reference_index : int, index of the slice to use as reference, default=None
NOTE: reference_index is required if clustering_type='reference'
'''
# make clustering_list
if clustering_type == 'ml':
clustering_list = max_likelihood_clustering(Qs)
elif clustering_type == 'reference':
if reference_index is None:
raise ValueError('Reference index required for reference clustering')
clustering_list = reference_clustering(Qs, Ts, reference_index)
else:
raise ValueError('Invalid clustering type')
# get diffmap inputs
population_list, labels_list, color_dict = get_diffmap_inputs(clustering_list, clustering_type)
# make transition_list
transition_list = Ts
plot_labeled_differentiation(population_list,
transition_list,
labels_list,
color_dict,
cell_type_labels,
clustering_type,
dotsize_factor=dsf,
linethick_factor=10,
title=title,
save_name=save_name,
stretch=stretch,
outline=outline)
return None
def plot_clusters_from_QT(Ss,
Qs,
Ts,
cell_type_labels=None,
clustering_type='ml',
reference_index=None,
title=None,
save_name=None,
dotsize=1,
flip=False):
'''
Args:
Ss : list of (N) np.ndarrays, of shape (n_t, 2), for each slice, spatial coords
Qs : list of (N) np.ndarrays, of shape (n_t, r_t), for each slice
Ts : list of (N-1) np.ndarray, of shape (r_t, r_{t+1}), for each transition
cell_type_labels : list of (N) lists of str, default=None
clustering_type : str, 'ml' or 'reference', default='ml'
reference_index : int, index of the slice to use as reference, default=None
NOTE: reference_index is required if clustering_type='reference'
'''
# make clustering_list
if clustering_type == 'ml':
clustering_list = max_likelihood_clustering(Qs)
elif clustering_type == 'reference':
if reference_index is None:
raise ValueError('Reference index required for reference clustering')
clustering_list = reference_clustering(Qs, Ts, reference_index)
else:
raise ValueError('Invalid clustering type')
plot_clustering_list(spatial_list=Ss,
clustering_list=clustering_list,
cell_type_labels=cell_type_labels,
clustering_type=clustering_type,
cmap='tab',
title=title,
save_name=save_name,
dotsize=dotsize,
flip=flip)
return None
################################################################################################
# plotting: both, directly from from output Qs, Ts
################################################################################################
def both_from_QT(Ss,
Qs,
Ts,
cell_type_labels=None,
clustering_type='ml',
reference_index=None,
save_name=None,
title=None):
'''
Args:
Ss : list of (N) np.ndarrays, of shape (n_t, 2), for each slice, spatial coords
Qs : list of (N) np.ndarrays, of shape (n_t, r_t), for each slice
Ts : list of (N-1) np.ndarray, of shape (r_t, r_{t+1}), for each transition
cell_type_labels : list of (N) lists of str, default=None
clustering_type : str, 'ml' or 'reference', default='ml'
reference_index : int, index of the slice to use as reference, default=None
NOTE: reference_index is required if clustering_type='reference'
save_name : str, file name to save the plot, default=None
title : str, title for the plot, default=None
'''
diffmap_from_QT(Qs=Qs,
Ts=Ts,
clustering_type=clustering_type,
cell_type_labels=cell_type_labels,
reference_index=reference_index,
title=title,
save_name=save_name)
plot_clusters_from_QT(Ss=Ss,
Qs=Qs,
Ts=Ts,
cell_type_labels=cell_type_labels,
clustering_type=clustering_type,
reference_index=reference_index,
title=title,
save_name=save_name)
return None
# TODO:
# whenever we're plotting the original zf clusters, it would be nice to use their original color scheme.
################################################################################################
# sankey plotting
################################################################################################
def rgba_to_plotly_string(rgba):
''' Convert a list of [r, g, b, a] values to an rgba string for plotly '''
r, g, b, a = rgba
return f'rgba({int(r * 255)}, {int(g * 255)}, {int(b * 255)}, {a})'
def plot_labeled_differentiation_sankey(population_list,
transition_list,
label_list,
color_dict,
cell_type_labels=None, # New parameter for node labels
clustering_type='ml',
reference_index=None,
dotsize_factor=1,
linethick_factor=10,
plot_height=600,
plot_width=1000,
save_name=None,
title=None,
save_as_svg=True, # New parameter for saving as SVG
threshold=0): # New parameter for thresholding transitions
'''
Args
population_list : list of lists, number of spots in each cluster for each slice
transition_list : list of np.ndarrays, cell type coupling matrices between consecutive slices
label_list : list of lists, unique cluster labels (ints) for each slice
color_dict : dict, dictionary with cluster labels as keys and colors as values
cell_type_labels : list of lists of str, default=None or None
clustering_type : str, 'ml' or 'reference', default='ml'
reference_index : int, index of the slice to use as reference, default=None
dotsize_factor : int, factor to scale the size of the dots, default=1
linethick_factor : int, factor to scale the thickness of the lines, default=10
plot_height : int, height of the plot in pixels, default=600
save_name : str, file name to save the plot, default=None
save_as_svg : bool, if True, saves the plot as SVG for vector editing
threshold : float, minimum value of transitions to include in the plot, default=0
Output
Description
'''
N_slices = len(population_list)
# Prepare node and link data for Sankey plot
node_labels = []
link_sources = []
link_targets = []
link_values = []
node_colors = []
node_idx_map = {} # To keep track of node indices for different slices
current_node_idx = 0
# Build nodes and transitions (links)
for slice_idx, population in enumerate(population_list):
for i, label in enumerate(label_list[slice_idx]):
# Add node label; handle None case
if cell_type_labels and cell_type_labels[slice_idx] is not None:
node_label = cell_type_labels[slice_idx][i] if cell_type_labels[slice_idx][i] is not None else str(label)
else:
node_label = str(label) # Default to the cluster label if no cell type labels are given
node_labels.append(node_label)
node_idx_map[(slice_idx, i)] = current_node_idx # Map to node index
# Convert color to a plotly-friendly format
node_colors.append(rgba_to_plotly_string(color_dict[label]))
current_node_idx += 1
for pair_ind, T in enumerate(transition_list):
r1 = T.shape[0]
r2 = T.shape[1]
for i in range(r1):
for j in range(r2):
if T[i, j] > threshold: # Apply threshold here
source_node = node_idx_map[(pair_ind, i)]
target_node = node_idx_map[(pair_ind + 1, j)]
link_sources.append(source_node)
link_targets.append(target_node)
link_values.append(T[i, j] * linethick_factor)
# Create the Sankey plot
fig = go.Figure(go.Sankey(
node=dict(
pad=15,
thickness=20,
line=dict(color="black", width=0.5),
label=node_labels,
color=node_colors, # Correctly formatted colors
),
link=dict(
source=link_sources, # Indices of source nodes
target=link_targets, # Indices of target nodes
value=link_values # Flow values for transitions
)
))
# Add title and adjust height
fig.update_layout(
title_text=title if title else 'Differentiation Map',
font_size=24,
height=plot_height,
width=plot_width
)
# Save plot if needed
if save_as_svg and save_name is not None:
fig.write_image(f"{save_name}.svg") # Save as SVG
elif save_as_svg and save_name is None:
fig.write_image("diffmap.svg") # Save as default format (e.g., PNG)
fig.show()
return None
def alphabetic_key(label):
''' Custom key to sort labels alphabetically, ignoring non-alphabetic characters using regex '''
# Use regular expression to remove non-alphabetic characters
return re.sub('[^a-zA-Z]', '', label)
def plot_labeled_differentiation_sankey_sorted(population_list,
transition_list,
label_list,
color_dict,
cell_type_labels=None, # New parameter for node labels
clustering_type='ml',
reference_index=None,
dotsize_factor=1,
linethick_factor=10,
plot_height=600, # New parameter for height adjustment
plot_width=800, # New parameter for width adjustment
save_name=None,
title=None,
save_as_svg=False,
threshold=0): # New parameter for saving as SVG
'''
Args
population_list : list of lists, number of spots in each cluster for each slice
transition_list : list of np.ndarrays, cell type coupling matrices between consecutive slices
label_list : list of lists, unique cluster labels (ints) for each slice
color_dict : dict, dictionary with cluster labels as keys and colors as values
cell_type_labels : list of lists of str, default=None or None
clustering_type : str, 'ml' or 'reference', default='ml'
reference_index : int, index of the slice to use as reference, default=None
dotsize_factor : int, factor to scale the size of the dots, default=1
linethick_factor : int, factor to scale the thickness of the lines, default=10
plot_height : int, height of the plot in pixels, default=600
plot_width : int, width of the plot in pixels, default=800
save_name : str, file name to save the plot, default=None
save_as_svg : bool, if True, saves the plot as SVG for vector editing
Output
Description
'''
N_slices = len(population_list)
# Prepare node and link data for Sankey plot
node_labels = []
link_sources = []
link_targets = []
link_values = []
node_colors = []
node_x = []
node_y = []
node_idx_map = {} # To keep track of node indices for different slices
current_node_idx = 0
# Step 1: Sort labels using the custom key that ignores non-alphabetic characters
sorted_label_list = []
sorted_indices_list = []
for slice_idx, labels in enumerate(label_list):
# Get sorted indices based on the custom alphabetic key, with fallback if cell_type_labels is None
sorted_indices = sorted(
range(len(labels)),
key=lambda x: alphabetic_key(cell_type_labels[slice_idx][x] if cell_type_labels and cell_type_labels[slice_idx] is not None else str(labels[x]))
)
sorted_indices_list.append(sorted_indices)
# Sort the labels according to the custom key, handle None case
sorted_labels = [
cell_type_labels[slice_idx][i] if cell_type_labels and cell_type_labels[slice_idx] is not None else str(labels[i])
for i in sorted_indices
]
sorted_label_list.append(sorted_labels)
# Step 2: Compute vertical positions (y-coordinates) for sorted labels
num_nodes = len(sorted_labels)
# Dynamically adjust the spacing between nodes to avoid overlap
# We space the y positions from 1 to 0, but with some additional padding
padding = 0.1 / num_nodes # This will introduce some space between nodes to prevent overlap
y_positions = np.linspace(1 - padding, padding, num_nodes) # Vertically space with a margin
# Build the nodes based on sorted labels and assign y-coordinates
for idx, i in enumerate(sorted_indices):
node_label = cell_type_labels[slice_idx][i] if cell_type_labels and cell_type_labels[slice_idx] is not None else str(labels[i])
node_labels.append(node_label)
node_idx_map[(slice_idx, i)] = current_node_idx # Map to node index
node_colors.append(rgba_to_plotly_string(color_dict[labels[i]]))
node_x.append(slice_idx / (N_slices - 1)) # Evenly spaced x-coordinates
node_y.append(y_positions[idx]) # Assign y-coordinates with spacing to prevent overlap
current_node_idx += 1
# Step 3: Reorder the transition links to reflect sorted indices
for pair_ind, T in enumerate(transition_list):
r1 = T.shape[0]
r2 = T.shape[1]
for i in range(r1):
for j in range(r2):