-
Notifications
You must be signed in to change notification settings - Fork 0
/
approximate_pattern_match.py
46 lines (34 loc) · 1.11 KB
/
approximate_pattern_match.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
import itertools
def matches(section, dict):
if section in dict:
return True
else:
return False
def mutations(word, hamming_distance, charset='ATCG'):
for indices in itertools.combinations(range(len(word)), hamming_distance):
for replacements in itertools.product(charset, repeat=hamming_distance):
mutation = list(word)
for index, replacement in zip(indices, replacements):
mutation[index] = replacement
yield "".join(mutation)
def generateMatches(pattern, dict, d):
dict.update(mutations(pattern, d))
text = 'CGCCCGAATCCAGAACGCATTCCCATATTTCGGGACCACTGGCCTCCACGGTACGGACGTCAATCAAAT'
pattern = 'ATTCTGGA'
d = 3
inFile = open('dataset_8_3.txt', 'r')
# pattern='CTTGATCAT' # inFile.readline().strip()
pattern = inFile.readline().strip()
text = inFile.readline().strip()
d = int(inFile.readline().strip())
i=0
dict=set()
generateMatches(pattern, dict, d)
results=[]
while i <= len(text) - len(pattern):
section = text[i:i + len(pattern)]
if matches(section, dict):
results.append(i)
i += 1
for i in results:
print i,