-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript2.py
More file actions
266 lines (248 loc) · 8.16 KB
/
script2.py
File metadata and controls
266 lines (248 loc) · 8.16 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
from __future__ import division
from sklearn import svm
import numpy as np
import pandas as pd
import scipy as scip
from scipy import linalg
from scipy.spatial.distance import pdist, squareform
import torch
from torch.autograd import Variable
from utility import *
from sklearn import metrics
# Definitions of functions
def accuracy(error):
num = 0
for item in error:
if item == 0:
num = num + 1
return (num/len(error))*100
# Hot Vector Representations for Amino Acids
table_hot = {'A': [1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],
'R': [0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],
'N': [0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0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0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0],
'S': [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0],
'T': [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0],
'W': [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0],
'Y': [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0],
'V': [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0]}
# Float representations for Amino Acids
table_float = {'A':1.0,
'R':2.0,
'N':3.0,
'D':4.0,
'C':5.0,
'E':6.0,
'Q':7.0,
'G':8.0,
'H':9.0,
'I':10.0,
'L':11.0,
'K':12.0,
'M':13.0,
'F':14.0,
'P':15.0,
'S':16.0,
'T':17.0,
'W':18.0,
'Y':19.0,
'V':20.0}
# Positive and Negative Datasets
pos_data = []
neg_data = []
# Read in the Positive Dataset
with open('pos.data') as f:
for line in f:
l = []
line = line.rstrip()
for AA in line:
l.append(table_hot[AA])
l.append(1.0)
pos_data.append(l)
# Read in the Negative Dataset
with open('neg.data') as f:
for line in f:
l = []
line = line.rstrip()
for AA in line:
l.append(table_hot[AA])
l.append(0.0)
neg_data.append(l)
# Data preparation
data = pos_data + neg_data
data = np.array(data)
np.random.shuffle(data)
data, test = np.array_split(data,2)
print len(data), len(test)
data = pd.DataFrame(data)
'''
print data.sample(frac=0.008,replace = True)
y_train = data[data.columns[20]]
del data[data.columns[20]]
x_train = data
#x_train = x_train.as_matrix()
y_train = np.array(y_train.values, dtype=np.float64)
'''
#print data
#test = pd.DataFrame(test)
#y_test = test[test.columns[20]]
#del test[test.columns[20]]
#x_test = test
#x_test = x_test.as_matrix()
#y_test = np.array(y_test.values, dtype=np.float64)
#
# Vanilla Deep Learning Model
'''
model = torch.nn.Sequential(
torch.nn.Conv1d(1,20,3),
torch.nn.LeakyReLU(0.1),
torch.nn.Dropout(),
torch.nn.MaxPool1d(2),
torch.nn.Conv1d(20,1,2),
torch.nn.LeakyReLU(0.1),
torch.nn.Dropout(),
torch.nn.MaxPool1d(2),
)
'''
'''
# 1D convolution
class Discrim(torch.nn.Module):
def __init__(self):
super(Discrim, self).__init__()
self.c1 = torch.nn.Conv1d(1,20,3)
self.relu = torch.nn.LeakyReLU(0.1)
self.drop = torch.nn.Dropout()
self.p1 = torch.nn.MaxPool1d(2)
self.c2 = torch.nn.Conv1d(20,1,2)
#torch.nn.LeakyReLU(0.1)
#torch.nn.Dropout()
self.p2 = torch.nn.MaxPool1d(2)
self.linear = torch.nn.Linear(4,4)
self.linear2 = torch.nn.Linear(4,1)
self.tanh = torch.nn.Tanh()
self.sig = torch.nn.Sigmoid()
def forward(self, input):
x = self.c1(input)
x = self.relu(x)
x = self.drop(x)
x = self.p1(x)
x = self.c2(x)
x = self.relu(x)
x = self.drop(x)
print x
print x.squeeze()
x = self.p2(x)
x = self.linear(x)
x = self.drop(x)
x = x.view(1,4)
x = self.linear2(x)
return self.sig(x)
'''
class Discrim(torch.nn.Module):
def __init__(self):
super(Discrim, self).__init__()
self.c1 = torch.nn.Conv2d(1,10,3)
self.relu =torch. nn.LeakyReLU(0.2)
self.drop = torch.nn.Dropout()
self.p1 = torch.nn.MaxPool2d(2)
self.c2 = torch.nn.Conv2d(10,10,2)
self.relu2 = torch.nn.LeakyReLU(0.1)
self.p2 = torch.nn.MaxPool2d(2)
self.c3 = torch.nn.Conv2d(10,1,1)
self.p3 = torch.nn.MaxPool2d(2)
self.linear = torch.nn.Linear(4,1)
self.tanh = torch.nn.Tanh()
self.sigmoid = torch.nn.Sigmoid()
def forward(self, input):
x = self.c1(input)
x = self.relu(x)
x = self.drop(x)
x = self.p1(x)
x = self.c2(x)
x = self.relu2(x)
x = self.drop(x)
x = self.p2(x)
x = self.c3(x)
x = self.p3(x)
x = self.linear(x.view(1,4))
return self.sigmoid(x)
loss_fn = torch.nn.MSELoss(size_average=True)
learning_rate = 1e-4
model = Discrim()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
for iteration in range(5000):
mini_batch = data.sample(n = 1,replace = True)
y_train = mini_batch[mini_batch.columns[20]]
del mini_batch[mini_batch.columns[20]]
x_train = mini_batch
x_train = x_train.values.tolist()[0]
x_train = np.array(x_train)
y_train = np.array(y_train.values, dtype=np.float64)
inpt_train_x = torch.from_numpy(x_train)
inpt_train_x = inpt_train_x.float()
inpt_train_y = torch.from_numpy(y_train)
inpt_train_y = inpt_train_y.float()
inpt_train_x = Variable(inpt_train_x)
inpt_train_y = Variable(inpt_train_y, requires_grad=False)
y_pred = model(inpt_train_x.view(1,1,20,20))
loss = loss_fn(y_pred, inpt_train_y)
if iteration%100 == 0:
print loss[0].data.numpy()
optimizer.zero_grad()
loss.backward()
optimizer.step()
'''
print x_test[0]
inpt_test_x = torch.from_numpy(x_test)
inpt_test_x = inpt_test_x.float()
inpt_test_x = Variable(inpt_test_x)
'''
total = 0
correct = 0
prediction = -1
for item in test:
features ,label = item[:20],item[20]
features = features.tolist()
#print features
features = np.array(features)
label = np.array(label, dtype=np.float64)
features = torch.from_numpy(features)
features = features.float()
features = Variable(features)
predict = model.forward(features.view(1,1,20,20)).data.numpy()
if predict[0][0] < 0.5:
prediction = 0.0
else:
prediction = 1.0
if prediction == label:
correct = correct + 1
total = total + 1
print correct, correct/total
'''
array = model.forward(inpt_test_x).data.numpy()
new_array = []
for index in array:
if index[0] >= 0.5:
new_array.append(1.0)
if index[0] < 0.0:
new_array.append(-1.0)
new_array = np.array(new_array)
error = y_test - new_array
a,b = calculate_roc(new_array.tolist(),y_test.tolist())
print a,b
#print metrics.auc([b],[a])
deep_acc = accuracy(error)
print 'Vanilla Deep Learning Model Accuracy: ' ,deep_acc, '%'
'''
print "done"