-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10_2_ald_compare_methods.py
446 lines (365 loc) · 13.3 KB
/
10_2_ald_compare_methods.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
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.16.2
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# %% [markdown]
# # Compare outcomes from differential analysis based on different imputation methods
#
# - load scores based on `10_1_ald_diff_analysis`
# %% tags=["hide-input"]
import logging
from pathlib import Path
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from IPython.display import display
import pimmslearn
import pimmslearn.databases.diseases
logger = pimmslearn.logging.setup_nb_logger()
plt.rcParams['figure.figsize'] = (2, 2)
fontsize = 5
pimmslearn.plotting.make_large_descriptors(fontsize)
logging.getLogger('fontTools').setLevel(logging.ERROR)
# catch passed parameters
args = None
args = dict(globals()).keys()
# %% [markdown]
# ## Parameters
# Default and set parameters for the notebook.
# %% tags=["parameters"]
folder_experiment = 'runs/appl_ald_data/plasma/proteinGroups'
target = 'kleiner'
model_key = 'VAE'
baseline = 'RSN'
out_folder = 'diff_analysis'
selected_statistics = ['p-unc', '-Log10 pvalue', 'qvalue', 'rejected']
disease_ontology = 5082 # code from https://disease-ontology.org/
# split diseases notebook? Query gene names for proteins in file from uniprot?
annotaitons_gene_col = 'PG.Genes'
# %% [markdown]
# Add set parameters to configuration
# %% tags=["hide-input"]
params = pimmslearn.nb.get_params(args, globals=globals())
args = pimmslearn.nb.Config()
args.folder_experiment = Path(params["folder_experiment"])
args = pimmslearn.nb.add_default_paths(args,
out_root=(
args.folder_experiment
/ params["out_folder"]
/ params["target"]
/ f"{params['baseline']}_vs_{params['model_key']}"))
args.update_from_dict(params)
args.scores_folder = scores_folder = (args.folder_experiment
/ params["out_folder"]
/ params["target"]
/ 'scores')
args.freq_features_observed = args.folder_experiment / 'freq_features_observed.csv'
args
# %% [markdown]
# ### Excel file for exports
# %%
files_out = dict()
writer_args = dict(float_format='%.3f')
fname = args.out_folder / 'diff_analysis_compare_methods.xlsx'
files_out[fname.name] = fname
writer = pd.ExcelWriter(fname)
logger.info("Writing to excel file: %s", fname)
# %% [markdown]
# ## Load scores
# %% [markdown]
# ### Load baseline model scores
# Show all statistics, later use selected statistics
# %% tags=["hide-input"]
fname = args.scores_folder / f'diff_analysis_scores_{args.baseline}.pkl'
scores_baseline = pd.read_pickle(fname)
scores_baseline
# %% [markdown]
# ### Load selected comparison model scores
# %% tags=["hide-input"]
fname = args.scores_folder / f'diff_analysis_scores_{args.model_key}.pkl'
scores_model = pd.read_pickle(fname)
scores_model
# %% [markdown]
# ### Combined scores
# show only selected statistics for comparsion
# %% tags=["hide-input"]
scores = scores_model.join(scores_baseline, how='outer')[[args.baseline, args.model_key]]
scores = scores.loc[:, pd.IndexSlice[scores.columns.levels[0].to_list(),
args.selected_statistics]]
scores
# %% [markdown]
# Models in comparison (name mapping)
# %% tags=["hide-input"]
models = pimmslearn.nb.Config.from_dict(
pimmslearn.pandas.index_to_dict(scores.columns.get_level_values(0)))
vars(models)
# %% [markdown]
# ## Describe scores
# %% tags=["hide-input"]
scores.describe()
# %% [markdown]
# ### One to one comparison of by feature:
# %% tags=["hide-input"]
scores = scores.loc[pd.IndexSlice[:, args.target], :]
scores.to_excel(writer, 'scores', **writer_args)
scores
# %% [markdown]
# And the descriptive statistics
# of the numeric values:
# %% tags=["hide-input"]
scores.describe()
# %% [markdown]
# and the boolean decision values
# %% tags=["hide-input"]
scores.describe(include=['bool', 'O'])
# %% [markdown]
# ## Load frequencies of observed features
# %% tags=["hide-input"]
freq_feat = pd.read_csv(args.freq_features_observed, index_col=0)
freq_feat.columns = pd.MultiIndex.from_tuples([('data', 'frequency'),])
freq_feat
# %% [markdown]
# ## Compare shared features
# %% tags=["hide-input"]
scores_common = (scores
.dropna()
.reset_index(-1, drop=True)
).join(
freq_feat, how='left'
)
scores_common
# %% [markdown]
# ### Annotate decisions in Confusion Table style:
# %% tags=["hide-input"]
def annotate_decision(scores, model, model_column):
return scores[(model_column, 'rejected')].replace({False: f'{model} (no) ', True: f'{model} (yes)'})
annotations = None
for model, model_column in models.items():
if annotations is not None:
annotations += ' - '
annotations += annotate_decision(scores_common,
model=model, model_column=model_column)
else:
annotations = annotate_decision(
scores_common, model=model, model_column=model_column)
annotations.name = 'Differential Analysis Comparison'
annotations.value_counts()
# %% [markdown]
# ### List different decisions between models
# %% tags=["hide-input"]
mask_different = (
(scores_common.loc[:, pd.IndexSlice[:, 'rejected']].any(axis=1))
& ~(scores_common.loc[:, pd.IndexSlice[:, 'rejected']].all(axis=1))
)
_to_write = scores_common.loc[mask_different]
_to_write.to_excel(writer, 'differences', **writer_args)
logger.info("Writen to Excel file under sheet 'differences'.")
_to_write
# %% [markdown]
# ## Plot qvalues of both models with annotated decisions
#
# Prepare data for plotting (qvalues)
# %% tags=["hide-input"]
var = 'qvalue'
to_plot = [scores_common[v][var] for v in models.values()]
for s, k in zip(to_plot, models.keys()):
s.name = k.replace('_', ' ')
to_plot.append(scores_common['data'])
to_plot.append(annotations)
to_plot = pd.concat(to_plot, axis=1)
to_plot
# %% [markdown]
# List of features with the highest difference in qvalues
# %% tags=["hide-input"]
# should it be possible to run not only RSN?
to_plot['diff_qvalue'] = (to_plot[str(args.baseline)] - to_plot[str(args.model_key)]).abs()
to_plot.loc[mask_different].sort_values('diff_qvalue', ascending=False)
# %% [markdown]
# ### Differences plotted with created annotations
# %% tags=["hide-input"]
figsize = (4, 4)
size = 5
fig, ax = plt.subplots(figsize=figsize)
x_col = to_plot.columns[0]
y_col = to_plot.columns[1]
ax = sns.scatterplot(data=to_plot,
x=x_col,
y=y_col,
s=size,
hue='Differential Analysis Comparison',
ax=ax)
_ = ax.legend(fontsize=fontsize,
title_fontsize=fontsize,
markerscale=0.4,
title='',
)
ax.set_xlabel(f"qvalue for {x_col}")
ax.set_ylabel(f"qvalue for {y_col}")
ax.hlines(0.05, 0, 1, color='grey', linestyles='dotted')
ax.vlines(0.05, 0, 1, color='grey', linestyles='dotted')
sns.move_legend(ax, "upper right")
files_out[f'diff_analysis_comparision_1_{args.model_key}'] = (
args.out_folder /
f'diff_analysis_comparision_1_{args.model_key}')
fname = files_out[f'diff_analysis_comparision_1_{args.model_key}']
pimmslearn.savefig(fig, name=fname)
# %% [markdown]
# - also showing how many features were measured ("observed") by size of circle
# %% tags=["hide-input"]
fig, ax = plt.subplots(figsize=figsize)
ax = sns.scatterplot(data=to_plot,
x=to_plot.columns[0],
y=to_plot.columns[1],
size='frequency',
s=size,
sizes=(5, 20),
hue='Differential Analysis Comparison')
_ = ax.legend(fontsize=fontsize,
title_fontsize=fontsize,
markerscale=0.6,
title='',
)
ax.set_xlabel(f"qvalue for {x_col}")
ax.set_ylabel(f"qvalue for {y_col}")
ax.hlines(0.05, 0, 1, color='grey', linestyles='dotted')
ax.vlines(0.05, 0, 1, color='grey', linestyles='dotted')
sns.move_legend(ax, "upper right")
files_out[f'diff_analysis_comparision_2_{args.model_key}'] = (
args.out_folder / f'diff_analysis_comparision_2_{args.model_key}')
pimmslearn.savefig(
fig, name=files_out[f'diff_analysis_comparision_2_{args.model_key}'])
# %% [markdown]
# ## Only features contained in model
# - this block exist due to a specific part in the ALD analysis of the paper
# %% tags=["hide-input"]
scores_model_only = scores.reset_index(level=-1, drop=True)
_diff = scores_model_only.index.difference(scores_common.index)
if not _diff.empty:
scores_model_only = (scores_model_only
.loc[
_diff,
args.model_key]
.sort_values(by='qvalue', ascending=True)
.join(freq_feat.squeeze().rename(freq_feat.columns.droplevel()[0])
)
)
display(scores_model_only)
else:
scores_model_only = None
logger.info("No features only in new comparision model.")
if not _diff.empty:
scores_model_only.to_excel(writer, 'only_model', **writer_args)
display(scores_model_only.rejected.value_counts())
scores_model_only_rejected = scores_model_only.loc[scores_model_only.rejected]
scores_model_only_rejected.to_excel(
writer, 'only_model_rejected', **writer_args)
# %% [markdown]
# ## DISEASES DB lookup
#
# Query diseases database for gene associations with specified disease ontology id.
# %% tags=["hide-input"]
data = pimmslearn.databases.diseases.get_disease_association(
doid=args.disease_ontology, limit=10000)
data = pd.DataFrame.from_dict(data, orient='index').rename_axis('ENSP', axis=0)
data = data.rename(columns={'name': args.annotaitons_gene_col}).reset_index(
).set_index(args.annotaitons_gene_col)
data
# %% [markdown]
# ## Shared features
# ToDo: new script -> DISEASES DB lookup
# %% tags=["hide-input"]
# %% tags=["hide-input"]
feat_name = scores.index.names[0] # first index level is feature name
if args.annotaitons_gene_col in scores.index.names:
logger.info(f"Found gene annotation in scores index: {scores.index.names}")
else:
logger.info(f"No gene annotation in scores index: {scores.index.names}"
" Exiting.")
import sys
sys.exit(0)
# %% tags=["hide-input"]
gene_to_PG = (scores.droplevel(
list(set(scores.index.names) - {feat_name, args.annotaitons_gene_col})
)
.index
.to_frame()
.reset_index(drop=True)
.set_index(args.annotaitons_gene_col)
)
gene_to_PG.head()
# %% tags=["hide-input"]
disease_associations_all = data.join(
gene_to_PG).dropna().reset_index().set_index(feat_name).join(annotations)
disease_associations_all
# %% [markdown]
# ## only by model
# %% tags=["hide-input"]
idx = disease_associations_all.index.intersection(scores_model_only.index)
disease_assocications_new = disease_associations_all.loc[idx].sort_values(
'score', ascending=False)
disease_assocications_new.head(20)
# %% tags=["hide-input"]
mask = disease_assocications_new.loc[idx, 'score'] >= 2.0
disease_assocications_new.loc[idx].loc[mask]
# %% [markdown]
# ## Only by model which were significant
# %% tags=["hide-input"]
idx = disease_associations_all.index.intersection(
scores_model_only_rejected.index)
disease_assocications_new_rejected = disease_associations_all.loc[idx].sort_values(
'score', ascending=False)
disease_assocications_new_rejected.head(20)
# %% tags=["hide-input"]
mask = disease_assocications_new_rejected.loc[idx, 'score'] >= 2.0
disease_assocications_new_rejected.loc[idx].loc[mask]
# %% [markdown]
# ## Shared which are only significant for by model
# %%
mask = (scores_common[(str(args.model_key), 'rejected')] & mask_different)
mask.sum()
# %% tags=["hide-input"]
idx = disease_associations_all.index.intersection(mask.index[mask])
disease_assocications_shared_rejected_by_model = (disease_associations_all.loc[idx].sort_values(
'score', ascending=False))
disease_assocications_shared_rejected_by_model.head(20)
# %% tags=["hide-input"]
mask = disease_assocications_shared_rejected_by_model.loc[idx, 'score'] >= 2.0
disease_assocications_shared_rejected_by_model.loc[idx].loc[mask]
# %% [markdown]
# ## Only significant by RSN
# %%
mask = (scores_common[(str(args.baseline), 'rejected')] & mask_different)
mask.sum()
# %% tags=["hide-input"]
idx = disease_associations_all.index.intersection(mask.index[mask])
disease_assocications_shared_rejected_by_RSN = (
disease_associations_all
.loc[idx]
.sort_values('score', ascending=False))
disease_assocications_shared_rejected_by_RSN.head(20)
# %% tags=["hide-input"]
mask = disease_assocications_shared_rejected_by_RSN.loc[idx, 'score'] >= 2.0
disease_assocications_shared_rejected_by_RSN.loc[idx].loc[mask]
# %% [markdown]
# ## Write to excel
# %% tags=["hide-input"]
disease_associations_all.to_excel(
writer, sheet_name='disease_assoc_all', **writer_args)
disease_assocications_new.to_excel(
writer, sheet_name='disease_assoc_new', **writer_args)
disease_assocications_new_rejected.to_excel(
writer, sheet_name='disease_assoc_new_rejected', **writer_args)
# %% [markdown]
# ## Outputs
# %% tags=["hide-input"]
writer.close()
files_out