-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmarking_permutations.py
281 lines (234 loc) · 9.78 KB
/
benchmarking_permutations.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
import sys
from ProteinsManager import ProteinsManager
from UniprotDB import UniprotDB
from uniProtein import uniProtein
from lsh import LSH
from ResultsDB import ResultsDB
from collections import OrderedDict
import matplotlib.pyplot as plt
import pandas as pd
import sqlite3
uniDB = UniprotDB("Uniprot_DB.sqlite")
#Construct the protein database
"""
uniDB.deleteProteins()
protManager = ProteinsManager()
uniDB.createTables()
protManager.loadProteins("Ecolx.xml",uniDB)
protManager.loadProteins("PseA7.xml",uniDB)
"""
minhash3 = LSH(0.3,32)
minhash4 = LSH(0.3,64)
minhash4b = LSH(0.3,96)
minhash5 = LSH(0.3,128)
# Create the minhashes
proteins = uniDB.extractProteins()
"""
minhashes3, lsh3 = minhash3.calculateLSH(proteins, 3)
minhashes4, lsh4 = minhash4.calculateLSH(proteins, 3)
minhashes4b, lsh4b = minhash5.calculateLSH(proteins, 3)
minhashes5, lsh5 = minhash6.calculateLSH(proteins, 3)
minhash3.saveLSH(32)
minhash4.saveLSH(64)
minhash4b.saveLSH(96)
minhash5.saveLSH(128)
"""
minhash3.loadLSH(32)
minhash4.loadLSH(64)
minhash4b.loadLSH(96)
minhash5.loadLSH(128)
resultsDB = ResultsDB("Results_DB_permutations.sqlite")
"""
#Create the BLAST table in the results database
handle = open('all_results_nofilter.txt', 'r')
resultsDB = ResultsDB("Results_DB_permutations.sqlite")
resultsDB.createBLASTtable()
resultsDB.deleteBLASTresults()
resultsDB.createBLASTtable()
for line in handle:
line = line[:-1].split('\t')
# Extract accessions from 'sp|A0A0R6L508|MCR1_ECOLX'-like string
line[0] = line[0].split('|')[1]
line[1] = line[1].split('|')[1]
print(line)
# Filter self-matches, add to the database
if line[0] != line[1]:
resultsDB.addBLASTresult(line[0], line[1], line[2], line[3])
# Create the LSH tables in the results database
resultsDB.createLSHtable("lshresults3")
resultsDB.deleteTable("lshresults3")
resultsDB.createLSHtable("lshresults3")
for query in minhash3.minhashes.keys():
print(query)
matches = minhash3.queryProtein(query)
for match in matches:
# Filter self-matches
if query != match:
jaccard = minhash3.estimateJaccard(query, match)
resultsDB.addLSHresult(query, match, jaccard, "lshresults3")
# Create the LSH tables in the results database
resultsDB.createLSHtable("lshresults4")
resultsDB.deleteTable("lshresults4")
resultsDB.createLSHtable("lshresults4")
for query in minhash4.minhashes.keys():
matches = minhash4.queryProtein(query)
for match in matches:
# Filter self-matches
if query != match:
jaccard = minhash4.estimateJaccard(query, match)
resultsDB.addLSHresult(query, match, jaccard, "lshresults4")
# Create the LSH tables in the results database
resultsDB.createLSHtable("lshresults4b")
resultsDB.deleteTable("lshresults4b")
resultsDB.createLSHtable("lshresults4b")
for query in minhash4b.minhashes.keys():
matches = minhash4b.queryProtein(query)
for match in matches:
# Filter self-matches
if query != match:
jaccard = minhash4b.estimateJaccard(query, match)
resultsDB.addLSHresult(query, match, jaccard, "lshresults4b")
# Create the LSH tables in the results database
resultsDB.createLSHtable("lshresults5")
resultsDB.deleteTable("lshresults5")
resultsDB.createLSHtable("lshresults5")
for query in minhash5.minhashes.keys():
matches = minhash5.queryProtein(query)
for match in matches:
# Filter self-matches
if query != match:
jaccard = minhash5.estimateJaccard(query, match)
resultsDB.addLSHresult(query, match, jaccard, "lshresults5")
"""
"""
for tablename in ['lshresults3','lshresults4','lshresults5']:
intersect = resultsDB.extractIntersectCount(tablename, 0, 30, 0.3)
lshresults = resultsDB.extractLSHcount(tablename, 0.3)
blastresults = resultsDB.extractBLASTcount(0, 30)
tp = intersect
fp = lshresults - intersect
fn = blastresults
tn = (len(proteins)-1) ** 2 - tp - fp - fn
precision = tp/(tp+fp)
recall = tp/(tp+fn)
accuracy = (tp + tn)/(tp + tn + fp + fn)
print("%s \n blastresults: %i\n lshresults: %i \n intersection: %i \nprecision: %0.3f recall: %0.3f accuracy: %0.3f\n" \
% (tablename, blastresults, lshresults, intersect, precision, recall, accuracy))
for x in resultsDB.extractBLASTresults(80.0, 100):
print(x)
print("XXXXXXXXXXXXXXXXXXXXXXX LSH RESULTS XXXXXXXXXXXXX")
for x in resultsDB.extractLSHresults('lshresults3', 0.5):
print(x)
"""
all_precisions = []
all_recalls = []
all_identities = []
all_alignments = []
identity_th, alignment_th, jaccard_th = 80.0, 100, 0.5
for tablename in ['lshresults4b']:
precisions = []
recalls = []
overall_intersect = resultsDB.extractIntersectCount(tablename, identity_th, alignment_th, jaccard_th)
nothreshold_intersect = resultsDB.extractIntersectCount(tablename, 0, 0, jaccard_th)
overall_lshresults = resultsDB.extractLSHcount(tablename, jaccard_th)
overall_blastresults = resultsDB.extractBLASTcount(identity_th, alignment_th)
#print("%s \n blastresults: %i\n lshresults: %i \n intersection: %i \n nr of LSH results matched with a BLAST identity and alignmentlength: %s \n nr of LSH results not matched with BLAST: %s \n" \
# % (tablename, overall_blastresults, overall_lshresults, overall_intersect, nothreshold_intersect, overall_lshresults - nothreshold_intersect))
all_identities.append(resultsDB.extractIntersectIdentity(tablename, 0, 0, jaccard_th))
all_alignments.append(resultsDB.extractIntersectAlignmentLength(tablename, 0, 0, jaccard_th))
all_jaccards = resultsDB.extractIntersectJaccard(tablename, 0, 0, jaccard_th)
for query in proteins:
intersect = resultsDB.extractIntersectCountPerProtein(query[0], tablename, identity_th, alignment_th, jaccard_th)
lshresults = resultsDB.extractLSHcountPerProtein(query[0],tablename, jaccard_th)
blastresults = resultsDB.extractBLASTcountPerProtein(query[0], identity_th, alignment_th)
tp = intersect
fp = lshresults - intersect
fn = blastresults - intersect
#tn = (len(proteins)-1) ** 2 - tp - fp - fn
precision = tp/(tp+fp) if (tp+fp) != 0 else -1
recall = tp/(tp+fn) if (tp+fn) != 0 else -1
#accuracy = (tp + tn)/(tp + tn + fp + fn)
if lshresults == 0 and blastresults == 0:
precision = -1
recall = -1
if precision != -1:
precisions.append(precision)
if recall != -1:
recalls.append(recall)
#print(query[0], intersect, lshresults, blastresults, precision, recall)
all_precisions.append(precisions)
all_recalls.append(recalls)
print("%s \n blastresults: %i\n lshresults: %i \n intersection: %i \n nr of LSH results matched with a BLAST identity and alignmentlength: %s \n nr of LSH results not matched with BLAST: %s \n" \
% (tablename, overall_blastresults, overall_lshresults, overall_intersect, nothreshold_intersect, overall_lshresults - nothreshold_intersect))
for i in range(len(all_precisions)):
plt.hist(all_precisions[i], bins=100)
plt.savefig('precisions%i.png' % i)
plt.close()
plt.hist(all_recalls[i], bins=100)
plt.savefig('recalls%i.png' % i)
plt.close()
print("Precision LSH%i: "% i, sum(all_precisions[i])/len(all_precisions[i]))
print("Recall LSH%i: " % i,sum(all_recalls[i])/len(all_recalls[i]))
print("Lengths precisions, recalls: %i, %i" %(len(all_precisions[i]), len(all_recalls[i])))
# Add the blast results to the identities and alignments
all_identities.append(resultsDB.extractBLASTidentities(identity_th, alignment_th))
all_alignments.append(resultsDB.extractBLASTalignmentlengths(identity_th, alignment_th))
print(len(proteins), "proteins in total")
import numpy as np
from numpy.polynomial.polynomial import polyfit
b, m = polyfit(all_jaccards,all_identities[0], 1)
fig1, ax1 = plt.subplots()
#ax1.set_title('Jaccard ~ identity')
ax1.scatter(all_jaccards,all_identities[0], s=[x/100 for x in all_alignments[0]], c='b')
#ax1.plot(all_jaccards, b + m * np.array(all_jaccards), '-')
ax1.set_ylabel('Alignment Identity (%)')
ax1.set_xlabel('Jaccard Similarity Score')
legend_sizes = np.array([47, 393, 1377])
# get the indices for each of the legend sizes
indices = [np.where(all_alignments[0]==v)[0][0] for v in legend_sizes]
print(indices)
# plot each point again, and its value as a label
legendlabels = [50, 400, 1400]
for n, i in enumerate(indices):
ax1.scatter(all_jaccards[i],all_identities[0][i],s=[x/100 for x in [all_alignments[0][i]]],
label='{:.0f}'.format(legendlabels[n]), c='b')
# add the legend
ax1.legend(scatterpoints=1, loc='lower right', title='Alignment Length')
ax1.scatter(all_jaccards,all_identities[0], s=[x/100 for x in all_alignments[0]])
plt.savefig('jaccard_identity_alignment.png')
plt.close()
fig1, ax1 = plt.subplots()
#ax1.set_title('Jaccard ~ alignmentlength')
ax1.scatter(all_jaccards,all_alignments[0], s=3)
ax1.set_ylabel('Alignment Length (bp)')
ax1.set_xlabel('Jaccard Similarity Score')
plt.savefig('jaccard_length.png')
plt.close()
"""
fig1, ax1 = plt.subplots()
ax1.set_title('Identity distribution')
ax1.boxplot(all_identities, labels=["LSH 32 perm.", "LSH 64 perm.","LSH 96 perm.","LSH 128 perm.","BLAST"])
ax1.set_ylabel('Alignment Identity (%)')
plt.savefig('identity_perm.png')
plt.close()
fig1, ax1 = plt.subplots()
ax1.set_title('Alignment length distribution')
ax1.boxplot(all_alignments, labels=["LSH 32 perm.", "LSH 64 perm.","LSH 96 perm.","LSH 128 perm.","BLAST"])
ax1.set_ylabel('Alignment Length (basepairs)')
plt.savefig('alignmentlength_perm.png')
plt.close()
"""
"""
#tp = intersect
#fp = lshResults - intersect
#fn = blastResults
#precision = tp/(tp+fp)
#recall = tp/(tp+fn)
#print("Precision:", precision)
#print("Recall:", recall)
fig1, ax1 = plt.subplots()
ax1.set_title('Identity distribution')
ax1.boxplot([intersect.iloc[:,2], blastResults.iloc[:,2]], labels=["LSH", "BLAST"])
ax1.set_ylabel('Alignment Identity (%)')
plt.savefig('test.png')
"""