-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis_functions.py
More file actions
196 lines (175 loc) · 8.4 KB
/
analysis_functions.py
File metadata and controls
196 lines (175 loc) · 8.4 KB
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
import matplotlib.pylab as plt
from sklearn.metrics import precision_score, confusion_matrix
from distance_dca import vectorize_dca_contacts
img_dir = "C:\\Users\\kmehr\\OneDrive\\Documents\\phd_research\\images\\2020\\DEC_2020\\"
def plot_tpr_as_function_of_pairs(n_pairs, true_positives_array, threshold, nSystems, fni=False):
"""
Plot TPR as function of top ranked DCA pairs
:param fni:
:param n_pairs:
:param true_positives_array:
:param threshold:
:param nSystems:
:return:
"""
from numpy.random import randint
plotNumber = randint(nSystems * n_pairs)
plt.figure(plotNumber)
plt.plot(range(n_pairs), true_positives_array, color='black', label='{}$\AA$'.format(threshold))
plt.xlabel("top ranked DCA intermonomer predictions")
plt.ylabel("TPR")
if fni:
imgname_tpr_vs_top_pairs = "TPR_as_function_top{}_pairs_{}systems_FNi_inter_plmDCA_aa{}.png".format(n_pairs,
nSystems,
threshold)
else:
imgname_tpr_vs_top_pairs = "TPR_as_function_top{}_interface_pairs_{}systems_FN_plmDCA_aa{}.png".format(n_pairs,
nSystems,
threshold)
plt.savefig(img_dir + imgname_tpr_vs_top_pairs, dpi=600, bbox_inches='tight')
plt.close()
def compare_two_tpr(n_pairs, tpr_unbinned_1, tpr_unbinned_2, threshold, nSystems):
"""
Plot TPR as function of top ranked DCA pairs
:param tpr_unbinned_2:
:param n_pairs:
:param tpr_unbinned_1:
:param threshold:
:param nSystems:
:return:
"""
from numpy.random import randint
plotNumber = randint(nSystems * n_pairs)
plt.figure(plotNumber)
plt.plot(range(n_pairs), tpr_unbinned_1, color='black', label='FNi {}$\AA$'.format(threshold))
plt.plot(range(n_pairs), tpr_unbinned_2, color='xkcd:red', label='FN {}$\AA$'.format(threshold))
plt.xlabel("top ranked DCA intermonomer predictions")
plt.ylabel("fraction of dca pairs with cutoff <= {}$\AA$".format(threshold))
plt.legend(loc="best")
imgname_tpr_vs_top_pairs = "compare_TP FNi_FN_top{}_{}systems_aa{}.png".format(n_pairs, nSystems, threshold)
plt.savefig(img_dir + imgname_tpr_vs_top_pairs, dpi=600, bbox_inches='tight')
plt.close()
def plot_tp_as_function_of_scores(TPR_norm, binned_scores, width, n_pairs, threshold, nSystems, fni=False):
"""
Plot TPR vs Binned FN-apc scores
:param TPR_norm:
:param binned_scores:
:param width:
:param n_pairs:
:param threshold:
:param nSystems:
:param fni:
:return:
"""
from numpy.random import randint
plotNumber = randint(nSystems * n_pairs)
plt.figure(plotNumber)
plt.bar(binned_scores, TPR_norm, width=width, align='edge', edgecolor="black", color="teal", alpha=0.7)
plt.ylabel("True Positives".format(threshold))
if fni:
plt.xlabel("FNi score")
imgname_tp_vs_binned_fn = "FNi_TP_as_function_dx{}_{}systems_inter_plmDCA_top{}_aa{}.png".format(width,
nSystems,
n_pairs,
threshold)
else:
plt.xlabel("FN scores")
imgname_tp_vs_binned_fn = "FN_TP_as_function_all_dx{}_{}systems_plmDCA_top{}_aa{}.png".format(width,
nSystems,
n_pairs,
threshold)
plt.savefig(img_dir + imgname_tp_vs_binned_fn, dpi=600, bbox_inches='tight')
# plt.xlim(0, 1.4)
# plt.ylim(0, 1)
plt.show()
plt.close()
def plot_fp_as_function_of_scores(TPR_norm, binned_scores, width, n_pairs, threshold, nSystems, fni=False):
"""
Plot TPR vs Binned FN-apc scores
:param TPR_norm:
:param binned_scores:
:param width:
:param n_pairs:
:param threshold:
:param nSystems:
:param fni:
:return:
"""
from numpy.random import randint
plotNumber = randint(nSystems * n_pairs)
plt.figure(plotNumber)
plt.bar(binned_scores, TPR_norm, width=width, align='edge', edgecolor="black", color="purple", alpha=0.7)
plt.ylabel("False Positives".format(threshold))
if fni:
plt.xlabel("FNi scores")
imgname_fp_vs_binned_fn = "FNi_FP_as_function_dx{}_{}systems_inter_plmDCA_top{}_aa{}.png".format(width,
nSystems,
n_pairs,
threshold)
else:
plt.xlabel("FN scores")
imgname_fp_vs_binned_fn = "FN_FP_as_function_interface_dx{}_{}systems_plmDCA_top{}_aa{}.png".format(width,
nSystems,
n_pairs,
threshold)
# plt.savefig(img_dir + imgname_fp_vs_binned_fn, dpi=600, bbox_inches='tight')
plt.xlim(0, 1.4)
# plt.ylim(0, 1)
plt.show()
# plt.close()
def plot_npairs_as_function_of_scores(norm, binned_scores, width, n_pairs, threshold, nSystems, fni=False):
# Plot Normalization (eg Number of pairs) vs Binned FN-apc scores
plt.figure(2)
plt.bar(binned_scores, norm, width=width, align='edge', edgecolor="black", color="navy", alpha=0.7, label="total contacts")
# plt.yscale(value="log")
plt.ylabel("P(TP)")
if fni:
plt.xlabel("binned FNi scores")
imgname_pairs_vs_fn = "FNi_pairs_as_function_dx{}_{}systems_top{}_aa{}.png".format(width, nSystems,
n_pairs, threshold)
else:
plt.xlabel("binned FN-apc scores")
imgname_pairs_vs_fn = "FN_pairs_as_function_interface_dx{}_{}systems_top{}_aa{}.png".format(width, nSystems,
n_pairs, threshold)
plt.legend(loc='best')
# plt.savefig(img_dir + imgname_pairs_vs_fn, dpi=600, bbox_inches='tight')
plt.show()
# plt.close()
def tpr_top_pairs(pdb_flat_matrix, dca_df, n_contacts, pdb_total_length):
"""
# This uses confusion matrix method. Don't use this. There is a better way by defining a cutoff threshold.
:param pdb_total_length:
:param pdb_flat_matrix:
:param dca_df:
:param n_contacts:
:return:
"""
tpr_list = []
for i in range(n_contacts):
if i > 0:
dca_flat_matrix = vectorize_dca_contacts(dca_df[:i], pdb_total_length)
tpr = precision_score(pdb_flat_matrix, dca_flat_matrix, zero_division=1)
tpr_list.append(tpr)
return tpr_list
def confusion_matrix_list(pdb_flat_matrix, dca_df, pdb_total_length):
"""
# This uses confusion matrix method to calculate tp. Dont use this.
:param pdb_flat_matrix:
:param dca_df:
:param pdb_total_length:
:return:
"""
tp_list = []
fp_list = []
fn_list = []
tn_list = []
n_contacts = len(dca_df)
for i in range(n_contacts + 1):
if i > 0:
dca_flat_matrix = vectorize_dca_contacts(dca_df[:i], pdb_total_length)
tn, fp, fn, tp = confusion_matrix(pdb_flat_matrix, dca_flat_matrix, normalize="pred").ravel()
tp_list.append(tp)
fp_list.append(fp)
fn_list.append(fn)
tn_list.append(tn)
return tp_list, fp_list, fn_list, tn_list