-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotconv.py
More file actions
165 lines (122 loc) · 4.87 KB
/
protconv.py
File metadata and controls
165 lines (122 loc) · 4.87 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
import numpy as np
"""# First Read files"""
def readFasta(filename):
f=open(filename,'r')
labels=[]
seqs=[]
i=0
for line in f.readlines():
if i%2==0:
sym=line.rstrip().split("|")[1]
labels.append(float(sym))
else:
seqs.append(line.rstrip())
i=i+1
return labels,seqs
train_labels,train_seqs=readFasta("/content/gdrive/My Drive/ACP/train.txt") # change to your own path
test_labels,test_seqs=readFasta("/content/gdrive/My Drive/ACP/test.txt") # change to your own path
def readFasta(filename1,filename2):
f1=open(filename1,'r')
labels=[]
seqs=[]
i=0
for line in f1.readlines():
if i%2==0:
labels.append(1)
else:
seqs.append(line.rstrip())
i=i+1
f1.close()
f2=open(filename2,'r')
i=0
for line in f2.readlines():
if i%2==0:
labels.append(0)
else:
seqs.append(line.rstrip())
i=i+1
f2.close()
return labels,seqs
train_labels,train_seqs=readFasta("/content/gdrive/My Drive/ProInFuse/train_pos.txt","/content/gdrive/My Drive/ProInFuse/train_neg.txt") # change to your own path
test_labels,test_seqs=readFasta("/content/gdrive/My Drive/ProInFuse/ind_pos.txt","/content/gdrive/My Drive/ProInFuse/ind_neg.txt") # change to your own path
# use any of the read files
import torch
from tape import ProteinBertModel, TAPETokenizer
model = ProteinBertModel.from_pretrained('bert-base')
tokenizer = TAPETokenizer(vocab='iupac')
num_of_features = 768
import numpy as np
X=np.zeros((len(train_seqs),num_of_features))
y=np.zeros(len(train_seqs))
ind_X=np.zeros((len(test_seqs),num_of_features))
ind_y=np.zeros(len(test_seqs))
# now lets populate X
i=0
for s in train_seqs:
#f=extractFeatures(s)
token_ids = torch.tensor([tokenizer.encode(s)])
output = model(token_ids)
sequence_output = output[0]
pooled_output = output[1]
X[i]=np.array(np.mean(sequence_output.detach().numpy(),axis=1) )
i=i+1
y=np.array(train_labels)
i=0
for s in test_seqs:
#f=extractFeatures(s)
token_ids = torch.tensor([tokenizer.encode(s)])
output = model(token_ids)
sequence_output = output[0]
pooled_output = output[1]
ind_X[i]=np.array(np.mean(sequence_output.detach().numpy(),axis=1) )
i=i+1
ind_y=np.array(test_labels)
print(X.shape)
print(ind_X.shape)
X_padded = np.concatenate((X, np.zeros((X.shape[0],16))), axis=1)
ind_X_padded = np.concatenate((ind_X, np.zeros((ind_X.shape[0],16))), axis=1)
from sklearn import preprocessing
std_scale = preprocessing.StandardScaler().fit(X_padded)
X_normalized = std_scale.transform(X_padded)
ind_X_normalized = std_scale.transform(ind_X_padded)
shape_unit=28
X_images=np.reshape(X_normalized,(X_normalized.shape[0],shape_unit,shape_unit))
ind_X_images = np.reshape(ind_X_normalized,(ind_X_normalized.shape[0],shape_unit,shape_unit))
print(X_images.shape)
print(ind_X_images.shape)
# %tensorflow_version 1.x
#import all libraries
#here comes the deep learning part
from keras.models import Sequential
from keras.layers import Conv1D,Conv2D, Dense, Flatten, Dropout,MaxPooling2D
from keras import layers
from keras.layers.pooling import MaxPooling1D,MaxPooling2D
from keras.optimizers import SGD, RMSprop
from keras import regularizers
model = Sequential()
model.add(Conv2D(filters=8, kernel_size=(3, 3), padding="same",activation='relu', input_shape=(28,28,1)))
model.add(layers.AveragePooling2D())
model.add(layers.Conv2D(filters=16, kernel_size=(3, 3), padding="same",activation='relu'))
model.add(layers.AveragePooling2D())
model.add(layers.Conv2D(filters=32, kernel_size=(3, 3),padding="same", activation='relu'))
model.add(layers.AveragePooling2D())
model.add(layers.Flatten())
model.add(layers.Dense(units=128, activation='relu',kernel_regularizer=regularizers.l2(1e-4),bias_regularizer=regularizers.l2(1e-4), activity_regularizer=regularizers.l2(1e-5)))
model.add(layers.Dense(units=64, activation='relu',kernel_regularizer=regularizers.l2(1e-4),bias_regularizer=regularizers.l2(1e-4),activity_regularizer=regularizers.l2(1e-5)))
model.add(layers.Dense(units=1, activation = 'sigmoid',kernel_regularizer=regularizers.l2(1e-4),bias_regularizer=regularizers.l2(1e-4),activity_regularizer=regularizers.l2(1e-5)))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
eX = np.expand_dims(all_images, -1)
eY = np.expand_dims(all_y, -1)
eIX = np.expand_dims(ind_X_images, -1)
eIY = np.expand_dims(ind_y, -1)
history = model.fit(x=eX,y=eY,epochs=50,shuffle=True,validation_data=(eIX, eIY),verbose=1,batch_size=64)
# Plot history: MAE
import matplotlib.pyplot as plt
plt.plot(history.history['accuracy'], label='training data')
plt.plot(history.history['val_accuracy'], label='validation data')
#plt.title('')
plt.ylabel('Accuracy')
plt.xlabel('No. epoch')
plt.legend(loc="lower right")
plt.show()