dspin 1.0.4__tar.gz → 1.0.6__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dspin
3
- Version: 1.0.4
3
+ Version: 1.0.6
4
4
  Summary: Regulatory network models from single-cell perturbation profiling
5
5
  Home-page: https://github.com/JialongJiang/DSPIN
6
6
  Author: Jialong Jiang
@@ -126,9 +126,11 @@ The first demo reconstructs the regulatory network of simulated hematopoietic st
126
126
 
127
127
  The second demo reconstructs regulatory network and response vector in a single-cell dataset collected in the ThomsonLab.In the dataset, human peripheral blood mononuclear cells (PBMCs) were treated with various signaling molecules with different dosages.
128
128
 
129
- [Demo2](https://colab.research.google.com/drive/1zrWFZWtaHQAzG88jgtovCPzt3wiXdlwf?usp=sharing)
129
+ [Demo2](https://colab.research.google.com/drive/1U2WbmKvai3Y_-vlLV0SY1dv1LHdFjg2w?usp=sharing)
130
130
 
131
131
 
132
132
  ## References
133
133
 
134
- 1. Pratapa, Aditya, et al. "Benchmarking algorithms for gene regulatory network inference from single-cell transcriptomic data." Nature methods 17.2 (2020): 147-154.
134
+ 1. Jiang, Jialong, et al. "D-SPIN constructs gene regulatory network models from multiplexed scRNA-seq data revealing organizing principles of cellular perturbation response." bioRxiv (2023).
135
+
136
+ 2. Pratapa, Aditya, et al. "Benchmarking algorithms for gene regulatory network inference from single-cell transcriptomic data." Nature methods 17.2 (2020): 147-154.
@@ -8,7 +8,7 @@ from scipy.linalg import orth
8
8
  from collections import deque
9
9
  import numba
10
10
  from sklearn.preprocessing import normalize
11
- from sklearn.decomposition import NMF
11
+ from sklearn.decomposition import NMF, PCA
12
12
  import numpy as np
13
13
  import matplotlib.pyplot as plt
14
14
  import scanpy as sc
@@ -138,6 +138,7 @@ def summary_components(all_components: np.array,
138
138
  # Convert to graph and apply Leiden algorithm
139
139
  G = nx.from_numpy_array(consensus)
140
140
  G = ig.Graph.from_networkx(G)
141
+ np.random.seed(0)
141
142
 
142
143
  optimiser = la.Optimiser()
143
144
  membership = np.ones(consensus.shape[0]) * (1 + max(cluster_labels))
@@ -489,19 +490,41 @@ def samp_moments(j_mat, h_vec, sample_size, mixing_time, samp_gap):
489
490
  # Monte Carlo Sampling
490
491
  for ii in range(tot_sampling):
491
492
  cur_ind = rand_ind[ii]
492
- # Perform flips and accepts/rejects new states according to Metropolis
493
- # rule
494
493
  j_sub = j_mat[cur_ind, :]
495
- # Compute acceptance probability and execute possible spin flips
496
- # Collecting samples
497
- if ii > mixing_time and (ii - mixing_time) % samp_gap == 0:
498
- rec_sample[:, batch_count - 1] = cur_spin[:, 0].copy()
499
- batch_count += 1
500
- if batch_count == per_batch + 1:
501
- batch_count = 1
502
- rec_sample = np.ascontiguousarray(rec_sample)
503
- rec_corr += rec_sample.dot(rec_sample.T)
504
- rec_mean += np.sum(rec_sample, axis=1)
494
+ accept_prob = 0.0
495
+ new_spin = 0.0
496
+ diff_energy = 0.0
497
+
498
+ if cur_spin[cur_ind] == 0:
499
+ if rand_flip[ii] == 0:
500
+ new_spin = 1.0
501
+ else:
502
+ new_spin = -1.0
503
+ diff_energy = -j_mat[cur_ind, cur_ind] - new_spin * (j_sub.dot(cur_spin) + h_vec[cur_ind])
504
+ accept_prob = min(1.0, np.exp(- diff_energy * beta)[0])
505
+ else:
506
+ if rand_flip[ii] == 0:
507
+ accept_prob = 0;
508
+ else:
509
+ diff_energy = cur_spin[cur_ind] * (j_sub.dot(cur_spin) + h_vec[cur_ind])
510
+ accept_prob = min(1.0, np.exp(- diff_energy * beta)[0])
511
+
512
+ if rand_prob[ii] < accept_prob:
513
+ if cur_spin[cur_ind] == 0:
514
+ cur_spin[cur_ind] = new_spin
515
+ else:
516
+ cur_spin[cur_ind] = 0
517
+
518
+ if ii > mixing_time:
519
+ if (ii - mixing_time) % samp_gap == 0:
520
+ rec_sample[:, batch_count - 1] = cur_spin[:, 0].copy()
521
+ batch_count += 1
522
+
523
+ if batch_count == per_batch + 1:
524
+ batch_count = 1
525
+ rec_sample = np.ascontiguousarray(rec_sample)
526
+ rec_corr += rec_sample.dot(rec_sample.T)
527
+ rec_mean += np.sum(rec_sample, axis=1)
505
528
 
506
529
  # Final processing of collected samples
507
530
  if batch_count != 1:
@@ -738,11 +761,13 @@ def compute_relative_responses(cur_h, samp_list, dict_samp_control, dict_samp_ba
738
761
  This function calculates the relative responses for a set of samples by comparing each sample's response
739
762
  to the average response of control samples. The control samples can be specific to the batch a sample belongs to,
740
763
  or a global set of control samples if no control samples are present in the batch.
764
+
741
765
  Parameters:
742
766
  cur_h (numpy.ndarray): A 2D array where each column represents a sample and each row represents a feature.
743
767
  samp_list (iterable): A list or iterable of sample identifiers.
744
768
  dict_samp_control (dict): A dictionary mapping sample identifiers to a boolean indicating if the sample is a control.
745
769
  dict_samp_batch (dict): A dictionary mapping sample identifiers to their respective batch identifiers.
770
+
746
771
  Returns:
747
772
  numpy.ndarray: A 2D array of the same shape as `cur_h`, containing the relative responses for each sample.
748
773
  """
@@ -755,10 +780,10 @@ def compute_relative_responses(cur_h, samp_list, dict_samp_control, dict_samp_ba
755
780
 
756
781
  # Iterate over each sample
757
782
  for index, sample in enumerate(samp_list):
758
- batch = dict_samp_batch[sample]
783
+ cur_batch = dict_samp_batch[sample]
759
784
 
760
785
  # Find control samples in the same batch
761
- batch_control_samples = [samp for samp, batch in dict_samp_batch.items() if batch == batch and dict_samp_control.get(samp, True)]
786
+ batch_control_samples = [samp for samp, batch in dict_samp_batch.items() if batch == cur_batch and dict_samp_control.get(samp, True)]
762
787
 
763
788
  # If there are controls in the batch, use their average
764
789
  if batch_control_samples:
@@ -770,3 +795,57 @@ def compute_relative_responses(cur_h, samp_list, dict_samp_control, dict_samp_ba
770
795
  relative_responses[:, index] = cur_h[:, index] - global_control_avg
771
796
 
772
797
  return relative_responses
798
+
799
+ def select_representative_sample(raw_data, num_select):
800
+ """
801
+ Select representative samples by performing KMeans clustering on the samples'
802
+ correlation and mean vectors and selecting the sample closest to the cluster center.
803
+
804
+ Parameters:
805
+ raw_data (list): A list of correlation and mean vectors for each sample.
806
+ num_select (int): The number of representative samples to select.
807
+
808
+ Returns:
809
+ list: A list of indices of the selected representative samples.
810
+ """
811
+
812
+ num_spin = raw_data[0][0].shape[1]
813
+ num_samp = len(raw_data)
814
+ raw_data_cat_vec = np.zeros([num_samp, int(num_spin * (num_spin + 3) / 2)])
815
+ ind1, ind2 = np.triu_indices(num_spin)
816
+
817
+ for ii in range(num_samp):
818
+ cur_corrmean = raw_data[ii]
819
+ triu_vect = cur_corrmean[0][ind1, ind2] / np.sqrt((num_spin + 1) / 2)
820
+ triu_vect = np.concatenate([triu_vect, cur_corrmean[1].flatten()])
821
+ raw_data_cat_vec[ii, :] = triu_vect
822
+
823
+ pca_all = PCA().fit(raw_data_cat_vec)
824
+ pca_rep = pca_all.transform(raw_data_cat_vec)
825
+ kmeans = KMeans(n_clusters=num_select, n_init=200)
826
+ kmeans.fit(raw_data_cat_vec)
827
+
828
+ use_data_list = []
829
+ for ii in range(num_select):
830
+ cur_data = np.where(kmeans.labels_ == ii)[0]
831
+
832
+ cur_center = kmeans.cluster_centers_[ii, :]
833
+ data_dist = np.linalg.norm(raw_data_cat_vec[cur_data, :] - cur_center, axis=1)
834
+ data_select = cur_data[np.argmin(data_dist)]
835
+
836
+ use_data_list.append(data_select)
837
+
838
+ sc.set_figure_params(figsize=[1, 1])
839
+ fig, grid = sc.pl._tools._panel_grid(0.2, 0.2, ncols=3, num_panels=9)
840
+
841
+ for ii in range(3):
842
+ for jj in range(ii + 1):
843
+ ax = plt.subplot(grid[ii * 3 + jj])
844
+ plt.scatter(pca_rep[use_data_list, ii + 1], pca_rep[use_data_list, jj], s=90, color='#aaaaaa')
845
+ plt.scatter(pca_rep[:, ii + 1], pca_rep[:, jj], s=20, c=kmeans.labels_, cmap='tab20', alpha=0.5)
846
+ plt.xlabel('PC' + str(ii + 2))
847
+ plt.ylabel('PC' + str(jj + 1))
848
+ plt.xticks([])
849
+ plt.yticks([])
850
+
851
+ return use_data_list
@@ -88,7 +88,7 @@ class AbstractDSPIN(ABC):
88
88
  os.makedirs(self.fig_folder, exist_ok=True)
89
89
 
90
90
  @property
91
- def onmf_rep_ori(self):
91
+ def program_representation(self):
92
92
  return self._onmf_rep_ori
93
93
 
94
94
  @property
@@ -106,9 +106,13 @@ class AbstractDSPIN(ABC):
106
106
  @property
107
107
  def responses(self):
108
108
  return self._responses
109
+
110
+ @property
111
+ def relative_responses(self):
112
+ return self._relative_responses
109
113
 
110
- @onmf_rep_ori.setter
111
- def onmf_rep_ori(self, value):
114
+ @program_representation.setter
115
+ def program_representation(self, value):
112
116
  self._onmf_rep_ori = value
113
117
 
114
118
  @network.setter
@@ -119,6 +123,10 @@ class AbstractDSPIN(ABC):
119
123
  def responses(self, value):
120
124
  self._responses = value
121
125
 
126
+ @relative_responses.setter
127
+ def relative_responses(self, value):
128
+ self._relative_responses = value
129
+
122
130
  def discretize(self) -> np.ndarray:
123
131
  """
124
132
  Discretizes the ONMF representation into three states (-1, 0, 1) using K-means clustering.
@@ -126,7 +134,7 @@ class AbstractDSPIN(ABC):
126
134
  Returns:
127
135
  - np.ndarray: The discretized ONMF representation.
128
136
  """
129
- onmf_rep_ori = self.onmf_rep_ori
137
+ onmf_rep_ori = self.program_representation
130
138
  fig_folder = self.fig_folder
131
139
 
132
140
  onmf_rep_tri = onmf_discretize(onmf_rep_ori, fig_folder)
@@ -195,7 +203,7 @@ class AbstractDSPIN(ABC):
195
203
  'cur_j': np.zeros((num_spin, num_spin)),
196
204
  'cur_h': np.zeros((num_spin, num_sample)),
197
205
  'save_path': self.save_path}
198
- params.update({'lambda_l1_j': 0.001,
206
+ params.update({'lambda_l1_j': 0.01,
199
207
  'lambda_l1_h': 0,
200
208
  'lambda_l2_j': 0,
201
209
  'lambda_l2_h': 0.005,
@@ -206,30 +214,27 @@ class AbstractDSPIN(ABC):
206
214
  if method == 'maximum_likelihood':
207
215
  params['stepsz'] = 0.2
208
216
  elif method == 'mcmc_maximum_likelihood':
209
- params['stepsz'] = 0.01
210
- params['mcmc_samplingsz'] = 5e5
217
+ params['stepsz'] = 0.02
218
+ params['mcmc_samplingsz'] = 2e5
211
219
  params['mcmc_samplingmix'] = 1e3
212
220
  params['mcmc_samplegap'] = 1
213
- else:
221
+ elif method == 'pseudo_likelihood':
214
222
  params['stepsz'] = 0.05
215
223
 
216
224
  return params
217
225
 
218
226
  def network_inference(self,
219
- sample_col_name: str = 'sample_id',
220
- control_col_name: str = None,
221
- batch_col_name: str = None,
222
- method: str = 'auto',
223
- params: dict = None,
224
- example_list: List[str] = None,
225
- record_step: int = 10):
227
+ sample_col_name: str = 'sample_id',
228
+ method: str = 'auto',
229
+ params: dict = None,
230
+ example_list: List[str] = None,
231
+ record_step: int = 10,
232
+ run_with_matlab: bool = False):
226
233
  """
227
234
  Execute the network inference using the specified method and parameters and record the results.
228
235
 
229
236
  Parameters:
230
237
  sample_col_name (str): The name of the sample column.
231
- batch_col_name (str, optional): The name of the batch column in adata, default is None.
232
- control_col_name (str, optional): The name of the control column in adata, default is None.
233
238
  method (str, optional): The method used for network inference, default is 'auto'.
234
239
  params (dict, optional): Dictionary of parameters to be used, default is None.
235
240
  example_list (List[str], optional): List of examples to be used, default is None.
@@ -239,29 +244,31 @@ class AbstractDSPIN(ABC):
239
244
  ValueError: If an invalid method is specified.
240
245
  """
241
246
 
247
+ self.sample_col_name = sample_col_name
248
+
242
249
  if method not in [
243
250
  'maximum_likelihood',
244
251
  'mcmc_maximum_likelihood',
245
252
  'pseudo_likelihood',
253
+ 'directed_pseudo_likelihood',
246
254
  'auto']:
247
255
  raise ValueError(
248
- "Method must be one of 'maximum_likelihood', 'mcmc_maximum_likelihood', 'pseudo_likelihood', or 'auto'.")
256
+ "Method must be one of 'maximum_likelihood', 'mcmc_maximum_likelihood', 'pseudo_likelihood', 'directed_pseudo_likelihood' or 'auto'.")
249
257
 
250
- if method == 'auto':
258
+ if method == 'auto':
259
+ samp_list = np.unique(self.adata.obs[sample_col_name])
260
+ num_sample = len(samp_list)
251
261
  if example_list is not None:
252
- if len(example_list) > 30:
253
- method = 'pseudo_likelihood'
262
+ num_sample = len(example_list)
263
+ if num_sample > 30:
264
+ method = 'pseudo_likelihood'
254
265
  else:
255
- samp_list = np.unique(self.adata.obs[sample_col_name])
256
- if len(samp_list) > 10:
257
- method = 'pseudo_likelihood'
266
+ if self.num_spin <= 12:
267
+ method = 'maximum_likelihood'
268
+ elif self.num_spin <= 25:
269
+ method = 'mcmc_maximum_likelihood'
258
270
  else:
259
- if self.num_spin <= 12:
260
- method = 'maximum_likelihood'
261
- elif self.num_spin <= 25:
262
- method = 'mcmc_maximum_likelihood'
263
- else:
264
- method = 'pseudo_likelihood'
271
+ method = 'pseudo_likelihood'
265
272
 
266
273
  print("Using {} for network inference.".format(method))
267
274
 
@@ -279,28 +286,46 @@ class AbstractDSPIN(ABC):
279
286
  if params is not None:
280
287
  train_dat.update(params)
281
288
 
282
- cur_j, cur_h = learn_network_adam(self.raw_data, method, train_dat)
283
- self._network = cur_j
284
- self._responses = cur_h
289
+ if run_with_matlab:
290
+ if method in ['maximum_likelihood', 'mcmc_maximum_likelihood']:
291
+ file_path = self.save_path + 'raw_data.mat'
292
+ elif method in ['pseudo_likelihood', 'directed_pseudo_likelihood']:
293
+ file_path = self.save_path + 'raw_data_state.mat'
294
+ savemat(file_path, {'raw_data': self._raw_data, 'sample_list': self.samp_list})
295
+ print("Data saved to {}. Please run the network inference in MATLAB and load the results back.".format(file_path))
296
+
297
+ else:
298
+ cur_j, cur_h = learn_network_adam(self.raw_data, method, train_dat)
299
+ self._network = cur_j
300
+ self._responses = cur_h
301
+
302
+ def response_relative_to_control(self,
303
+ control_col_name: str,
304
+ batch_col_name: str = None):
305
+ """
306
+ Compute the relative responses based on the control samples in each batch or all batches.
285
307
 
286
- if control_col_name is not None:
308
+ Parameters:
309
+ control_col_name (str, optional): The name of the control column in adata, default is None.
310
+ batch_col_name (str, optional): The name of the batch column in adata, default is None.
311
+ """
287
312
 
288
- assert(np.sum(self.adata.obs[control_col_name]) > 0)
313
+ assert(np.sum(self.adata.obs[control_col_name]) > 0)
289
314
 
290
- unique_sample_control = self.adata.obs[['sample_id', control_col_name]].drop_duplicates().set_index('sample_id')
291
- # only remain samples in self.samp_list
292
- unique_sample_control = unique_sample_control.loc[self.samp_list]
293
- sample_to_control_dict = unique_sample_control[control_col_name].to_dict()
315
+ unique_sample_control = self.adata.obs[[self.sample_col_name, control_col_name]].drop_duplicates().set_index(self.sample_col_name)
316
+ # only remain samples in self.samp_list
317
+ unique_sample_control = unique_sample_control.loc[self.samp_list]
318
+ sample_to_control_dict = unique_sample_control[control_col_name].to_dict()
294
319
 
295
- if batch_col_name is None:
296
- sample_batch_dict = {sample: 0 for sample in self.samp_list}
297
- else:
320
+ if batch_col_name is None:
321
+ sample_batch_dict = {sample: 0 for sample in self.samp_list}
322
+ else:
298
323
 
299
- unique_sample_batch = self.adata.obs[['sample_id', 'batch']].drop_duplicates().set_index('sample_id')
300
- unique_sample_batch = unique_sample_batch.loc[self.samp_list]
301
- sample_batch_dict = unique_sample_batch['batch'].to_dict()
302
-
303
- self._relative_responses = compute_relative_responses(self._responses, self.samp_list, sample_to_control_dict, sample_batch_dict)
324
+ unique_sample_batch = self.adata.obs[[self.sample_col_name, batch_col_name]].drop_duplicates().set_index(self.sample_col_name)
325
+ unique_sample_batch = unique_sample_batch.loc[self.samp_list]
326
+ sample_batch_dict = unique_sample_batch[batch_col_name].to_dict()
327
+
328
+ self._relative_responses = compute_relative_responses(self._responses, self.samp_list, sample_to_control_dict, sample_batch_dict)
304
329
 
305
330
 
306
331
  class GeneDSPIN(AbstractDSPIN):
@@ -475,6 +500,7 @@ class ProgramDSPIN(AbstractDSPIN):
475
500
 
476
501
  preprograms = self.prior_programs
477
502
  adata = self.adata
503
+
478
504
  # Create the directory for saving ONMF decompositions if it doesn't
479
505
  # exist
480
506
  os.makedirs(self.save_path + 'onmf/', exist_ok=True)
@@ -575,11 +601,12 @@ class ProgramDSPIN(AbstractDSPIN):
575
601
  num_subsample: int = 10000,
576
602
  num_subsample_large: int = None,
577
603
  num_repeat: int = 10,
604
+ seed: int = 0,
578
605
  balance_obs: str = None,
579
606
  balance_method: str = None,
580
607
  max_sample_rate: float = 2,
581
608
  prior_programs: List[List[str]] = None,
582
- summary_method: str = 'kmeans',):
609
+ summary_method: str = 'kmeans'):
583
610
  """
584
611
  Discovers gene programs by performing ONMF decomposition on the given annotated data object.
585
612
 
@@ -649,6 +676,7 @@ class ProgramDSPIN(AbstractDSPIN):
649
676
  self.prior_programs_mask = prior_program_mask
650
677
  self.prior_programs_ind = prior_program_ind
651
678
 
679
+ np.random.seed(seed)
652
680
  # Perform subsampling and standard deviation clipping on the matrix
653
681
  self.matrix_std, self.large_subsample_matrix = self.subsample_matrix_balance(
654
682
  num_subsample_large, std_clip_percentile=20)
@@ -658,6 +686,7 @@ class ProgramDSPIN(AbstractDSPIN):
658
686
  num_onmf_components,
659
687
  num_repeat,
660
688
  num_subsample,
689
+ seed,
661
690
  std_clip_percentile=20)
662
691
 
663
692
  # Summarize the ONMF decompositions
@@ -675,6 +704,7 @@ class ProgramDSPIN(AbstractDSPIN):
675
704
  num_onmf_components,
676
705
  num_spin),
677
706
  thres=0.01)
707
+ print('Gene programs saved to {}'.format(file_path))
678
708
  self.gene_program_csv = file_path
679
709
 
680
710
  gene_matrix = self.adata.X
@@ -726,8 +756,13 @@ class DSPIN(object):
726
756
 
727
757
  # if the number of genes is less than the number of spins, use
728
758
  # GeneDSPIN
729
- if adata.shape[1] <= num_spin:
759
+ if adata.shape[1] == num_spin:
760
+ return GeneDSPIN(adata, save_path, num_spin=num_spin, **kwargs)
761
+ elif adata.shape[1] < num_spin:
762
+ warnings.warn(
763
+ "Number of genes is less than the number of spins. Using number of genes as num_spin.")
764
+ num_spin = adata.shape[1]
730
765
  return GeneDSPIN(adata, save_path, num_spin=num_spin, **kwargs)
731
- # otherwise, use ProgramDSPIN because ONMF is needed
766
+ # otherwise, use ProgramDSPIN because oNMF is needed
732
767
  else:
733
768
  return ProgramDSPIN(adata, save_path, num_spin=num_spin, **kwargs)
@@ -3,7 +3,7 @@ import csv
3
3
  import pandas as pd
4
4
  from typing import List
5
5
 
6
- system_question = "Your are a desperate biology phd student studying gene expression from a dataset of cancer patients. Given following a list of genes in the gene program, you have two tasks: \
6
+ system_question = "Your are a desperate biology phd professor studying gene expression from a dataset of cancer patients. Given following a list of genes in the gene program, you have two tasks: \
7
7
  Firstly, summarize functions of the program in a few important functions. \
8
8
  Secondly, give a higher-level name of the gene program based on its function. You are required by your mentor to name it as short as possible.\
9
9
  You get 100 score if you can name it in two or three words, 80 score if you can name it in four words, 20 score for five or more words. \
@@ -17,6 +17,8 @@ import scanpy as sc
17
17
  import igraph as ig
18
18
  import leidenalg as la
19
19
  import networkx as nx
20
+ from scipy import optimize
21
+ from scipy.spatial.distance import pdist
20
22
 
21
23
 
22
24
  def onmf_to_csv(features, gene_name, file_path, thres=0.01):
@@ -56,6 +58,89 @@ def onmf_to_csv(features, gene_name, file_path, thres=0.01):
56
58
 
57
59
  return file_path
58
60
 
61
+ def assign_program_position(onmf_rep_ori, umap_all, repulsion=2):
62
+ """
63
+ Assign the position of gene programs on the UMAP plot.
64
+
65
+ Args:
66
+ onmf_rep_ori (numpy.ndarray): The gene or gene program representation of the transformed data.
67
+ umap_all (numpy.ndarray): The UMAP coordinates of all cells.
68
+ repulsion (float): The repulsion strength for the layout optimization.
69
+
70
+ Returns:
71
+ numpy.ndarray: The assigned positions of gene programs on the UMAP plot.
72
+ """
73
+
74
+ num_spin = onmf_rep_ori.shape[1]
75
+ program_umap_pos = np.zeros([num_spin, 2])
76
+ for ii in range(num_spin):
77
+ weight_sub = onmf_rep_ori[:, ii].copy()
78
+ weight_sub[weight_sub < np.percentile(weight_sub, 99.5)] = 0
79
+ program_umap_pos[ii, :] = np.sum(umap_all * weight_sub.reshape(- 1, 1) / np.sum(weight_sub), axis=0)
80
+
81
+ ori_pos = program_umap_pos.copy()
82
+ def layout_loss_fun(xx):
83
+ xx = xx.reshape(- 1, 2)
84
+ attract = np.sum((xx - ori_pos) ** 2)
85
+ repulse = np.sum(repulsion / pdist(xx))
86
+ return attract + repulse
87
+
88
+ opt_res = optimize.minimize(layout_loss_fun, program_umap_pos.flatten())
89
+ program_umap_pos = opt_res.x.reshape(- 1, 2)
90
+
91
+ sc.set_figure_params(figsize=[4, 4])
92
+
93
+ if umap_all.shape[0] <= 5e4:
94
+ plt.scatter(umap_all[:, 0], umap_all[:, 1], s=1, c='#bbbbbb', alpha=min(1, 1e4 / umap_all.shape[0]))
95
+ else:
96
+ sele_ind = np.random.choice(umap_all.shape[0], size=50000, replace=False).astype(int)
97
+ plt.scatter(umap_all[sele_ind, 0], umap_all[sele_ind, 1], s=1, c='#bbbbbb', alpha=0.2)
98
+
99
+ for ii in range(num_spin):
100
+ plt.text(program_umap_pos[ii, 0], program_umap_pos[ii, 1], str(ii), fontsize=10)
101
+ plt.axis('off')
102
+
103
+ return program_umap_pos
104
+
105
+ def gene_program_on_umap(onmf_rep, umap_all, program_umap_pos, fig_folder=None, subsample=True):
106
+ """
107
+ Plot gene programs on the UMAP plot.
108
+
109
+ Args:
110
+ onmf_rep (numpy.ndarray): The gene or gene program representation of the transformed data.
111
+ umap_all (numpy.ndarray): The UMAP coordinates of all cells.
112
+ program_umap_pos (numpy.ndarray): The assigned positions of gene programs on the UMAP plot.
113
+ fig_folder (str): The folder where the output figure is saved.
114
+ subsample (bool): Whether to subsample the data for plotting.
115
+ """
116
+
117
+ num_spin = onmf_rep.shape[1]
118
+
119
+ if subsample:
120
+ num_subsample = 20000
121
+ sub_ind = np.random.choice(onmf_rep.shape[0], num_subsample, replace=False)
122
+ onmf_rep = onmf_rep[sub_ind, :]
123
+ umap_all = umap_all[sub_ind, :]
124
+
125
+ sc.set_figure_params(figsize=[2, 2])
126
+ fig, grid = sc.pl._tools._panel_grid(0.2, 0.06, ncols=6, num_panels=num_spin)
127
+ for spin in range(num_spin):
128
+ ax = plt.subplot(grid[spin])
129
+
130
+ plot_data = onmf_rep[:, spin].copy()
131
+ plot_data /= np.percentile(plot_data, 95)
132
+ plot_data = plot_data.clip(0, 3)
133
+
134
+ plt.scatter(umap_all[:, 0], umap_all[:, 1], c=plot_data, s=1,
135
+ alpha=0.5, vmax=1.2, cmap='BuPu', vmin=-0.1)
136
+ plt.text(program_umap_pos[spin, 0], program_umap_pos[spin, 1], str(spin), fontsize=12, path_effects=[patheffects.withStroke(linewidth=3, foreground='w')])
137
+ ax.set_aspect('equal')
138
+ plt.xticks([])
139
+ plt.yticks([])
140
+ plt.title(spin)
141
+
142
+ if fig_folder is not None:
143
+ plt.savefig(fig_folder + 'gene_program_on_umap.png', dpi=300, bbox_inches='tight')
59
144
 
60
145
  def gene_program_decomposition(onmf_summary,
61
146
  num_spin,
@@ -329,7 +414,6 @@ def adjust_label_position(pos, offset=0.1):
329
414
  coordinates[1] + np.sin(theta) * offset)
330
415
  return adjusted_pos
331
416
 
332
-
333
417
  def plot_final(
334
418
  cur_j,
335
419
  gene_program_name,
@@ -427,3 +511,5 @@ def plot_final(
427
511
  if not title:
428
512
  title = 'Gene Regulatory Network Reconstructed by D-SPIN'
429
513
  ax.set_title(title, fontsize=node_fontsize * 1.5, y = 1.05)
514
+
515
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dspin
3
- Version: 1.0.4
3
+ Version: 1.0.6
4
4
  Summary: Regulatory network models from single-cell perturbation profiling
5
5
  Home-page: https://github.com/JialongJiang/DSPIN
6
6
  Author: Jialong Jiang
@@ -13,7 +13,6 @@ dspin/__init__.py
13
13
  dspin/compute.py
14
14
  dspin/dspin.py
15
15
  dspin/gpt.py
16
- dspin/metadata_drug.py
17
16
  dspin/plot.py
18
17
  dspin.egg-info/PKG-INFO
19
18
  dspin.egg-info/SOURCES.txt
@@ -21,7 +21,7 @@ install_requires = [
21
21
  # ]
22
22
 
23
23
  # Managing your version (adjust accordingly)
24
- version = "1.0.4" # You can manage versioning in a more sophisticated way if needed
24
+ version = "1.0.6" # You can manage versioning in a more sophisticated way if needed
25
25
 
26
26
  # Long description (usually a README file)
27
27
  readme = open("README_PyPI.md").read() # Adjust the file name if necessary
@@ -1,13 +0,0 @@
1
- import numpy as np
2
-
3
- num_spin = 30
4
-
5
- net_class = [[14, 15, 19, 16, 18, 17], [23, 22, 24, 25], [13, 10, 11, 12], [28, 26, 27, 29, 0], [9, 3, 2, 4], [7, 8, 21], [6, 1, 5], [20]]
6
-
7
- spin_order = [spin for cur_list in net_class for spin in cur_list]
8
- net_class_len = [len(cur_list) for cur_list in net_class]
9
-
10
- spin_name = ['Antigen presentation', 'Inflammatory cytokines', 'M2 macrophage', 'Monocyte', 'Myeloid growth', 'Metallothionein', 'Pathogen response', 'Inflammatory macrophage', 'Complement/chemokine', 'Myeloid cell', 'T-cell cytotoxicity', 'Effector CD8 T-cell', 'NK cell', 'Granzyme K T-cell', 'Naive T-cell', 'T-cell maintenance', 'Lymphocyte', 'T-cell', 'Resting maintainence', 'Housekeeping', 'Stress response', 'Dendritic cell', 'Histone', 'T-cell activation', 'Regulatory T-cell', 'T-cell exhaustion', 'B-cell activation', 'Naive B-cell', 'B-cell', 'Resting B-cell']
11
-
12
- spin_pname = ['P' + str(spin_order.index(ii) + 1) for ii in range(num_spin)]
13
- spin_name_extend = ['P' + str(spin_order.index(ii) + 1) + '-' + spin_name[ii] for ii in range(num_spin)]
File without changes
File without changes
File without changes
File without changes
File without changes