-
Notifications
You must be signed in to change notification settings - Fork 25
/
utils_get_afterpca.py
311 lines (251 loc) · 9.08 KB
/
utils_get_afterpca.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
from Bio.Alphabet import generic_dna
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Align import MultipleSeqAlignment
from Bio import AlignIO
from Bio.SubsMat import MatrixInfo
import json
import numpy as np
from collections import Counter
import pandas as pd
import re
from sklearn.decomposition import PCA
from sklearn.preprocessing import Normalizer, StandardScaler,MinMaxScaler,RobustScaler
from sklearn.metrics import roc_curve, auc, precision_recall_curve, confusion_matrix, f1_score,accuracy_score
import collections
import itertools
import matplotlib.pyplot as plt
from alternative_encoding import *
def dict_inventory(inventory):
dicA, dicB, dicC = {}, {}, {}
dic = {'A': dicA, 'B': dicB, 'C': dicC}
for hla in inventory:
type_ = hla[4] # A,B,C
first2 = hla[6:8] # 01
last2 = hla[8:] # 01
try:
dic[type_][first2].append(last2)
except KeyError:
dic[type_][first2] = []
dic[type_][first2].append(last2)
return dic
def rescue_unknown_hla(hla, dic_inventory):
type_ = hla[4]
first2 = hla[6:8]
last2 = hla[8:]
big_category = dic_inventory[type_]
#print(hla)
if not big_category.get(first2) == None:
small_category = big_category.get(first2)
distance = [abs(int(last2) - int(i)) for i in small_category]
optimal = min(zip(small_category, distance), key=lambda x: x[1])[0]
return 'HLA-' + str(type_) + '*' + str(first2) + str(optimal)
else:
small_category = list(big_category.keys())
distance = [abs(int(first2) - int(i)) for i in small_category]
optimal = min(zip(small_category, distance), key=lambda x: x[1])[0]
return 'HLA-' + str(type_) + '*' + str(optimal) + str(big_category[optimal][0])
def blosum50_new(peptide):
amino = 'ARNDCQEGHILKMFPSTWYV-'
dic = MatrixInfo.blosum50
matrix = np.zeros([21, 21])
for i in range(matrix.shape[0]):
for j in range(matrix.shape[1]):
try:
matrix[i, j] = dic[(amino[i], amino[j])]
except KeyError:
try:
matrix[i, j] = dic[(amino[j], amino[i])]
except:
matrix[i, j] = -1
encoded = np.empty([len(peptide), 21]) # (seq_len,21)
for i in range(len(peptide)):
query = peptide[i]
if query == 'X': query = '-'
query = query.upper()
encoded[i, :] = matrix[:, amino.index(query)]
return encoded
def paratope_dic(hla):
df = hla
dic = {}
for i in range(df.shape[0]):
hla = df['hla'].iloc[i]
paratope = df['paratope'].iloc[i]
dic[hla] = paratope
return dic
def peptide_data(peptide): # return numpy array [10,21,1]
length = len(peptide)
if length == 10:
encode = blosum50_new(peptide)
elif length == 9:
peptide = peptide[:5] + '-' + peptide[5:]
encode = blosum(peptide)
encode = encode.reshape(encode.shape[0], encode.shape[1], -1)
return encode
def hla_data(hla, dic_inventory, hla_type): # return numpy array [46,21,1]
dic = paratope_dic(hla)
try:
seq = dic[hla_type]
except KeyError:
hla_type = rescue_unknown_hla(hla_type, dic_inventory)
seq = dic[hla_type]
encode = blosum(seq)
encode = encode.reshape(encode.shape[0], encode.shape[1], -1)
return encode
def construct(ori, hla, dic_inventory):
series = []
for i in range(ori.shape[0]):
peptide = ori['peptide'].iloc[i]
hla_type = ori['HLA'].iloc[i]
immuno = np.array(ori['immunogenecity'].iloc[i]).reshape(1,-1) # [1,1]
encode_pep = peptide_data(peptide) # [10,21]
encode_hla = hla_data(hla, dic_inventory, hla_type) # [46,21]
series.append((encode_pep, encode_hla, immuno))
return series
def draw_ROC(y_true,y_pred):
fpr,tpr,_ = roc_curve(y_true,y_pred,pos_label=1)
area_mine = auc(fpr,tpr)
fig = plt.figure()
lw = 2
plt.plot(fpr, tpr, color='darkorange',
lw=lw, label='ROC curve (area = %0.2f)' % area_mine)
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()
def draw_PR(y_true,y_pred):
precision,recall,cutoff = precision_recall_curve(y_true,y_pred,pos_label=1)
area_PR = auc(recall,precision)
baseline = np.sum(np.array(y_true) == 1) / len(y_true)
plt.figure()
lw = 2
plt.plot(recall,precision, color='darkorange',
lw=lw, label='PR curve (area = %0.2f)' % area_PR)
plt.plot([0, 1], [baseline, baseline], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('PR curve example')
plt.legend(loc="lower right")
plt.show()
return cutoff
def draw_history(history):
plt.subplot(211)
plt.title('Loss')
plt.plot(history.history['loss'], label='train')
plt.plot(history.history['val_loss'], label='test')
plt.legend()
# plot accuracy during training
plt.subplot(212)
plt.title('Accuracy')
plt.plot(history.history['accuracy'], label='train')
plt.plot(history.history['val_accuracy'], label='test')
plt.legend()
plt.show()
def add_X(array):
me = np.mean(array)
array = np.append(array, me)
return array
def read_index(path):
with open(path, 'r') as f:
data = f.readlines()
array = []
for line in data:
line = line.lstrip(' ').rstrip('\n')
line = re.sub(' +', ' ', line)
items = line.split(' ')
items = [float(i) for i in items]
array.extend(items)
array = np.array(array)
array = add_X(array)
Index = collections.namedtuple('Index',
['A', 'R', 'N', 'D', 'C', 'Q', 'E', 'G', 'H', 'I', 'L', 'K', 'M', 'F', 'P', 'S',
'T', 'W', 'Y', 'V', 'X'])
I = Index._make(array)
return I, array # namedtuple
def read_all_indices():
table = np.empty([21, 566])
for i in range(566):
if len(str(i)) == 1:
ii = '00' + str(i)
elif len(str(i)) == 2:
ii = '0' + str(i)
else:
ii = str(i)
NA_list_str = ['472', '473', '474', '475', '476', '477', '478', '479', '480', '481', '520', '523', '524']
NA_list_int = [int(i) for i in NA_list_str]
if ii in NA_list_str: continue
path = '/Users/ligk2e/Desktop/NeoAntigenWorkflow/immunogenecity/AAindex1/index{0}.txt'.format(ii)
_, array = read_index(path)
table[:, i] = array
table = np.delete(table, NA_list_int, 1)
return table
def scaling(table): # scale the features
table_scaled = RobustScaler().fit_transform(table)
return table_scaled
def wrapper_read_scaling():
table = read_all_indices()
table_scaled = scaling(table)
return table_scaled
def pca_get_components(result):
pca= PCA()
pca.fit(result)
result = pca.explained_variance_ratio_
sum_ = 0
for index,var in enumerate(result):
sum_ += var
if sum_ > 0.95:
return index # 25 components
def pca_apply_reduction(result): # if 95%, 12 PCs, if 99%, 17 PCs, if 90%,9 PCs
pca = PCA(n_components=9) # or strictly speaking ,should be 26, since python is 0-index
new = pca.fit_transform(result)
return new
# from an excel to txt
def clean_series(series): # give a pandas series
if series.dtype == object: # pandas will store str as object since string has variable length, you can use astype('|S')
clean = []
for item in series:
item = item.lstrip(' ') # remove leading whitespace
item = item.rstrip(' ') # remove trailing whitespace
item = item.replace(' ', '') # replace all whitespace in the middle
clean.append(item)
else:
clean = series
return pd.Series(clean)
def clean_data_frame(data): # give a pandas dataFrame
peptide_clean = clean_series(data['peptide'])
hla_clean = clean_series(data['HLA'])
immunogenecity_clean = clean_series(data['immunogenecity'])
data_clean = pd.concat([peptide_clean, hla_clean, immunogenecity_clean], axis=1)
data_clean.columns = ['peptide', 'HLA', 'immunogenecity']
return data_clean
def convert_hla(hla):
cond = True
hla = hla.replace(':', '')
if len(hla) < 9:
cond = False # HLA-A3
elif len(hla) == 9: # HLA-A3002
f = hla[0:5] # HLA-A
e = hla[5:] # 3002
hla = f + '*' + e
return hla, cond
def convert_hla_series(df):
new = []
col = []
for i in df['HLA']:
hla, cond = convert_hla(i)
col.append(cond)
if cond == True: new.append(hla)
df = df.loc[pd.Series(col)]
df = df.set_index(pd.Index(np.arange(df.shape[0])))
df['HLA'] = new
return df
def test_no_space(series):
for i in series:
if ' ' in i:
print('damn')