dspin 1.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dspin/plot.py ADDED
@@ -0,0 +1,776 @@
1
+ # -*-coding:utf-8 -*-
2
+ '''
3
+ @Time : 2023/10/30 15:41
4
+ @Author : Jialong Jiang, Yingying Gong
5
+ '''
6
+
7
+ import matplotlib.pyplot as plt
8
+ import matplotlib.patheffects as patheffects
9
+ from sklearn.cluster import KMeans
10
+ import numpy as np
11
+ import pandas as pd
12
+ import csv
13
+ import scanpy as sc
14
+ import igraph as ig
15
+ import leidenalg as la
16
+ import networkx as nx
17
+ from scipy import optimize
18
+ from scipy.spatial.distance import pdist, squareform
19
+ from scipy.cluster.hierarchy import linkage, leaves_list
20
+ import matplotlib as mpl
21
+ from typing import List, Dict, Optional, Tuple
22
+
23
+
24
+ def onmf_to_csv(features, gene_name, file_path, thres=0.01, if_write_weights=False):
25
+ """
26
+ Extract gene names in ONMF to CSV.
27
+
28
+ Parameters:
29
+ features (numpy.ndarray): oNMF features to be written to CSV.
30
+ gene_name (List[str]): Names of the genes corresponding to feature indices.
31
+ file_path (str): Path of the file where the CSV will be written.
32
+ thres (float): Threshold value for filtering features.
33
+ if_write_weights (bool): If True, writes weights to the CSV file.
34
+
35
+ Returns:
36
+ str: Path of the created CSV file.
37
+ """
38
+ num_spin = features.shape[0]
39
+ # Initialize a variable to record the maximum number of genes observed.
40
+ rec_max_gene = 0
41
+ with open(file_path, 'w', newline='') as file:
42
+ write = csv.writer(file)
43
+ for spin in range(num_spin):
44
+ # Count the number of genes that exceed the threshold for the
45
+ # current spin
46
+ num_gene_show = np.sum(features[spin, :] > thres)
47
+ rec_max_gene = max(rec_max_gene, num_gene_show)
48
+ # Get the indices of the top genes for the current spin
49
+ gene_ind = np.argsort(-features[spin, :])[:num_gene_show]
50
+ cur_line = list(gene_name[gene_ind])
51
+ # Insert the spin number at the beginning of the row
52
+ cur_line.insert(0, spin)
53
+ write.writerow(cur_line) # Write the current line to the CSV file
54
+
55
+ if if_write_weights:
56
+ cur_line = [features[spin, ind].round(6) for ind in gene_ind]
57
+ cur_line.insert(0, f'Weights_{spin}')
58
+ write.writerow(cur_line)
59
+
60
+ # Read the created CSV, transpose it and save it again
61
+ pd_csv = pd.read_csv(file_path, names=range(rec_max_gene + 1))
62
+ pd_csv = pd_csv.transpose()
63
+ pd_csv.to_csv(file_path, header=False, index=False)
64
+
65
+ return file_path
66
+
67
+
68
+ def onmf_gene_program_info(features, gene_name, num_gene_show, fig_folder=None):
69
+ """
70
+ Plot gene program compositions and weights from oNMF.
71
+
72
+ Parameters:
73
+ features (numpy.ndarray): oNMF features to be plotted.
74
+ gene_name (List[str]): Names of the genes corresponding to feature indices.
75
+ num_gene_show (int): Number of genes to show in the plot.
76
+ fig_folder (str): Folder where the figure will be saved.
77
+
78
+ Returns:
79
+ str: Path of the created figure.
80
+ """
81
+ thres = 0.01
82
+ num_spin = features.shape[0]
83
+ sc.set_figure_params(figsize=[1.5, 3.6])
84
+ fig, grid = sc.pl._tools._panel_grid(0.26, 0.9, ncols=6, num_panels=num_spin)
85
+
86
+ for spin in range(num_spin):
87
+ ax = plt.subplot(grid[spin])
88
+
89
+ cur_num_gene_show = min(num_gene_show, np.sum(features[spin, :] > thres))
90
+ gene_ind = np.argsort(- features[spin, :])[: cur_num_gene_show]
91
+ plt.plot(features[spin, gene_ind], np.arange(cur_num_gene_show), 'o')
92
+ plt.grid()
93
+ plt.xlim([0, 1.1 * np.max(features[spin, gene_ind])])
94
+ plt.ylim([-0.5, num_gene_show - 0.5])
95
+ plt.yticks(np.arange(cur_num_gene_show), gene_name[gene_ind], fontsize=9);
96
+ plt.gca().invert_yaxis()
97
+
98
+ plt.title(spin)
99
+
100
+ if fig_folder is not None:
101
+ plt.savefig(fig_folder + 'onmf_gene_program_info.png', dpi=300, bbox_inches='tight')
102
+
103
+ return(fig_folder + 'onmf_gene_program_info.png')
104
+
105
+
106
+ def assign_program_position(onmf_rep_ori, umap_all, repulsion=2):
107
+ """
108
+ Assign the position of gene programs on the UMAP plot.
109
+
110
+ Args:
111
+ onmf_rep_ori (numpy.ndarray): The gene or gene program representation of the transformed data.
112
+ umap_all (numpy.ndarray): The UMAP coordinates of all cells.
113
+ repulsion (float): The repulsion strength for the layout optimization.
114
+
115
+ Returns:
116
+ numpy.ndarray: The assigned positions of gene programs on the UMAP plot.
117
+ """
118
+
119
+ num_spin = onmf_rep_ori.shape[1]
120
+ program_umap_pos = np.zeros([num_spin, 2])
121
+ for ii in range(num_spin):
122
+ weight_sub = onmf_rep_ori[:, ii].copy()
123
+ weight_sub[weight_sub < np.percentile(weight_sub, 99.5)] = 0
124
+ program_umap_pos[ii, :] = np.sum(umap_all * weight_sub.reshape(- 1, 1) / np.sum(weight_sub), axis=0)
125
+
126
+ ori_pos = program_umap_pos.copy()
127
+ def layout_loss_fun(xx):
128
+ xx = xx.reshape(- 1, 2)
129
+ attract = np.sum((xx - ori_pos) ** 2)
130
+ repulse = np.sum(repulsion / pdist(xx))
131
+ return attract + repulse
132
+
133
+ opt_res = optimize.minimize(layout_loss_fun, program_umap_pos.flatten())
134
+ program_umap_pos = opt_res.x.reshape(- 1, 2)
135
+
136
+ sc.set_figure_params(figsize=[4, 4])
137
+
138
+ if umap_all.shape[0] <= 5e4:
139
+ plt.scatter(umap_all[:, 0], umap_all[:, 1], s=1, c='#bbbbbb', alpha=min(1, 1e4 / umap_all.shape[0]))
140
+ else:
141
+ sele_ind = np.random.choice(umap_all.shape[0], size=50000, replace=False).astype(int)
142
+ plt.scatter(umap_all[sele_ind, 0], umap_all[sele_ind, 1], s=1, c='#bbbbbb', alpha=0.2)
143
+
144
+ for ii in range(num_spin):
145
+ plt.text(program_umap_pos[ii, 0], program_umap_pos[ii, 1], str(ii), fontsize=10)
146
+ plt.axis('off')
147
+
148
+ return program_umap_pos
149
+
150
+
151
+ def gene_program_on_umap(onmf_rep, umap_all, program_umap_pos, fig_folder=None, subsample=True):
152
+ """
153
+ Plot gene programs on the UMAP plot.
154
+
155
+ Args:
156
+ onmf_rep (numpy.ndarray): The gene or gene program representation of the transformed data.
157
+ umap_all (numpy.ndarray): The UMAP coordinates of all cells.
158
+ program_umap_pos (numpy.ndarray): The assigned positions of gene programs on the UMAP plot.
159
+ fig_folder (str): The folder where the output figure is saved.
160
+ subsample (bool): Whether to subsample the data for plotting.
161
+
162
+ Returns:
163
+ str: Path of the created figure.
164
+ """
165
+
166
+ num_spin = onmf_rep.shape[1]
167
+
168
+ if subsample:
169
+ num_subsample = 20000
170
+ sub_ind = np.random.choice(onmf_rep.shape[0], num_subsample, replace=False)
171
+ onmf_rep = onmf_rep[sub_ind, :]
172
+ umap_all = umap_all[sub_ind, :]
173
+
174
+ sc.set_figure_params(figsize=[2, 2])
175
+ fig, grid = sc.pl._tools._panel_grid(0.2, 0.06, ncols=6, num_panels=num_spin)
176
+ for spin in range(num_spin):
177
+ ax = plt.subplot(grid[spin])
178
+
179
+ plot_data = onmf_rep[:, spin].copy()
180
+ plot_data /= np.percentile(plot_data, 95)
181
+ plot_data = plot_data.clip(0, 3)
182
+
183
+ plt.scatter(umap_all[:, 0], umap_all[:, 1], c=plot_data, s=1,
184
+ alpha=0.5, vmax=1.2, cmap='BuPu', vmin=-0.1)
185
+ plt.text(program_umap_pos[spin, 0], program_umap_pos[spin, 1], str(spin), fontsize=12, path_effects=[patheffects.withStroke(linewidth=3, foreground='w')])
186
+ ax.set_aspect('equal')
187
+ plt.xticks([])
188
+ plt.yticks([])
189
+ plt.title(spin)
190
+
191
+ if fig_folder is not None:
192
+ plt.savefig(fig_folder + 'gene_program_on_umap.png', dpi=300, bbox_inches='tight')
193
+ return fig_folder + 'gene_program_on_umap.png'
194
+
195
+
196
+ def visualize_program_expression(onmf_summary, spin_name, gene_matrix, onmf_rep_tri, fig_folder=None,
197
+ num_gene_select=10, n_clusters=5, subsample=10000):
198
+ """
199
+ Heatmap comparisons between gene expression matrix and gene program representation.
200
+
201
+ Args:
202
+ onmf_summary (sklearn.decomposition.NMF): The ONMF summary object.
203
+ spin_name (List[str]): The names of gene programs.
204
+ gene_matrix (numpy.ndarray): The gene expression matrix.
205
+ onmf_rep_tri (numpy.ndarray): The gene program representation.
206
+ fig_folder (str): The folder where the output figure is saved.
207
+ num_gene_select (int): The number of genes to be selected for each gene program. Default is 10.
208
+ n_clusters (int): The number of clusters for KMeans clustering. Default is 5.
209
+ subsample (int): The number cell subsampling for visualization. Default is 10000.
210
+
211
+ Returns:
212
+ str: Path of the created figure.
213
+ """
214
+ # Extracting feature components from the ONMF summary object
215
+ features = onmf_summary.components_
216
+ num_spin, num_gene = features.shape
217
+
218
+ # Identifying the gene programs by selecting the highest feature component
219
+ # for each gene
220
+ gene_mod_ind = np.argmax(features, axis=0)
221
+
222
+ # For each spin, identifying the top selected genes and aggregating them into gene_mod_use
223
+ gene_mod_use = []
224
+ for ind in range(num_spin):
225
+ gene_in_mod = np.where(gene_mod_ind == ind)[0]
226
+ cur_gene = gene_in_mod[np.argsort(- features[ind,
227
+ gene_in_mod])[: num_gene_select]]
228
+ gene_mod_use += list(cur_gene)
229
+ gene_mod_use = np.array(gene_mod_use)
230
+
231
+ # Creating a subset of cells and reordering them based on KMeans clustering
232
+ np.random.seed(0)
233
+
234
+ onmf_rep_subset = onmf_rep_tri
235
+ subset_ind = range(gene_matrix.shape[0])
236
+ if subsample:
237
+ if gene_matrix.shape[0] > subsample:
238
+ subset_ind = np.random.choice(range(gene_matrix.shape[0]), size=subsample, replace=False)
239
+ onmf_rep_subset = onmf_rep_tri[subset_ind, :]
240
+
241
+ cell_order = np.argsort(KMeans(n_clusters=n_clusters).fit_predict(onmf_rep_subset))
242
+ gene_matrix_subset = gene_matrix[subset_ind, :][:, gene_mod_use].copy()
243
+ gene_matrix_subset /= np.max(gene_matrix, axis=0)[gene_mod_use].clip(0.2, np.inf)
244
+
245
+ sc.set_figure_params(figsize=[10, 5])
246
+
247
+ # Plotting Gene Expression Subplot Before Decomposition
248
+ plt.subplot(1, 2, 1)
249
+ plt.imshow(gene_matrix_subset[cell_order, :].T,
250
+ aspect='auto', cmap='Blues', interpolation='none')
251
+ plt.ylabel('Gene')
252
+ plt.xlabel('Cell')
253
+ plt.title('Gene expression')
254
+ plt.grid()
255
+
256
+ # Plotting Gene Program Expression Subplot After Decomposition
257
+ plt.subplot(1, 2, 2)
258
+ plt.imshow(onmf_rep_subset[cell_order, :].T,
259
+ aspect='auto', cmap='Blues', interpolation='none')
260
+ plt.yticks(range(num_spin), spin_name, fontsize=12)
261
+ plt.gca().yaxis.set_ticks_position('right')
262
+ plt.xlabel('Cell')
263
+ plt.title('Gene program expression')
264
+ plt.grid()
265
+
266
+ if fig_folder is not None:
267
+ plt.savefig(fig_folder + 'gene_program_decomposition.png', bbox_inches='tight')
268
+ return fig_folder + 'gene_program_decomposition.png'
269
+
270
+
271
+ def format_label(label):
272
+ """
273
+ Format the label string by inserting a newline character after every second word.
274
+
275
+ Parameters:
276
+ label (str): The label string to be formatted.
277
+
278
+ Returns:
279
+ str: The formatted label string.
280
+ """
281
+ parts = label.split('_')
282
+ i = 0
283
+ # Loop through the parts and insert a newline character after every second
284
+ # part
285
+ while i < len(parts) - 1:
286
+ if i % 2 == 1:
287
+ parts[i] = parts[i] + '\n'
288
+ i += 1
289
+
290
+ return '_'.join(parts)
291
+
292
+
293
+ def temporary_spin_name(csv_file, num_gene: int = 4):
294
+ """
295
+ Create temporary spin names based on the CSV file content.
296
+
297
+ Parameters:
298
+ csv_file (str): Path to the CSV file containing the data.
299
+ num_gene (int): Number of genes to be considered for creating the name.
300
+
301
+ Returns:
302
+ List[str]: A list containing the generated temporary spin names.
303
+ """
304
+ df = pd.read_csv(csv_file, header=None)
305
+ # Create spin names by joining the first 4 elements (or less if not
306
+ # available) of each column in the dataframe
307
+ spin_names = ['P' + '_'.join(map(str, df[col][:num_gene])) for col in df.columns]
308
+ spin_names_formatted = [format_label(name) for name in spin_names]
309
+ return spin_names_formatted
310
+
311
+
312
+ def plot_network_heatmap(j_mat: np.ndarray,
313
+ module_list: List[List[int]] = None,
314
+ spin_name_list: Optional[List[str]] = None,
315
+ fig_folder: Optional[str] = None) -> None:
316
+ """
317
+ Plot a heatmap of the network adjacency matrix with modules highlighted.
318
+
319
+ Parameters
320
+ ----------
321
+ j_mat : np.ndarray
322
+ The network adjacency matrix.
323
+ modules : List[List[int]]
324
+ List of modules, where each module is a list of node indices.
325
+ spin_name_list : List[str], optional
326
+ List of names for the spins/nodes. If None, will use generic names.
327
+ fig_folder : str, optional
328
+ Folder path to save the figure. If None, will not save the figure.
329
+ """
330
+ num_spin = j_mat.shape[0]
331
+ j_mat = j_mat.copy()
332
+ np.fill_diagonal(j_mat, 0)
333
+
334
+ if module_list is None:
335
+ module_list = [list(np.arange(num_spin))]
336
+
337
+ # Create spin order based on modules
338
+ spin_order = [spin for cur_list in module_list for spin in cur_list]
339
+ net_class_len = [len(cur_list) for cur_list in module_list]
340
+
341
+ # Create custom colormap
342
+ cmap_hvec = mpl.colors.LinearSegmentedColormap.from_list("", ['#E84B23', '#FFFFFF', '#3285CC'])
343
+
344
+ color_thres = np.percentile(np.abs(j_mat[j_mat != 0]), 95)
345
+
346
+ # Plot heatmap
347
+ sc.set_figure_params(figsize=[0.18 * num_spin + 0.5, 0.18 * num_spin])
348
+ plt.imshow(j_mat[:, spin_order][spin_order, :], cmap=cmap_hvec, vmin=- color_thres, vmax=color_thres)
349
+
350
+ # Add module boundaries
351
+ for ii in range(len(net_class_len)):
352
+ plt.axhline(np.sum(net_class_len[:ii]) - 0.5, color='k', linewidth=1)
353
+ plt.axvline(np.sum(net_class_len[:ii]) - 0.5, color='k', linewidth=1)
354
+
355
+ fsize = 9
356
+
357
+ # Set labels
358
+ if spin_name_list is not None:
359
+ plt.xticks(np.arange(len(spin_order)), np.array(spin_name_list)[spin_order], rotation=90, fontsize=fsize)
360
+ plt.yticks(np.arange(len(spin_order)), np.array(spin_name_list)[spin_order], fontsize=fsize)
361
+ else:
362
+ plt.xticks(np.arange(len(spin_order)), [f'P{spin}' for spin in spin_order], rotation=90, fontsize=fsize)
363
+ plt.yticks(np.arange(len(spin_order)), [f'P{spin}' for spin in spin_order], fontsize=fsize)
364
+
365
+ plt.colorbar(fraction=0.6 / num_spin)
366
+ plt.grid()
367
+
368
+ # Save figure if folder is provided
369
+ if fig_folder is not None:
370
+ plt.savefig(fig_folder + 'network_heatmap.png', dpi=300, bbox_inches='tight')
371
+
372
+ plt.show()
373
+
374
+
375
+ def plot_response_heatmap(h_vec: np.ndarray,
376
+ module_list: List[List[int]]=None,
377
+ spin_name_list: Optional[List[str]] = None,
378
+ sample_list: Optional[List[str]] = None,
379
+ fig_folder: Optional[str] = None) -> None:
380
+ """
381
+ Plot a heatmap of the response matrix with modules highlighted.
382
+
383
+ Parameters
384
+ ----------
385
+ h_vec : np.ndarray
386
+ The response vector.
387
+ modules : List[List[int]]
388
+ List of modules, where each module is a list of node indices.
389
+ spin_name_list : List[str], optional
390
+ List of names for the spins/nodes. If None, will use generic names.
391
+ sample_list : List[str], optional
392
+ List of names for the samples. If None, will use generic names.
393
+ fig_folder : str, optional
394
+ Folder path to save the figure. If None, will not save the figure.
395
+ """
396
+ num_spin, num_sample = h_vec.shape
397
+ h_vec = h_vec.copy()
398
+
399
+ if module_list is None:
400
+ module_list = [list(leaves_list(linkage(h_vec, method='ward')))]
401
+
402
+ # Create spin order based on modules
403
+ spin_order = [spin for cur_list in module_list for spin in cur_list]
404
+ net_class_len = [len(cur_list) for cur_list in module_list]
405
+ sample_order = leaves_list(linkage(h_vec.T, method='ward'))
406
+
407
+ cmap_hvec = mpl.colors.LinearSegmentedColormap.from_list("", ['#E84B23', '#FFFFFF', '#3285CC'])
408
+
409
+ # Plot heatmap
410
+ sc.set_figure_params(figsize=[0.16 * num_spin + 0.5, 0.16 * num_sample])
411
+ plt.imshow(h_vec.T[sample_order, :][:, spin_order], cmap=cmap_hvec, vmin=- 1, vmax=1, aspect='auto')
412
+
413
+ # Add module boundaries
414
+ for ii in range(len(net_class_len)):
415
+ plt.axvline(np.sum(net_class_len[:ii]) - 0.5, color='k', linewidth=1)
416
+
417
+ # Set labels
418
+ if spin_name_list is not None:
419
+ plt.xticks(np.arange(len(spin_order)), np.array(spin_name_list)[spin_order], rotation=90, fontsize=8)
420
+ else:
421
+ plt.xticks(np.arange(len(spin_order)), [f'P{spin}' for spin in spin_order], rotation=90, fontsize=8)
422
+
423
+ plt.yticks(np.arange(len(sample_order)), np.array(sample_list)[sample_order], fontsize=8)
424
+
425
+ plt.colorbar(fraction=1 / num_spin)
426
+ plt.grid()
427
+
428
+ # Save figure if folder is provided
429
+ if fig_folder is not None:
430
+ plt.savefig(fig_folder + 'module_heatmap.png', dpi=300, bbox_inches='tight')
431
+
432
+ plt.show()
433
+
434
+
435
+ def create_undirected_network(j_mat, node_names, thres_strength=0.05):
436
+
437
+ np.fill_diagonal(j_mat, 0)
438
+
439
+ # Filter weak connections
440
+ j_filt = j_mat.copy()
441
+ j_filt[np.abs(j_mat) < thres_strength] = 0
442
+ np.fill_diagonal(j_filt, 0)
443
+
444
+ # Create networkx graph
445
+ G = nx.from_numpy_array(j_filt)
446
+ G = nx.relabel_nodes(G, {ii: node_names[ii] for ii in range(len(node_names))})
447
+
448
+ return G, j_filt
449
+
450
+
451
+ def create_directed_network(j_mat, node_names,thres_strength=0.05, thres_direction=0.05):
452
+
453
+ np.fill_diagonal(j_mat, 0)
454
+
455
+ num_spin = j_mat.shape[0]
456
+ j_mat_directed = np.zeros(j_mat.shape)
457
+
458
+ for ind1 in range(num_spin):
459
+ for ind2 in range(num_spin):
460
+ if np.abs(j_mat[ind1, ind2]) > (1 + thres_direction) * np.abs(j_mat[ind2, ind1]):
461
+ j_mat_directed[ind1, ind2] = j_mat[ind1, ind2]
462
+ elif np.abs(j_mat[ind2, ind1]) > (1 + thres_direction) * np.abs(j_mat[ind1, ind2]):
463
+ j_mat_directed[ind2, ind1] = j_mat[ind2, ind1]
464
+ else:
465
+ j_mat_directed[ind1, ind2] = (j_mat[ind1, ind2] + j_mat[ind2, ind1]) / 2
466
+ j_mat_directed[ind2, ind1] = j_mat_directed[ind1, ind2]
467
+
468
+ j_mat_directed[np.abs(j_mat_directed) < thres_strength] = 0
469
+
470
+ G = nx.from_numpy_array(j_mat_directed.T, create_using=nx.DiGraph)
471
+ G = nx.relabel_nodes(G, {ii: node_names[ii] for ii in range(len(node_names))})
472
+
473
+ return G, j_mat_directed
474
+
475
+
476
+ def layout_loss_fun(xx, ori_pos, network):
477
+
478
+ stength_module = 12
479
+ strength_repulsion = 12
480
+ strength_spring = 1
481
+
482
+ spring_length = 2
483
+ spring_weight = np.abs(network)
484
+
485
+ module_length = 1
486
+
487
+ xx = xx.reshape(- 1, 2)
488
+ module_dist = np.clip(np.sqrt(((xx - ori_pos) ** 2).sum(axis=1)) - module_length, 0, None)
489
+ module_attract = np.sum(module_dist ** 2)
490
+ repulse = np.sum(1 / pdist(xx))
491
+
492
+ pairwise_distance = squareform(pdist(xx))
493
+ clip_dist = np.clip(pairwise_distance - spring_length, 0, None)
494
+ spring_energy = np.sum(clip_dist ** 2 * spring_weight)
495
+
496
+ return stength_module * module_attract + strength_repulsion * repulse + strength_spring * spring_energy
497
+
498
+
499
+ def remove_peripheral_nodes(G, min_in_degree=1, min_out_degree=1, max_iter=100):
500
+
501
+ cur_size = len(G.nodes)
502
+
503
+ for repeat in range(max_iter):
504
+
505
+ remove_nodes1 = [node for node, out_degree in G.out_degree() if out_degree <= min_out_degree]
506
+ remove_nodes2 = [node for node, in_degree in G.in_degree() if in_degree <= min_in_degree]
507
+ remove_nodes = list(set(remove_nodes1 + remove_nodes2))
508
+ G.remove_nodes_from(remove_nodes)
509
+
510
+ if cur_size == len(G.nodes):
511
+ break
512
+ else:
513
+ cur_size = len(G.nodes)
514
+
515
+ print(f'{len(G.nodes)} nodes after filtering ')
516
+
517
+ return G
518
+
519
+
520
+ def compute_modules(G_nx, resolution: float = 1.0,
521
+ seed: int = 1) -> List[List[int]]:
522
+ """
523
+ Compute network modules using Leiden community detection.
524
+
525
+ Parameters
526
+ ----------
527
+ resolution : float, optional
528
+ Resolution parameter for Leiden community detection. Higher values lead to more modules. Default is 1.0.
529
+ seed : int, optional
530
+ Random seed for Leiden community detection. Default is 0.
531
+ thres : float, optional
532
+ Threshold for filtering weak connections. Default is 0.
533
+
534
+ Returns
535
+ -------
536
+ List[List[int]]
537
+ List of modules, where each module is a list of node indices.
538
+ """
539
+
540
+ adj_matrix = nx.to_numpy_array(G_nx, weight='weight')
541
+ sub_gene_list = list(G_nx.nodes)
542
+
543
+ G = nx.from_numpy_array(adj_matrix)
544
+
545
+ G = ig.Graph.from_networkx(G)
546
+
547
+ # Separate positive and negative edges
548
+ G_pos = G.subgraph_edges(G.es.select(weight_gt=0), delete_vertices=False)
549
+ G_neg = G.subgraph_edges(G.es.select(weight_lt=0), delete_vertices=False)
550
+ G_neg.es['weight'] = [-w for w in G_neg.es['weight']]
551
+
552
+ # Apply Leiden community detection
553
+ part_pos = la.RBConfigurationVertexPartition(G_pos, weights='weight', resolution_parameter=resolution)
554
+ part_neg = la.RBConfigurationVertexPartition(G_neg, weights='weight', resolution_parameter=resolution)
555
+
556
+ optimiser = la.Optimiser()
557
+ optimiser.set_rng_seed(seed)
558
+
559
+ diff = optimiser.optimise_partition_multiplex([part_pos, part_neg], layer_weights=[1, -1])
560
+
561
+ # Extract modules
562
+ net_class = list(part_pos)
563
+
564
+ print(f'Identified {len(net_class)} modules with sizes {np.array([len(cur_list) for cur_list in net_class])}')
565
+
566
+ abs_degree = np.abs(adj_matrix).sum(axis=1)
567
+
568
+ # rank node inside each module by degree
569
+ for ii in range(len(net_class)):
570
+ cur_list = net_class[ii]
571
+ cur_list = sorted(cur_list, key=lambda xx: abs_degree[xx], reverse=True)
572
+ net_class[ii] = cur_list
573
+
574
+ return net_class
575
+
576
+
577
+ def compute_module_angle(module_size, gap_size):
578
+
579
+ module_plots_size = np.sqrt(module_size)
580
+
581
+ angle_list = []
582
+ cur_angle = 0
583
+
584
+ for ii in range(len(module_plots_size)):
585
+ angle_list.append(cur_angle)
586
+ cur_angle += module_plots_size[ii] + gap_size
587
+
588
+ angle_list = np.array(angle_list)
589
+ angle_list /= (cur_angle / (2 * np.pi))
590
+
591
+ return angle_list
592
+
593
+
594
+ def compute_network_positions(network, net_class, circle_size=None, module_angle=None, gap_size=0.5):
595
+
596
+ module_size = np.array([len(cur_list) for cur_list in net_class])
597
+ num_spin = np.sum(module_size)
598
+
599
+ if module_angle is None:
600
+ module_angle = compute_module_angle(module_size, gap_size)
601
+
602
+ if circle_size is None:
603
+ circle_size = num_spin ** 0.3
604
+
605
+ module_center = circle_size * np.array([np.cos(module_angle), np.sin(module_angle)]).T
606
+
607
+ pos_module = np.zeros((num_spin, 2))
608
+
609
+ for ii in range(len(module_size)):
610
+ cur_list = net_class[ii]
611
+ pos_module[cur_list, :] = module_center[ii, :]
612
+
613
+ pos_module += np.random.normal(0, 0.05, pos_module.shape)
614
+
615
+ ori_pos = pos_module.copy()
616
+
617
+ opt_res = optimize.minimize(lambda xx : layout_loss_fun(xx, ori_pos, network), pos_module.flatten())
618
+ pos_raw = opt_res.x.reshape(- 1, 2)
619
+
620
+ pos = {ii: pos_raw[ii] for ii in range(num_spin)}
621
+
622
+ return pos
623
+
624
+
625
+ def plot_network_diagram(j_mat: np.ndarray,
626
+ modules: List[List[int]],
627
+ directed: bool = False,
628
+ pos: Optional[Dict[int, Tuple[float, float]]] = None,
629
+ weight_thres: float = None,
630
+ spin_name_list_short: Optional[List[str]] = None,
631
+ fig_folder: Optional[str] = None) -> None:
632
+ """
633
+ Plot the network diagram with modules highlighted.
634
+
635
+ Parameters
636
+ ----------
637
+ j_mat : np.ndarray
638
+ The network adjacency matrix.
639
+ modules : List[List[int]]
640
+ List of modules, where each module is a list of node indices.
641
+ directed : bool, optional
642
+ Whether the network is directed. If True, will plot the network as a directed network.
643
+ pos : Dict[int, Tuple[float, float]], optional
644
+ Dictionary mapping node indices to (x, y) positions. If None, positions will be computed automatically.
645
+ weight_thres : float, optional
646
+ Threshold for filtering weak connections. If None, will filter by the 10th percentile of the absolute nonzero values of the network adjacency matrix.
647
+ spin_name_list_short : List[str], optional
648
+ Short names for the spins/nodes (used for labels). If None, will use generic names.
649
+ fig_folder : str, optional
650
+ Folder path to save the figure. If None, will not save the figure.
651
+ """
652
+ num_spin = j_mat.shape[0]
653
+
654
+ # Filter network for visualization
655
+ if weight_thres is None:
656
+ thres = np.percentile(np.abs(j_mat[j_mat != 0]), 10)
657
+ else:
658
+ thres = weight_thres
659
+
660
+ j_filt = j_mat.copy()
661
+ j_filt[np.abs(j_mat) < thres] = 0
662
+ np.fill_diagonal(j_filt, 0)
663
+
664
+ if directed:
665
+ G = nx.from_numpy_array(j_filt, create_using=nx.DiGraph)
666
+ else:
667
+ G = nx.from_numpy_array(j_filt)
668
+
669
+ # Compute positions
670
+ if pos is None:
671
+ pos = compute_network_positions(j_mat, modules)
672
+
673
+ # Create labels
674
+ if spin_name_list_short is not None:
675
+ label = {ii: spin_name_list_short[ii] for ii in range(num_spin)}
676
+ else:
677
+ label = {ii: f'P{ii}' for ii in range(num_spin)}
678
+
679
+ # Plot network
680
+ fig_size = 4 * num_spin ** 0.25
681
+ sc.set_figure_params(figsize=[fig_size, fig_size])
682
+ fig, ax = plt.subplots()
683
+
684
+ # Draw edges
685
+ eposi = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] > 0]
686
+ wposi = np.array([d['weight'] for (u, v, d) in G.edges(data=True) if d['weight'] > 0])
687
+
688
+ enega = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] < 0]
689
+ wnega = np.array([d['weight'] for (u, v, d) in G.edges(data=True) if d['weight'] < 0])
690
+
691
+ linewz = 0.5 / thres
692
+
693
+ # Draw positive edges
694
+ if len(eposi) > 0:
695
+ nx.draw_networkx_edges(G, pos, ax=ax, edgelist=eposi, width=linewz * wposi,
696
+ edge_color='#3285CC', alpha=0.7)
697
+
698
+ # Draw negative edges
699
+ if len(enega) > 0:
700
+ nx.draw_networkx_edges(G, pos, ax=ax, edgelist=enega, width=linewz * (-wnega),
701
+ edge_color='#E84B23', alpha=0.7)
702
+
703
+ # Draw nodes
704
+ node_colors = ['#f0dab1'] * num_spin
705
+ nx.draw_networkx_nodes(G, pos, ax=ax, node_size=300, node_color=node_colors,
706
+ edgecolors='black', linewidths=1)
707
+
708
+ # Draw labels
709
+ for spin in range(num_spin):
710
+ plt.text(pos[spin][0], pos[spin][1], label[spin], fontsize=10,
711
+ path_effects=[patheffects.withStroke(linewidth=3, foreground='w')],
712
+ ha='center', va='center')
713
+
714
+ ax.set_aspect('equal')
715
+ ax.set_axis_off()
716
+
717
+ # Save figure if folder is provided
718
+ if fig_folder is not None:
719
+ plt.savefig(fig_folder + 'network_diagram.png', dpi=300, bbox_inches='tight')
720
+
721
+ plt.show()
722
+
723
+ def plot_top_regulators(program_interactions: np.ndarray,
724
+ program_index: int,
725
+ gene_list: np.array,
726
+ program_name: Optional[str] = None,
727
+ thres: Optional[float] = None,
728
+ fig_folder: Optional[str] = None) -> None:
729
+
730
+ """
731
+ Plot the top regulators of a program.
732
+
733
+ Parameters
734
+ ----------
735
+ program_interactions : np.ndarray
736
+ The program interactions.
737
+ program_index : int
738
+ The index of the program.
739
+ gene_list : np.array
740
+ The list of genes.
741
+ program_name : Optional[str], optional
742
+ The name of the program.
743
+ thres : Optional[float], optional
744
+ The threshold for the regulators.
745
+ fig_folder : Optional[str], optional
746
+ The folder to save the figure.
747
+ """
748
+
749
+ cur_regulators = program_interactions[:, program_index]
750
+ if thres is None:
751
+ thres = np.sort(np.abs(cur_regulators))[::-1][20]
752
+
753
+ num_gene_show_pos = np.sum(cur_regulators >= thres)
754
+ num_gene_show_neg = np.sum(cur_regulators <= -thres)
755
+ num_gene_show = num_gene_show_pos + num_gene_show_neg
756
+
757
+ plot_ind_pos = np.argsort(cur_regulators)[::-1][:num_gene_show_pos]
758
+ plot_ind_neg = np.argsort(cur_regulators)[:num_gene_show_neg]
759
+
760
+ plot_ind = np.concatenate([plot_ind_pos, plot_ind_neg])
761
+
762
+ sc.set_figure_params(figsize=(num_gene_show * 0.17, 2))
763
+
764
+ plt.scatter(np.arange(num_gene_show), cur_regulators[plot_ind], c=np.where(cur_regulators[plot_ind] > 0,'#3285CC', '#E84B23'), zorder=10)
765
+ plt.xticks(np.arange(num_gene_show), np.array(gene_list)[plot_ind], rotation=90, fontsize=9);
766
+ plt.yticks(fontsize=10)
767
+ plt.ylabel('Regulator strength', fontsize=10)
768
+ plt.xlabel('Regulator', fontsize=10)
769
+
770
+ if program_name is not None:
771
+ plt.title(program_name, fontsize=10)
772
+
773
+ if fig_folder is not None:
774
+ plt.savefig(fig_folder + 'top_regulators.png')
775
+
776
+ plt.show()