-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py~
240 lines (184 loc) · 5.9 KB
/
main.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
#!/usr/bin/python
import sys
# sudo easy_install xlwt
import xlwt
from tempfile import TemporaryFile
from xlwt import Workbook
thr1 = 0.066
thr2 = -0.052
def is_disordered2(val):
if val <= thr1 and val >= thr2:
return True
else:
return False
def is_disordered1(val):
if val >= thr3:
return True
else:
return False
#rename to Protein
class NMR:
def __init__(self):
self.data = {}
self.size = 0
def parse1(self, filename):
f = open(filename, "r")
str = f.read()
lines = str.split('\n')
for line in lines:
d = line.split('\t')
if len(d) == 11:
num = int(d[0].strip())
val = int(d[9].strip())
self.add(num, is_disordered1(val))
def parse2(self, filename):
f = open(filename, "r")
str = f.read()
lines = str.split('\n')
for line in lines:
d = line.split('\t')
if len(d) == 2:
num = int(d[0]) + mismatch_fix
val = float(d[1])
self.add(num, is_disordered2(val))
# adding number and ordered or disordered
def add(self, num, is_disordered):
if num in self.data:
raise Exception("duplicate residue number")
self.data[num] = is_disordered
self.size = max(self.size, num)
def get_size(self):
return self.size
def is_present(self, num):
if num in self.data:
return True
else:
return False
def is_dis(self, num):
if not self.is_present(num):
raise Exception("no data for this number")
return self.data[num]
# check num of arguments
def usage():
print "Usage: ./main.py <file1> <file2> thr1 mismatch_fix"
def compare(nmr1, nmr2):
tp = 0
tn = 0
fp = 0
fn = 0
i = 1
begin_dis_seq1 = -1
begin_dis_seq2 = -1
seq_string1 = ""
seq_string2 = ""
while i <= max(nmr1.get_size(), nmr2.get_size()):
if nmr1.is_present(i):
if begin_dis_seq1 == -1 and nmr1.is_dis(i):
begin_dis_seq1 = i
if begin_dis_seq1 <> -1 and not nmr1.is_dis(i):
seq_string1 += "MD Disorder interval: " + str(begin_dis_seq1) + " - " + str(i-1) + "\n"
begin_dis_seq1 = -1
if nmr2.is_present(i):
if begin_dis_seq2 == -1 and nmr2.is_dis(i):
begin_dis_seq2 = i
if begin_dis_seq2 <> -1 and not nmr2.is_dis(i):
seq_string2 += "ncSPC Disorder interval: " + str(begin_dis_seq2) + " - " + str(i-1) + "\n"
begin_dis_seq2 = -1
if nmr1.is_present(i) and nmr2.is_present(i):
if nmr1.is_dis(i) and nmr2.is_dis(i):
tp += 1
elif nmr1.is_dis(i) and not nmr2.is_dis(i):
fp += 1
elif not nmr1.is_dis(i) and nmr2.is_dis(i):
fn += 1
elif not nmr1.is_dis(i) and not nmr2.is_dis(i):
tn += 1
else:
print "SHIT"
i += 1
if begin_dis_seq1 <> -1:
seq_string1 += "MD Disorder interval: " + str(begin_dis_seq1) + " - " + str(nmr1.get_size()) + "\n"
if begin_dis_seq2 <> -1:
seq_string2 += "ncSPC Disorder interval: " + str(begin_dis_seq2) + " - " + str(nmr2.get_size()) + "\n"
data_row = sheet1.row(thr3+1)
data_row.write(0, thr3)
len_of_dis = tp+fn
print "Length of Disorder: ", len_of_dis
data_row.write(1, len_of_dis)
hits = tp+tn
print "Hits: ", hits
data_row.write(2, hits)
print "True Positive: ", tp
data_row.write(3, tp)
print "True Negative: ", tn
data_row.write(4, tn)
print "False Positive: ", fp
data_row.write(5, fp)
print "Flase Negative: ", fn
data_row.write(6, fn)
if tp+fp<>0:
accuracy = float(tp)/float(tp+fp)
else:
accuracy = "N/A"
print "Accuracy: ", accuracy
data_row.write(7, accuracy)
if tp+fn<>0:
coverage = float(tp)/float(tp+fn)
else:
coverage = "N/A"
print "Coverage = TP rate: ", coverage
data_row.write(8, coverage)
if fp+tn<>0:
fp_rate = float(fp)/float(fp+tn)
else:
fp_rate = "N/A"
print "FP Rate: ", fp_rate
data_row.write(9, fp_rate)
if tn+fn<>0:
accuracy_ordered = float(tn)/float(tn+fn)
else:
accuracy_ordered = "N/A"
print "Accuracy Ordered: ", accuracy_ordered
data_row.write(10, accuracy_ordered)
if tp+fp<>0:
coverage_ordered = float(tn)/float(tn+fp)
else:
coverage_ordered = "N/A"
print "Coverage Ordered: ", coverage_ordered
data_row.write(11, coverage_ordered)
print seq_string1, seq_string2
data_row.write(12, seq_string1)
data_row.write(13, seq_string2)
if len(sys.argv) <> 5:
usage()
#thr3 = int(sys.argv[3])
mismatch_fix = int(sys.argv[3])
# some hacky code to get the protein number from the file name
f = sys.argv[1]
ar = f.split("/")
protein_num = ar[len(ar)-1].split(".")[0]
book = Workbook()
sheet1 = book.add_sheet(protein_num)
header_row = sheet1.row(0)
header_row.write(0, "\\")
header_row.write(1, "Length of Disorder")
header_row.write(2, "Hits")
header_row.write(3, "True Positives")
header_row.write(4, "True Negative")
header_row.write(5, "False Positives")
header_row.write(6, "False Negatives")
header_row.write(7, "Accuracy")
header_row.write(8, "Coverage/TP Rate")
header_row.write(9, "FP Rate")
header_row.write(10, "Accuracy Ordered")
header_row.write(11, "Coverage Ordered")
nmr2 = NMR()
nmr2.parse2(sys.argv[2])
for th in range(10):
thr3 = th
print "################################# ", thr3
nmr1 = NMR()
nmr1.parse1(sys.argv[1])
compare(nmr1, nmr2)
book.save(protein_num + ".xls")
book.save(TemporaryFile())