-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicProgramming.py
More file actions
302 lines (232 loc) · 8.7 KB
/
DynamicProgramming.py
File metadata and controls
302 lines (232 loc) · 8.7 KB
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
__author__ = 'yuxinsun'
import networkx as nx
import numpy as np
from RootKernel import create_word_list
import operator
# Dynamic programming for FSM with DAG
# Input:
# t: u*y, where u is the misclassification cost, y is the desired labels
# dag: DAG
# l: length of transitions
# Output:
# opt: stop criterion in LPBoost
# kern: a column of features for the selected substring
# sub: selected substring
# att: 'Positive' or 'Negative'
def DPNormPN(t, dag, l):
M = len(t)
t = np.asarray(t)
nml = np.sqrt(l)
for node in dag.nodes():
dag.node[node]['opt_pos'] = np.zeros(l+1, float) # l+1 vector of max values can be achieved at node "node"/ pos
dag.node[node]['pos_pos'] = ['N/A']*(l+1) # l+1 vector of pointers to previous node, which can give the max/ pos
dag.node[node]['opt_neg'] = np.zeros(l+1, float) # l+1 vector of max values can be achieved at node "node"/ neg
dag.node[node]['pos_neg'] = ['N/A']*(l+1) # l+1 vector of pointers to previous node, which can give the max/ neg
dag.node[node]['att'] = [] # a scalar that indicates whether positive or negative kernels can give the max
dag.node[node]['opt'] = 0 # a scalar that stores the max between positive and negative values
counter = 1
while counter <= l:
# Loop over all nodes
for nd in dag.nodes():
# Loop over all edges that points to node nd
# for ed in dag.in_edges(nd): # inefficient, use DiGraph.predecessors() instead
for nd_pre in dag.predecessors(nd):
# nd_pre = ed[0] # previous node under current iteration
temp = np.dot(dag[nd_pre][nd]['kern'], t)
# Positive kernels
key_pos = temp/nml + dag.node[nd_pre]['opt_pos'][counter-1]
if key_pos > dag.node[nd]['opt_pos'][counter]:
dag.node[nd]['opt_pos'][counter] = key_pos
dag.node[nd]['pos_pos'][counter] = nd_pre
# Negative kernels
key_neg = -temp/nml + dag.node[nd_pre]['opt_neg'][counter-1]
if key_neg > dag.node[nd]['opt_neg'][counter]:
dag.node[nd]['opt_neg'][counter] = key_neg
dag.node[nd]['pos_neg'][counter] = nd_pre
if counter == l:
if dag.node[nd]['opt_pos'][counter] >= dag.node[nd]['opt_neg'][counter]:
dag.node[nd]['att'] = 'Positive'
dag.node[nd]['opt'] = dag.node[nd]['opt_pos'][counter]
else:
dag.node[nd]['att'] = 'Negative'
dag.node[nd]['opt'] = dag.node[nd]['opt_neg'][counter]
counter += 1
# Track substring
# Positive
opt = nx.get_node_attributes(dag, 'opt') # get 'opt' attributes from all nodes
opt_key = opt.keys()
opt_val = opt.values()
opt_val = np.asarray(opt_val)
ind = np.argmax(opt_val) # get index for the node that gives the maximum at l transitions
nd = opt_key[ind] # get the state at the end of best path
opt = np.max(opt_val)
# Track best path and compute corresponding
sub = []
sub.append(nd)
kern = np.zeros([M, 1])
att = dag.node[nd]['att']
for i in range(l, 0, -1):
if att == 'Positive':
nd_pre = dag.node[nd]['pos_pos'][i]
if nd_pre == 'N/A':
break
kern += dag[nd_pre][nd]['kern']
sub.append(nd_pre)
nd = nd_pre
else:
nd_pre = dag.node[nd]['pos_neg'][i]
if nd_pre == 'N/A':
break
kern -= dag[nd_pre][nd]['kern']
sub.append(nd_pre)
nd = nd_pre
kern /= nml
return opt, kern, sub, att
# Feature selection
# Input:
# dag: DAG where features are to be selected
# sub: a list of nodes/ states
# att: 'Positive' or 'Negative'
# Output:
# kern: a column of features for the selected substring
def featNorm(dag, subs, att):
kern = []
for j in range(0, len(subs)):
sub = subs[j]
sub.reverse()
key = 0
for i in range(0, len(sub)-1):
key += np.asarray(dag[sub[i]][sub[i+1]]['kern'])
if att[j] == 'Negative':
key = -key
key /= np.sqrt(len(sub)-1)
kern.append(key)
kern = np.asarray(kern)
kern = np.transpose(kern)
return kern
# Sometimes the returned substrings do not need to be reversed
def featNormNew(dag, subs, att):
kern = []
for j in range(0, len(subs)):
sub = subs[j]
key = 0
for i in range(0, len(sub)-1):
key += np.asarray(dag[sub[i]][sub[i+1]]['kern'])
if att[j] == 'Negative':
key = -key
key /= np.sqrt(len(sub)-1)
kern.append(key)
kern = np.asarray(kern)
kern = np.transpose(kern)
return kern
##########################################################
# #
# Dynamic Programming for String Kernels #
# #
##########################################################
# Dynamic programming for spectrum kernels
# Input:
# x: input data
# t: u*y, where u is the misclassification cost, y is the desired labels
# L: length of substrings (not transitions/paths)
# alphabet: amino acid alphabet
# Output:
# val: stop criterion for LPBoost
# kern: a column of features for selected substrings
# sub: selected substring
# att: 'Positive' or 'Negative'
def DPString(x, t, L, alphabet):
word = create_word_list(alphabet,3)
M = len(t)
N = len(alphabet)
dic = dict(zip(word, x.transpose()))
DP_pos = np.zeros([N, N])
DP_neg = np.zeros([N, N])
SUB_pos = []
SUB_neg = []
nml = np.sqrt(L-3+1)
for l in range(0, L-3+1):
DPre_pos = np.copy(DP_pos)
DPre_neg = np.copy(DP_neg)
SUB_pos.append({})
SUB_neg.append({})
for l2 in range(0, len(alphabet)):
ltr2 = alphabet[l2]
for l1 in range(0, len(alphabet)):
ltr1 = alphabet[l1]
temp_pos = {}
temp_neg = {}
for l3 in range(0, len(alphabet)):
ltr3 = alphabet[l3]
temp_pos[ltr3] = DPre_pos[l3, l2] + np.dot(np.transpose(t), dic[ltr1+ltr2+ltr3])/nml
temp_neg[ltr3] = DPre_neg[l3, l2] - np.dot(np.transpose(t), dic[ltr1+ltr2+ltr3])/nml
temp = dict(map(lambda item: (item[1],item[0]),temp_pos.items()))
pos_key = temp[max(temp.keys())]
pos_val = temp_pos[pos_key]
temp = dict(map(lambda item: (item[1],item[0]),temp_neg.items()))
neg_key = temp[max(temp.keys())]
neg_val = temp_neg[neg_key]
DP_pos[l2, l1] = pos_val
DP_neg[l2, l1] = neg_val
SUB_pos[l][ltr2+ltr1] = pos_key
SUB_neg[l][ltr2+ltr1] = neg_key
# Track subscripts
pos_val = np.max(DP_pos)
neg_val = np.max(DP_neg)
if pos_val >= neg_val:
val = pos_val
att = 'Positive'
temp = np.unravel_index(DP_pos.argmax(), DP_pos.shape)
l2 = temp[0]
l1 = temp[1]
ltr2 = alphabet[l2]
ltr1 = alphabet[l1]
sub = ltr1+ltr2
kern = np.zeros([M, 1])
for i in range(L-3,-1,-1):
temp = SUB_pos[i][ltr2+ltr1]
sub += temp
ltr2 = sub[-1]
ltr1 = sub[-2]
kern += np.transpose(np.matrix(dic[sub[-3:]]))
kern = kern/nml
else:
val = neg_val
att = 'Negative'
temp = np.unravel_index(DP_neg.argmax(), DP_neg.shape)
l2 = temp[0]
l1 = temp[1]
ltr2 = alphabet[l2]
ltr1 = alphabet[l1]
sub = ltr1+ltr2
kern = np.zeros([M, 1])
for i in range(L-3,-1,-1):
temp = SUB_neg[i][ltr2+ltr1]
sub += temp
ltr2 = sub[-1]
ltr1 = sub[-2]
kern -= np.transpose(np.matrix(dic[sub[-3:]]))
kern = kern/nml
return val, kern, sub, att
# Return features according to selected substrings
# Input:
# x: input data
# subs: selected substrings
# atts: 'Positive' or 'Negative'
# word: word list of all possible combinations of p amino acid, p being the spectra
# Output:
# kern: columns of features for selected substrings
def featString(x, subs, atts, word):
dic = dict(zip(word, x.transpose()))
kern = []
for k in range(0, len(subs)):
sub = subs[k]
att = atts[k]
temp = 0
for i in range(0, len(sub)-3+1):
temp += dic[sub[i:i+3]]
temp = temp/np.sqrt(len(sub)-3+1)
if att == 'Negative':
temp = -temp
kern.append(temp)
return kern