forked from nd-hung/DL4DistancePrediction2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContactUtils.py
373 lines (284 loc) · 12.4 KB
/
ContactUtils.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
364
365
366
367
368
369
370
371
372
373
import os
import sys
import numpy as np
import pickle as cPickle
import config
import DataProcessor
import DistanceUtils
import Metrics
## load a contact matrix in text format. This matrix has L rows and L columns
def LoadContactMatrix(file=None):
if file is None:
print('please provide a distance matrix file')
exit(-1)
if not os.path.isfile(file):
print('The distance matrix file does not exist: ', file)
exit(-1)
content = np.genfromtxt(file, dtype=np.float32)
return content
## this function loads contact prediction in CASP format and save it into a matrix
## here we assume the contact matrix is symmetric and 0 indicating that there is no predicted contact
def LoadContactMatrixInCASPFormat(filename):
fh = open(filename, 'w')
content = [ line.strip() for line in list(fh) ]
fh.close()
if len(content) < 5:
print('the input file contains fewer than 5 lines')
return None
##the first line must be "PFRMAT RR"
if content[0] != "PFRMAT RR":
print('The first line of the input file is not PFRMAT RR')
return False
if content[1].startswith('TARGET') is not True:
print ('The second line of the input file shall start with TARGET.')
return None
targetName = content[1].split()[1]
sequence=""
probs = []
for line in content[2:]:
if line.startswith('AUTHOR'):
author=line.split()[1]
continue
if line.startswith('METHOD'):
method=line.split()[1]
continue
if line.startswith('MODEL'):
modelNum=np.int32( line.split()[1] )
assert modelNum==1, "currently only Model 1 is supported"
columns = line.split()
if len(columns) == 1:
sequence += columns[0]
elif len(columns) == 5:
indices = [ int(x) for x in columns[:2] ]
bounds = [ int(x) for x in columns[2:4] ]
prob = np.float32(columns[4])
if bounds[0] !=0 or bounds[1] !=8:
print('wrong distance bound in the following line: ')
print(line)
return None
if prob > 1 or prob <0 :
print('The confidence score in the following line shall be between 0 and 1: ')
print (line)
return None
if indices[0]<1 or indices[0]>len(sequence) or indices[1]<1 or indices[1]>len(sequence):
print('The residue index in the following line is out of range: ')
print(line)
return None
if indices[0] > indices[1]:
print ('The first index in a residue pair shall be smaller than the 2nd one:')
print(line)
return None
probs.append( indices + [ prob ] )
else:
print('The following line in the input file has an incorrect format: ')
print(line)
return None
contactMatrix = np.zeros( (len(sequence), len(sequence)), dtype=np.float32)
for p in probs:
contactMatrix[ p[0], p[1] ] = p[2]
contactMatrix[ p[1], p[0] ] = p[2]
return contactMatrix, targetName, sequence
##this function assumes that the residue index starts from 1
def SaveContactMatrixInCASPFormat(target, sequence, contactMatrix, filename,
probScaleFactor=config.ProbScaleFactor):
if probScaleFactor == 1:
contactMatrix2 = contactMatrix
else:
contactMatrix2 = np.power(contactMatrix, probScaleFactor)
threshold4CASP = 0.05
fh = open(filename, 'w')
##write the header information
fh.write('PFRMAT RR\n')
fh.write('TARGET ' + target + '\n')
fh.write('AUTHOR RaptorX-Contact\n')
fh.write('METHOD deep dilated residual networks (one variant of deep CNN). Consult [email protected] for details.\n')
fh.write('MODEL 1\n')
segmentLen = 50
for i in range(0, len(sequence), segmentLen):
fh.write(sequence[i:i+segmentLen]+'\n')
##here we only consider the upper triangle, so this function cannot apply to nonsymmetri matrices such as CaCg, NO and HB matrices
m = np.triu(contactMatrix2, 6)
flatten_index = np.argsort( -1. * m, axis=None)
real_index = np.unravel_index( flatten_index, contactMatrix2.shape )
"""
contactMapSize = np.prod(contactMatrix2.shape)
if contactMapSize > 300000:
threshold4CASP = 0.1
"""
## numAllowedPairs is the maximum number of pairs allowed by CASP
numAllowedPairs = 300000
seqLen = len(sequence)
maxNumPairs = len(real_index )
## the minimum number of long-range residue pairs
minNumLRPairs = min(3*seqLen, maxNumPairs)
## the maximum number of allowed mediumm and short-range residue paris
maxNumMSRPairs = maxNumPairs
if maxNumPairs > numAllowedPairs:
maxNumMRRPairs = max(0, maxNumPairs - minNumLRPairs)
numPairs, numMSRPairs = 0, 0
for i, j in zip(real_index[0], real_index[1]):
if numPairs > numAllowedPairs:
break
if i >= j:
continue
offset = abs(i - j)
if offset < 6:
continue
if numPairs > 160000 and contactMatrix2[i, j]<threshold4CASP :
continue
"""
if offset < 24 and numMSRPairs > maxNumMSRPairs:
continue
"""
numPairs += 1
if offset < 24:
numMSRPairs += 1
line = ' '.join( [ str(v) for v in [i+1, j+1, 0, 8] ] +[ "%.7f" % (contactMatrix2[i, j]) ] ) + '\n'
fh.write(line)
fh.write('END\n')
fh.close()
## convert a dist prob matrix to a contact prob matrix
## labelOf8 is the cutoff label for contact
#def Distance2Contact(distProb, distLabelType):
def Distance2Contact(distProb, labelOf8=1):
"""
#labelOf8 = DistanceUtils.LabelsOfOneDistance(config.ContactDefinition, config.distCutoffs[distLabelType] )
ContactProb = dict()
for k, m in distProb.iteritems():
ContactProb[k] = np.sum( m[:, :, :labelOf8], axis=2)
"""
contactProb = np.sum( distProb[:, :, :labelOf8], axis=2)
return contactProb
##calculate MCC of a predicted contact matrix using a given score cutoff
##here we consider three cases: long-range contacts, long + medium-range contacts, long + medium- + short-range contacts
def CalcMCCF1(pred=None, truth=None, probCutoff=0.5, contactCutoff=8.0):
if pred is None:
print('please provide a predicted contact matrix')
exit(-1)
if truth is None:
print('please provide a true distance matrix')
exit(-1)
assert pred.shape == truth.shape
## in case the matrix is not square, e.g., interfacial contact matrix
seqLen = pred.shape[0]
seqLen2 = pred.shape[1]
pred_binary = (pred>probCutoff)
truth_binary = ( 0<truth) & (truth<contactCutoff )
pred_truth = pred_binary * 2 + truth_binary
numPredicted = np.sum(pred_binary)
numTruths = np.sum(truth_binary)
#print "#predicted=", numPredicted, "#natives=", numTruths
mask_LR = np.triu_indices(seqLen, 24, m=seqLen2)
mask_MLR = np.triu_indices(seqLen, 12, m=seqLen2)
mask_SMLR = np.triu_indices(seqLen, 6, m=seqLen2)
metrics = []
for mask in [ mask_LR, mask_MLR, mask_SMLR]:
res = pred_truth[mask]
total = res.shape[0]
count = np.bincount(res, minlength=4)
assert (total == np.sum(count) )
## pred=0, truth=0
TN = count[0]
## pred=0, truth=1
FN = count[1]
## pred=1, truth=0
FP = count[2]
## pred=1, truth=1
TP = count[3]
#print TP, FP, TN, FN
MCC = Metrics.MCC(TP, FP, TN, FN)
F1, precision, recall = Metrics.F1(TP, FP, TN, FN)
metrics.extend ([MCC, TP, FP, TN, FN, F1, precision, recall])
return np.array(metrics)
##this program outputs an array of contact prediction accuracy, arranged in the order of long-, medium-, long+medium- and short-range.
## for each range, the accuracy is calculated on the top L*ratio prediction where L is the sequence length.
## pred and truth are 2D matrices. Each entry in pred is a confidence score assigned to the corresponding residue pair indicating how likely this pair forms a contact
## truth is the ground truth distance matrix. The larger the distance, the more unlikely it is a contact. It is fine that one entry has value -1.
## in this distance matrix, only the entries with value between 0 and contactCutoff are treated as contacts.
def TopAccuracy(pred=None, truth=None, ratio=[1, 0.5, 0.2, 0.1], contactCutoff=8.0):
if pred is None:
print('please provide a predicted contact matrix')
exit(1)
if truth is None:
print('please provide a true distance matrix')
exit(1)
assert pred.shape == truth.shape
pred_truth = np.dstack( (pred, truth) )
M1s = np.ones_like(truth, dtype = np.int8)
mask_ER = np.triu(M1s, 48)
mask_LR = np.triu(M1s, 24)
mask_MLR = np.triu(M1s, 12)
mask_SMLR = np.triu(M1s, 6)
mask_MR = mask_MLR - mask_LR
mask_SR = mask_SMLR - mask_MLR
seqLen = pred.shape[0]
accs = []
for mask in [ mask_ER, mask_LR, mask_MR, mask_MLR, mask_SR]:
res = pred_truth[mask.nonzero()]
if res.size == 0:
accs.extend( [0.0] * len(ratio) )
continue
res_sorted = res [ (-res[:,0]).argsort() ]
for r in ratio:
numTops = int(seqLen * r)
numTops = min(numTops, res_sorted.shape[0] )
topLabels = res_sorted[:numTops, 1]
numCorrects = ( (0<topLabels) & (topLabels<contactCutoff ) ).sum()
accuracy = numCorrects * 1./numTops
accs.append(accuracy)
return np.array(accs)
## Evaluate contact prediction for a single protein. predictedContactMatrix is a dictionary in which each key is a response.
## here response represents a pair of amino acid type, e.g., CbCb, CaCa, CgCg
def EvaluateSingleContactPrediction(predictedContactMatrix, nativefile):
native = DataProcessor.LoadNativeDistMatrixFromFile(nativefile)
accuracy = dict()
for response in predictedContactMatrix.keys():
if response.startswith('HB'):
accuracy[response] = TopAccuracy(pred=predictedContactMatrix[response], truth=native[response], contactCutoff=config.MaxHBDistance)
else:
accuracy[response] = TopAccuracy(pred=predictedContactMatrix[response], truth=native[response])
return accuracy
## pred is a 2D contact matrix, each entry has a prob value
def EvaluateSingleCbCbContactPrediction(pred, nativefile):
native = DataProcessor.LoadNativeDistMatrixFromFile(nativefile)
accuracy = TopAccuracy(pred, truth=native['CbCb'])
return accuracy
## predictedContactMatrices is a dictionary of contact matrices. Each key is a protein name
def EvaluateContactPredictions(predictedContactMatrices, nativefolder):
## load the ground truth
allnatives = dict()
for name in predictedContactMatrices.keys():
allnatives[name] = DataProcessor.LoadNativeDistMatrix(name, nativefolder)
"""
if allnatives[name] is None:
print 'WARNING: cannot find the native distance matrix for protein ', name
"""
allaccuracy = dict()
for name, results in predictedContactMatrices.iteritems():
if not allnatives.has_key(name) or allnatives[name] is None:
continue
allaccuracy[name] = dict()
for response in results.keys():
##apt = Response2LabelName(response)
apt = response
if response.startswith('HB'):
allaccuracy[name][response] = TopAccuracy(pred=predictedContactMatrices[name][response], truth=allnatives[name][apt], contactCutoff=config.MaxHBDistance)
else:
allaccuracy[name][response] = TopAccuracy(pred=predictedContactMatrices[name][response], truth=allnatives[name][apt])
## calculate average contact prediction accuracy
allaccuracyByApt = dict()
for name, results in allaccuracy.iteritems():
for apt in results.keys():
if not allaccuracyByApt.has_key(apt):
allaccuracyByApt[apt] = [ results[apt] ]
else:
allaccuracyByApt[apt].append(results[apt])
avgacc = dict()
for apt in allaccuracyByApt.keys():
avgaccuracyByApt = np.average(allaccuracyByApt[apt], axis=0)
avgacc[apt] = avgaccuracyByApt
"""
print '******************Average contact prediction accuracy for atom pair type: ', apt, '*********************'
print avgaccuracyByApt
"""
return avgacc, allaccuracy