-
Notifications
You must be signed in to change notification settings - Fork 0
/
nn2.py
242 lines (203 loc) · 7.56 KB
/
nn2.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
"""
NN with electrostatic potential
"""
from collections import defaultdict
from sklearn import svm
import numpy as np
from math import sqrt
import tensorflow as tf
"""
Set up a dictionary for converting the pdb 3-letters residue name to fasta 1-letter residue name
"""
pdb2fasta = {'CYS': 'C', 'ASP': 'D', 'SER': 'S', 'GLN': 'Q', 'LYS': 'K',
'ILE': 'I', 'PRO': 'P', 'THR': 'T', 'PHE': 'F', 'ASN': 'N',
'GLY': 'G', 'HIS': 'H', 'LEU': 'L', 'ARG': 'R', 'TRP': 'W',
'ALA': 'A', 'VAL':'V', 'GLU': 'E', 'TYR': 'Y', 'MET': 'M'}
# Get the electrostatic potential according to the residue name
values = pdb2fasta.values()
fasta2elec = dict.fromkeys(values, 0.5)
fasta2elec[pdb2fasta['ARG']] = 1.0
fasta2elec[pdb2fasta['LYS']] = 1.0
fasta2elec[pdb2fasta['HIS']] = 1.0
fasta2elec[pdb2fasta['ASP']] = 0.0
fasta2elec[pdb2fasta['GLU']] = 0.0
fasta2elec['<s>'] = 0.5
fasta2elec['</s>'] = 0.5
def PreprocessData():
"""
Read the dataset from dataset directory
input.txt: It is the multiple lines formed by residue names and each line represent a specific protein
output.txt: It is the multiple lines formed by 1 or 0, and each line represent a specific label for the corresponding residue on the protein
"""
pathname = "dataset/"
# read the input.txt file
with open(pathname + "input.txt", 'r') as f:
lines = f.readlines()
input_set = []
for line in lines:
if line.endswith("\n"):
line = line[:-1]
string = line.split(" ")
startLine = ["<s>"] * 4
endLine = ["</s>"] * 4
string = startLine + string + endLine
input_set.append(string)
# read the output.txt file
with open(pathname + "output.txt", 'r') as f:
lines = f.readlines()
output_set = []
for line in lines:
if line.endswith("\n"):
line = line[:-1]
string = line.split(" ")
startLine = ['-1'] * 4
endLine = ['-1'] * 4
string = startLine + string + endLine
output_set.append(string)
# Combine the data in input file and the output file together
combine_set = []
for i in range(len(input_set)):
inputList = input_set[i]
outputList = output_set[i]
tupleSet = []
for j in range(len(inputList)):
item = (inputList[j], outputList[j])
tupleSet.append(item)
combine_set.append(tupleSet)
return combine_set
def BuildDataset(combine_set, index):
"""
Build the dataset such that each line of the dataset represent an instance
Build the inputset which is very similar to dataset except that it doesn't have the output
"""
tmp_set = combine_set[:index] + combine_set[(index + 1):]
test_set = combine_set[index]
training_set = [item for sublist in tmp_set for item in sublist]
dataset = []
inputset = []
outputset = []
# loop the residue sequence in training set
for i in range(len(training_set)):
# each instance represent a specific sample including all the input features for feeding model
item = training_set[i]
if item[1] == '-1':
continue
instance = []
tag = item[1]
outputset.append(tag)
for j in range(9):
index = i - 4 + j
res_name = training_set[index][0]
instance.append(res_name)
inputset.append(list(instance))
instance.append(tag)
dataset.append(list(instance))
return (dataset, inputset, outputset, test_set)
def BuildPSSM(input_set):
"""
Build the PSSM matrix for generating the input feature
"""
# Initialize the pssm
pssm = [{} for i in range(9)]
total_res_dict = {}
residue_list = pdb2fasta.values() + ['<s>', '</s>']
for residue in residue_list:
for i in range(len(pssm)):
total_res_dict[residue] = 0.0
pssm[i][residue] = 0.0
# Count the residue
for instance in input_set:
for i in range(len(instance)):
residue = instance[i]
pssm[i][residue] += 1.0
total_res_dict[residue] += 1.0
# Calculate the log value
total_res_num = sum(total_res_dict.values())
for residue in residue_list:
total_res_dict[residue] = total_res_dict[residue] / float(total_res_num)
for res_dict in pssm:
for residue in residue_list:
frequency = float(res_dict[residue] + total_res_dict[residue]) / float(len(input_set) + 1)
res_dict[residue] = np.log(frequency / total_res_dict[residue])
return pssm
def preprocess_data(pssm, input_set, output_set, test_set):
"""
Preprocess the dataset such that use the value in pssm to replace the residue and form input features
"""
X_train = []
Y_train = []
X_test = []
Y_test = []
# generate the training set with electrostatic potential
for i in range(len(input_set)):
instance = input_set[i]
if output_set[i] == '0':
tag = [1., 0.]
else:
tag = [0., 1.]
Y_train.append(tag)
input_feature = []
for i in range(len(instance)):
residue = instance[i]
input_feature.append(pssm[i][residue])
input_feature.append(fasta2elec[residue])
X_train.append(input_feature)
# generate the test set with electrostatic potential
for i in range(len(test_set)):
item = test_set[i]
if item[1] == '-1':
continue
input_feature = []
if output_set[i] == '0':
tag = [1., 0.]
else:
tag = [0., 1.]
Y_test.append(tag)
for j in range(9):
index = i - 4 + j
res_name = test_set[index][0]
input_feature.append(pssm[j][res_name])
input_feature.append(fasta2elec[res_name])
X_test.append(input_feature)
return (X_train, Y_train, X_test, Y_test)
def Cross_validation():
"""
leave one out cross validation for the dataset
"""
test_accuracy = []
combine_set = PreprocessData()
f = open("ann2.txt", 'w')
for i in range(len(combine_set)):
print i
string = "running time: " + str(i) + '\n'
f.write(string)
# prepare the training set and the test set
(dataset, input_set, output_set, test_set) = BuildDataset(combine_set, i)
pssm = BuildPSSM(input_set)
(X_train, Y_train, X_test, Y_test) = preprocess_data(pssm, input_set, output_set, test_set)
# training the model with neural network in tensorflow
x = tf.placeholder("float", [None, len(X_train[0])])
W = tf.Variable(tf.zeros([len(X_train[0]), 2]))
b = tf.Variable(tf.zeros([2]))
y = tf.nn.softmax(tf.matmul(x,W) + b)
y_ = tf.placeholder("float", [None,2])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(1000):
sess.run(train_step, feed_dict={x: X_train, y_: Y_train})
# predict and calculate the accuracy
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
test_acc = sess.run(accuracy, feed_dict={x: X_test, y_: Y_test})
sess.close()
print "test accuracy: ", test_acc
string = "test accuracy is " + str(test_acc) + '\n'
f.write(string)
test_accuracy.append(test_acc)
print np.mean(test_accuracy)
f.close()
return test_accuracy
test_accuracy = Cross_validation()