-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_stimulatedPBMC.py
267 lines (202 loc) · 10.8 KB
/
example_stimulatedPBMC.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline
from sciRED import ensembleFCA as efca
from sciRED import glm
from sciRED import rotations as rot
from sciRED import metrics as met
from sciRED.utils import preprocess as proc
from sciRED.utils import visualize as vis
from sciRED.utils import corr
from sciRED.examples import ex_preprocess as exproc
from sciRED.examples import ex_visualize as exvis
import time
np.random.seed(10)
NUM_COMPONENTS = 30
NUM_GENES = 2000
NUM_COMP_TO_VIS = 5
data_file_path = '/home/delaram/sciFA/Data/PBMC_Lupus_Kang8vs8_data.h5ad'
data = exproc.import_AnnData(data_file_path)
data, gene_idx = proc.get_sub_data(data, num_genes=NUM_GENES) # subset the data to num_genes HVGs
y, genes, num_cells, num_genes = proc.get_data_array(data)
y_sample, y_stim, y_cell_type, y_cluster = exproc.get_metadata_humanPBMC(data)
colors_dict_humanPBMC = exvis.get_colors_dict_humanPBMC(y_sample, y_stim, y_cell_type)
plt_legend_sample = exvis.get_legend_patch(y_sample, colors_dict_humanPBMC['sample'] )
plt_legend_stim = exvis.get_legend_patch(y_stim, colors_dict_humanPBMC['stim'] )
plt_legend_cell_type = exvis.get_legend_patch(y_cell_type, colors_dict_humanPBMC['cell_type'] )
#### design matrix - library size only
x = proc.get_library_design_mat(data, lib_size='nCount_originalexp')
#### design matrix - library size and sample
#x_sample = proc.get_design_mat(metadata_col='ind', data=data)
#x = np.column_stack((data.obs.nCount_originalexp, x_sample))
#x = sm.add_constant(x) ## adding the intercept
### fit GLM to each gene
glm_fit_dict = glm.poissonGLM(y, x)
resid_pearson = glm_fit_dict['resid_pearson']
print('pearson residuals: ', resid_pearson.shape) # numpy array of shape (num_genes, num_cells)
print('y shape: ', y.shape) # (num_cells, num_genes)
y = resid_pearson.T # (num_cells, num_genes)
print('y shape: ', y.shape) # (num_cells, num_genes)
####################################
#### Running PCA on the data ######
####################################
### using pipeline to scale the gene expression data first
start = time.time()
pipeline = Pipeline([('scaling', StandardScaler()), ('pca', PCA(n_components=NUM_COMPONENTS))])
pca_scores = pipeline.fit_transform(y)
pca = pipeline.named_steps['pca']
pca_loading = pca.components_
rotation_results_varimax = rot.varimax(pca_loading.T)
varimax_loading = rotation_results_varimax['rotloading']
pca_scores_varimax = rot.get_rotated_scores(pca_scores, rotation_results_varimax['rotmat'])
end = time.time()
print('Time to fit sciRED - numcomp '+ str(NUM_COMPONENTS)+ ' : ', end-start)
print('Time to fit sciRED (min) - numcomp '+ str(NUM_COMPONENTS)+ ' : ', (end-start)/60)
plt.plot(pca.explained_variance_ratio_)
pca_loading.shape #(factors, genes)
### make a dictionary of colors for each sample in y_sample
vis.plot_pca(pca_scores, NUM_COMP_TO_VIS,
cell_color_vec= colors_dict_humanPBMC['cell_type'],
legend_handles=True,
title='PCA of gene expression data',
plt_legend_list=plt_legend_cell_type)
vis.plot_pca(pca_scores, NUM_COMP_TO_VIS,
cell_color_vec= colors_dict_humanPBMC['stim'],
legend_handles=True,
title='PCA of gene expression data',
plt_legend_list=plt_legend_stim)
vis.plot_pca(pca_scores, NUM_COMP_TO_VIS,
cell_color_vec= colors_dict_humanPBMC['sample'],
legend_handles=True,
title='PCA of gene expression data',
plt_legend_list=plt_legend_sample)
#### plot the loadings of the factors
vis.plot_factor_loading(pca_loading.T, genes, 0, 1, fontsize=10,
num_gene_labels=2,
title='Scatter plot of the loading vectors',
label_x=True, label_y=True)
####################################
#### Matching between factors and covariates ######
####################################
######## Applying varimax rotation to the factor scores
rotation_results_varimax = rot.varimax(pca_loading.T)
varimax_loading = rotation_results_varimax['rotloading']
pca_scores_varimax = rot.get_rotated_scores(pca_scores, rotation_results_varimax['rotmat'])
title = 'Varimax PCA of pearson residuals'
vis.plot_pca(pca_scores_varimax, NUM_COMP_TO_VIS,
cell_color_vec= colors_dict_humanPBMC['sample'],
legend_handles=True,
title=title,
plt_legend_list=plt_legend_sample)
vis.plot_pca(pca_scores_varimax, NUM_COMP_TO_VIS,
cell_color_vec= colors_dict_humanPBMC['cell_type'],
legend_handles=True,
title=title,
plt_legend_list=plt_legend_cell_type)
vis.plot_pca(pca_scores_varimax, NUM_COMP_TO_VIS,
cell_color_vec= colors_dict_humanPBMC['stim'],
legend_handles=True,
title=title,
plt_legend_list=plt_legend_stim)
varimax_loading_df = pd.DataFrame(varimax_loading)
varimax_loading_df.columns = ['F'+str(i) for i in range(1, varimax_loading_df.shape[1]+1)]
varimax_loading_df.index = genes
### save the varimax_loading_df and varimax_scores to a csv file
pca_scores_varimax_df = pd.DataFrame(pca_scores_varimax)
pca_scores_varimax_df.columns = ['F'+str(i) for i in range(1, pca_scores_varimax_df.shape[1]+1)]
pca_scores_varimax_df.index = data.obs.index.values
pca_scores_varimax_df_merged = pd.concat([data.obs, pca_scores_varimax_df], axis=1)
pca_scores_varimax_df_merged.to_csv('~/sciFA/Results/pca_scores_varimax_df_merged_lupusPBMC.csv')
varimax_loading_df.to_csv('~/sciFA/Results/varimax_loading_df_lupusPBMC.csv')
########################
######## PCA factors
factor_loading = pca_loading
factor_scores = pca_scores
##### Varimax factors
factor_loading = rotation_results_varimax['rotloading']
factor_scores = pca_scores_varimax
covariate_vec = y_stim
covariate_level = np.unique(covariate_vec)[1]
########################
### read the varimax_loading_df and varimax_scores from a csv file
varimax_loading_df = pd.read_csv('../Results/varimax_loading_df_lupusPBMC.csv', index_col=0)
pca_scores_varimax_df = pd.read_csv('../Results/pca_scores_varimax_df_merged_lupusPBMC.csv', index_col=0)
pca_scores_varimax_df = pca_scores_varimax_df.iloc[:,8:] ### remove the first column 8 of the dataframes
####################################
#### FCAT score calculation ######
####################################
### FCAT needs to be calculated for each covariate separately
fcat_sample = efca.FCAT(y_sample, factor_scores, scale='standard', mean='arithmatic')
fcat_stim = efca.FCAT(y_stim, factor_scores, scale='standard', mean='arithmatic')
fcat_cell_type = efca.FCAT(y_cell_type, factor_scores, scale='standard', mean='arithmatic')
### concatenate FCAT table for protocol and cell line
fcat = pd.concat([fcat_sample, fcat_stim, fcat_cell_type], axis=0)
vis.plot_FCAT(fcat, title='', color='coolwarm',
x_axis_fontsize=20, y_axis_fontsize=20, title_fontsize=22,
x_axis_tick_fontsize=32, y_axis_tick_fontsize=34)
fcat = fcat[fcat.index != 'NA'] ### remove the rownames called NA from table
### using Otsu's method to calculate the threshold
threshold = efca.get_otsu_threshold(fcat.values.flatten())
vis.plot_histogram(fcat.values.flatten(),
xlabel='Factor-Covariate Association scores',
title='FCAT score distribution',
threshold=threshold)
## rownames of the FCAT table
all_covariate_levels = fcat.index.values
matched_factor_dist, percent_matched_fact = efca.get_percent_matched_factors(fcat, threshold)
matched_covariate_dist, percent_matched_cov = efca.get_percent_matched_covariates(fcat, threshold=threshold)
print('percent_matched_fact: ', percent_matched_fact)
print('percent_matched_cov: ', percent_matched_cov)
vis.plot_matched_factor_dist(matched_factor_dist)
vis.plot_matched_covariate_dist(matched_covariate_dist,
covariate_levels=all_covariate_levels)
### select the factors that are matched with any covariate level
matched_factor_index = np.where(matched_factor_dist>0)[0]
fcat_matched = fcat.iloc[:,matched_factor_index]
x_labels_matched = fcat_matched.columns.values
vis.plot_FCAT(fcat_matched, x_axis_label=x_labels_matched, title='', color='coolwarm',
x_axis_fontsize=40, y_axis_fontsize=39, title_fontsize=40,
x_axis_tick_fontsize=36, y_axis_tick_fontsize=38,
save=False, save_path='../Plots/mean_importance_df_matched_PBMC.pdf')
factor_libsize_correlation = corr.get_factor_libsize_correlation(factor_scores, library_size = data.obs.nCount_originalexp)
vis.plot_factor_cor_barplot(factor_libsize_correlation,
title='Correlation of factors with library size',
y_label='Correlation', x_label='Factors')
### concatenate FCAT table for protocol and cell line
fcat = pd.concat([fcat_stim, fcat_cell_type], axis=0)
fcat = fcat[fcat.index != 'NA'] ### remove the rownames called NA from table
vis.plot_FCAT(fcat, title='', color='coolwarm',
x_axis_fontsize=40, y_axis_fontsize=39, title_fontsize=40,
x_axis_tick_fontsize=36, y_axis_tick_fontsize=40,
save=False, save_path='../Plots/mean_importance_df_matched_PBMC.pdf')
stim_fcat_sorted_scores, stim_factors_sorted = vis.plot_sorted_factor_FCA_scores(fcat, 'stim')
####################################
#### Bimodality scores
silhouette_score = met.kmeans_bimodal_score(factor_scores, time_eff=True)
bimodality_index = met.bimodality_index(factor_scores)
bimodality_score = np.mean([silhouette_score, bimodality_index], axis=0)
bimodality_score = bimodality_index
#### Effect size
factor_variance = met.factor_variance(factor_scores)
## Specificity
simpson_fcat = met.simpson_diversity_index(fcat)
### label dependent factor metrics
asv_cell_type = met.average_scaled_var(factor_scores, covariate_vector=y_cell_type, mean_type='arithmetic')
asv_stim = met.average_scaled_var(factor_scores, covariate_vector=y_stim, mean_type='arithmetic')
asv_sample = met.average_scaled_var(factor_scores, y_sample, mean_type='arithmetic')
########### create factor-interpretibility score table (FIST) ######
metrics_dict = {'Bimodality':bimodality_score,
'Specificity':simpson_fcat,
'Effect size': factor_variance,
'Homogeneity (cell type)':asv_cell_type,
"Homogeneity (stimulated)":asv_stim,
'Homogeneity (sample)':asv_sample}
fist = met.FIST(metrics_dict)
vis.plot_FIST(fist, title='Scaled metrics for all the factors')
### subset the first 15 factors of fist dataframe
vis.plot_FIST(fist.iloc[0:15,:])
vis.plot_FIST(fist.iloc[matched_factor_index,:])