This repository has been archived by the owner on Jul 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_rbfsvm.py
166 lines (139 loc) · 5.39 KB
/
model_rbfsvm.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
import os
import numpy as np
import pandas
from sklearn import svm
import pickle
from pandas.core.frame import DataFrame
from sklearn.ensemble import RandomForestClassifier
path = os.getcwd()
###### Parsing train dataset######
# Read data from 3 line fasta file and store them in a data frame
def rawtoframe(filename):
seqID1, seq1, seqTopo1= [], [], []
with open(filename) as f:
data = f.read().splitlines()
for i in range(len(data)):
if i%3 == 1:
seq1.append(data[i])
if i%3 == 2:
seqTopo1.append(data[i])
if i%3 == 0:
seqID1.append(data[i])
seqData1 = {
"seqID":seqID1,
"seq":seq1,
"seqTopo":seqTopo1
}
seqData = DataFrame(seqData1)
# Convert every sequence and sequence topology from list to arrays
for i in range(len(seqData.seq)):
a = list(seqData.seq[i])
seqData.seq[i]=a
for i in range(len(seqData.seqTopo)):
a = list(seqData.seqTopo[i])
seqData.seqTopo[i]=a
return seqData
# Vectorising data.
def seq_converter(seq):
aa_dic = { 'A':[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],
'R':[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],
'N':[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],
'D':[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],
'C':[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],
'Q':[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],
'E':[0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,],
'G':[0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,],
'H':[0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,],
'I':[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,],
'L':[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,],
'K':[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,],
'M':[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,],
'F':[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,],
'P':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,],
'S':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,],
'T':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,],
'Y':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,],
'W':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,],
'V':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,]}
for i in range(len(seq)):
for j in range(len(seq[i])):
if seq[i][j] in aa_dic:
seq[i][j] = aa_dic.get(seq[i][j])
else:
seq[i][j] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,]
return (seq)
def topo_converter(seqTopo):
for i in range(len(seqTopo)):
for j in range(len(seqTopo[i])):
if seqTopo[i][j]=='H':
seqTopo[i][j] = 0
if seqTopo[i][j]=='E':
seqTopo[i][j] = 1
if seqTopo[i][j]=='C':
seqTopo[i][j] = 2
return seqTopo
# Read raw data into a dataframe and convert them into vectors.
def binary_rawdata(filename):
data = rawtoframe(filename)
# Converting residues from letters into numbers
seq_converter(data.seq)
topo_converter(data.seqTopo)
return data
########### adding winodws #############
# Add slide window to evaluate the environment's impact on topology
def data_window(windowsize,data):
# Adding head and tails in protein sequence data.
for i in range(len(data)):
seqFirst=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,]
seqLast=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,]
halfwin = int((windowsize-1)/2)
for j in range(halfwin):
data.seq[i].append(seqLast)
data.seq[i].insert(0,seqFirst)
# Creating a slide window.The basic element in one window is #windowsize*AA
for m in range(len(data)):
seq_single = []
for p in range(len(data.seqTopo[m])):
temp = []
for n in range(windowsize):
temp.extend(data.seq[m][p+n])
seq_single.append(temp)
data.seq[m]=seq_single
return data
# Transfering data into a binary array to be used in svm
def data_svm(data):
sequence = []
structure = []
data.seq = np.array(data.seq)
data.seqTopo = np.array(data.seqTopo)
for i in range(len(data)):
for j in range(len(data.seq[i])):
sequence.append(data.seq[i][j])
for k in range(len(data.seqTopo[i])):
structure.append(data.seqTopo[i][k])
dataSVM = DataFrame({
'seq':sequence,
'seqTopo':structure
})
return dataSVM
### Prediction ####
if __name__ == "__main__":
print("Parsing data...")
dataBinary = binary_rawdata("data/trainset.dat")
print("Adding window...")
dataWind = data_window(15,dataBinary)
print("SVM prediction preparing...")
dataSVM = data_svm(dataWind)
dataSeq = pandas.Series.tolist(dataSVM.seq)
dataStruc = pandas.Series.tolist(dataSVM.seqTopo)
print("Model building...")
clf = svm.SVC(kernel='rbf', C=1, random_state=0)
clf.fit(dataSeq,dataStruc)
print("Saving models...")
filepath = os.path.join('models', 'rbfsvm15.pkl')
if not os.path.exists('models'):
os.makedirs('models')
with open(filepath,'wb') as f:
pickle.dump(clf, f)
print("Model Built!")
pass