-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
620 lines (496 loc) · 23.3 KB
/
utils.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
from sklearn.model_selection import train_test_split
from statistics import mean, median, mode, quantiles
import pandas as pd
from data_makers import *
import itertools
# tts across cell lines
def cell_line_split(X, y, test_size=0.2, random_state=0):
cl = []
#loop to extract cell lines from X
for i in X.index:
cell = i.split('::')[0]
if cell not in cl: # remove repeats
cl.append(cell)
cl_train, cl_test = train_test_split(cl, test_size=test_size, random_state=random_state) ## tts the cell lines
assert len(set(cl_train).intersection(cl_test)) == 0
train_indexes = []
test_indexes = []
# split indexes in X df by the previous cell lines tts
for i in X.index:
cell_line = i.split('::')[0]
if cell_line in cl_train:
train_indexes.append(i)
if cell_line in cl_test:
test_indexes.append(i)
# perform individual splits for the NN inputs
if y is None:
X_train = X.reindex(train_indexes)
X_test = X.reindex(test_indexes)
return X_train, X_test
else:
X_train = X.reindex(train_indexes)
X_test = X.reindex(test_indexes)
y_train = y.reindex(train_indexes)
y_test = y.reindex(test_indexes)
return X_train, X_test, y_train, y_test
#function to find the largest elements
def Nmaxelements(list1, N):
list2 = list1[:] #produce temporary list for the function
max_list=[]
for i in range(N):
max_list.append(max(list2))
list2.remove(max(list2))
return max_list
# function to order the features by 'importance'
def topFeatures(classify, X_main, topX = 22786, N = 22786):
#extract data to at least remove one hot features
rfr_names = list(X_main[:topX].columns)
rfr_scores = list(classify[:topX])
#create dictionaries to allow for easy sorting
rfr_dict_scores = {val: i for i, val in enumerate(rfr_scores)}
rfr_dict_names = {i: val for i, val in enumerate(rfr_names)}
#produce ordered lists of largest scores and names
rfr_largest_scores = Nmaxelements(rfr_scores, N)
rfr_largest_indexes = [rfr_dict_scores[s] for s in rfr_largest_scores]
rfr_largest_names = [rfr_dict_names[i] for i in rfr_largest_indexes]
return rfr_largest_names, rfr_largest_scores
#returns the top specified number of features
def feat_finder(file_list, topX = 20):
dict_list = []
for f in file_list:
feature_dict = {}
with open(f, "r") as features:
lines = features.readlines()
for i in lines:
phospho = i.split(':')[0]
score = i.split(':')[1]
score = score.split("\n")[0]
feature_dict[phospho] = float(score)
dict_list.append(feature_dict)
feat_list = [list(fd)[:topX] for fd in dict_list]
scores = {}
for i1, dic in enumerate(feat_list):
if len(feat_list) > 2:
check_list = feat_list[:i1] + feat_list[i1+1:]
check_list = check_list[0] + check_list[1]
for i2, item in enumerate(dic):
if item in check_list and item not in scores:
scores[item]=[[i2], [dict_list[i1][item]]]
elif item in check_list and item in scores:
scores[item][0].append(i2)
scores[item][1].append(dict_list[i1][item])
elif len(feat_list) == 2:
check_list = feat_list[:i1] + feat_list[i1+1:]
for i2, item in enumerate(dic):
if item in check_list[0] and item not in scores:
scores[item]=[[i2], [dict_list[i1][item]]]
elif item in check_list[0] and item in scores:
scores[item][0].append(i2)
scores[item][1].append(dict_list[i1][item])
ranker = []
for i in scores:
scores[i].insert(1, mean(scores[i][0]))
scores[i].insert(3, mean(scores[i][2]))
sorted_dict = dict(sorted(scores.items(), key=lambda item: item[1][1]))
return sorted_dict
#separates the X dataframe by cancer type
def cancer_lines(X_main):
cl_data = pd.read_excel("data/41467_2021_22170_MOESM3_ESM.xlsx")
# remove unwanted columns, duplicates, reset index
cl_to_cancer = cl_data[['Cancer', 'Cell line']]
cl_to_cancer = cl_to_cancer.drop_duplicates(subset=["Cell line"], keep='first')
cl_to_cancer = cl_to_cancer.reset_index(drop=True)
# create a list of cell lines that are present in our final dataset
unique_cls = []
for i in X_main.index:
cl = i.split('::')[0]
if cl not in unique_cls:
unique_cls.append(cl)
# remove the cell lines not present in final dataset
for i, cl in enumerate(cl_to_cancer['Cell line']):
if cl not in unique_cls:
cl_to_cancer = cl_to_cancer.drop(i)
#sort by cancer
cl_to_cancer = cl_to_cancer.sort_values(by='Cancer')
cl_to_cancer = cl_to_cancer.reset_index(drop=True)
#separate all the cell lines by cancer type
AML_lines = cl_to_cancer[cl_to_cancer['Cancer']=='AML']
hepato_lines = cl_to_cancer[cl_to_cancer['Cancer']=='Hepatocellular']
esophag_lines = cl_to_cancer[cl_to_cancer['Cancer']=='Esophageal']
return AML_lines, hepato_lines, esophag_lines
#function that turns arrays containing model output descriptive stats into dataframe
#title must be string
#data is 2d array with all arrays with descriptive stats
#headers is an array of the same length as data but with header names for the descriptive stats
def table_make(title, data, headers, file):
title = {}
for index, array in enumerate(data):
title[headers[index]] = array
df = pd.DataFrame.from_dict(title)
df.index = df[headers[0]]
df = df.drop([headers[0]], axis=1)
if file:
df.to_csv(file)
return df
#filter X for cancer type
def cancer_filter(X, y, cancer = 'AML'):
cl_data = pd.read_excel("data/41467_2021_22170_MOESM3_ESM.xlsx")
# remove unwanted columns, duplicates, reset index
cl_to_cancer = cl_data[['Cancer', 'Cell line']]
cl_to_cancer = cl_to_cancer.drop_duplicates(subset=["Cell line"], keep='first')
cl_to_cancer = cl_to_cancer.reset_index(drop=True)
#sort by cancer
cl_to_cancer = cl_to_cancer.sort_values(by='Cancer')
cl_to_cancer = cl_to_cancer.reset_index(drop=True)
#isolate a list of only AML lines
cancer_lines = cl_to_cancer[cl_to_cancer['Cancer']==cancer]
cl = list(cancer_lines['Cell line'])
# reindex the dataframes for these targets
X = X.reindex(cl)
y = y.reindex(cl)
return X, y
# changes the residue style
def residue_changer(phos_list):
residue_dict = {'Ser':'S', 'Thr':'T', 'Met':'M', 'Tyr':'Y', 'Arg':'R', 'Lys':'K'}
new_phos_list = []
for i, feat in enumerate(phos_list):
#isolate the residue from the phosphopeptide name
prot, residue = feat.split('(')
residue = residue.split(')')[0]
if residue[:3] in residue_dict.keys():
#replace the one letter symbol for the residue for the three letter symbol seen in the SIGNOR database
new_symbol = residue_dict[residue[:3]]
new_residue = new_symbol+residue[3:]
new_phos = f'{prot}({new_residue});'
new_phos_list.append(new_phos)
# double check if format is S939 rather than Ser939
elif isinstance(residue[0], str) and isinstance(residue[1], int):
phos = f'{prot}({residue});'
new_phos_list.append(phos)
return new_phos_list
# returns the phosphopeptides associated with a given list of kinases
def kinase_target_finder(kinases):
#kinase targets from SIGNOR database
signor_df = pd.read_csv('data/human_phosphorylations_26_05_23.txt', sep='\t')
signor_df = signor_df[['ENTITYA', 'MECHANISM', 'ENTITYB', 'RESIDUE']]
signor_df = signor_df[signor_df['MECHANISM']=='phosphorylation']
#filter signor using targets
signor_df = signor_df[signor_df['ENTITYA'].isin(kinases)]
signor_df = signor_df.reset_index()
targs_phospho = [f'{i[1][3]}({i[1][4]});' for i in signor_df.iterrows()]
targs_phospho = residue_changer(targs_phospho)
return targs_phospho
# returns a number of metrics regarding significant drug targets
def SHAP_targets(X_main, explainer, X_test, y_test, dtype = 'phospho', strict = False):
indexes = []
total_feats = []
percent_of_total = []
total_sig = []
percent_of_sig = []
targets = []
percent_sig = []
sig_targs = []
insig_targs = []
if dtype == 'phospho':
# dictionary of the drugs and the targets of them
from data_makers import drugData
dd = drugData(X_main, isPhospho = True)
target_lists = [i.split(', ') for i in dd['Targets']]
drug_dict = {dd.index[i]:target_lists[i] for i in range(len(target_lists))}
elif dtype == 'proteomic':
# dictionary of the drugs and the targets of them
from data_makers import drugData
dd = drugData(X_main)
target_lists = [i.split('; ') for i in dd['Targets']]
drug_dict = {dd.index[i]:target_lists[i] for i in range(len(target_lists))}
elif dtype == 'mix':
from data_makers import drugData
# Load drug datasets
dd_phos = drugData(X_main, isPhospho=True)
dd_prot = drugData(X_main)
# Split target lists
target_lists_phos = [i.split(', ') for i in dd_phos['Targets']]
target_lists_prot = [i.split('; ') for i in dd_prot['Targets']]
# Create a dictionary with drug indices as keys and their corresponding target lists as values
drug_dict = {dd_prot.index[i]: target_lists_prot[i] for i in range(len(target_lists_prot))}
# Merge target lists for drugs present in both datasets
for i, row in dd_phos.iterrows():
if i in drug_dict:
targs = row['Targets'].split(', ')
drug_dict[i].extend(targs)
for i in range(len(X_test)):
try:
if dtype == 'proteomic':
#calculate shap scores
shaps = explainer.shap_values(X_test.iloc[i:i+1][:386], y_test[i], check_additivity=False)
if strict:
upper_quartile = np.quantile(shaps[0], 0.90)
else:
upper_quartile = np.quantile(shaps[0], 0.75)
sig_shaps = [x for x in shaps[0] if x > upper_quartile]
elif dtype == 'phospho':
#calculate shap scores
shaps = explainer.shap_values(X_test.iloc[i:i+1][:130], y_test[i], check_additivity=False)
if strict:
upper_quartile = np.quantile(shaps[0], 0.90)
else:
upper_quartile = np.quantile(shaps[0], 0.75)
sig_shaps = [x for x in shaps[0] if x > upper_quartile]
elif dtype == 'mix':
#calculate shap scores
shaps = explainer.shap_values(X_test.iloc[i:i+1][:516], y_test[i], check_additivity=False)
if strict:
upper_quartile = np.quantile(shaps[0], 0.90)
else:
upper_quartile = np.quantile(shaps[0], 0.75)
sig_shaps = [x for x in shaps[0] if x > upper_quartile]
#targets for the specific drug
cl, drug = y_test.index[i].split('::')
shap_targets = drug_dict[drug]
print(shap_targets)
#find where the targets are situated in the shap list
index = [i2 for i2, v in enumerate(X_test.iloc[i:i+1]) if v in shap_targets]
#find the shap values for the targets
shap_vals = [shaps[0][i3] for i3 in index]
#METRICS
significant_targets = [shap_targets[i4] for i4, sh in enumerate(shap_vals) if sh > upper_quartile]
insignificant_targets = set(shap_targets).difference(significant_targets)
percent_significant = (len(significant_targets)/len(shap_targets))*100
t_feats = len(shaps[0])
p_of_total = (len(shap_targets)/t_feats)*100
t_sig = len(sig_shaps)
p_of_sig = (len(significant_targets)/t_sig)*100
indexes.append(f'{cl}::{drug}')
total_feats.append(t_feats)
percent_of_total.append(p_of_total)
total_sig.append(t_sig)
percent_of_sig.append(p_of_sig)
targets.append(shap_targets)
percent_sig.append(percent_significant)
sig_targs.append(significant_targets)
insig_targs.append(insignificant_targets)
except KeyError as e:
print(f"No targets found for {cl}{drug} : {e}")
return indexes, (total_feats, percent_of_total, total_sig, percent_of_sig), (targets, percent_sig, sig_targs, insig_targs)
# returns a number of metrics regarding significant drug targets
def SHAP_targets_NN(X_main, explainer, X_test, y_test, dtype = 'phospho', strict=False):
indexes = []
total_feats = []
percent_of_total = []
total_sig = []
percent_of_sig = []
targets = []
percent_sig = []
sig_targs = []
insig_targs = []
xo_test, xd_test = X_test
if dtype == 'phospho':
# dictionary of the drugs and the targets of them
from data_makers import drugData
dd = drugData(X_main, isPhospho = True)
target_lists = [i.split(', ') for i in dd['Targets']]
drug_dict = {dd.index[i]:target_lists[i] for i in range(len(target_lists))}
elif dtype == 'proteomic':
# dictionary of the drugs and the targets of them
from data_makers import drugData
dd = drugData(X_main)
target_lists = [i.split('; ') for i in dd['Targets']]
drug_dict = {dd.index[i]:target_lists[i] for i in range(len(target_lists))}
elif dtype == 'mix':
from data_makers import drugData
# Load drug datasets
dd_phos = drugData(X_main, isPhospho=True)
dd_prot = drugData(X_main)
# Split target lists
target_lists_phos = [i.split(', ') for i in dd_phos['Targets']]
target_lists_prot = [i.split('; ') for i in dd_prot['Targets']]
# Create a dictionary with drug indices as keys and their corresponding target lists as values
drug_dict = {dd_prot.index[i]: target_lists_prot[i] for i in range(len(target_lists_prot))}
# Merge target lists for drugs present in both datasets
for i, row in dd_phos.iterrows():
if i in drug_dict:
targs = row['Targets'].split(', ')
drug_dict[i].extend(targs)
for i in range(len(xo_test)):
try:
shaps = explainer.shap_values([np.array([xo_test.iloc[i]]), np.array([xd_test.iloc[i]])])
if strict:
upper_quartile = np.quantile(shaps[0][0][0], 0.90)
else:
upper_quartile = np.quantile(shaps[0][0][0], 0.75)
sig_shaps = [x for x in shaps[0][0][0] if x > upper_quartile]
#targets for the specific drug
cl, drug = y_test.index[i].split('::')
drug_targets = drug_dict[drug]
#find where the targets are situated in the shap list
index = [i2 for i2, v in enumerate(xo_test.iloc[i:i+1]) if v in drug_targets]
#find the shap values for the targets
shap_vals = [shaps[0][0][0][i3] for i3 in index]
#METRICS
significant_targets = [drug_targets[i4] for i4, sh in enumerate(shap_vals) if sh > upper_quartile and sh > 0]
insignificant_targets = set(drug_targets).difference(significant_targets)
percent_significant = (len(significant_targets)/len(drug_targets))*100
t_feats = len(shaps[0][0][0])
p_of_total = (len(drug_targets)/t_feats)*100
t_sig = len(sig_shaps)
p_of_sig = (len(significant_targets)/t_sig)*100
indexes.append(f'{cl}::{drug}')
total_feats.append(t_feats)
percent_of_total.append(p_of_total)
total_sig.append(t_sig)
percent_of_sig.append(p_of_sig)
targets.append(drug_targets)
percent_sig.append(percent_significant)
sig_targs.append(significant_targets)
insig_targs.append(insignificant_targets)
except KeyError as e:
print(f"No targets found for {cl}{drug} : {e}")
return indexes, (total_feats, percent_of_total, total_sig, percent_of_sig), (targets, percent_sig, sig_targs, insig_targs)
# returns a number of metrics regarding significant drug targets
def IG_targets_NN(X_main, explainer, X_test, y_test, dtype = 'phospho', strict = False):
indexes = []
total_feats = []
percent_of_total = []
total_sig = []
percent_of_sig = []
targets = []
percent_sig = []
sig_targs = []
insig_targs = []
xo_test, xd_test = X_test
if dtype == 'phospho':
# dictionary of the drugs and the targets of them
from data_makers import drugData
dd = drugData(X_main, isPhospho = True)
target_lists = [i.split(', ') for i in dd['Targets']]
drug_dict = {dd.index[i]:target_lists[i] for i in range(len(target_lists))}
elif dtype == 'proteomic':
# dictionary of the drugs and the targets of them
from data_makers import drugData
dd = drugData(X_main)
target_lists = [i.split('; ') for i in dd['Targets']]
drug_dict = {dd.index[i]:target_lists[i] for i in range(len(target_lists))}
elif dtype == 'mix':
from data_makers import drugData
# Load drug datasets
dd_phos = drugData(X_main, isPhospho=True)
dd_prot = drugData(X_main)
# Split target lists
target_lists_phos = [i.split(', ') for i in dd_phos['Targets']]
target_lists_prot = [i.split('; ') for i in dd_prot['Targets']]
# Create a dictionary with drug indices as keys and their corresponding target lists as values
drug_dict = {dd_prot.index[i]: target_lists_prot[i] for i in range(len(target_lists_prot))}
# Merge target lists for drugs present in both datasets
for i, row in dd_phos.iterrows():
if i in drug_dict:
targs = row['Targets'].split(', ')
drug_dict[i].extend(targs)
for i in range(len(xo_test)):
try:
#explainer instance
explanation = explainer.explain([xo_test[i:(i+1)].values, np.array(xd_test[i:(i+1)])],
baselines=None,
target=None)
attributions = explanation.attributions #top features for this explanation
if strict:
upper_quartile = np.quantile(attributions[0][0], 0.90)
else:
upper_quartile = np.quantile(attributions[0][0], 0.75)
sig_shaps = [x for x in attributions[0][0] if x > upper_quartile]
#targets for the specific drug
cl, drug = y_test.index[i].split('::')
drug_targets = drug_dict[drug]
#find where the targets are situated in the shap list
index = [i2 for i2, v in enumerate(xo_test.iloc[i:i+1]) if v in drug_targets]
#find the shap values for the targets
ig_vals = [attributions[0][0][i3] for i3 in index]
#METRICS
significant_targets = [drug_targets[i4] for i4, sh in enumerate(ig_vals) if sh > upper_quartile and sh > 0]
insignificant_targets = set(drug_targets).difference(significant_targets)
percent_significant = (len(significant_targets)/len(drug_targets))*100
t_feats = len(attributions[0][0])
p_of_total = (len(drug_targets)/t_feats)*100
t_sig = len(sig_shaps)
p_of_sig = (len(significant_targets)/t_sig)*100
indexes.append(f'{cl}::{drug}')
total_feats.append(t_feats)
percent_of_total.append(p_of_total)
total_sig.append(t_sig)
percent_of_sig.append(p_of_sig)
targets.append(drug_targets)
percent_sig.append(percent_significant)
sig_targs.append(significant_targets)
insig_targs.append(insignificant_targets)
except KeyError as e:
print(f"No targets found for {cl}{drug} : {e}")
return indexes, (total_feats, percent_of_total, total_sig, percent_of_sig), (targets, percent_sig, sig_targs, insig_targs)
# returns index of a target
def index_finder(targ, df):
index = 0
for i, v in df.iterrows():
if i == targ:
return index
index+=1
print('No index found')
#standardise by removing the mean from each point and dividing by sd
def standardiser(train, test):
#set the mean
mean = train.mean(axis=0)
std = train.std(axis=0)
# standardise both the train and test on the training set mean
train -= mean
train /= std
test -= mean
test /= std
return train, test
# turns the IC50 values into classes
def classyFire(y):
for i in range(len(y)):
if y[i] < 2.36:
y[i] = 0
elif y[i] > 2.36 and y[i] < 5.26:
y[i] = 1
elif y[i] > 5.26:
y[i] = 2
print(f'0 : high responsiveness\n1 : intermediate responsiveness\n2 : low responsiveness')
return y
# removes the one hot from the outputs for explainability techniques
def drug_feat_remover(exp_list):
# remove drug features
feat_list = []
for i, LIME in enumerate(exp_list):
matches = re.findall(r'[A-Z]', LIME[0])
if matches and LIME[1]>0:
feat_list.append(f'{LIME[0]}: {LIME[1]}')
return feat_list
# calculate electronic markers for drug response
def EMDR_finder(drug, dtype='phospho', rawData = True):
#read in data types
if dtype == 'phospho' and rawData == False:
EMDR_df = pd.read_csv('EMDR/EMDR_phos/EMDR_vals.csv')
elif dtype == 'phospho' and rawData:
EMDR_df = pd.read_csv('EMDR/raw_EMDR_phos/normal_EMDR_vals.csv', index_col=0)
elif dtype == 'proteomic' and rawData == False:
print('Only raw data available')
elif dtype == 'proteomic' and rawData:
EMDR_df = pd.read_csv('EMDR/raw_EMDR_prot/normal_EMDR_vals.csv', index_col=0)
EMDR_feature_split = EMDR_df['EMDRs'][drug].split("'") # split the string that is outputted into an array
even_feats = itertools.islice(EMDR_feature_split, 1, None, 2) # remove odd indexes in arrays as these contain "'" and no features due to split
EMDR_features = list(itertools.chain(even_feats)) # chain this together into an array again
return EMDR_features
def drug_or_cl_sig(target, df, isDrug=True):
indexes = []
sig_list = []
if isDrug:
for row in df.iterrows():
drug = row[1]['cl:drug'].split('::')[1]
if drug == target:
sig_list.append(row[1]['percent significant'])
indexes.append(row[0])
elif not isDrug:
for row in df.iterrows():
cell_line = row[1]['cl:drug'].split('::')[0]
if cell_line == target:
sig_list.append(row[1]['percent significant'])
indexes.append(row[0])
return indexes, sig_list