-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathconvert_dataset.py
363 lines (310 loc) · 14 KB
/
convert_dataset.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import time
import numpy as np
import argparse
from copy import deepcopy
from scipy import interpolate
parser = argparse.ArgumentParser('Preprocessing: Generate training/validation/testing features from pdb')
parser.add_argument('--MDfolder', type=str, default="data/pdb/",
help='folder of pdb MD')
parser.add_argument('--pdb-start', type=int, default="1",
help='select pdb file window from start, e.g. in tutorial it is ca_1.pdb')
parser.add_argument('--pdb-end', type=int, default="56",
help='select pdb file window to end')
parser.add_argument('--num-residues', type=int, default=77,
help='Number of residues of the MD pdb')
parser.add_argument('--feature-size', type=int, default=6,
help='The number of features used in study( position (X,Y,Z) + velocity (X,Y,Z) ).')
parser.add_argument('--train-interval', type=int, default=60,
help='intervals in trajectory in training')
parser.add_argument('--validate-interval', type=int, default=60,
help='intervals in trajectory in validate')
parser.add_argument('--test-interval', type=int, default=100,
help='intervals in trajectory in test')
args = parser.parse_args()
def read_feature_file(filename, feature_size=1, gene_size=10, timestep_size=21):
"""
Read single expriments of all time points
"""
feature = np.zeros((timestep_size, feature_size, gene_size))
time_count = -1
with open(filename) as f:
lines = f.readlines()
for line in lines:
line = line.strip()
if(time_count >= 0 and time_count < timestep_size):
words = line.split()
data_count = 0
for word in words:
feature[time_count, 0, data_count] = word
data_count += 1
time_count += 1
f.close()
# Use interpole
feature = timepoint_sim(feature, 4)
return feature
def read_feature_Residue_file(filename):
resdict = {}
count = 0
with open(filename) as f:
lines = f.readlines()
for line in lines:
line = line.strip()
words = line.split(",")
if count > 0:
feature = np.zeros((len(words)-1))
for i in range(len(words)-1):
feature[i] = words[i+1]
resdict[words[0]] = feature
count = count+1
return resdict
def read_feature_MD_file(filename, timestep_size, feature_size, num_residues, interval):
"""
Read single expriments of all time points
"""
feature = np.zeros((timestep_size, feature_size, num_residues))
flag = False
nflag = False
modelNum = 0
with open(filename) as f:
lines = f.readlines()
for line in lines:
line = line.strip()
words = line.split()
if(line.startswith("MODEL")):
modelNum = int(words[1])
if (modelNum % interval == 1):
flag = True
if (modelNum % interval == 2):
nflag = True
elif(line.startswith("ATOM") and words[2] == "CA" and flag):
numStep = int(modelNum/interval)
feature[numStep, 0, int(words[5])-1] = float(words[6])
feature[numStep, 1, int(words[5])-1] = float(words[7])
feature[numStep, 2, int(words[5])-1] = float(words[8])
elif(line.startswith("ATOM") and words[2] == "CA" and nflag):
numStep = int(modelNum/interval)
feature[numStep, 3, int(
words[5])-1] = float(words[6])-feature[numStep, 0, int(words[5])-1]
feature[numStep, 4, int(
words[5])-1] = float(words[7])-feature[numStep, 1, int(words[5])-1]
feature[numStep, 5, int(
words[5])-1] = float(words[8])-feature[numStep, 2, int(words[5])-1]
elif(line.startswith("ENDMDL") and flag):
flag = False
elif(line.startswith("ENDMDL") and nflag):
nflag = False
f.close()
return feature
def read_feature_MD_file_slidingwindow(filename, timestep_size, feature_size, num_residues, interval, window_choose, aa_start, aa_end):
# read single expriments of all time points
feature = np.zeros((timestep_size, feature_size, num_residues))
flag = False
nflag = False
modelNum = 0
with open(filename) as f:
lines = f.readlines()
for line in lines:
line = line.strip()
words = line.split()
if(line.startswith("MODEL")):
modelNum = int(words[1])
if (modelNum % interval == window_choose):
flag = True
if (modelNum % interval == (window_choose+1)):
nflag = True
elif(line.startswith("ATOM") and words[2] == "CA" and int(words[4]) >= aa_start and int(words[4]) <= aa_end and flag):
numStep = int(modelNum/interval)
feature[numStep, 0, int(words[4])-aa_start] = float(words[5])
feature[numStep, 1, int(words[4])-aa_start] = float(words[6])
feature[numStep, 2, int(words[4])-aa_start] = float(words[7])
elif(line.startswith("ATOM") and words[2] == "CA" and int(words[4]) >= aa_start and int(words[4]) <= aa_end and nflag):
numStep = int(modelNum/interval)
feature[numStep, 3, int(
words[4])-aa_start] = float(words[5])-feature[numStep, 0, int(words[4])-aa_start]
feature[numStep, 4, int(
words[4])-aa_start] = float(words[6])-feature[numStep, 1, int(words[4])-aa_start]
feature[numStep, 5, int(
words[4])-aa_start] = float(words[7])-feature[numStep, 2, int(words[4])-aa_start]
elif(line.startswith("ENDMDL") and flag):
flag = False
elif(line.startswith("ENDMDL") and nflag):
nflag = False
f.close()
# print(feature.shape)
return feature
def read_feature_MD_file_resi(filename, resDict, feature_size, num_residues, timestep_size, interval):
# read single expriments of all time points
feature = np.zeros((timestep_size, feature_size, num_residues))
flag = False
nflag = False
modelNum = 0
with open(filename) as f:
lines = f.readlines()
for line in lines:
line = line.strip()
words = line.split()
if(line.startswith("MODEL")):
modelNum = int(words[1])
if (modelNum % interval == 1):
flag = True
if (modelNum % interval == 2):
nflag = True
elif(line.startswith("ATOM") and words[2] == "CA" and flag):
numStep = int(modelNum/interval)
feature[numStep, 0, int(words[4])-1] = float(words[5])
feature[numStep, 1, int(words[4])-1] = float(words[6])
feature[numStep, 2, int(words[4])-1] = float(words[7])
featureResi = resDict[words[3]]
for i in range(6, 6+featureResi.shape[0]):
feature[numStep, i, int(words[4])-1] = featureResi[i-6]
elif(line.startswith("ATOM") and words[2] == "CA" and nflag):
numStep = int(modelNum/interval)
feature[numStep, 3, int(
words[4])-1] = float(words[5])-feature[numStep, 0, int(words[4])-1]
feature[numStep, 4, int(
words[4])-1] = float(words[6])-feature[numStep, 1, int(words[4])-1]
feature[numStep, 5, int(
words[4])-1] = float(words[7])-feature[numStep, 2, int(words[4])-1]
elif(line.startswith("ENDMDL") and flag):
flag = False
elif(line.startswith("ENDMDL") and nflag):
nflag = False
f.close()
return feature
def read_edge_file(filename, gene_size):
edges = np.zeros((gene_size, gene_size))
count = 0
with open(filename) as f:
lines = f.readlines()
for line in lines:
line = line.strip()
words = line.split()
data_count = 0
for word in words:
edges[count, data_count] = word
data_count += 1
count += 1
f.close()
return edges
def convert_dataset(feature_filename, edge_filename, experiment_size=5):
features = list()
edges = np.zeros((experiment_size, experiment_size))
for i in range(1, experiment_size+1):
features.append(read_feature_file(feature_filename+"_"+str(i)+".txt"))
count = 0
with open(edge_filename) as f:
lines = f.readlines()
for line in lines:
line = line.strip()
words = line.split()
data_count = 0
for word in words:
edges[count, data_count] = word
data_count += 1
count += 1
f.close()
features = np.stack(features, axis=0)
edges = np.tile(edges, (features.shape[0], 1)).reshape(
features.shape[0], features.shape[3], features.shape[3])
return features, edges
def convert_dataset_sim(feature_filename, edge_filename, experiment_size=5, gene_size=5, sim_size=50000):
features = list()
edges = np.zeros((gene_size, gene_size))
for i in range(1, experiment_size+1):
features.append(read_feature_file(
feature_filename+"_"+str(i)+".txt"), gene_size=5)
count = 0
with open(edge_filename) as f:
lines = f.readlines()
for line in lines:
line = line.strip()
words = line.split()
data_count = 0
for word in words:
edges[count, data_count] = word
data_count += 1
count += 1
f.close()
features = np.stack(features, axis=0)
features_out = np.zeros(
(sim_size, features.shape[1], features.shape[2], features.shape[3]))
edges_out = np.zeros((sim_size, gene_size, gene_size))
for i in range(sim_size):
index = np.random.permutation(np.arange(experiment_size))
num = np.random.permutation(np.arange(experiment_size))[0]
features_out[i, :, :, :] = features[num, :, :, :][:, :, index]
edges_out[i, :, :] = edges[index, :][:, index]
# Add noise
features_out = features_out + \
np.random.randn(
sim_size, features.shape[1], features.shape[2], features.shape[3])
return features_out, edges_out
def convert_dataset_md(feature_filename, startIndex, experiment_size, timestep_size, feature_size, num_residues, interval):
features = list()
edges = list()
for i in range(startIndex, experiment_size+1):
print("Start: "+str(i)+"th PDB")
features.append(read_feature_MD_file(feature_filename+"smd"+str(i) +
".pdb", timestep_size, feature_size, num_residues, interval))
edges.append(np.zeros((num_residues, num_residues)))
features = np.stack(features, axis=0)
edges = np.stack(edges, axis=0)
return features, edges
def convert_dataset_md_single(MDfolder, startIndex, experiment_size, timestep_size, feature_size, num_residues, interval, pdb_start, pdb_end, aa_start, aa_end):
"""
Convert in single md file in single skeleton
"""
features = list()
edges = list()
for i in range(startIndex, experiment_size+1):
print("Start: "+str(i)+"th PDB")
for j in range(pdb_start, pdb_end+1):
# print(str(i)+" "+str(j))
features.append(read_feature_MD_file_slidingwindow(MDfolder+"ca_"+str(
i)+".pdb", timestep_size, feature_size, num_residues, interval, j, aa_start, aa_end))
edges.append(np.zeros((num_residues, num_residues)))
print("***")
print(len(features))
print("###")
features = np.stack(features, axis=0)
edges = np.stack(edges, axis=0)
return features, edges
def timepoint_sim(feature, fold):
# hard code now,fold=4
# feature_shape: [timestep, feature_size, gene]
step = 1/fold
timestep = feature.shape[0]
genes = feature.shape[2]
x = np.arange(timestep)
xnew = np.arange(0, (timestep-1)+step, step)
feature_out = np.zeros((xnew.shape[0], 1, genes))
for gene in range(genes):
y = feature[:, 0, gene]
tck = interpolate.splrep(x, y, s=0)
ynew = interpolate.splev(xnew, tck, der=0)
feature_out[:, 0, gene] = ynew
return feature_out
MDfolder = args.MDfolder
feature_size = args.feature_size
num_residues = args.num_residues
pdb_start = args.pdb_start
pdb_end = args.pdb_end
train_interval = args.train_interval
validate_interval = args.validate_interval
test_interval = args.test_interval
# Generate training/validating/testing
print("Generate Train")
features, edges = convert_dataset_md_single(MDfolder, startIndex=1, experiment_size=1, timestep_size=50,
feature_size=feature_size, num_residues=num_residues, interval=train_interval, pdb_start=pdb_start, pdb_end=pdb_end, aa_start=1, aa_end=num_residues)
np.save('data/features.npy', features)
np.save('data/edges.npy', edges)
print("Generate Valid")
features_valid, edges_valid = convert_dataset_md_single(MDfolder, startIndex=1, experiment_size=1, timestep_size=50,
feature_size=feature_size, num_residues=num_residues, interval=validate_interval, pdb_start=pdb_start, pdb_end=pdb_end, aa_start=1, aa_end=num_residues)
np.save('data/features_valid.npy', features_valid)
np.save('data/edges_valid.npy', edges_valid)
print("Generate Test")
features_test, edges_test = convert_dataset_md_single(MDfolder, startIndex=1, experiment_size=1, timestep_size=50,
feature_size=feature_size, num_residues=num_residues, interval=test_interval, pdb_start=pdb_start, pdb_end=pdb_end, aa_start=1, aa_end=num_residues)
np.save('data/features_test.npy', features_test)
np.save('data/edges_test.npy', edges_test)