-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernels.py
387 lines (280 loc) · 10.4 KB
/
kernels.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import import_ipynb
import numpy as np
from tqdm import tqdm as tqdm
from itertools import product, combinations
from copy import deepcopy
from scipy.sparse.linalg import eigs
from numpy.linalg import multi_dot
# In[2]:
def u_phi(x, k, b_s):
#We started with spectrum kernel and here we compute the feature vector of x sequence.
# In b_s we have a list of all combination of 'A', 'C', 'G', 'T'
u_phi = np.zeros(len(b_s))
for i in range(len(x) - k + 1):
seq = x[i:i + k]
for i, b in enumerate(b_s):
u_phi[i] += (b == seq)
return u_phi
def compute_spectrum_k(X, k):
#For every input sequences we compute this one for Spectrum kernel
#Xshows some features
#K show s the length of the sequence
n = X.shape[0]
K = np.zeros((n, n))
b_s = [''.join(c) for c in product('ACGT', repeat=k)]
u_phi = []
for i, x in tqdm(enumerate(X.loc[:, 'seq']), total=n, desc='Computing feature vectors'):
u_phi.append(u_phi(x, k, b_s))
for i, x in tqdm(enumerate(X.loc[:, 'seq']), total=n, desc='Building kernel'):
for j, y in enumerate(X.loc[:, 'seq']):
if j >= i:
K[i, j] = np.dot(u_phi[i], u_phi[j])
K[j, i] = K[i, j]
K = K
return K
# In[3]:
def weight_b(d, k):
#For this step we need to calculate the weights for Weighted degree kernel(k)
#d shows the maximum degree
return 2 * (d - k + 1) / d / (d + 1)
def com_two_seq(x, y, d, L):
c_t = 0
for k in range(1, d + 1):
weight_b_k = weight_b(d, k)
c_st = 0
for l in range(1, L - k + 1):
c_st += (x[l:l + k] == y[l:l + k])
c_t += weight_b_k * c_st
return c_t
def com_k_seq(X, d):
#weighted degree kernel(d) needs to compute k for each sequences
#d shows maximom degree like preivious
n = X.shape[0]
K = np.zeros((n, n))
for i, x in tqdm(enumerate(X.loc[:, 'seq']), total=n, desc='Building kernel'):
L = len(x)
K[i, i] = L - 1 + (1 - d) / 3
for j, y in enumerate(X.loc[:, 'seq']):
if j > i:
K[i, j] = com_two_seq(x, y, d, L)
K[j, i] = K[i, j]
return K
# In[4]:
def mega(s):
#Compute mega coefficients for Weight Degree Kernel with Shifts
#return: maga(s)
return 1/2/(s+1)
def com_k_x_y(x, y, d, S, L):
c_t = 0
for k in range(1, d + 1):
weight_b_k = weight_b(d, k)
c_st = 0
for i in range(1, L - k + 1):
for s in range(0, S+1):
if s+i < L:
c_st += mega(s) * ((x[i+s:i+s+k] == y[i:i+k]) + (x[i:i+k] == y[i+s:i+s+k]))
c_t += weight_b_k * c_st
return c_t
def com_shift_k(X, d, S):
n = X.shape[0]
K = np.zeros((n, n))
for i, x in tqdm(enumerate(X.loc[:, 'seq']), total=n, desc='Building kernel'):
L = len(x)
for j, y in enumerate(X.loc[:, 'seq']):
if j >= i:
K[i, j] = com_k_x_y(x, y, d, S, L)
K[j, i] = K[i, j]
return K
# In[5]:
def fvector_MK(x, k, m, b_s):
#Compute feature vector of sequence x for Mismatch Kernel (k,m)
phi_km = np.zeros(len(b_s))
for i in range(101 - k + 1):
seq = x[i:i + k]
for i, b in enumerate(b_s):
phi_km[i] += (np.sum(seq != b) <= m)
return phi_km
def replace_let_num(x):
return x.replace('A', '1').replace('C', '2').replace('G', '3').replace('T', '4')
def format(x):
#format the data
return np.array(list(replace_let_num(x))).astype(int)
def mismatch_kernel(X, k, m):
n = X.shape[0]
K = np.zeros((n, n))
b_s = np.array([format(''.join(c)) for c in product('ACGT', repeat=k)])
phi_km_x = np.zeros((n, len(b_s)))
for i, x in tqdm(enumerate(X.loc[:, 'seq']), total=n, desc='Computing feature vectors'):
x = format(x)
phi_km_x[i] = fvector_MK(x, k, m, b_s)
for i, x in tqdm(enumerate(X.loc[:, 'seq']), total=n, desc='Building kernel'):
for j, y in enumerate(X.loc[:, 'seq']):
if j >= i:
K[i, j] = np.dot(phi_km_x[i], phi_km_x[j])
K[j, i] = K[i, j]
K = kernel_normalize(K)
return K
# In[6]:
S = np.array([[4, 0, 0, 0], [0, 9, -3, -1], [0, -3, 6, 2], [0, -1, -2, 5]])
def sw_kernel(x, y, e=11, d=1, beta=0.5):
#smith waterman kernel implementation
x, y = format(x) - 1, format(y) - 1
n_x, n_y = len(x), len(y)
M, X, Y, X2, Y2 = [np.zeros((n_x + 1, n_y + 1))] * 5
for i in range(1, n_x):
for j in range(1, n_y):
M[i, j] = np.exp(beta * S[x[i], y[j]]) * max(1, X[i - 1, j - 1], Y[i - 1, j - 1], M[i - 1, j - 1])
X[i, j] = max(np.exp(beta * d) * M[i - 1, j], np.exp(beta * e) * X[i - 1, j])
Y[i, j] = max(np.exp(beta * d) * M[i, j - 1], np.exp(beta * d) * X[i, j - 1], np.exp(beta * e) * Y[i, j - 1])
X2[i, j] = max(M[i - 1, j], X2[i - 1, j])
Y2[i, j] = max(M[i, j - 1], X2[i, j - 1], Y2[i, j - 1])
return (1/beta) * np.log(max(1, X2[n_x, n_y], Y2[n_x, n_y], M[n_x, n_y]))
def local_align_kernel(x, y, e, d, beta):
#local alignment kernel implemented here
x, y = format(x)-1, format(y)-1
n_x, n_y = len(x), len(y)
M, X, Y, X2, Y2 = [np.zeros((n_x + 1, n_y + 1))]*5
for i in range(1, n_x):
for j in range(1, n_y):
M[i, j] = np.exp(beta * S[x[i], y[j]]) * (1 + X[i-1, j-1] + Y[i-1, j-1] + M[i-1, j-1])
X[i, j] = np.exp(beta * d) * M[i-1, j] + np.exp(beta * e) * X[i-1, j]
Y[i, j] = np.exp(beta * d) * (M[i, j-1] + X[i, j-1]) + np.exp(beta * e) * Y[i, j-1]
X2[i, j] = M[i-1, j] + X2[i-1, j]
Y2[i, j] = M[i, j-1] + X2[i, j-1] + Y2[i, j-1]
return (1/beta) * np.log(1 + X2[n_x, n_y] + Y2[n_x, n_y] + M[n_x, n_y])
def execute_al(X, e=11, d=1, beta=0.5, smith=0, eig=1):
n = X.shape[0]
K = np.zeros((n, n))
for i, x in tqdm(enumerate(X.loc[:, 'seq']), total=n, desc='Building kernel'):
for j, y in enumerate(X.loc[:, 'seq']):
if j >= i:
K[i, j] = sw_kernel(x, y, e, d, beta) if smith else local_align_kernel(x, y, e, d, beta)
K[j, i] = K[i, j]
K1 = deepcopy(K)
if eig == 1:
vp = np.min(np.real(eigs(K1)[0]))
s = vp if vp < 0 else 0
np.fill_diagonal(K1, np.diag(K1) - s * np.ones(n))
else:
for i in tqdm(range(K1.shape[0]), desc='Empirical kernel'):
for j in range(i, n):
K1[i, j] = np.dot(K[i], K[j])
K1[j, i] = K1[i, j]
return K
# In[7]:
def mem_rec(func):
#A Method to recursion memory
memory = {}
def mem_recd(*args):
key = '-'.join('[%s]' % arg for arg in args)
if key not in memory:
memory[key] = func(*args)
return memory[key]
return mem_recd
@mem_rec
def rec_bk(lbda, k, x, y):
if k == 0:
return 1
n_x, n_y = len(x), len(y)
if n_x < k or n_y < k:
return 0
sub_x, sub_y = x[:-1], y[:-1]
return (
lbda * rec_bk(lbda, k, sub_x, y)
+ lbda * rec_bk(lbda, k, x, sub_y)
- (lbda**2) * rec_bk(lbda, k, sub_x, sub_y)
+ ((lbda**2) * rec_bk(lbda, k-1, sub_x, sub_y) if x[-1] == y[-1] else 0)
)
@mem_rec
def rec_kk(lbda, k, x, y):
if k == 0:
return 1
n_x, n_y = len(x), len(y)
if n_x < k or n_y < k:
return 0
sub_x = x[:-1]
a = x[-1]
return (
rec_kk(lbda, k, sub_x, y)
+ (lbda**2) * sum(rec_bk(lbda, k-1, sub_x, y[:j]) for j in range(n_y) if y[j] == a)
)
def compute_string_kernel(X, lbda, k):
n = X.shape[0]
K = np.zeros((n, n))
for i, x in tqdm(enumerate(X.loc[:, 'seq']), total=n, desc='Building kernel'):
for j, y in enumerate(X.loc[:, 'seq']):
if j >= i:
K[i, j] = rec_kk(lbda, k, x, y)
K[j, i] = K[i, j]
return K
# In[8]:
def center_K(K):
#Implemetation of center Kernel
n = K.shape[0]
B = np.eye(n) - np.ones((n, n))/n
return multi_dot([B, K, B])
def kernel_normalize(K):
if K[0, 0] == 1:
print('Kernel already normalized')
else:
n = K.shape[0]
diag = np.sqrt(np.diag(K))
for i in range(n):
d = diag[i]
for j in range(i+1, n):
K[i, j] /= (d * diag[j])
K[j, i] = K[i, j]
np.fill_diagonal(K, np.ones(n))
return K
# In[9]:
def kernel_gappy(x, k, g, b_s):
phi = np.zeros(len(b_s))
gap_set = sum([list(combinations(x[i:i+k], k-g)) for i in range(101 - k + 1)], [])
for i, b in enumerate(b_s):
phi[i] = (b in gap_set)
return phi
def compute_gappy_k(X, k, g):
n = X.shape[0]
K = np.zeros((n, n))
b_s = np.array([format(''.join(c)) for c in product('ACGT', repeat=k)])
for i, x in tqdm(enumerate(X.loc[:, 'seq']), total=n, desc='Building kernel'):
x = format(x)
phi_x = kernel_gappy(x, k, g, b_s)
for j, y in enumerate(X.loc[:, 'seq']):
if j >= i:
K[i, j] = np.dot(phi_x, kernel_gappy(y, k, g, b_s))
K[j, i] = K[i, j]
K = kernel_normalize(K)
return K
# In[11]:
def select_method(X, method):
m = method.split('_')
if method[:2] == 'spectrum_kernel':
k = int(m[1][1:])
K = compute_spectrum_k(X, k)
elif method[:2] == 'weighted_degree' and method[2] != 'S':
print(m)
d = int(m[1][1:])
K = com_k_seq(X, d)
elif method[:2] == 'mismatch_lernel':
k, m = int(m[1][1:]), int(m[2][1:])
K = mismatch_kernel(X, k, m)
elif method[:2] == 'local_align_kernel':
e, d, beta = [float(m[i][1:]) for i in range(1, 4)]
smith, eig = int(m[4][5:]), int(m[5][3:])
K = execute_al(X, e, d, beta, smith, eig)
elif method[:3] == 'weighted_degree_shift':
d, S = int(m[1][1:]), int(m[2][1:])
K = com_shift_k(X, d, S)
elif method[:2] == 'string_kernel':
lbda, k = float(m[1][1:]), int(m[2][1:])
K = compute_string_kernel(X, lbda, k)
elif method[:2] == 'gappy_kernel':
k, g = int(m[1][1:]), int(m[2][1:])
K = get_kernel_gappy(X, k, g)
else:
NotImplementedError('Method not implemented. Please refer to the documentation for choosing among available methods')
return K