-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
219 lines (159 loc) · 5.09 KB
/
util.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
import heapq
import json
import os
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
def create_dir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def get_separation(mode):
if mode == 'long':
return 24
if mode == 'medium+long':
return 12
def get_real_contacts(contact_map,mode):
contacts = {}
separation = get_separation(mode)
for i in range(len(contact_map)):
for j in range(len(contact_map[0])):
if abs(i - j) >= separation:
if contact_map[i][j] == 1:
if i+1 in contacts:
contacts[i+1].append(j+1)
else:
contacts[i+1] = [j+1]
return contacts
def contact_accuracy(real_contacts,predicted_contacts):
correct = 0
incorrect = 0
#Compare the two dict
for i in predicted_contacts:
if i in real_contacts:
c = predicted_contacts[i]
for j in c:
if j in real_contacts[i]:
correct += 1
else:
incorrect += 1
else:
incorrect += len(predicted_contacts[i])
accuracy = float(correct)/float(correct + incorrect)
return accuracy,correct,incorrect
def load_test_contacts(mode):
if mode == 'long':
file = 'casp13_L_contacts.json'
if mode == 'medium_and_long':
file = 'casp13_ML_contacts.json'
contacts = {}
path = '/net/kihara/home/jain163/Desktop/Projects/folding/Z_Project/casp13/casp13_contact_map/'
with open(path+file) as f:
data = json.load(f)
return data
def load_domains():
domains = {}
path = '/net/kihara/home/jain163/Desktop/Projects/folding/Z_Project/casp13/casp13_contact_map/'
file = 'domains_evaluated_corrected'
# file = 'domains_evaluated'
f = open(path+file,'r')
for row in f:
r = row.strip()
t = r.split()
if t[1] == 'evaluated':
tmp = t[0].split(':')
target = tmp[0].split('-')[0]
domain_num = tmp[0].split('-')[1]
seq_num = tmp[1]
if target not in domains:
domains[target] = {}
domains[target][domain_num] = seq_num
return domains
def inside_domain(domain_aa,i):
#Check if i is within a domain
domain_seqeunces = domain_aa.split(',')
for cur_range in domain_seqeunces:
low = int(cur_range.split('-')[0])
high = int(cur_range.split('-')[1])
if int(i) >= low and int(i) <= high:
return True #If i is within any range, it is accepted
return False
def get_predicted_contacts(contact_map,mode,l_by, domain_aa):
contacts = {}
separation = get_separation(mode)
domain_seqeunces = domain_aa.split(',')
total_length = 0
for cur_range in domain_seqeunces:
low = int(cur_range.split('-')[0])
high = int(cur_range.split('-')[1])
length = high - low + 1
total_length = total_length + length
l = int(total_length / l_by)
contact_heap = []
for i in range(len(contact_map)):
for j in range(len(contact_map[0])):
if abs(i - j) >= int(separation):
val = -contact_map[i][j] #Doing negative to create a max heap
heapq.heappush(contact_heap,(val,i+1,j+1))
while ( l > 0):
if len(contact_heap) > 0:
prob,r,c = heapq.heappop(contact_heap)
if inside_domain(domain_aa,r) and inside_domain(domain_aa,c): #Check both amino acid are inside domain
if c not in contacts or r not in contacts[c]: # For i,j check if j,i is not already included
if r in contacts:
contacts[r].append(c)
else:
contacts[r] = [c]
l = l - 1
else:
contacts[99999] = []
while(l>0):
contacts[99999].append(l)
l = l - 1
return contacts
def distance_bin_2_contact(distance_bin_pred_matrix):
#Combine pred of first 9 bins
# 1 bin of <4
# 8 bins between 4-8 with 0.5 interval
distance_bin_pred_matrix = np.array(distance_bin_pred_matrix)
assert len(distance_bin_pred_matrix) == 20
prot_len = len(distance_bin_pred_matrix[0][0])
contact_map_prob = np.zeros((prot_len,prot_len))
for i in range(0,9):
contact_map_prob += np.array(distance_bin_pred_matrix[i])
return contact_map_prob
def real_distance_2_contact(real_dist_map):
cutoff_bin_id = 9
# less than cutoff = 1 (contact), greater than cutoff = 0 (no contact), distance is -1 = 2 (no information)
contact_map = np.where(real_dist_map == -1, 2, (np.where(real_dist_map <= cutoff_bin_id, 1, 0)))
# print(real_dist_map.shape)
# print(real_dist_map)
# print(contact_map)
return contact_map
def torsional_acc(predicted_angles, real_angles):
total = len(real_angles)
correct = (predicted_angles == real_angles).sum()
accuracy = float(correct) / float(total)
return accuracy
def print_options(opt):
"""Print and save options
It will print both current options and default values(if different).
It will save options into a text file / [checkpoints_dir] / opt.txt
"""
message = ''
message += '----------------- Options ---------------\n'
for k, v in sorted(vars(opt).items()):
comment = ''
# default = parser.get_default(k)
# if v != default:
# comment = '\t[default: %s]' % str(default)
message += '{:>25}: {:<30}{}\n'.format(str(k), str(v), comment)
message += '----------------- End -------------------'
print(message)
# save to the disk
expr_dir = opt.output_dir
create_dir(expr_dir)
file_name = os.path.join(expr_dir, 'opt.txt')
with open(file_name, 'wt') as opt_file:
opt_file.write(message)
opt_file.write('\n')