-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataio.py
286 lines (261 loc) · 10 KB
/
dataio.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
'''
File: Contains subroutines to load input features and output maps
'''
import pickle
import random
import os
import numpy as np
def load_list(file_lst, max_items = 1000000):
if max_items < 0:
max_items = 1000000
protein_list = []
f = open(file_lst, 'r')
for l in f.readlines():
protein_list.append(l.strip().split()[0])
if (max_items < len(protein_list)):
protein_list = protein_list[:max_items]
return protein_list
def summarize_channels(x, y):
print(' Channel Avg Max Sum')
for i in range(len(x[0, 0, :])):
(m, s, a) = (x[:, :, i].flatten().max(), x[:, :, i].flatten().sum(), x[:, :, i].flatten().mean())
print(' %7s %10.4f %10.4f %10.1f' % (i+1, a, m, s))
print(" Ymin = %.2f Ymean = %.2f Ymax = %.2f" % (y.min(), y.mean(), y.max()) )
def get_bulk_output_contact_maps(pdb_id_list, all_dist_paths, OUTL):
YY = np.full((len(pdb_id_list), OUTL, OUTL, 1), 100.0)
for i, pdb in enumerate(pdb_id_list):
Y = get_map(pdb, all_dist_paths)
ly = len(Y[:, 0])
assert ly <= OUTL
YY[i, :ly, :ly, 0] = Y
if np.any(np.isnan(Y)):
print('')
print('WARNING:')
print('Some pdbs in the following list have NaNs in their distances:', pdb_id_list)
np.seterr(invalid='ignore')
YY[ YY < 8.0 ] = 1.0
YY[ YY >= 8.0 ] = 0.0
return YY.astype(np.float32)
def get_bulk_output_dist_maps(pdb_id_list, all_dist_paths, OUTL):
YY = np.full((len(pdb_id_list), OUTL, OUTL, 1), np.inf)
for i, pdb in enumerate(pdb_id_list):
Y = get_map(pdb, all_dist_paths)
ly = len(Y[:, 0])
assert ly <= OUTL
YY[i, :ly, :ly, 0] = Y
return YY.astype(np.float32)
def get_input_output_dist(pdb_id_list, all_feat_paths, all_dist_paths, pad_size, OUTL, expected_n_channels):
XX = np.full((len(pdb_id_list), OUTL, OUTL, expected_n_channels), 0.0)
YY = np.full((len(pdb_id_list), OUTL, OUTL, 1), 100.0)
for i, pdb in enumerate(pdb_id_list):
X = get_feature(pdb, all_feat_paths, expected_n_channels)
assert len(X[0, 0, :]) == expected_n_channels
Y0 = get_map(pdb, all_dist_paths, len(X[:, 0, 0]))
assert len(X[:, 0, 0]) >= len(Y0[:, 0])
if len(X[:, 0, 0]) != len(Y0[:, 0]):
print('')
print('WARNING!! Different len(X) and len(Y) for ', pdb, len(X[:, 0, 0]), len(Y0[:, 0]))
l = len(X[:, 0, 0])
Y = np.full((l, l), np.nan)
Y[:len(Y0[:, 0]), :len(Y0[:, 0])] = Y0
Xpadded = np.zeros((l + pad_size, l + pad_size, len(X[0, 0, :])), dtype=np.float32)
Xpadded[int(pad_size/2) : l+int(pad_size/2), int(pad_size/2) : l+int(pad_size/2), :] = X
Ypadded = np.full((l + pad_size, l + pad_size), 100.0, dtype=np.float32)
Ypadded[int(pad_size/2) : l+int(pad_size/2), int(pad_size/2) : l+int(pad_size/2)] = Y
l = len(Xpadded[:, 0, 0])
if l <= OUTL:
XX[i, :l, :l, :] = Xpadded
YY[i, :l, :l, 0] = Ypadded
else:
rx = random.randint(0, l - OUTL)
ry = random.randint(0, l - OUTL)
assert rx + OUTL <= l
assert ry + OUTL <= l
XX[i, :, :, :] = Xpadded[rx:rx+OUTL, ry:ry+OUTL, :]
YY[i, :, :, 0] = Ypadded[rx:rx+OUTL, ry:ry+OUTL]
return XX.astype(np.float32), YY.astype(np.float32)
def get_input_output_bins(pdb_id_list, all_feat_paths, all_dist_paths, pad_size, OUTL, expected_n_channels, bins):
XX = np.full((len(pdb_id_list), OUTL, OUTL, expected_n_channels), 0.0)
YY = np.full((len(pdb_id_list), OUTL, OUTL, len(bins)), 0.0)
for i, pdb in enumerate(pdb_id_list):
X = get_feature(pdb, all_feat_paths, expected_n_channels)
assert len(X[0, 0, :]) == expected_n_channels
Y0 = dist_map_to_bins(get_map(pdb, all_dist_paths, len(X[:, 0, 0])), bins)
assert len(X[:, 0, 0]) >= len(Y0[:, 0])
if len(X[:, 0, 0]) != len(Y0[:, 0]):
print('')
print('WARNING!! Different len(X) and len(Y) for ', pdb, len(X[:, 0, 0]), len(Y0[:, 0]))
l = len(X[:, 0, 0])
Y = np.full((l, l, len(Y0[0, 0, :])), np.nan)
Y[:len(Y0[:, 0]), :len(Y0[:, 0]), :] = Y0
Xpadded = np.zeros((l + pad_size, l + pad_size, len(X[0, 0, :])))
Xpadded[int(pad_size/2) : l+int(pad_size/2), int(pad_size/2) : l+int(pad_size/2), :] = X
Ypadded = np.full((l + pad_size, l + pad_size, len(bins)), 0.0)
Ypadded[int(pad_size/2) : l+int(pad_size/2), int(pad_size/2) : l+int(pad_size/2), :] = Y
l = len(Xpadded[:, 0, 0])
if l <= OUTL:
XX[i, :l, :l, :] = Xpadded
YY[i, :l, :l, :] = Ypadded
else:
rx = random.randint(0, l - OUTL)
ry = random.randint(0, l - OUTL)
assert rx + OUTL <= l
assert ry + OUTL <= l
XX[i, :, :, :] = Xpadded[rx:rx+OUTL, ry:ry+OUTL, :]
YY[i, :, :, :] = Ypadded[rx:rx+OUTL, ry:ry+OUTL, :]
return XX.astype(np.float32), YY.astype(np.float32)
def get_sequence(pdb, feature_file):
features = pickle.load(open(feature_file, 'rb'))
return features['seq']
def get_feature(pdb, all_feat_paths, expected_n_channels):
features = None
for path in all_feat_paths:
if os.path.exists(path + pdb + '.pkl'):
features = pickle.load(open(path + pdb + '.pkl', 'rb'))
if features == None:
print('Expected feature file for', pdb, 'not found at', all_feat_paths)
exit(1)
l = len(features['seq'])
seq = features['seq']
# Create X and Y placeholders
X = np.full((l, l, expected_n_channels), 0.0)
# Add secondary structure
ss = features['ss']
assert ss.shape == (3, l)
fi = 0
for j in range(3):
a = np.repeat(ss[j].reshape(1, l), l, axis = 0)
X[:, :, fi] = a
fi += 1
X[:, :, fi] = a.T
fi += 1
# Add PSSM
pssm = features['pssm']
assert pssm.shape == (l, 22)
for j in range(22):
a = np.repeat(pssm[:, j].reshape(1, l), l, axis = 0)
X[:, :, fi] = a
fi += 1
X[:, :, fi] = a.T
fi += 1
# Add SA
sa = features['sa']
assert sa.shape == (l, )
a = np.repeat(sa.reshape(1, l), l, axis = 0)
X[:, :, fi] = a
fi += 1
X[:, :, fi] = a.T
fi += 1
# Add entrophy
entropy = features['entropy']
assert entropy.shape == (l, )
a = np.repeat(entropy.reshape(1, l), l, axis = 0)
X[:, :, fi] = a
fi += 1
X[:, :, fi] = a.T
fi += 1
# Add CCMpred
ccmpred = features['ccmpred']
assert ccmpred.shape == ((l, l))
X[:, :, fi] = ccmpred
fi += 1
# Add FreeContact
freecon = features['freecon']
assert freecon.shape == ((l, l))
X[:, :, fi] = freecon
fi += 1
# Add potential
potential = features['potential']
assert potential.shape == ((l, l))
X[:, :, fi] = potential
fi += 1
assert fi == expected_n_channels
assert X.max() < 100.0
assert X.min() > -100.0
return X
def get_map(pdb, all_dist_paths, expected_l = -1):
seqy = None
mypath = ''
for path in all_dist_paths:
if os.path.exists(path + pdb + '-cb.npy'):
mypath = path + pdb + '-cb.npy'
(ly, seqy, cb_map) = np.load(path + pdb + '-cb.npy', allow_pickle = True)
if seqy == None:
print('Expected distance map file for', pdb, 'not found at', all_dist_paths)
exit(1)
if 'cameo' not in mypath and expected_l > 0:
assert expected_l == ly
assert cb_map.shape == ((expected_l, expected_l))
Y = cb_map
# Only CAMEO dataset has this issue
if 'cameo' not in mypath:
assert not np.any(np.isnan(Y))
if np.any(np.isnan(Y)):
np.seterr(invalid='ignore')
print('')
print('WARNING!! Some values in the pdb structure of', pdb, 'l = ', ly, 'are missing or nan! Indices are: ', np.where(np.isnan(np.diagonal(Y))))
Y[Y < 1.0] = 1.0
Y[0, 0] = Y[0, 1]
Y[ly-1, ly-1] = Y[ly-1, ly-2]
for q in range(1, ly-1):
if np.isnan(Y[q, q]):
continue
if np.isnan(Y[q, q-1]) and np.isnan(Y[q, q+1]):
Y[q, q] = 1.0
elif np.isnan(Y[q, q-1]):
Y[q, q] = Y[q, q+1]
elif np.isnan(Y[q, q+1]):
Y[q, q] = Y[q, q-1]
else:
Y[q, q] = (Y[q, q-1] + Y[q, q+1]) / 2.0
assert np.nanmax(Y) <= 500.0
assert np.nanmin(Y) >= 1.0
return Y
def save_dist_rr(pdb, pred_matrix, feature_file, file_rr):
sequence = get_sequence(pdb, feature_file)
rr = open(file_rr, 'w')
rr.write(sequence + "\n")
P = np.copy(pred_matrix)
L = len(P[:])
for j in range(0, L):
for k in range(j, L):
P[j, k] = (P[k, j, 0] + P[j, k, 0]) / 2.0
for j in range(0, L):
for k in range(j, L):
if abs(j - k) < 5:
continue
rr.write("%i %i %0.3f %.3f 1\n" %(j+1, k+1, P[j][k], P[j][k]) )
rr.close()
print('Written RR ' + file_rr + ' !')
def save_contacts_rr(pdb, all_feat_paths, pred_matrix, file_rr):
for path in all_feat_paths:
if os.path.exists(path + pdb + '.pkl'):
features = pickle.load(open(path + pdb + '.pkl', 'rb'))
if features == None:
print('Expected feature file for', pdb, 'not found at', all_feat_paths)
exit(1)
sequence = features['seq']
rr = open(file_rr, 'w')
rr.write(sequence + "\n")
P = np.copy(pred_matrix)
L = len(P[:])
for j in range(0, L):
for k in range(j, L):
P[j, k] = (P[k, j, 0] + P[j, k, 0]) / 2.0
for j in range(0, L):
for k in range(j, L):
if abs(j - k) < 5:
continue
rr.write("%i %i 0 8 %.5f\n" %(j+1, k+1, (P[j][k])) )
rr.close()
print('Written RR ' + file_rr + ' !')
def dist_map_to_bins(Y, bins):
L = len(Y[:, 0])
B = np.full((L, L, len(bins)), 0)
for i in range(L):
for j in range(L):
for bin_i, bin_range in bins.items():
min_max = [float(x) for x in bin_range.split()]
if Y[i, j] > min_max[0] and Y[i, j] <= min_max[1]:
B[i, j, bin_i] = 1
return B