-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapproximate_pigeonhole.py
90 lines (78 loc) · 2.8 KB
/
approximate_pigeonhole.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
# This can be performed using any algorithm
# Boyer Moore
def approximate_match_boyer(p, t, n):
segment_length = int(round(len(p) / (n+1)))
all_matches = set()
for i in range(n+1):
start = i*segment_length
end = min((i+1)*segment_length, len(p))
p_bm = BoyerMoore(p[start:end], alphabet='ACGT') # Boyer Moore
matches = boyer_moore(p[start:end], p_bm, t)
# Extend matching segments to see if whole p matches
for m in matches:
if m < start or m-start+len(p) > len(t):
continue
mismatches = 0
for j in range(0, start):
if not p[j] == t[m-start+j]:
mismatches += 1
if mismatches > n:
break
for j in range(end, len(p)):
if not p[j] == t[m-start+j]:
mismatches += 1
if mismatches > n:
break
if mismatches <= n:
all_matches.add(m - start)
return list(all_matches)
# Substring Index
def approximate_match_index(p, t, n):
segment_length = int(round(len(p) / (n+1)))
all_matches = set()
hits = 0
index_t = Index(t, 8)
for i in range(n+1):
start = i*segment_length
end = min((i+1)*segment_length, len(p))
matches = index_t.query(p[start:end])
hits += len(matches)
# Extend matching segments to see if whole p matches
for m in matches:
if m < start or m-start+len(p) > len(t):
continue
mismatches = 0
for j in range(0, start):
if not p[j] == t[m-start+j]:
mismatches += 1
if mismatches > n:
break
for j in range(end, len(p)):
if not p[j] == t[m-start+j]:
mismatches += 1
if mismatches > n:
break
if mismatches <= n:
all_matches.add(m - start)
return list(all_matches), hits
# Subsequence Index
def approximate_match_subindex(p, t, n):
all_matches = set()
hits = 0
index_t = SubseqIndex(t, 8, 3)
for start in range(n+1):
matches = index_t.query(p[start:])
hits += len(matches)
# Extend matching segments to see if whole p matches
for m in matches:
if m < start or m-start+len(p) > len(t):
continue
mismatches = 0
for j in range(0, len(p)):
if not p[j] == t[m-start+j]:
mismatches += 1
if mismatches > n:
break
if mismatches <= n:
all_matches.add(m - start)
return list(all_matches), hits